source: MondoRescue/trunk/mondo/mondo/src/libmondo-string.c@ 795

Last change on this file since 795 was 783, checked in by Bruno Cornec, 18 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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