source: MondoRescue/branches/3.3/mondo/src/common/libmondo-string.c

Last change on this file was 3893, checked in by Bruno Cornec, 3 months ago

Remove more warnings - switch and size_t/int comparisons

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