source: MondoRescue/branches/3.2/mondo/src/common/libmondo-string.c@ 3611

Last change on this file since 3611 was 3611, checked in by Bruno Cornec, 7 years ago

Remove more static allocation

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