source: trunk/mondo/src/common/libmondo-string.c @ 1185

Last change on this file since 1185 was 1185, checked in by bruno, 13 years ago

Merges in trunk modifications coming from stable (common)

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