source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-string.c@ 2413

Last change on this file since 2413 was 2413, checked in by Bruno Cornec, 15 years ago
  • Remove function turn_wildcard_chars_into_literal_chars and replace it with a call to mr_stresc more generic
  • Property svn:keywords set to Id
File size: 28.3 KB
Line 
1/* $Id: libmondo-string.c 2413 2009-09-20 00:44:23Z 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 2413 2009-09-20 00:44:23Z 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 partition.
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 *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 */
74void 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 */
113char *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 */
160char *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 */
184long 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 */
242char *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 */
271char *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 */
361char *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 */
389char *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 */
421char *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 */
478char *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 */
529char *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 */
558int 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 */
585int 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 */
636char *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 */
666void 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 */
729char *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 */
754char *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 */
822char *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 */
853int severity_of_difference(char *fn, char *out_reason) {
854
855 int sev;
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 sev = 3;
870 mr_asprintf(reason, "Changed since backup. Consider running a differential backup in a day or two.");
871
872 if (!strncmp(filename, "/var/", 5)) {
873 sev = 2;
874 mr_free(reason);
875 mr_asprintf(reason, "/var's contents will change regularly, inevitably.");
876 }
877 if (!strncmp(filename, "/home", 5)) {
878 sev = 2;
879 mr_free(reason);
880 mr_asprintf(reason, "It's in your /home directory. Therefore, it is important.");
881 }
882 if (!strncmp(filename, "/usr/", 5)) {
883 sev = 3;
884 mr_free(reason);
885 mr_asprintf(reason, "You may have installed/removed software during the backup.");
886 }
887 if (!strncmp(filename, "/etc/", 5)) {
888 sev = 3;
889 mr_free(reason);
890 mr_asprintf(reason, "Do not edit config files while backing up your PC.");
891 }
892 if (!strcmp(filename, "/etc/adjtime")
893 || !strcmp(filename, "/etc/mtab")) {
894 sev = 1;
895 mr_free(reason);
896 mr_asprintf(reason, "This file changes all the time. It's OK.");
897 }
898 if (!strncmp(filename, "/root/", 6)) {
899 sev = 3;
900 mr_free(reason);
901 mr_asprintf(reason, "Were you compiling/editing something in /root?");
902 }
903 if (!strncmp(filename, "/root/.", 7)) {
904 sev = 2;
905 mr_free(reason);
906 mr_asprintf(reason, "Temp or 'dot' files changed in /root.");
907 }
908 if (!strncmp(filename, "/var/lib/", 9)) {
909 sev = 2;
910 mr_free(reason);
911 mr_asprintf(reason, "Did you add/remove software during backing?");
912 }
913 if (!strncmp(filename, "/var/lib/rpm", 12)) {
914 sev = 3;
915 mr_free(reason);
916 mr_asprintf(reason, "Did you add/remove software during backing?");
917 }
918 if (!strncmp(filename, "/var/lib/slocate", 16)) {
919 sev = 1;
920 mr_free(reason);
921 mr_asprintf(reason, "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
922 }
923 if (!strncmp(filename, "/var/log/", 9)
924 || strstr(filename, "/.xsession")
925 || !strcmp(filename + strlen(filename) - 4, ".log")) {
926 sev = 1;
927 mr_free(reason);
928 mr_asprintf(reason, "Log files change frequently as the computer runs. Fret not.");
929 }
930 if (!strncmp(filename, "/var/spool", 10)) {
931 sev = 1;
932 mr_free(reason);
933 mr_asprintf(reason, "Background processes or printers were active. This does not affect the integrity of your backup.");
934 }
935 if (!strncmp(filename, "/var/spool/mail", 10)) {
936 sev = 2;
937 mr_free(reason);
938 mr_asprintf(reason, "Mail was sent/received during backup.");
939 }
940 if (filename[strlen(filename) - 1] == '~') {
941 sev = 1;
942 mr_free(reason);
943 mr_asprintf(reason, "Backup copy of another file which was modified recently.");
944 }
945 if (strstr(filename, "cache")) {
946 sev = 1;
947 mr_free(reason);
948 mr_asprintf(reason, "Part of a cache of data. Caches change from time to time. Don't worry.");
949 }
950 if (!strncmp(filename, "/var/run/", 9)
951 || !strncmp(filename, "/var/lock", 8)
952 || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP")
953 || strstr(filename, "/.Xauthority")) {
954 sev = 1;
955 mr_free(reason);
956 mr_asprintf(reason, "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
957 }
958 if (out_reason) {
959 strcpy(out_reason, reason);
960 }
961 mr_free(filename);
962 mr_free(reason);
963 return (sev);
964}
965
966
967
968/**
969 * Compare the filenames in two filelist entries (s_filelist_entry*) casted
970 * to void*.
971 * @param va The first filelist entry, cast as a @c void pointer.
972 * @param vb The second filelist entry, cast as a @c void pointer.
973 * @return The return value of strcmp().
974 */
975int compare_two_filelist_entries(void *va, void *vb)
976{
977 static int res;
978 struct s_filelist_entry *fa, *fb;
979
980 assert(va != NULL);
981 assert(vb != NULL);
982 fa = (struct s_filelist_entry *) va;
983 fb = (struct s_filelist_entry *) vb;
984 res = strcmp(fa->filename, fb->filename);
985 return (res);
986}
987
988
989
990
991
992
993
994/**
995 * Generate a line intended to be passed to update_evalcall_form(), indicating
996 * the current media fill percentage (or number of kilobytes if size is not known).
997 * @param bkpinfo The backup media structure. Fields used:
998 * - @c bkpinfo->backup_media_type
999 * - @c bkpinfo->media_size
1000 * - @c bkpinfo->scratchdir
1001 * @return The string indicating media fill.
1002 * @note The returned string points to static storage that will be overwritten with each call.
1003 */
1004char *percent_media_full_comment()
1005{
1006 /*@ int *********************************************** */
1007 int percentage;
1008 int j;
1009
1010 /*@ buffers ******************************************* */
1011 char *outstr = NULL;
1012 char *pos_w_commas = NULL;
1013 char *mds = NULL;
1014 char *tmp = NULL;
1015
1016 assert(bkpinfo != NULL);
1017
1018 if (bkpinfo->media_size[g_current_media_number] <= 0) {
1019 mr_asprintf(tmp, "%lld", g_tape_posK);
1020 mr_asprintf(pos_w_commas, "%s", commarize(tmp));
1021 mr_free(tmp);
1022 mr_asprintf(outstr, "Volume %d: %s kilobytes archived so far", g_current_media_number, pos_w_commas);
1023 mr_free(pos_w_commas);
1024 return (outstr);
1025 }
1026
1027/* update screen */
1028 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1029 percentage =
1030 (int) (g_tape_posK / 10 /
1031 bkpinfo->media_size[g_current_media_number]);
1032 if (percentage > 100) {
1033 percentage = 100;
1034 }
1035 mr_asprintf(outstr, "Volume %d: [", g_current_media_number);
1036 } else {
1037 percentage =
1038 (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 /
1039 bkpinfo->media_size[g_current_media_number]);
1040 mds = media_descriptor_string(bkpinfo->backup_media_type);
1041 mr_asprintf(outstr, "%s %d: [", mds, g_current_media_number);
1042 mr_free(mds);
1043 }
1044 for (j = 0; j < percentage; j += 5) {
1045 mr_strcat(outstr, "*");
1046 }
1047 for (; j < 100; j += 5) {
1048 mr_strcat(outstr, ".");
1049 }
1050 mr_strcat(outstr, "] %d%% used", percentage);
1051 return (outstr);
1052}
1053
1054/**
1055 * Get a string form of @p type_of_bkp.
1056 * @param type_of_bkp The backup type to stringify.
1057 * @return The stringification of @p type_of_bkp.
1058 * @note The returned string points to static storage that will be overwritten with each call.
1059 */
1060char *media_descriptor_string(t_bkptype type_of_bkp) {
1061
1062 char *type_of_backup = NULL;
1063
1064 switch (type_of_bkp) {
1065 case dvd:
1066 mr_asprintf(type_of_backup, "DVD");
1067 break;
1068 case cdr:
1069 mr_asprintf(type_of_backup, "CDR");
1070 break;
1071 case cdrw:
1072 mr_asprintf(type_of_backup, "CDRW");
1073 break;
1074 case tape:
1075 mr_asprintf(type_of_backup, "tape");
1076 break;
1077 case cdstream:
1078 mr_asprintf(type_of_backup, "CDR");
1079 break;
1080 case udev:
1081 mr_asprintf(type_of_backup, "udev");
1082 break;
1083 case iso:
1084 mr_asprintf(type_of_backup, "ISO");
1085 break;
1086 case netfs:
1087 mr_asprintf(type_of_backup, "%s", bkpinfo->netfs_proto);
1088 break;
1089 case usb:
1090 mr_asprintf(type_of_backup, "USB");
1091 break;
1092 default:
1093 mr_asprintf(type_of_backup, "ISO");
1094 }
1095 return (type_of_backup);
1096}
Note: See TracBrowser for help on using the repository browser.