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

Last change on this file since 1178 was 1178, checked in by Bruno Cornec, 17 years ago

Continue to merge trunk memory management enhancements for libmondo-tools.c & libmondo-string.c
Introduction + test of a new function mr_strcat

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