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

Last change on this file since 1663 was 1663, checked in by Bruno Cornec, 13 years ago
  • Fix bug #197 (based on an initial patch of Scott Cummings)
  • Fix a bug where df was using locale to print messages and wasn't filtered correctly
  • mkdtemp checked in configure
  • reset_bkpinfo called as early as possible by both main program.
  • It creates a tmpdir cleanly with mkdtemp in setup_tmpdir subfunction, which takes in account TMPIR and TMP env var. Remains to see what tmpfs does and tests
  • configure.in should also be filtered.
  • Remove g_bkpinfo_DONTUSETHIS
  • remove bkpinfo also from header files
  • Render bkpinfo global (potential issue on thread, but should not be a problem as that structure is indeed static during archive)
  • Apply patch from Andree Leidenfrost, modified a bit to use bkpinfo->tmpdir instead of /tmp or MINDI_CACHE when appropriate. Fix security issues in mondo. Thanks al ot Andree for catching all those issues.
  • /tmp => /var/log for mondorestore.log in mindi
  • Update linux terminfo to fix a color issue (Andree Leidenfrost)
  • Removes useless log file (Andree Leidenfrost)
  • replace vi with find_my_editor during restore (Andree Leidenfrost)
  • sync in bg in mindi (VMWare issue to look at)
  • mindi/mindi-busybox have a different version than mondo for pb
  • PB-SUF also added to spec file
  • Fix a bug for pb build (omission of PB-SUF declaration)

(merge -r1631:1662 $SVN_M/branches/2.2.5)

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