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

Last change on this file since 3885 was 3882, checked in by Bruno Cornec, 4 months ago

More cleanup

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