source: MondoRescue/branches/stable/mondo/src/common/libmondo-string.c@ 1817

Last change on this file since 1817 was 1693, checked in by Bruno Cornec, 17 years ago
  • Remove useless copy from mindi to mondo at end of USB handling
  • Fix PB macro vs tools/*2build issue
  • make_usb_fs change of interface (doesn't need a parameter)
  • Fix USB support in mondo to avoid multiple copies of files
  • Use first partiion in mondo for USB device
  • Fixes for USB CLI for both mondo/mindi
  • Try to add USB support for mondoarchive with new functions
  • du => deb for similarity with other distro type under pbconf
  • migrate gento build files under pb
  • remove now obsolete rpm spec file and gentoo build files from distributions
  • Remove DOCDIR usage in mindi + various build fixes

(merge -r1680:1692 $SVN_M/branches/2.2.5)

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