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

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