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

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

remove center_string function to keep only mr_center_string

  • Property svn:keywords set to Id
File size: 27.9 KB
Line 
1/* $Id: libmondo-string.c 3858 2024-03-06 23:52:46Z bruno $ */
2
3
4/**
5 * @file
6 * Functions for handling strings.
7 */
8
9#include "my-stuff.h"
10#include "mr_mem.h"
11#include "mr_str.h"
12#include "mondostructures.h"
13#include "libmondo-string.h"
14#include "lib-common-externs.h"
15#include "libmondo-files-EXT.h"
16#include "libmondo-gui-EXT.h"
17#include "libmondo-tools-EXT.h"
18
19/*@unused@*/
20//static char cvsid[] = "$Id: libmondo-string.c 3858 2024-03-06 23:52:46Z bruno $";
21
22extern int g_current_media_number;
23extern long long g_tape_posK;
24
25/* Reference to global bkpinfo */
26extern struct s_bkpinfo *bkpinfo;
27
28
29/**
30 * @addtogroup stringGroup
31 * @{
32 */
33/**
34 * Build a partition name from a drive and a partition number.
35 * @param drive The drive basename of the partition name (e.g. /dev/hda)
36 * @param partno The partition number (e.g. 1)
37 * @param partition Where to put the partition name (e.g. /dev/hda1)
38 * @return @p partition.
39 * @note If @p drive ends in a digit, then 'p' (on Linux) or 's' (on *BSD) is added before @p partno.
40 */
41char *build_partition_name(char *partition, const char *drive, int partno)
42{
43 char *p, *c;
44
45 assert(partition != NULL);
46 assert_string_is_neither_NULL_nor_zerolength(drive);
47 assert(partno >= 0);
48
49 p = strcpy(partition, drive);
50 /* is this a devfs device path? */
51 c = strrchr(partition, '/');
52 if (c && strncmp(c, "/disc", 5) == 0) {
53 /* yup it's devfs, return the "part" path */
54 /* This strcpy is safe */
55 strcpy(c + 1, "part");
56 p = c + 5;
57 } else {
58 p += strlen(p);
59 if (isdigit(p[-1])) {
60 *p++ =
61#ifdef BSD
62 's';
63#else
64 'p';
65#endif
66 }
67 }
68 sprintf(p, "%d", partno);
69 return(partition);
70}
71
72
73/**
74 * Pad a string on both sides so it appears centered.
75 * @param in 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, "zero");
390 break;
391 case 1:
392 mr_asprintf(output, "one");
393 break;
394 case 2:
395 mr_asprintf(output, "two");
396 break;
397 case 3:
398 mr_asprintf(output, "three");
399 break;
400 case 4:
401 mr_asprintf(output, "four");
402 break;
403 case 5:
404 mr_asprintf(output, "five");
405 break;
406 case 6:
407 mr_asprintf(output, "six");
408 break;
409 case 7:
410 mr_asprintf(output, "seven");
411 break;
412 case 8:
413 mr_asprintf(output, "eight");
414 break;
415 case 9:
416 mr_asprintf(output, "nine");
417 case 10:
418 mr_asprintf(output, "ten");
419 default:
420 mr_asprintf(output, "%d", i);
421 }
422 return (output);
423}
424
425
426
427
428/**
429 * Replace all occurences of @p token with @p value while copying @p ip to @p output.
430 * @param ip The input string containing zero or more <tt>token</tt>s.
431 * @param output The output string written with the <tt>token</tt>s replaced by @p value.
432 * @param token The token to be relaced with @p value.
433 * @param value The value to replace @p token.
434 */
435
436/* TODO: consider mr_strtok */
437void resolve_naff_tokens(char *output, char *ip, char *value, const char *token)
438{
439 /*@ buffers *** */
440 char *input;
441
442 /*@ pointers * */
443 char *p;
444
445 input = malloc(2000);
446 assert(value != NULL);
447
448 strcpy(output, ip); /* just in case the token doesn't appear in string at all */
449 for (strcpy(input, ip); strstr(input, token); strcpy(input, output)) {
450 strcpy(output, input);
451 p = strstr(output, token);
452 *p = '\0';
453 strcat(output, value);
454 p = strstr(input, token) + strlen(token);
455 strcat(output, p);
456 }
457 paranoid_free(input);
458}
459
460
461
462
463
464/**
465 * Generate the filename of slice @p sliceno of biggiefile @p bigfileno
466 * in @p path with suffix @p s. The format is as follows: @p path, followed
467 * by "/slice-" and @p bigfileno zero-padded to 7 places, followed by
468 * a dot and @p sliceno zero-padded to 5 places, followed by ".dat" and the
469 * suffix. The string is a minimum of 24 characters long.
470 * @param bigfileno The biggiefile number. Starts from 0.
471 * @param sliceno The slice number of biggiefile @p bigfileno. 0 is a "header"
472 * slice (no suffix) containing the biggiestruct, then are the compressed
473 * slices, then an empty uncompressed "trailer" slice.
474 * @param path The path to append (with a / in the middle) to the slice filename.
475 * @param s If not "" then add a "." and this to the end.
476 * @return The slice filename.
477 * @note The returned value points to static storage and will be overwritten with each call.
478 */
479char *slice_fname(long bigfileno, long sliceno, char *path, char *s)
480{
481
482 /*@ buffers **************************************************** */
483 static char output[MAX_STR_LEN];
484 char *suffix = NULL;
485
486 /*@ end vars *************************************************** */
487
488 assert_string_is_neither_NULL_nor_zerolength(path);
489 if (s[0] != '\0') {
490 mr_asprintf(suffix, ".%s", s);
491 } else {
492 mr_asprintf(suffix, "");
493 }
494 sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno, suffix);
495 mr_free(suffix);
496 return (output);
497}
498
499
500/**
501 * Generate a spinning symbol based on integer @p i.
502 * The symbol rotates through the characters / - \ | to form an ASCII "spinner"
503 * if successively written to the same location on screen.
504 * @param i The amount of progress or whatever else to use to determine the character
505 * for this iteration of the spinner.
506 * @return The character for this iteration.
507 */
508int special_dot_char(int i)
509{
510 switch (i % 4) {
511 case 0:
512 return ('/');
513 case 1:
514 return ('-');
515 case 2:
516 return ('\\');
517 case 3:
518 return ('|');
519 default:
520 return ('.');
521 }
522 return ('.');
523}
524
525
526/**
527 * Compare @p stringA and @p stringB. This uses an ASCII sort for everything
528 * up to the digits on the end but a numerical sort for the digits on the end.
529 * @param stringA The first string to compare.
530 * @param stringB The second string to compare.
531 * @return The same as strcmp() - <0 if A<B, 0 if A=B, >0 if A>B.
532 * @note This function only does a numerical sort on the @e last set of numbers. If
533 * there are any in the middle those will be sorted ASCIIbetically.
534 */
535int strcmp_inc_numbers(char *stringA, char *stringB)
536{
537 /*@ int ********************************************************* */
538 int i;
539 int start_of_numbers_in_A;
540 int start_of_numbers_in_B;
541 int res;
542
543 /*@ long ******************************************************* */
544 long numA;
545 long numB;
546
547 /*@ end vars *************************************************** */
548 assert(stringA != NULL);
549 assert(stringB != NULL);
550
551 if (strlen(stringA) == strlen(stringB)) {
552 return (strcmp(stringA, stringB));
553 }
554 for (i = (int) strlen(stringA); i > 0 && isdigit(stringA[i - 1]); i--);
555 if (i == (int) strlen(stringA)) {
556 return (strcmp(stringA, stringB));
557 }
558 start_of_numbers_in_A = i;
559 for (i = (int) strlen(stringB); i > 0 && isdigit(stringB[i - 1]); i--);
560 if (i == (int) strlen(stringB)) {
561 return (strcmp(stringA, stringB));
562 }
563 start_of_numbers_in_B = i;
564 if (start_of_numbers_in_A != start_of_numbers_in_B) {
565 return (strcmp(stringA, stringB));
566 }
567 res = strncmp(stringA, stringB, (size_t) i);
568 if (res) {
569 return (res);
570 }
571 numA = atol(stringA + start_of_numbers_in_A);
572 numB = atol(stringB + start_of_numbers_in_B);
573 return ((int) (numA - numB));
574}
575
576
577
578/**
579 * Strip excess baggage from @p input, which should be a line from afio.
580 * For now this copies the whole line unless it finds a set of quotes, in which case
581 * it copies their contents only.
582 * @param input The input line (presumably from afio).
583 * @return The stripped line.
584 * @note The returned string points to static storage that will be overwritten with each call.
585 */
586char *strip_afio_output_line(char *input)
587{
588 /*@ buffer ****************************************************** */
589 static char output[MAX_STR_LEN];
590
591 /*@ pointers **************************************************** */
592 char *p;
593 char *q;
594 /*@ end vars *************************************************** */
595
596 assert(input != NULL);
597 strcpy(output, input);
598 p = strchr(input, '\"');
599 if (p) {
600 q = strchr(++p, '\"');
601 if (q) {
602 strcpy(output, p);
603 *(strchr(output, '\"')) = '\0';
604 }
605 }
606 return (output);
607}
608
609
610
611/**
612 * Remove all characters whose ASCII value is less than or equal to 32
613 * (spaces and control characters) from both sides of @p in_out.
614 * @param in_out The string to strip spaces/control characters from (modified).
615 */
616void strip_spaces(char *in_out)
617{
618 /*@ buffers ***************************************************** */
619 char *tmp = NULL;
620
621 /*@ int ******************************************************** */
622 int i;
623
624 /*@ end vars *************************************************** */
625
626 assert(in_out != NULL);
627 malloc_string(tmp);
628 for (i = 0; in_out[i] <= ' ' && i < (int) strlen(in_out) -1; i++);
629 strcpy(tmp, in_out + i);
630 for (i = (int) strlen(tmp) -1; i >= 0 && tmp[i] <= ' '; i--);
631 i++;
632 tmp[i] = '\0';
633
634 // Now tmp contains the stripped string
635 strcpy(in_out,tmp);
636 paranoid_free(tmp);
637}
638
639/**
640 * Remove all characters whose ASCII value is less than or equal to 32
641 * (spaces and control characters) from both sides of @p in_out.
642 * @param in_out The string to strip spaces/control characters from (modified).
643 */
644void strip_spaces2(char *in_out)
645{
646 /*@ buffers ***************************************************** */
647 char *tmp;
648
649 /*@ pointers **************************************************** */
650 char *p;
651
652 /*@ int ******************************************************** */
653 int i;
654 int original_incoming_length;
655
656 /*@ end vars *************************************************** */
657
658 assert(in_out != NULL);
659 malloc_string(tmp);
660 original_incoming_length = (int) strlen(in_out);
661 for (i = 0; in_out[i] <= ' ' && i < (int) strlen(in_out); i++);
662 strcpy(tmp, in_out + i);
663 for (i = (int) strlen(tmp); i > 0 && tmp[i - 1] <= 32; i--);
664 tmp[i] = '\0';
665 for (i = 0; i < original_incoming_length && MAX_STR_LEN; i++) {
666 in_out[i] = ' ';
667 }
668 in_out[i] = '\0';
669 i = 0;
670 p = tmp;
671 while (*p != '\0') {
672 in_out[i] = *(p++);
673 in_out[i + 1] = '\0';
674 if (in_out[i] < 32 && i > 0) {
675 if (in_out[i] == 8) {
676 i--;
677 } else if (in_out[i] == 9) {
678 in_out[i++] = ' ';
679 } else if (in_out[i] == '\r') // added 1st October 2003 -- FIXME
680 {
681 strcpy(tmp, in_out + i);
682 strcpy(in_out, tmp);
683 i = -1;
684 continue;
685 } else if (in_out[i] == '\t') {
686 for (i++; i % 5; i++);
687 } else if (in_out[i] >= 10 && in_out[i] <= 13) {
688 break;
689 } else {
690 i--;
691 }
692 } else {
693 i++;
694 }
695 }
696 in_out[i] = '\0';
697 paranoid_free(tmp);
698}
699
700
701/**
702 * If there are double quotes "" around @p incoming then remove them.
703 * This does not affect other quotes that may be embedded within the string.
704 * @param incoming The string to trim quotes from (modified).
705 * @return @p incoming.
706 */
707char *trim_empty_quotes(char *incoming)
708{
709 /*@ buffer ****************************************************** */
710 static char outgoing[MAX_STR_LEN];
711
712 /*@ end vars *************************************************** */
713 assert(incoming != NULL);
714
715 if (incoming[0] == '\"' && incoming[strlen(incoming) - 1] == '\"') {
716 strcpy(outgoing, incoming + 1);
717 outgoing[strlen(outgoing) - 1] = '\0';
718 } else {
719 strcpy(outgoing, incoming);
720 }
721 return (outgoing);
722}
723
724
725
726
727/**
728 * Remove any partition info from @p partition, leaving just the drive name.
729 * @param partition The partition name soon-to-become drive name. (modified)
730 * @return @p partition.
731 */
732char *truncate_to_drive_name(const char *partition)
733{
734 int i = strlen(partition) - 1;
735 char *c;
736 char *trunc = NULL;
737
738 assert_string_is_neither_NULL_nor_zerolength(partition);
739 mr_asprintf(trunc, "%s", partition);
740
741#ifdef __FreeBSD__
742
743 if (islower(trunc[i])) // BSD subpartition
744 i--;
745 if (trunc[i-1] == 's') {
746 while (isdigit(trunc[i]))
747 i--;
748 i--;
749 }
750 trunc[i+1] = '\0';
751
752#else
753
754 /* first see if it's a devfs style device */
755 c = strrchr(trunc, '/');
756 if (c && strncmp(c, "/part", 5) == 0) {
757 /* yup it's devfs, return the "disc" path */
758 strcpy(c + 1, "disc");
759 return trunc;
760 }
761 /* then see if it's a dm style device */
762 if (c && strncmp(c, "/dm-", 4) == 0) {
763 /* yup it's dm, return the full path */
764 return trunc;
765 }
766
767
768 for (i = strlen(trunc); isdigit(trunc[i-1]); i--)
769 continue;
770 if (trunc[i-1] == 'p' && isdigit(trunc[i-2])) {
771 i--;
772 } else {
773 /* Some full devices like this /dev/mapper/mpath0
774 /dev/cciss/c0d0 may be used as partition names */
775 if ((strstr(trunc,"/dev/mapper/mpath") != NULL) ||
776 (strstr(trunc,"/dev/cciss/c") != NULL) ||
777 (strstr(trunc,"/dev/rd/") != NULL)) {
778 return trunc;
779 }
780 }
781 trunc[i] = '\0';
782
783#endif
784
785 return trunc;
786}
787
788
789
790
791
792/**
793 * Turn a RAID level number (-1 to 5) into a friendly string. The string
794 * is either "Linear RAID" for -1, or " RAID %-2d " (%d = @p raid_level)
795 * for anything else.
796 * @param raid_level The RAID level to stringify.
797 * @return The string form of @p raid_level.
798 * @note The returned value points to storage that needs to be freed by caller
799 */
800char *turn_raid_level_number_to_string(int raid_level)
801{
802
803 /*@ buffer ********************************************************** */
804 char *output = NULL;
805
806
807
808 if (raid_level >= 0) {
809 mr_asprintf(output, " RAID %-2d ", raid_level);
810 } else {
811 mr_asprintf(output, "Linear RAID");
812 }
813 return(output);
814}
815
816
817
818
819
820
821
822
823
824/**
825 * Determine the severity (1-3, 1 being low) of the fact that
826 * @p fn changed in the live filesystem (verify/compare).
827 * @param fn The filename that changed.
828 * @param out_reason If non-NULL, a descriptive reason for the difference will be copied here.
829 * @return The severity (1-3).
830 */
831int severity_of_difference(char *fn, char **out_reason) {
832
833 int sev = 3;
834 char *reason = NULL;
835 char *filename = NULL;
836
837 // out_reason might be null on purpose, so don't bomb if it is :) OK?
838 assert_string_is_neither_NULL_nor_zerolength(fn);
839 if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
840 mr_asprintf(filename, "%s", fn + strlen(MNT_RESTORING));
841 } else if (fn[0] != '/') {
842 mr_asprintf(filename, "/%s", fn);
843 } else {
844 mr_asprintf(filename, "%s", fn);
845 }
846
847 mr_asprintf(reason, "Changed since backup. Consider running a differential backup in a day or two.");
848
849 if (!strncmp(filename, "/var/", 5)) {
850 sev = 2;
851 mr_free(reason);
852 mr_asprintf(reason, "/var's contents will change regularly, inevitably.");
853 }
854 if (!strncmp(filename, "/home", 5)) {
855 sev = 2;
856 mr_free(reason);
857 mr_asprintf(reason, "It's in your /home directory. Therefore, it is important.");
858 }
859 if (!strncmp(filename, "/usr/", 5)) {
860 sev = 3;
861 mr_free(reason);
862 mr_asprintf(reason, "You may have installed/removed software during the backup.");
863 }
864 if (!strncmp(filename, "/etc/", 5)) {
865 sev = 3;
866 mr_free(reason);
867 mr_asprintf(reason, "Do not edit config files while backing up your machine.");
868 }
869 if (!strcmp(filename, "/etc/adjtime")
870 || !strcmp(filename, "/etc/mtab")) {
871 sev = 1;
872 mr_free(reason);
873 mr_asprintf(reason, "This file changes all the time. It's OK.");
874 }
875 if (!strncmp(filename, "/root/", 6)) {
876 sev = 3;
877 mr_free(reason);
878 mr_asprintf(reason, "Were you compiling/editing something in /root?");
879 }
880 if (!strncmp(filename, "/root/.", 7)) {
881 sev = 2;
882 mr_free(reason);
883 mr_asprintf(reason, "Temp or 'dot' files changed in /root.");
884 }
885 if (!strncmp(filename, "/var/lib/", 9)) {
886 sev = 2;
887 mr_free(reason);
888 mr_asprintf(reason, "Did you add/remove software during backing?");
889 }
890 if (!strncmp(filename, "/var/lib/rpm", 12)) {
891 sev = 3;
892 mr_free(reason);
893 mr_asprintf(reason, "Did you add/remove software during backing?");
894 }
895 if (!strncmp(filename, "/var/lib/slocate", 16)) {
896 sev = 1;
897 mr_free(reason);
898 mr_asprintf(reason, "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
899 }
900 if (!strncmp(filename, "/var/log/", 9)
901 || strstr(filename, "/.xsession")
902 || !strcmp(filename + strlen(filename) - 4, ".log")) {
903 sev = 1;
904 mr_free(reason);
905 mr_asprintf(reason, "Log files change frequently as the computer runs. Fret not.");
906 }
907 if (!strncmp(filename, "/var/spool", 10)) {
908 sev = 1;
909 mr_free(reason);
910 mr_asprintf(reason, "Background processes or printers were active. This does not affect the integrity of your backup.");
911 }
912 if (!strncmp(filename, "/var/spool/mail", 10)) {
913 sev = 2;
914 mr_free(reason);
915 mr_asprintf(reason, "Mail was sent/received during backup.");
916 }
917 if (filename[strlen(filename) - 1] == '~') {
918 sev = 1;
919 mr_free(reason);
920 mr_asprintf(reason, "Backup copy of another file which was modified recently.");
921 }
922 if (strstr(filename, "cache")) {
923 sev = 1;
924 mr_free(reason);
925 mr_asprintf(reason, "Part of a cache of data. Caches change from time to time. Don't worry.");
926 }
927 if (!strncmp(filename, "/var/run/", 9)
928 || !strncmp(filename, "/var/lock", 8)
929 || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP")
930 || strstr(filename, "/.Xauthority")) {
931 sev = 1;
932 mr_free(reason);
933 mr_asprintf(reason, "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
934 }
935 if (out_reason) {
936 strcpy(*out_reason, reason);
937 }
938 mr_free(filename);
939 mr_free(reason);
940 return (sev);
941}
942
943
944
945/**
946 * Compare the filenames in two filelist entries (s_filelist_entry*) casted
947 * to void*.
948 * @param va The first filelist entry, cast as a @c void pointer.
949 * @param vb The second filelist entry, cast as a @c void pointer.
950 * @return The return value of strcmp().
951 */
952int compare_two_filelist_entries(void *va, void *vb)
953{
954 static int res;
955 struct s_filelist_entry *fa, *fb;
956
957 assert(va != NULL);
958 assert(vb != NULL);
959 fa = (struct s_filelist_entry *) va;
960 fb = (struct s_filelist_entry *) vb;
961 res = strcmp(fa->filename, fb->filename);
962 return (res);
963}
964
965
966
967
968
969
970
971/**
972 * Generate a line intended to be passed to update_evalcall_form(), indicating
973 * the current media fill percentage (or number of kilobytes if size is not known).
974 * @param bkpinfo The backup media structure. Fields used:
975 * - @c bkpinfo->backup_media_type
976 * - @c bkpinfo->media_size
977 * - @c bkpinfo->scratchdir
978 * @return The string indicating media fill.
979 * @note The returned string points to static storage that will be overwritten with each call.
980 */
981char *percent_media_full_comment()
982{
983 /*@ int *********************************************** */
984 int percentage;
985 int j;
986
987 /*@ buffers ******************************************* */
988 char * outstr = NULL;
989 char *pos_w_commas = NULL;
990 char *mds = NULL;
991
992 assert(bkpinfo != NULL);
993
994 if (bkpinfo->media_size <= 0) {
995 mr_asprintf(pos_w_commas, "%lld", g_tape_posK);
996 mr_asprintf(outstr, "Volume %d: %s kilobytes archived so far", g_current_media_number, pos_w_commas);
997 mr_free(pos_w_commas);
998 return (outstr);
999 }
1000
1001 /* update screen */
1002 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1003 percentage = (int) (g_tape_posK / 10 / bkpinfo->media_size);
1004 if (percentage > 100) {
1005 percentage = 100;
1006 }
1007 mr_asprintf(outstr, "Volume %d: [", g_current_media_number);
1008 } else {
1009 percentage = (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 / bkpinfo->media_size);
1010 mds = media_descriptor_string(bkpinfo->backup_media_type);
1011 mr_asprintf(outstr, "%s %d: [", mds, g_current_media_number);
1012 mr_free(mds);
1013 }
1014 for (j = 0; j < percentage; j += 5) {
1015 mr_strcat(outstr, "*");
1016 }
1017 for (; j < 100; j += 5) {
1018 mr_strcat(outstr, ".");
1019 }
1020 mr_strcat(outstr, "] %d%% used", percentage);
1021 return (outstr);
1022}
1023
1024
1025/**
1026 * Get a string form of @p type_of_bkp.
1027 * @param type_of_bkp The backup type to stringify.
1028 * @return The stringification of @p type_of_bkp.
1029 * @note The returned string points to static storage that will be overwritten with each call.
1030 */
1031char *media_descriptor_string(t_bkptype type_of_bkp) {
1032
1033 char *type_of_backup = NULL;
1034
1035 switch (type_of_bkp) {
1036 case dvd:
1037 mr_asprintf(type_of_backup, "DVD");
1038 break;
1039 case cdr:
1040 mr_asprintf(type_of_backup, "CDR");
1041 break;
1042 case cdrw:
1043 mr_asprintf(type_of_backup, "CDRW");
1044 break;
1045 case tape:
1046 mr_asprintf(type_of_backup, "tape");
1047 break;
1048 case cdstream:
1049 mr_asprintf(type_of_backup, "CDR");
1050 break;
1051 case udev:
1052 mr_asprintf(type_of_backup, "udev");
1053 break;
1054 case iso:
1055 mr_asprintf(type_of_backup, "ISO");
1056 break;
1057 case netfs:
1058 mr_asprintf(type_of_backup, "%s", bkpinfo->netfs_proto);
1059 break;
1060 case usb:
1061 mr_asprintf(type_of_backup, "USB");
1062 break;
1063 default:
1064 mr_asprintf(type_of_backup, "ISO");
1065 }
1066 return (type_of_backup);
1067}
Note: See TracBrowser for help on using the repository browser.