1 | /* $Id: libmondo-string.c 2508 2010-01-04 18:21:55Z bruno $ */
|
---|
2 |
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * @file
|
---|
6 | * Functions for handling strings.
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include "my-stuff.h"
|
---|
10 | #include "mr_mem.h"
|
---|
11 | #include "mondostructures.h"
|
---|
12 | #include "libmondo-string.h"
|
---|
13 | #include "lib-common-externs.h"
|
---|
14 | #include "libmondo-files-EXT.h"
|
---|
15 | #include "libmondo-gui-EXT.h"
|
---|
16 | #include "libmondo-tools-EXT.h"
|
---|
17 |
|
---|
18 | /*@unused@*/
|
---|
19 | //static char cvsid[] = "$Id: libmondo-string.c 2508 2010-01-04 18:21:55Z bruno $";
|
---|
20 |
|
---|
21 | extern int g_current_media_number;
|
---|
22 | extern long long g_tape_posK;
|
---|
23 |
|
---|
24 | /* Reference to global bkpinfo */
|
---|
25 | extern struct s_bkpinfo *bkpinfo;
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * @addtogroup stringGroup
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * Build a partition name from a drive and a partition number.
|
---|
34 | * @param drive The drive basename of the partition name (e.g. /dev/hda)
|
---|
35 | * @param partno The partition number (e.g. 1)
|
---|
36 | * @return @p partition.
|
---|
37 | * @note If @p drive ends in a digit, then 'p' (on Linux) or 's' (on *BSD) is added before @p partno.
|
---|
38 | */
|
---|
39 | char *build_partition_name(const char *drive, int partno)
|
---|
40 | {
|
---|
41 | char *c = NULL;
|
---|
42 | char *partition = NULL;
|
---|
43 |
|
---|
44 | assert_string_is_neither_NULL_nor_zerolength(drive);
|
---|
45 | assert(partno >= 0);
|
---|
46 |
|
---|
47 | mr_asprintf(partition, "%s", drive);
|
---|
48 | /* is this a devfs device path? */
|
---|
49 | c = strrchr(partition, '/');
|
---|
50 | if (c && strncmp(c, "/disc", 5) == 0) {
|
---|
51 | /* yup it's devfs, return the "part" path */
|
---|
52 | /* This strcpy is safe */
|
---|
53 | strcpy(c + 1, "part");
|
---|
54 | } else {
|
---|
55 | if (isdigit(partition[-1])) {
|
---|
56 | mr_strcat(partition, "%c",
|
---|
57 | #ifdef BSD
|
---|
58 | 's');
|
---|
59 | #else
|
---|
60 | 'p');
|
---|
61 | #endif
|
---|
62 | }
|
---|
63 | }
|
---|
64 | mr_strcat(partition, "%d", partno);
|
---|
65 | return(partition);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Pad a string on both sides so it appears centered.
|
---|
71 | * @param in_out The string to be center-padded (modified).
|
---|
72 | * @param width The width of the final result.
|
---|
73 | */
|
---|
74 | void center_string(char *in_out, int width)
|
---|
75 | {
|
---|
76 | char scratch[MAX_STR_LEN];
|
---|
77 | char *p;
|
---|
78 | int i; /* purpose */
|
---|
79 | int len; /* purpose */
|
---|
80 | int mid; /* purpose */
|
---|
81 | int x; /* purpose */
|
---|
82 |
|
---|
83 | assert(in_out != NULL);
|
---|
84 | assert(width > 2);
|
---|
85 |
|
---|
86 | if (strlen(in_out) == 0) {
|
---|
87 | return;
|
---|
88 | }
|
---|
89 | for (p = in_out; *p == ' '; p++);
|
---|
90 | strcpy(scratch, p);
|
---|
91 | strip_spaces (scratch);
|
---|
92 | len = (int) strlen(scratch);
|
---|
93 | mid = width / 2;
|
---|
94 | x = mid - len / 2;
|
---|
95 | for (i = 0; i < x; i++) {
|
---|
96 | in_out[i] = ' ';
|
---|
97 | }
|
---|
98 | in_out[i] = '\0';
|
---|
99 | strcat(in_out, scratch);
|
---|
100 | for (i = x + len ; i < width - 1; i++) {
|
---|
101 | in_out[i] = ' ';
|
---|
102 | }
|
---|
103 | in_out[i] = '\0';
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Add commas every third place in @p input.
|
---|
109 | * @param input The string to commarize.
|
---|
110 | * @return The string with commas.
|
---|
111 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
112 | */
|
---|
113 | char *commarize(char *input)
|
---|
114 | {
|
---|
115 | char pos_w_commas[MAX_STR_LEN];
|
---|
116 | static char output[MAX_STR_LEN];
|
---|
117 | char tmp[MAX_STR_LEN];
|
---|
118 | int j;
|
---|
119 |
|
---|
120 | assert(input != NULL);
|
---|
121 |
|
---|
122 | strcpy(tmp, input);
|
---|
123 | if (strlen(tmp) > 6) {
|
---|
124 | strcpy(pos_w_commas, tmp);
|
---|
125 | j = (int) strlen(pos_w_commas);
|
---|
126 | tmp[j - 6] = ',';
|
---|
127 | strcpy(tmp + j - 5, pos_w_commas + j - 6);
|
---|
128 | strcpy(pos_w_commas, tmp);
|
---|
129 | }
|
---|
130 | if (strlen(tmp) > 3) {
|
---|
131 | j = (int) strlen(tmp);
|
---|
132 | strcpy(pos_w_commas, tmp);
|
---|
133 | pos_w_commas[j - 3] = ',';
|
---|
134 | strcpy(pos_w_commas + j - 2, tmp + j - 3);
|
---|
135 | } else {
|
---|
136 | strcpy(pos_w_commas, tmp);
|
---|
137 | }
|
---|
138 | strcpy(output, pos_w_commas);
|
---|
139 | return (output);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 |
|
---|
144 |
|
---|
145 |
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Turn an entry from the RAID editor's disklist into a GUI-friendly string.
|
---|
152 | * The format is: the device left-aligned and padded to 24 places, followed by a space and the
|
---|
153 | * index, right-aligned and padded to eight places. The total string length
|
---|
154 | * is exactly 33.
|
---|
155 | * @param disklist The disklist to operate on.
|
---|
156 | * @param lino The line number from @p disklist to convert to a string.
|
---|
157 | * @return The string form of the disklist entry.
|
---|
158 | * @note The returned string points to static storage and will be overwritten with each call.
|
---|
159 | */
|
---|
160 | char *disklist_entry_to_string(struct list_of_disks *disklist, int lino)
|
---|
161 | {
|
---|
162 |
|
---|
163 | /*@ buffers ********************************************************** */
|
---|
164 | static char output[MAX_STR_LEN];
|
---|
165 |
|
---|
166 | assert(disklist != NULL);
|
---|
167 |
|
---|
168 | sprintf(output, "%-24s %8d", disklist->el[lino].device,
|
---|
169 | disklist->el[lino].index);
|
---|
170 | return (output);
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Turn a "friendly" sizestring into a number of megabytes.
|
---|
179 | * Supports the suffixes 'k'/'K', 'm'/'M', and 'g'/'G'. Calls
|
---|
180 | * fatal_error() if an unknown suffix is encountered.
|
---|
181 | * @param incoming The sizestring to convert (e.g. "40m", "2g").
|
---|
182 | * @return The size in megabytes.
|
---|
183 | */
|
---|
184 | long friendly_sizestr_to_sizelong(char *incoming)
|
---|
185 | {
|
---|
186 | long outval;
|
---|
187 | int i;
|
---|
188 | char *tmp1 = NULL;
|
---|
189 | char ch;
|
---|
190 |
|
---|
191 | assert_string_is_neither_NULL_nor_zerolength(incoming);
|
---|
192 |
|
---|
193 | if (!incoming[0]) {
|
---|
194 | return (0);
|
---|
195 | }
|
---|
196 | if (strchr(incoming, '.')) {
|
---|
197 | fatal_error("Please use integers only. No decimal points.");
|
---|
198 | }
|
---|
199 |
|
---|
200 | mr_asprintf(tmp1, "%s", incoming);
|
---|
201 | i = (int) strlen(tmp1);
|
---|
202 | if (tmp1[i - 1] == 'B' || tmp1[i - 1] == 'b') {
|
---|
203 | tmp1[i - 1] = '\0';
|
---|
204 | }
|
---|
205 | for (i = 0; i < (int) strlen(tmp1) && isdigit(tmp1[i]); i++);
|
---|
206 | ch = tmp1[i];
|
---|
207 | tmp1[i] = '\0';
|
---|
208 | outval = atol(tmp1);
|
---|
209 | paranoid_free(tmp1);
|
---|
210 |
|
---|
211 | if (ch == 'g' || ch == 'G') {
|
---|
212 | outval = outval * 1024;
|
---|
213 | } else if (ch == 'k' || ch == 'K') {
|
---|
214 | outval = outval / 1024;
|
---|
215 | } else if (ch == 't' || ch == 'T') // terabyte
|
---|
216 | {
|
---|
217 | outval *= 1048576;
|
---|
218 | } else if (ch == 'Y' || ch == 'y') // yottabyte - the biggest measure in the info file
|
---|
219 | {
|
---|
220 | log_it
|
---|
221 | ("Oh my gosh. You actually think a YOTTABYTE will get you anywhere? What're you going to do with 1,208,925,819,614,629,174,706,176 bytes of data?!?!");
|
---|
222 | popup_and_OK
|
---|
223 | ("That sizespec is more than 1,208,925,819,614,629,174,706,176 bytes. You have a shocking amount of data. Please send a screenshot to the list :-)");
|
---|
224 | fatal_error("Integer overflow.");
|
---|
225 | } else if (ch != 'm' && ch != 'M') {
|
---|
226 | mr_asprintf(tmp1, "Re: parameter '%s' - bad multiplier ('%c')", incoming, ch);
|
---|
227 | fatal_error(tmp1);
|
---|
228 | }
|
---|
229 | return (outval);
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Add spaces to the right of @p incoming to make it @p width characters wide.
|
---|
236 | * @param incoming The string to left-pad.
|
---|
237 | * @param width The width to pad it to.
|
---|
238 | * @return The left-padded string.
|
---|
239 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
240 | * @bug Why does center_string() modify its argument but leftpad_string() returns a modified copy?
|
---|
241 | */
|
---|
242 | char *leftpad_string(char *incoming, int width)
|
---|
243 | {
|
---|
244 | /*@ buffers ***************************************************** */
|
---|
245 | static char output[MAX_STR_LEN];
|
---|
246 |
|
---|
247 | /*@ ints ******************************************************** */
|
---|
248 | int i;
|
---|
249 |
|
---|
250 | /*@ end vars **************************************************** */
|
---|
251 | assert(incoming != NULL);
|
---|
252 | assert(width > 2);
|
---|
253 |
|
---|
254 | strcpy(output, incoming);
|
---|
255 | for (i = (int) strlen(output); i < width; i++) {
|
---|
256 | output[i] = ' ';
|
---|
257 | }
|
---|
258 | output[i] = '\0';
|
---|
259 | return (output);
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Turn a marker byte (e.g. BLK_START_OF_BACKUP) into a string (e.g. "BLK_START_OF_BACKUP").
|
---|
266 | * Unknown markers are identified as "BLK_UNKNOWN (%d)" where %d is the decimal value.
|
---|
267 | * @param marker The marker byte to stringify.
|
---|
268 | * @return @p marker as a string.
|
---|
269 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
270 | */
|
---|
271 | char *marker_to_string(int marker)
|
---|
272 | {
|
---|
273 | /*@ buffer ****************************************************** */
|
---|
274 | static char outstr[MAX_STR_LEN];
|
---|
275 |
|
---|
276 |
|
---|
277 | /*@ end vars *************************************************** */
|
---|
278 |
|
---|
279 | switch (marker) {
|
---|
280 | case BLK_START_OF_BACKUP:
|
---|
281 | strcpy(outstr, "BLK_START_OF_BACKUP");
|
---|
282 | break;
|
---|
283 | case BLK_START_OF_TAPE:
|
---|
284 | strcpy(outstr, "BLK_START_OF_TAPE");
|
---|
285 | break;
|
---|
286 | case BLK_START_AN_AFIO_OR_SLICE:
|
---|
287 | strcpy(outstr, "BLK_START_AN_AFIO_OR_SLICE");
|
---|
288 | break;
|
---|
289 | case BLK_STOP_AN_AFIO_OR_SLICE:
|
---|
290 | strcpy(outstr, "BLK_STOP_AN_AFIO_OR_SLICE");
|
---|
291 | break;
|
---|
292 | case BLK_START_AFIOBALLS:
|
---|
293 | strcpy(outstr, "BLK_START_AFIOBALLS");
|
---|
294 | break;
|
---|
295 | case BLK_STOP_AFIOBALLS:
|
---|
296 | strcpy(outstr, "BLK_STOP_AFIOBALLS");
|
---|
297 | break;
|
---|
298 | case BLK_STOP_BIGGIEFILES:
|
---|
299 | strcpy(outstr, "BLK_STOP_BIGGIEFILES");
|
---|
300 | break;
|
---|
301 | case BLK_START_A_NORMBIGGIE:
|
---|
302 | strcpy(outstr, "BLK_START_A_NORMBIGGIE");
|
---|
303 | break;
|
---|
304 | case BLK_START_A_PIHBIGGIE:
|
---|
305 | strcpy(outstr, "BLK_START_A_PIHBIGGIE");
|
---|
306 | break;
|
---|
307 | case BLK_START_EXTENDED_ATTRIBUTES:
|
---|
308 | strcpy(outstr, "BLK_START_EXTENDED_ATTRIBUTES");
|
---|
309 | break;
|
---|
310 | case BLK_STOP_EXTENDED_ATTRIBUTES:
|
---|
311 | strcpy(outstr, "BLK_STOP_EXTENDED_ATTRIBUTES");
|
---|
312 | break;
|
---|
313 | case BLK_START_EXAT_FILE:
|
---|
314 | strcpy(outstr, "BLK_START_EXAT_FILE");
|
---|
315 | break;
|
---|
316 | case BLK_STOP_EXAT_FILE:
|
---|
317 | strcpy(outstr, "BLK_STOP_EXAT_FILE");
|
---|
318 | break;
|
---|
319 | case BLK_START_BIGGIEFILES:
|
---|
320 | strcpy(outstr, "BLK_START_BIGGIEFILES");
|
---|
321 | break;
|
---|
322 | case BLK_STOP_A_BIGGIE:
|
---|
323 | strcpy(outstr, "BLK_STOP_A_BIGGIE");
|
---|
324 | break;
|
---|
325 | case BLK_END_OF_TAPE:
|
---|
326 | strcpy(outstr, "BLK_END_OF_TAPE");
|
---|
327 | break;
|
---|
328 | case BLK_END_OF_BACKUP:
|
---|
329 | strcpy(outstr, "BLK_END_OF_BACKUP");
|
---|
330 | break;
|
---|
331 | case BLK_ABORTED_BACKUP:
|
---|
332 | strcpy(outstr, "BLK_ABORTED_BACKUP");
|
---|
333 | break;
|
---|
334 | case BLK_START_FILE:
|
---|
335 | strcpy(outstr, "BLK_START_FILE");
|
---|
336 | break;
|
---|
337 | case BLK_STOP_FILE:
|
---|
338 | strcpy(outstr, "BLK_STOP_FILE");
|
---|
339 | break;
|
---|
340 | default:
|
---|
341 | sprintf(outstr, "BLK_UNKNOWN (%d)", marker);
|
---|
342 | break;
|
---|
343 | }
|
---|
344 | return (outstr);
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 |
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Turn a line from the mountlist into a GUI-friendly string.
|
---|
352 | * The format is as follows: the left-aligned @p device field padded to 24 places,
|
---|
353 | * a space, the left-aligned @p mountpoint field again padded to 24 places, a space,
|
---|
354 | * the left-aligned @p format field padded to 10 places, a space, and the right-aligned
|
---|
355 | * @p size field (in MB) padded to 8 places. The total string length is exactly 69.
|
---|
356 | * @param mountlist The mountlist to operate on.
|
---|
357 | * @param lino The line number in @p mountlist to stringify.
|
---|
358 | * @return The string form of <tt>mountlist</tt>-\>el[<tt>lino</tt>].
|
---|
359 | * @note The returned string points to static storage and will be overwritten with each call.
|
---|
360 | */
|
---|
361 | char *mountlist_entry_to_string(struct mountlist_itself *mountlist,
|
---|
362 | int lino)
|
---|
363 | {
|
---|
364 |
|
---|
365 | /*@ buffer *********************************************************** */
|
---|
366 | static char output[MAX_STR_LEN];
|
---|
367 |
|
---|
368 | assert(mountlist != NULL);
|
---|
369 |
|
---|
370 | sprintf(output, "%-24s %-24s %-10s %8lld", mountlist->el[lino].device,
|
---|
371 | mountlist->el[lino].mountpoint, mountlist->el[lino].format,
|
---|
372 | mountlist->el[lino].size / 1024L);
|
---|
373 | return (output);
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 |
|
---|
378 |
|
---|
379 |
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Generate a friendly string containing "X blah blah disk(s)"
|
---|
383 | * @param noof_disks The number of disks (the X).
|
---|
384 | * @param label The "blah blah" part in the middle. If you leave this blank
|
---|
385 | * there will be a weird double space in the middle, so pass *something*.
|
---|
386 | * @return The string containing "X blah blah disk(s)".
|
---|
387 | * @note The returned string points to static storage and will be overwritten with each call.
|
---|
388 | */
|
---|
389 | char *number_of_disks_as_string(int noof_disks, char *label)
|
---|
390 | {
|
---|
391 |
|
---|
392 | /*@ buffers ********************************************************* */
|
---|
393 | char *output = NULL;
|
---|
394 |
|
---|
395 | /*@ char ******************************************************** */
|
---|
396 | char p;
|
---|
397 |
|
---|
398 | assert(label != NULL);
|
---|
399 |
|
---|
400 | if (noof_disks > 1) {
|
---|
401 | p = 's';
|
---|
402 | } else {
|
---|
403 | p = ' ';
|
---|
404 | }
|
---|
405 | mr_asprintf(output, "%d %s disk%c", noof_disks, label, p);
|
---|
406 | while (strlen(output) < 14) {
|
---|
407 | mr_strcat(output, " ");
|
---|
408 | }
|
---|
409 | return (output);
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Change @p i into a friendly string. If @p i is \<= 10 then write out the
|
---|
416 | * number (e.g. "one", "two", ..., "nine", "ten", "11", ...).
|
---|
417 | * @param i The number to stringify.
|
---|
418 | * @return The string form of @p i.
|
---|
419 | * @note The returned value points to static strorage that will be overwritten with each call.
|
---|
420 | */
|
---|
421 | char *number_to_text(int i)
|
---|
422 | {
|
---|
423 |
|
---|
424 | /*@ buffers ***************************************************** */
|
---|
425 | static char output[MAX_STR_LEN];
|
---|
426 |
|
---|
427 |
|
---|
428 | /*@ end vars *************************************************** */
|
---|
429 |
|
---|
430 | switch (i) {
|
---|
431 | case 0:
|
---|
432 | strcpy(output, "zero");
|
---|
433 | break;
|
---|
434 | case 1:
|
---|
435 | strcpy(output, "one");
|
---|
436 | break;
|
---|
437 | case 2:
|
---|
438 | strcpy(output, "two");
|
---|
439 | break;
|
---|
440 | case 3:
|
---|
441 | strcpy(output, "three");
|
---|
442 | break;
|
---|
443 | case 4:
|
---|
444 | strcpy(output, "four");
|
---|
445 | break;
|
---|
446 | case 5:
|
---|
447 | strcpy(output, "five");
|
---|
448 | break;
|
---|
449 | case 6:
|
---|
450 | strcpy(output, "six");
|
---|
451 | break;
|
---|
452 | case 7:
|
---|
453 | strcpy(output, "seven");
|
---|
454 | break;
|
---|
455 | case 8:
|
---|
456 | strcpy(output, "eight");
|
---|
457 | break;
|
---|
458 | case 9:
|
---|
459 | strcpy(output, "nine");
|
---|
460 | case 10:
|
---|
461 | strcpy(output, "ten");
|
---|
462 | default:
|
---|
463 | sprintf(output, "%d", i);
|
---|
464 | }
|
---|
465 | return (output);
|
---|
466 | }
|
---|
467 |
|
---|
468 |
|
---|
469 |
|
---|
470 |
|
---|
471 | /**
|
---|
472 | * Replace all occurences of @p token with @p value while copying @p ip to @p output.
|
---|
473 | * @param ip The input string containing zero or more <tt>token</tt>s. The string will be altered by this function
|
---|
474 | * @param output The output string written with the <tt>token</tt>s replaced by @p value.
|
---|
475 | * @param token The token to be relaced with @p value.
|
---|
476 | * @param value The value to replace @p token.
|
---|
477 | */
|
---|
478 | char *resolve_naff_tokens(char *ip, char *value, char *token)
|
---|
479 | {
|
---|
480 | /*@ buffers *** */
|
---|
481 | char *output = NULL;
|
---|
482 |
|
---|
483 | /*@ pointers * */
|
---|
484 | char *p = NULL; /* points to token to modify */
|
---|
485 | char *q = NULL; /* points to start of string/new string */
|
---|
486 |
|
---|
487 | assert_string_is_neither_NULL_nor_zerolength(token);
|
---|
488 | assert(value != NULL);
|
---|
489 |
|
---|
490 | q = ip;
|
---|
491 | while ((p = strstr(q, token)) != NULL) {
|
---|
492 | *p = '\0';
|
---|
493 | if (output) {
|
---|
494 | mr_strcat(output, "%s%s", q, value);
|
---|
495 | } else {
|
---|
496 | mr_asprintf(output, "%s%s", q, value);
|
---|
497 | }
|
---|
498 | p+= strlen(token);
|
---|
499 | q = p;
|
---|
500 | }
|
---|
501 | if (output) {
|
---|
502 | mr_strcat(output, "%s", q);
|
---|
503 | } else {
|
---|
504 | /* just in case the token doesn't appear in string at all */
|
---|
505 | mr_asprintf(output, "%s", q);
|
---|
506 | }
|
---|
507 | return(output);
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 |
|
---|
512 |
|
---|
513 |
|
---|
514 | /**
|
---|
515 | * Generate the filename of slice @p sliceno of biggiefile @p bigfileno
|
---|
516 | * in @p path with suffix @p s. The format is as follows: @p path, followed
|
---|
517 | * by "/slice-" and @p bigfileno zero-padded to 7 places, followed by
|
---|
518 | * a dot and @p sliceno zero-padded to 5 places, followed by ".dat" and the
|
---|
519 | * suffix. The string is a minimum of 24 characters long.
|
---|
520 | * @param bigfileno The biggiefile number. Starts from 0.
|
---|
521 | * @param sliceno The slice number of biggiefile @p bigfileno. 0 is a "header"
|
---|
522 | * slice (no suffix) containing the biggiestruct, then are the compressed
|
---|
523 | * slices, then an empty uncompressed "trailer" slice.
|
---|
524 | * @param path The path to append (with a / in the middle) to the slice filename.
|
---|
525 | * @param s If not "" then add a "." and this to the end.
|
---|
526 | * @return The slice filename.
|
---|
527 | * @note The returned value points to static storage and will be overwritten with each call.
|
---|
528 | */
|
---|
529 | char *slice_fname(long bigfileno, long sliceno, char *path, char *s)
|
---|
530 | {
|
---|
531 |
|
---|
532 | /*@ buffers **************************************************** */
|
---|
533 | static char output[MAX_STR_LEN];
|
---|
534 | char *suffix = NULL;
|
---|
535 |
|
---|
536 | /*@ end vars *************************************************** */
|
---|
537 |
|
---|
538 | assert_string_is_neither_NULL_nor_zerolength(path);
|
---|
539 | if (s[0] != '\0') {
|
---|
540 | mr_asprintf(suffix, ".%s", s);
|
---|
541 | } else {
|
---|
542 | mr_asprintf(suffix, "");
|
---|
543 | }
|
---|
544 | sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno, suffix);
|
---|
545 | mr_free(suffix);
|
---|
546 | return (output);
|
---|
547 | }
|
---|
548 |
|
---|
549 |
|
---|
550 | /**
|
---|
551 | * Generate a spinning symbol based on integer @p i.
|
---|
552 | * The symbol rotates through the characters / - \ | to form an ASCII "spinner"
|
---|
553 | * if successively written to the same location on screen.
|
---|
554 | * @param i The amount of progress or whatever else to use to determine the character
|
---|
555 | * for this iteration of the spinner.
|
---|
556 | * @return The character for this iteration.
|
---|
557 | */
|
---|
558 | int special_dot_char(int i)
|
---|
559 | {
|
---|
560 | switch (i % 4) {
|
---|
561 | case 0:
|
---|
562 | return ('/');
|
---|
563 | case 1:
|
---|
564 | return ('-');
|
---|
565 | case 2:
|
---|
566 | return ('\\');
|
---|
567 | case 3:
|
---|
568 | return ('|');
|
---|
569 | default:
|
---|
570 | return ('.');
|
---|
571 | }
|
---|
572 | return ('.');
|
---|
573 | }
|
---|
574 |
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * Compare @p stringA and @p stringB. This uses an ASCII sort for everything
|
---|
578 | * up to the digits on the end but a numerical sort for the digits on the end.
|
---|
579 | * @param stringA The first string to compare.
|
---|
580 | * @param stringB The second string to compare.
|
---|
581 | * @return The same as strcmp() - <0 if A<B, 0 if A=B, >0 if A>B.
|
---|
582 | * @note This function only does a numerical sort on the @e last set of numbers. If
|
---|
583 | * there are any in the middle those will be sorted ASCIIbetically.
|
---|
584 | */
|
---|
585 | int strcmp_inc_numbers(char *stringA, char *stringB)
|
---|
586 | {
|
---|
587 | /*@ int ********************************************************* */
|
---|
588 | int i;
|
---|
589 | int start_of_numbers_in_A;
|
---|
590 | int start_of_numbers_in_B;
|
---|
591 | int res;
|
---|
592 |
|
---|
593 | /*@ long ******************************************************* */
|
---|
594 | long numA;
|
---|
595 | long numB;
|
---|
596 |
|
---|
597 | /*@ end vars *************************************************** */
|
---|
598 | assert(stringA != NULL);
|
---|
599 | assert(stringB != NULL);
|
---|
600 |
|
---|
601 | if (strlen(stringA) == strlen(stringB)) {
|
---|
602 | return (strcmp(stringA, stringB));
|
---|
603 | }
|
---|
604 | for (i = (int) strlen(stringA); i > 0 && isdigit(stringA[i - 1]); i--);
|
---|
605 | if (i == (int) strlen(stringA)) {
|
---|
606 | return (strcmp(stringA, stringB));
|
---|
607 | }
|
---|
608 | start_of_numbers_in_A = i;
|
---|
609 | for (i = (int) strlen(stringB); i > 0 && isdigit(stringB[i - 1]); i--);
|
---|
610 | if (i == (int) strlen(stringB)) {
|
---|
611 | return (strcmp(stringA, stringB));
|
---|
612 | }
|
---|
613 | start_of_numbers_in_B = i;
|
---|
614 | if (start_of_numbers_in_A != start_of_numbers_in_B) {
|
---|
615 | return (strcmp(stringA, stringB));
|
---|
616 | }
|
---|
617 | res = strncmp(stringA, stringB, (size_t) i);
|
---|
618 | if (res) {
|
---|
619 | return (res);
|
---|
620 | }
|
---|
621 | numA = atol(stringA + start_of_numbers_in_A);
|
---|
622 | numB = atol(stringB + start_of_numbers_in_B);
|
---|
623 | return ((int) (numA - numB));
|
---|
624 | }
|
---|
625 |
|
---|
626 |
|
---|
627 |
|
---|
628 | /**
|
---|
629 | * Strip excess baggage from @p input, which should be a line from afio.
|
---|
630 | * For now this copies the whole line unless it finds a set of quotes, in which case
|
---|
631 | * it copies their contents only.
|
---|
632 | * @param input The input line (presumably from afio).
|
---|
633 | * @return The stripped line.
|
---|
634 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
635 | */
|
---|
636 | char *strip_afio_output_line(char *input)
|
---|
637 | {
|
---|
638 | /*@ buffer ****************************************************** */
|
---|
639 | static char output[MAX_STR_LEN];
|
---|
640 |
|
---|
641 | /*@ pointers **************************************************** */
|
---|
642 | char *p;
|
---|
643 | char *q;
|
---|
644 | /*@ end vars *************************************************** */
|
---|
645 |
|
---|
646 | assert(input != NULL);
|
---|
647 | strcpy(output, input);
|
---|
648 | p = strchr(input, '\"');
|
---|
649 | if (p) {
|
---|
650 | q = strchr(++p, '\"');
|
---|
651 | if (q) {
|
---|
652 | strcpy(output, p);
|
---|
653 | *(strchr(output, '\"')) = '\0';
|
---|
654 | }
|
---|
655 | }
|
---|
656 | return (output);
|
---|
657 | }
|
---|
658 |
|
---|
659 |
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Remove all characters whose ASCII value is less than or equal to 32
|
---|
663 | * (spaces and control characters) from both sides of @p in_out.
|
---|
664 | * @param in_out The string to strip spaces/control characters from (modified).
|
---|
665 | */
|
---|
666 | void strip_spaces(char *in_out)
|
---|
667 | {
|
---|
668 | /*@ buffers ***************************************************** */
|
---|
669 | char *tmp;
|
---|
670 |
|
---|
671 | /*@ pointers **************************************************** */
|
---|
672 | char *p;
|
---|
673 |
|
---|
674 | /*@ int ******************************************************** */
|
---|
675 | int i;
|
---|
676 | int original_incoming_length;
|
---|
677 |
|
---|
678 | /*@ end vars *************************************************** */
|
---|
679 |
|
---|
680 | assert(in_out != NULL);
|
---|
681 | malloc_string(tmp);
|
---|
682 | original_incoming_length = (int) strlen(in_out);
|
---|
683 | for (i = 0; in_out[i] <= ' ' && i < (int) strlen(in_out); i++);
|
---|
684 | strcpy(tmp, in_out + i);
|
---|
685 | for (i = (int) strlen(tmp); i > 0 && tmp[i - 1] <= 32; i--);
|
---|
686 | tmp[i] = '\0';
|
---|
687 | for (i = 0; i < original_incoming_length && MAX_STR_LEN; i++) {
|
---|
688 | in_out[i] = ' ';
|
---|
689 | }
|
---|
690 | in_out[i] = '\0';
|
---|
691 | i = 0;
|
---|
692 | p = tmp;
|
---|
693 | while (*p != '\0') {
|
---|
694 | in_out[i] = *(p++);
|
---|
695 | in_out[i + 1] = '\0';
|
---|
696 | if (in_out[i] < 32 && i > 0) {
|
---|
697 | if (in_out[i] == 8) {
|
---|
698 | i--;
|
---|
699 | } else if (in_out[i] == 9) {
|
---|
700 | in_out[i++] = ' ';
|
---|
701 | } else if (in_out[i] == '\r') // added 1st October 2003 -- FIXME
|
---|
702 | {
|
---|
703 | strcpy(tmp, in_out + i);
|
---|
704 | strcpy(in_out, tmp);
|
---|
705 | i = -1;
|
---|
706 | continue;
|
---|
707 | } else if (in_out[i] == '\t') {
|
---|
708 | for (i++; i % 5; i++);
|
---|
709 | } else if (in_out[i] >= 10 && in_out[i] <= 13) {
|
---|
710 | break;
|
---|
711 | } else {
|
---|
712 | i--;
|
---|
713 | }
|
---|
714 | } else {
|
---|
715 | i++;
|
---|
716 | }
|
---|
717 | }
|
---|
718 | in_out[i] = '\0';
|
---|
719 | paranoid_free(tmp);
|
---|
720 | }
|
---|
721 |
|
---|
722 |
|
---|
723 | /**
|
---|
724 | * If there are double quotes "" around @p incoming then remove them.
|
---|
725 | * This does not affect other quotes that may be embedded within the string.
|
---|
726 | * @param incoming The string to trim quotes from (modified).
|
---|
727 | * @return @p incoming.
|
---|
728 | */
|
---|
729 | char *trim_empty_quotes(char *incoming)
|
---|
730 | {
|
---|
731 | /*@ buffer ****************************************************** */
|
---|
732 | static char outgoing[MAX_STR_LEN];
|
---|
733 |
|
---|
734 | /*@ end vars *************************************************** */
|
---|
735 | assert(incoming != NULL);
|
---|
736 |
|
---|
737 | if (incoming[0] == '\"' && incoming[strlen(incoming) - 1] == '\"') {
|
---|
738 | strcpy(outgoing, incoming + 1);
|
---|
739 | outgoing[strlen(outgoing) - 1] = '\0';
|
---|
740 | } else {
|
---|
741 | strcpy(outgoing, incoming);
|
---|
742 | }
|
---|
743 | return (outgoing);
|
---|
744 | }
|
---|
745 |
|
---|
746 |
|
---|
747 |
|
---|
748 |
|
---|
749 | /**
|
---|
750 | * Remove any partition info from @p partition, leaving just the drive name.
|
---|
751 | * @param partition The partition name soon-to-become drive name. (modified)
|
---|
752 | * @return @p partition.
|
---|
753 | */
|
---|
754 | char *truncate_to_drive_name(char *partition)
|
---|
755 | {
|
---|
756 | int i = strlen(partition) - 1;
|
---|
757 | char *c;
|
---|
758 | char *trunc = NULL;
|
---|
759 |
|
---|
760 | assert_string_is_neither_NULL_nor_zerolength(partition);
|
---|
761 | mr_asprintf(trunc, "%s", partition);
|
---|
762 |
|
---|
763 | #ifdef __FreeBSD__
|
---|
764 |
|
---|
765 | if (islower(trunc[i])) // BSD subpartition
|
---|
766 | i--;
|
---|
767 | if (trunc[i-1] == 's') {
|
---|
768 | while (isdigit(trunc[i]))
|
---|
769 | i--;
|
---|
770 | i--;
|
---|
771 | }
|
---|
772 | trunc[i+1] = '\0';
|
---|
773 |
|
---|
774 | #else
|
---|
775 |
|
---|
776 | /* first see if it's a devfs style device */
|
---|
777 | c = strrchr(trunc, '/');
|
---|
778 | if (c && strncmp(c, "/part", 5) == 0) {
|
---|
779 | /* yup it's devfs, return the "disc" path */
|
---|
780 | strcpy(c + 1, "disc");
|
---|
781 | return trunc;
|
---|
782 | }
|
---|
783 | /* then see if it's a dm style device */
|
---|
784 | if (c && strncmp(c, "/dm-", 4) == 0) {
|
---|
785 | /* yup it's dm, return the full path */
|
---|
786 | return trunc;
|
---|
787 | }
|
---|
788 |
|
---|
789 |
|
---|
790 | for (i = strlen(trunc); isdigit(trunc[i-1]); i--)
|
---|
791 | continue;
|
---|
792 | if (trunc[i-1] == 'p' && isdigit(trunc[i-2])) {
|
---|
793 | i--;
|
---|
794 | } else {
|
---|
795 | /* Some full devices like this /dev/mapper/mpath0
|
---|
796 | /dev/cciss/c0d0 may be used as partition names */
|
---|
797 | if ((strstr(trunc,"/dev/mapper/mpath") != NULL) ||
|
---|
798 | (strstr(trunc,"/dev/cciss/c") != NULL) ||
|
---|
799 | (strstr(trunc,"/dev/rd/") != NULL)) {
|
---|
800 | return trunc;
|
---|
801 | }
|
---|
802 | }
|
---|
803 | trunc[i] = '\0';
|
---|
804 |
|
---|
805 | #endif
|
---|
806 |
|
---|
807 | return trunc;
|
---|
808 | }
|
---|
809 |
|
---|
810 |
|
---|
811 |
|
---|
812 |
|
---|
813 |
|
---|
814 | /**
|
---|
815 | * Turn a RAID level number (-1 to 5) into a friendly string. The string
|
---|
816 | * is either "Linear RAID" for -1, or " RAID %-2d " (%d = @p raid_level)
|
---|
817 | * for anything else.
|
---|
818 | * @param raid_level The RAID level to stringify.
|
---|
819 | * @return The string form of @p raid_level.
|
---|
820 | * @note The returned value points to static storage that will be overwritten with each call.
|
---|
821 | */
|
---|
822 | char *turn_raid_level_number_to_string(int raid_level)
|
---|
823 | {
|
---|
824 |
|
---|
825 | /*@ buffer ********************************************************** */
|
---|
826 | static char output[MAX_STR_LEN];
|
---|
827 |
|
---|
828 |
|
---|
829 |
|
---|
830 | if (raid_level >= 0) {
|
---|
831 | sprintf(output, " RAID %-2d ", raid_level);
|
---|
832 | } else {
|
---|
833 | sprintf(output, "Linear RAID");
|
---|
834 | }
|
---|
835 | return (output);
|
---|
836 | }
|
---|
837 |
|
---|
838 |
|
---|
839 |
|
---|
840 |
|
---|
841 |
|
---|
842 |
|
---|
843 |
|
---|
844 |
|
---|
845 |
|
---|
846 | /**
|
---|
847 | * Determine the severity (1-3, 1 being low) of the fact that
|
---|
848 | * @p fn changed in the live filesystem (verify/compare).
|
---|
849 | * @param fn The filename that changed.
|
---|
850 | * @param out_reason If non-NULL, a descriptive reason for the difference will be copied here.
|
---|
851 | * @return The severity (1-3).
|
---|
852 | */
|
---|
853 | int severity_of_difference(char *fn, char **out_reason) {
|
---|
854 |
|
---|
855 | int sev = 3;
|
---|
856 | char *reason = NULL;
|
---|
857 | char *filename = NULL;
|
---|
858 |
|
---|
859 | // out_reason might be null on purpose, so don't bomb if it is :) OK?
|
---|
860 | assert_string_is_neither_NULL_nor_zerolength(fn);
|
---|
861 | if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
|
---|
862 | mr_asprintf(filename, "%s", fn + strlen(MNT_RESTORING));
|
---|
863 | } else if (fn[0] != '/') {
|
---|
864 | mr_asprintf(filename, "/%s", fn);
|
---|
865 | } else {
|
---|
866 | mr_asprintf(filename, "%s", fn);
|
---|
867 | }
|
---|
868 |
|
---|
869 | mr_asprintf(reason, "Changed since backup. Consider running a differential backup in a day or two.");
|
---|
870 |
|
---|
871 | if (!strncmp(filename, "/var/", 5)) {
|
---|
872 | sev = 2;
|
---|
873 | mr_free(reason);
|
---|
874 | mr_asprintf(reason, "/var's contents will change regularly, inevitably.");
|
---|
875 | }
|
---|
876 | if (!strncmp(filename, "/home", 5)) {
|
---|
877 | sev = 2;
|
---|
878 | mr_free(reason);
|
---|
879 | mr_asprintf(reason, "It's in your /home directory. Therefore, it is important.");
|
---|
880 | }
|
---|
881 | if (!strncmp(filename, "/usr/", 5)) {
|
---|
882 | sev = 3;
|
---|
883 | mr_free(reason);
|
---|
884 | mr_asprintf(reason, "You may have installed/removed software during the backup.");
|
---|
885 | }
|
---|
886 | if (!strncmp(filename, "/etc/", 5)) {
|
---|
887 | sev = 3;
|
---|
888 | mr_free(reason);
|
---|
889 | mr_asprintf(reason, "Do not edit config files while backing up your PC.");
|
---|
890 | }
|
---|
891 | if (!strcmp(filename, "/etc/adjtime")
|
---|
892 | || !strcmp(filename, "/etc/mtab")) {
|
---|
893 | sev = 1;
|
---|
894 | mr_free(reason);
|
---|
895 | mr_asprintf(reason, "This file changes all the time. It's OK.");
|
---|
896 | }
|
---|
897 | if (!strncmp(filename, "/root/", 6)) {
|
---|
898 | sev = 3;
|
---|
899 | mr_free(reason);
|
---|
900 | mr_asprintf(reason, "Were you compiling/editing something in /root?");
|
---|
901 | }
|
---|
902 | if (!strncmp(filename, "/root/.", 7)) {
|
---|
903 | sev = 2;
|
---|
904 | mr_free(reason);
|
---|
905 | mr_asprintf(reason, "Temp or 'dot' files changed in /root.");
|
---|
906 | }
|
---|
907 | if (!strncmp(filename, "/var/lib/", 9)) {
|
---|
908 | sev = 2;
|
---|
909 | mr_free(reason);
|
---|
910 | mr_asprintf(reason, "Did you add/remove software during backing?");
|
---|
911 | }
|
---|
912 | if (!strncmp(filename, "/var/lib/rpm", 12)) {
|
---|
913 | sev = 3;
|
---|
914 | mr_free(reason);
|
---|
915 | mr_asprintf(reason, "Did you add/remove software during backing?");
|
---|
916 | }
|
---|
917 | if (!strncmp(filename, "/var/lib/slocate", 16)) {
|
---|
918 | sev = 1;
|
---|
919 | mr_free(reason);
|
---|
920 | mr_asprintf(reason, "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
|
---|
921 | }
|
---|
922 | if (!strncmp(filename, "/var/log/", 9)
|
---|
923 | || strstr(filename, "/.xsession")
|
---|
924 | || !strcmp(filename + strlen(filename) - 4, ".log")) {
|
---|
925 | sev = 1;
|
---|
926 | mr_free(reason);
|
---|
927 | mr_asprintf(reason, "Log files change frequently as the computer runs. Fret not.");
|
---|
928 | }
|
---|
929 | if (!strncmp(filename, "/var/spool", 10)) {
|
---|
930 | sev = 1;
|
---|
931 | mr_free(reason);
|
---|
932 | mr_asprintf(reason, "Background processes or printers were active. This does not affect the integrity of your backup.");
|
---|
933 | }
|
---|
934 | if (!strncmp(filename, "/var/spool/mail", 10)) {
|
---|
935 | sev = 2;
|
---|
936 | mr_free(reason);
|
---|
937 | mr_asprintf(reason, "Mail was sent/received during backup.");
|
---|
938 | }
|
---|
939 | if (filename[strlen(filename) - 1] == '~') {
|
---|
940 | sev = 1;
|
---|
941 | mr_free(reason);
|
---|
942 | mr_asprintf(reason, "Backup copy of another file which was modified recently.");
|
---|
943 | }
|
---|
944 | if (strstr(filename, "cache")) {
|
---|
945 | sev = 1;
|
---|
946 | mr_free(reason);
|
---|
947 | mr_asprintf(reason, "Part of a cache of data. Caches change from time to time. Don't worry.");
|
---|
948 | }
|
---|
949 | if (!strncmp(filename, "/var/run/", 9)
|
---|
950 | || !strncmp(filename, "/var/lock", 8)
|
---|
951 | || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP")
|
---|
952 | || strstr(filename, "/.Xauthority")) {
|
---|
953 | sev = 1;
|
---|
954 | mr_free(reason);
|
---|
955 | mr_asprintf(reason, "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
|
---|
956 | }
|
---|
957 |
|
---|
958 | if (reason != NULL) {
|
---|
959 | *out_reason = reason;
|
---|
960 | }
|
---|
961 | mr_free(filename);
|
---|
962 | return (sev);
|
---|
963 | }
|
---|
964 |
|
---|
965 |
|
---|
966 |
|
---|
967 | /**
|
---|
968 | * Compare the filenames in two filelist entries (s_filelist_entry*) casted
|
---|
969 | * to void*.
|
---|
970 | * @param va The first filelist entry, cast as a @c void pointer.
|
---|
971 | * @param vb The second filelist entry, cast as a @c void pointer.
|
---|
972 | * @return The return value of strcmp().
|
---|
973 | */
|
---|
974 | int compare_two_filelist_entries(void *va, void *vb)
|
---|
975 | {
|
---|
976 | static int res;
|
---|
977 | struct s_filelist_entry *fa, *fb;
|
---|
978 |
|
---|
979 | assert(va != NULL);
|
---|
980 | assert(vb != NULL);
|
---|
981 | fa = (struct s_filelist_entry *) va;
|
---|
982 | fb = (struct s_filelist_entry *) vb;
|
---|
983 | res = strcmp(fa->filename, fb->filename);
|
---|
984 | return (res);
|
---|
985 | }
|
---|
986 |
|
---|
987 |
|
---|
988 |
|
---|
989 |
|
---|
990 |
|
---|
991 |
|
---|
992 |
|
---|
993 | /**
|
---|
994 | * Generate a line intended to be passed to update_evalcall_form(), indicating
|
---|
995 | * the current media fill percentage (or number of kilobytes if size is not known).
|
---|
996 | * @param bkpinfo The backup media structure. Fields used:
|
---|
997 | * - @c bkpinfo->backup_media_type
|
---|
998 | * - @c bkpinfo->media_size
|
---|
999 | * - @c bkpinfo->scratchdir
|
---|
1000 | * @return The string indicating media fill.
|
---|
1001 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
1002 | */
|
---|
1003 | char *percent_media_full_comment()
|
---|
1004 | {
|
---|
1005 | /*@ int *********************************************** */
|
---|
1006 | int percentage;
|
---|
1007 | int j;
|
---|
1008 |
|
---|
1009 | /*@ buffers ******************************************* */
|
---|
1010 | char *outstr = NULL;
|
---|
1011 | char *pos_w_commas = NULL;
|
---|
1012 | char *mds = NULL;
|
---|
1013 | char *tmp = NULL;
|
---|
1014 |
|
---|
1015 | assert(bkpinfo != NULL);
|
---|
1016 |
|
---|
1017 | if (bkpinfo->media_size[g_current_media_number] <= 0) {
|
---|
1018 | mr_asprintf(tmp, "%lld", g_tape_posK);
|
---|
1019 | mr_asprintf(pos_w_commas, "%s", commarize(tmp));
|
---|
1020 | mr_free(tmp);
|
---|
1021 | mr_asprintf(outstr, "Volume %d: %s kilobytes archived so far", g_current_media_number, pos_w_commas);
|
---|
1022 | mr_free(pos_w_commas);
|
---|
1023 | return (outstr);
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | /* update screen */
|
---|
1027 | if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
|
---|
1028 | percentage =
|
---|
1029 | (int) (g_tape_posK / 10 /
|
---|
1030 | bkpinfo->media_size[g_current_media_number]);
|
---|
1031 | if (percentage > 100) {
|
---|
1032 | percentage = 100;
|
---|
1033 | }
|
---|
1034 | mr_asprintf(outstr, "Volume %d: [", g_current_media_number);
|
---|
1035 | } else {
|
---|
1036 | percentage =
|
---|
1037 | (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 /
|
---|
1038 | bkpinfo->media_size[g_current_media_number]);
|
---|
1039 | mds = media_descriptor_string(bkpinfo->backup_media_type);
|
---|
1040 | mr_asprintf(outstr, "%s %d: [", mds, g_current_media_number);
|
---|
1041 | mr_free(mds);
|
---|
1042 | }
|
---|
1043 | for (j = 0; j < percentage; j += 5) {
|
---|
1044 | mr_strcat(outstr, "*");
|
---|
1045 | }
|
---|
1046 | for (; j < 100; j += 5) {
|
---|
1047 | mr_strcat(outstr, ".");
|
---|
1048 | }
|
---|
1049 | mr_strcat(outstr, "] %d%% used", percentage);
|
---|
1050 | return (outstr);
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | /**
|
---|
1054 | * Get a string form of @p type_of_bkp.
|
---|
1055 | * @param type_of_bkp The backup type to stringify.
|
---|
1056 | * @return The stringification of @p type_of_bkp.
|
---|
1057 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
1058 | */
|
---|
1059 | char *media_descriptor_string(t_bkptype type_of_bkp) {
|
---|
1060 |
|
---|
1061 | char *type_of_backup = NULL;
|
---|
1062 |
|
---|
1063 | switch (type_of_bkp) {
|
---|
1064 | case dvd:
|
---|
1065 | mr_asprintf(type_of_backup, "DVD");
|
---|
1066 | break;
|
---|
1067 | case cdr:
|
---|
1068 | mr_asprintf(type_of_backup, "CDR");
|
---|
1069 | break;
|
---|
1070 | case cdrw:
|
---|
1071 | mr_asprintf(type_of_backup, "CDRW");
|
---|
1072 | break;
|
---|
1073 | case tape:
|
---|
1074 | mr_asprintf(type_of_backup, "tape");
|
---|
1075 | break;
|
---|
1076 | case cdstream:
|
---|
1077 | mr_asprintf(type_of_backup, "CDR");
|
---|
1078 | break;
|
---|
1079 | case udev:
|
---|
1080 | mr_asprintf(type_of_backup, "udev");
|
---|
1081 | break;
|
---|
1082 | case iso:
|
---|
1083 | mr_asprintf(type_of_backup, "ISO");
|
---|
1084 | break;
|
---|
1085 | case netfs:
|
---|
1086 | mr_asprintf(type_of_backup, "%s", bkpinfo->netfs_proto);
|
---|
1087 | break;
|
---|
1088 | case usb:
|
---|
1089 | mr_asprintf(type_of_backup, "USB");
|
---|
1090 | break;
|
---|
1091 | default:
|
---|
1092 | mr_asprintf(type_of_backup, "ISO");
|
---|
1093 | }
|
---|
1094 | return (type_of_backup);
|
---|
1095 | }
|
---|