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

Last change on this file since 3911 was 3893, checked in by Bruno Cornec, 16 months ago

Remove more warnings - switch and size_t/int comparisons

  • Property svn:keywords set to Id
File size: 27.8 KB
RevLine 
[557]1/* $Id: libmondo-string.c 3893 2024-03-13 15:49:30Z 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 3893 2024-03-13 15:49:30Z 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:
[3892]389 mr_asprintf(output, "%s", "zero");
[128]390 break;
391 case 1:
[3892]392 mr_asprintf(output, "%s", "one");
[128]393 break;
394 case 2:
[3892]395 mr_asprintf(output, "%s", "two");
[128]396 break;
397 case 3:
[3892]398 mr_asprintf(output, "%s", "three");
[128]399 break;
400 case 4:
[3892]401 mr_asprintf(output, "%s", "four");
[128]402 break;
403 case 5:
[3892]404 mr_asprintf(output, "%s", "five");
[128]405 break;
406 case 6:
[3892]407 mr_asprintf(output, "%s", "six");
[128]408 break;
409 case 7:
[3892]410 mr_asprintf(output, "%s", "seven");
[128]411 break;
412 case 8:
[3892]413 mr_asprintf(output, "%s", "eight");
[128]414 break;
415 case 9:
[3892]416 mr_asprintf(output, "%s", "nine");
417 break;
[128]418 case 10:
[3892]419 mr_asprintf(output, "%s", "ten");
420 break;
[128]421 default:
[3611]422 mr_asprintf(output, "%d", i);
[3892]423 break;
[128]424 }
425 return (output);
[1]426}
427
428
429
430
431/**
432 * Replace all occurences of @p token with @p value while copying @p ip to @p output.
433 * @param ip The input string containing zero or more <tt>token</tt>s.
434 * @param output The output string written with the <tt>token</tt>s replaced by @p value.
435 * @param token The token to be relaced with @p value.
436 * @param value The value to replace @p token.
437 */
[3288]438
439/* TODO: consider mr_strtok */
[3851]440void resolve_naff_tokens(char *output, char *ip, char *value, const char *token)
[1]441{
[128]442 /*@ buffers *** */
443 char *input;
[1]444
[128]445 /*@ pointers * */
446 char *p;
[1]447
[128]448 input = malloc(2000);
449 assert(value != NULL);
[1]450
[128]451 strcpy(output, ip); /* just in case the token doesn't appear in string at all */
452 for (strcpy(input, ip); strstr(input, token); strcpy(input, output)) {
453 strcpy(output, input);
454 p = strstr(output, token);
455 *p = '\0';
456 strcat(output, value);
457 p = strstr(input, token) + strlen(token);
458 strcat(output, p);
459 }
460 paranoid_free(input);
[1]461}
462
463
464
465
466
467/**
468 * Generate the filename of slice @p sliceno of biggiefile @p bigfileno
469 * in @p path with suffix @p s. The format is as follows: @p path, followed
470 * by "/slice-" and @p bigfileno zero-padded to 7 places, followed by
471 * a dot and @p sliceno zero-padded to 5 places, followed by ".dat" and the
472 * suffix. The string is a minimum of 24 characters long.
473 * @param bigfileno The biggiefile number. Starts from 0.
474 * @param sliceno The slice number of biggiefile @p bigfileno. 0 is a "header"
475 * slice (no suffix) containing the biggiestruct, then are the compressed
476 * slices, then an empty uncompressed "trailer" slice.
477 * @param path The path to append (with a / in the middle) to the slice filename.
478 * @param s If not "" then add a "." and this to the end.
479 * @return The slice filename.
480 * @note The returned value points to static storage and will be overwritten with each call.
481 */
[128]482char *slice_fname(long bigfileno, long sliceno, char *path, char *s)
[1]483{
484
[128]485 /*@ buffers **************************************************** */
486 static char output[MAX_STR_LEN];
[3191]487 char *suffix = NULL;
[1]488
[128]489 /*@ end vars *************************************************** */
[1]490
[128]491 assert_string_is_neither_NULL_nor_zerolength(path);
492 if (s[0] != '\0') {
[3191]493 mr_asprintf(suffix, ".%s", s);
[128]494 } else {
[3191]495 mr_asprintf(suffix, "");
[128]496 }
[3191]497 sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno, suffix);
498 mr_free(suffix);
[128]499 return (output);
[1]500}
501
502
503/**
504 * Generate a spinning symbol based on integer @p i.
505 * The symbol rotates through the characters / - \ | to form an ASCII "spinner"
506 * if successively written to the same location on screen.
507 * @param i The amount of progress or whatever else to use to determine the character
508 * for this iteration of the spinner.
509 * @return The character for this iteration.
510 */
[128]511int special_dot_char(int i)
[1]512{
[128]513 switch (i % 4) {
514 case 0:
515 return ('/');
516 case 1:
517 return ('-');
518 case 2:
519 return ('\\');
520 case 3:
521 return ('|');
522 default:
523 return ('.');
524 }
525 return ('.');
[1]526}
527
528
529/**
530 * Compare @p stringA and @p stringB. This uses an ASCII sort for everything
531 * up to the digits on the end but a numerical sort for the digits on the end.
532 * @param stringA The first string to compare.
533 * @param stringB The second string to compare.
534 * @return The same as strcmp() - <0 if A<B, 0 if A=B, >0 if A>B.
535 * @note This function only does a numerical sort on the @e last set of numbers. If
536 * there are any in the middle those will be sorted ASCIIbetically.
537 */
[128]538int strcmp_inc_numbers(char *stringA, char *stringB)
[1]539{
[128]540 /*@ int ********************************************************* */
541 int i;
542 int start_of_numbers_in_A;
543 int start_of_numbers_in_B;
544 int res;
[1]545
[128]546 /*@ long ******************************************************* */
547 long numA;
548 long numB;
[1]549
[128]550 /*@ end vars *************************************************** */
551 assert(stringA != NULL);
552 assert(stringB != NULL);
[1]553
[128]554 if (strlen(stringA) == strlen(stringB)) {
555 return (strcmp(stringA, stringB));
556 }
557 for (i = (int) strlen(stringA); i > 0 && isdigit(stringA[i - 1]); i--);
558 if (i == (int) strlen(stringA)) {
559 return (strcmp(stringA, stringB));
560 }
561 start_of_numbers_in_A = i;
562 for (i = (int) strlen(stringB); i > 0 && isdigit(stringB[i - 1]); i--);
563 if (i == (int) strlen(stringB)) {
564 return (strcmp(stringA, stringB));
565 }
566 start_of_numbers_in_B = i;
567 if (start_of_numbers_in_A != start_of_numbers_in_B) {
568 return (strcmp(stringA, stringB));
569 }
570 res = strncmp(stringA, stringB, (size_t) i);
571 if (res) {
572 return (res);
573 }
574 numA = atol(stringA + start_of_numbers_in_A);
575 numB = atol(stringB + start_of_numbers_in_B);
576 return ((int) (numA - numB));
[1]577}
578
579
580
581/**
582 * Strip excess baggage from @p input, which should be a line from afio.
583 * For now this copies the whole line unless it finds a set of quotes, in which case
584 * it copies their contents only.
585 * @param input The input line (presumably from afio).
586 * @return The stripped line.
587 * @note The returned string points to static storage that will be overwritten with each call.
588 */
[128]589char *strip_afio_output_line(char *input)
[1]590{
[128]591 /*@ buffer ****************************************************** */
592 static char output[MAX_STR_LEN];
[1]593
[128]594 /*@ pointers **************************************************** */
595 char *p;
596 char *q;
597 /*@ end vars *************************************************** */
[1]598
[128]599 assert(input != NULL);
600 strcpy(output, input);
601 p = strchr(input, '\"');
602 if (p) {
603 q = strchr(++p, '\"');
604 if (q) {
605 strcpy(output, p);
606 *(strchr(output, '\"')) = '\0';
607 }
[1]608 }
[128]609 return (output);
[1]610}
611
612
613
614/**
615 * Remove all characters whose ASCII value is less than or equal to 32
616 * (spaces and control characters) from both sides of @p in_out.
617 * @param in_out The string to strip spaces/control characters from (modified).
618 */
[128]619void strip_spaces(char *in_out)
[1]620{
[128]621 /*@ buffers ***************************************************** */
[2980]622 char *tmp = NULL;
623
624 /*@ int ******************************************************** */
625 int i;
626
627 /*@ end vars *************************************************** */
628
629 assert(in_out != NULL);
630 malloc_string(tmp);
631 for (i = 0; in_out[i] <= ' ' && i < (int) strlen(in_out) -1; i++);
632 strcpy(tmp, in_out + i);
633 for (i = (int) strlen(tmp) -1; i >= 0 && tmp[i] <= ' '; i--);
634 i++;
635 tmp[i] = '\0';
636
637 // Now tmp contains the stripped string
638 strcpy(in_out,tmp);
639 paranoid_free(tmp);
640}
641
642/**
643 * Remove all characters whose ASCII value is less than or equal to 32
644 * (spaces and control characters) from both sides of @p in_out.
645 * @param in_out The string to strip spaces/control characters from (modified).
646 */
647void strip_spaces2(char *in_out)
648{
649 /*@ buffers ***************************************************** */
[128]650 char *tmp;
[1]651
[128]652 /*@ pointers **************************************************** */
653 char *p;
[1]654
[128]655 /*@ int ******************************************************** */
656 int i;
657 int original_incoming_length;
[1]658
[128]659 /*@ end vars *************************************************** */
[1]660
[128]661 assert(in_out != NULL);
662 malloc_string(tmp);
663 original_incoming_length = (int) strlen(in_out);
664 for (i = 0; in_out[i] <= ' ' && i < (int) strlen(in_out); i++);
665 strcpy(tmp, in_out + i);
666 for (i = (int) strlen(tmp); i > 0 && tmp[i - 1] <= 32; i--);
667 tmp[i] = '\0';
668 for (i = 0; i < original_incoming_length && MAX_STR_LEN; i++) {
669 in_out[i] = ' ';
[1]670 }
[128]671 in_out[i] = '\0';
672 i = 0;
673 p = tmp;
674 while (*p != '\0') {
675 in_out[i] = *(p++);
676 in_out[i + 1] = '\0';
677 if (in_out[i] < 32 && i > 0) {
678 if (in_out[i] == 8) {
679 i--;
680 } else if (in_out[i] == 9) {
681 in_out[i++] = ' ';
682 } else if (in_out[i] == '\r') // added 1st October 2003 -- FIXME
683 {
684 strcpy(tmp, in_out + i);
685 strcpy(in_out, tmp);
686 i = -1;
687 continue;
688 } else if (in_out[i] == '\t') {
689 for (i++; i % 5; i++);
690 } else if (in_out[i] >= 10 && in_out[i] <= 13) {
691 break;
692 } else {
693 i--;
694 }
695 } else {
696 i++;
697 }
[1]698 }
[128]699 in_out[i] = '\0';
700 paranoid_free(tmp);
[1]701}
702
703
704/**
705 * If there are double quotes "" around @p incoming then remove them.
706 * This does not affect other quotes that may be embedded within the string.
707 * @param incoming The string to trim quotes from (modified).
708 * @return @p incoming.
709 */
[128]710char *trim_empty_quotes(char *incoming)
[1]711{
[128]712 /*@ buffer ****************************************************** */
713 static char outgoing[MAX_STR_LEN];
[1]714
[128]715 /*@ end vars *************************************************** */
716 assert(incoming != NULL);
[1]717
[128]718 if (incoming[0] == '\"' && incoming[strlen(incoming) - 1] == '\"') {
719 strcpy(outgoing, incoming + 1);
720 outgoing[strlen(outgoing) - 1] = '\0';
721 } else {
722 strcpy(outgoing, incoming);
723 }
724 return (outgoing);
[1]725}
726
727
728
729
730/**
731 * Remove any partition info from @p partition, leaving just the drive name.
732 * @param partition The partition name soon-to-become drive name. (modified)
733 * @return @p partition.
734 */
[3042]735char *truncate_to_drive_name(const char *partition)
[1]736{
[128]737 int i = strlen(partition) - 1;
738 char *c;
[2230]739 char *trunc = NULL;
[1]740
[2237]741 assert_string_is_neither_NULL_nor_zerolength(partition);
[3185]742 mr_asprintf(trunc, "%s", partition);
[2230]743
[1]744#ifdef __FreeBSD__
745
[2230]746 if (islower(trunc[i])) // BSD subpartition
[128]747 i--;
[2230]748 if (trunc[i-1] == 's') {
749 while (isdigit(trunc[i]))
[128]750 i--;
751 i--;
752 }
[2230]753 trunc[i+1] = '\0';
[1]754
755#else
756
[128]757 /* first see if it's a devfs style device */
[2230]758 c = strrchr(trunc, '/');
[128]759 if (c && strncmp(c, "/part", 5) == 0) {
760 /* yup it's devfs, return the "disc" path */
761 strcpy(c + 1, "disc");
[2230]762 return trunc;
[128]763 }
[2052]764 /* then see if it's a dm style device */
[2105]765 if (c && strncmp(c, "/dm-", 4) == 0) {
[2052]766 /* yup it's dm, return the full path */
[2230]767 return trunc;
[2052]768 }
[1]769
[2105]770
[2230]771 for (i = strlen(trunc); isdigit(trunc[i-1]); i--)
[128]772 continue;
[2230]773 if (trunc[i-1] == 'p' && isdigit(trunc[i-2])) {
[128]774 i--;
[2105]775 } else {
776 /* Some full devices like this /dev/mapper/mpath0
[2190]777 /dev/cciss/c0d0 may be used as partition names */
[2230]778 if ((strstr(trunc,"/dev/mapper/mpath") != NULL) ||
779 (strstr(trunc,"/dev/cciss/c") != NULL) ||
780 (strstr(trunc,"/dev/rd/") != NULL)) {
781 return trunc;
[2105]782 }
[128]783 }
[2230]784 trunc[i] = '\0';
[1]785
786#endif
787
[2230]788 return trunc;
[1]789}
790
791
792
793
794
795/**
796 * Turn a RAID level number (-1 to 5) into a friendly string. The string
797 * is either "Linear RAID" for -1, or " RAID %-2d " (%d = @p raid_level)
798 * for anything else.
799 * @param raid_level The RAID level to stringify.
800 * @return The string form of @p raid_level.
[3611]801 * @note The returned value points to storage that needs to be freed by caller
[1]802 */
[128]803char *turn_raid_level_number_to_string(int raid_level)
[1]804{
805
[128]806 /*@ buffer ********************************************************** */
[3611]807 char *output = NULL;
[1]808
809
810
[128]811 if (raid_level >= 0) {
[3611]812 mr_asprintf(output, " RAID %-2d ", raid_level);
[128]813 } else {
[3611]814 mr_asprintf(output, "Linear RAID");
[128]815 }
[3611]816 return(output);
[1]817}
818
819
820
821
822
823
824
825
826
827/**
828 * Determine the severity (1-3, 1 being low) of the fact that
829 * @p fn changed in the live filesystem (verify/compare).
830 * @param fn The filename that changed.
831 * @param out_reason If non-NULL, a descriptive reason for the difference will be copied here.
832 * @return The severity (1-3).
833 */
[3194]834int severity_of_difference(char *fn, char **out_reason) {
[1]835
[3191]836 int sev = 3;
837 char *reason = NULL;
838 char *filename = NULL;
839
840 // out_reason might be null on purpose, so don't bomb if it is :) OK?
[128]841 assert_string_is_neither_NULL_nor_zerolength(fn);
842 if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
[3191]843 mr_asprintf(filename, "%s", fn + strlen(MNT_RESTORING));
[128]844 } else if (fn[0] != '/') {
[3191]845 mr_asprintf(filename, "/%s", fn);
[128]846 } else {
[3191]847 mr_asprintf(filename, "%s", fn);
[128]848 }
[1]849
[3191]850 mr_asprintf(reason, "Changed since backup. Consider running a differential backup in a day or two.");
851
[128]852 if (!strncmp(filename, "/var/", 5)) {
853 sev = 2;
[3191]854 mr_free(reason);
855 mr_asprintf(reason, "/var's contents will change regularly, inevitably.");
[128]856 }
857 if (!strncmp(filename, "/home", 5)) {
858 sev = 2;
[3191]859 mr_free(reason);
860 mr_asprintf(reason, "It's in your /home directory. Therefore, it is important.");
[128]861 }
862 if (!strncmp(filename, "/usr/", 5)) {
863 sev = 3;
[3191]864 mr_free(reason);
865 mr_asprintf(reason, "You may have installed/removed software during the backup.");
[128]866 }
867 if (!strncmp(filename, "/etc/", 5)) {
868 sev = 3;
[3191]869 mr_free(reason);
[3653]870 mr_asprintf(reason, "Do not edit config files while backing up your machine.");
[128]871 }
872 if (!strcmp(filename, "/etc/adjtime")
873 || !strcmp(filename, "/etc/mtab")) {
874 sev = 1;
[3191]875 mr_free(reason);
876 mr_asprintf(reason, "This file changes all the time. It's OK.");
[128]877 }
878 if (!strncmp(filename, "/root/", 6)) {
879 sev = 3;
[3191]880 mr_free(reason);
881 mr_asprintf(reason, "Were you compiling/editing something in /root?");
[128]882 }
883 if (!strncmp(filename, "/root/.", 7)) {
884 sev = 2;
[3191]885 mr_free(reason);
886 mr_asprintf(reason, "Temp or 'dot' files changed in /root.");
[128]887 }
888 if (!strncmp(filename, "/var/lib/", 9)) {
889 sev = 2;
[3191]890 mr_free(reason);
891 mr_asprintf(reason, "Did you add/remove software during backing?");
[128]892 }
893 if (!strncmp(filename, "/var/lib/rpm", 12)) {
894 sev = 3;
[3191]895 mr_free(reason);
896 mr_asprintf(reason, "Did you add/remove software during backing?");
[128]897 }
898 if (!strncmp(filename, "/var/lib/slocate", 16)) {
899 sev = 1;
[3191]900 mr_free(reason);
901 mr_asprintf(reason, "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
[128]902 }
903 if (!strncmp(filename, "/var/log/", 9)
904 || strstr(filename, "/.xsession")
905 || !strcmp(filename + strlen(filename) - 4, ".log")) {
906 sev = 1;
[3191]907 mr_free(reason);
908 mr_asprintf(reason, "Log files change frequently as the computer runs. Fret not.");
[128]909 }
910 if (!strncmp(filename, "/var/spool", 10)) {
911 sev = 1;
[3191]912 mr_free(reason);
913 mr_asprintf(reason, "Background processes or printers were active. This does not affect the integrity of your backup.");
[128]914 }
915 if (!strncmp(filename, "/var/spool/mail", 10)) {
916 sev = 2;
[3191]917 mr_free(reason);
918 mr_asprintf(reason, "Mail was sent/received during backup.");
[128]919 }
920 if (filename[strlen(filename) - 1] == '~') {
921 sev = 1;
[3191]922 mr_free(reason);
923 mr_asprintf(reason, "Backup copy of another file which was modified recently.");
[128]924 }
925 if (strstr(filename, "cache")) {
926 sev = 1;
[3191]927 mr_free(reason);
928 mr_asprintf(reason, "Part of a cache of data. Caches change from time to time. Don't worry.");
[128]929 }
930 if (!strncmp(filename, "/var/run/", 9)
931 || !strncmp(filename, "/var/lock", 8)
932 || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP")
933 || strstr(filename, "/.Xauthority")) {
934 sev = 1;
[3191]935 mr_free(reason);
936 mr_asprintf(reason, "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
[128]937 }
938 if (out_reason) {
[3194]939 strcpy(*out_reason, reason);
[128]940 }
[3191]941 mr_free(filename);
942 mr_free(reason);
[128]943 return (sev);
[1]944}
945
946
947
948/**
949 * Compare the filenames in two filelist entries (s_filelist_entry*) casted
950 * to void*.
951 * @param va The first filelist entry, cast as a @c void pointer.
952 * @param vb The second filelist entry, cast as a @c void pointer.
953 * @return The return value of strcmp().
954 */
[128]955int compare_two_filelist_entries(void *va, void *vb)
[1]956{
[128]957 static int res;
958 struct s_filelist_entry *fa, *fb;
[1]959
[128]960 assert(va != NULL);
961 assert(vb != NULL);
962 fa = (struct s_filelist_entry *) va;
963 fb = (struct s_filelist_entry *) vb;
964 res = strcmp(fa->filename, fb->filename);
965 return (res);
[1]966}
967
968
969
[3878]970/**
971 * Get a string form of @p type_of_bkp.
972 * @param type_of_bkp The backup type to stringify.
973 * @return The stringification of @p type_of_bkp.
974 * @note The returned string points to static storage that will be overwritten with each call.
975 */
976char *media_descriptor_string(t_bkptype type_of_bkp) {
[1]977
[3878]978 char *type_of_backup = NULL;
[1]979
[3878]980 switch (type_of_bkp) {
[3881]981 case optical:
982 mr_asprintf(type_of_backup, "optical");
[3878]983 break;
984 case tape:
985 mr_asprintf(type_of_backup, "tape");
986 break;
987 case udev:
988 mr_asprintf(type_of_backup, "udev");
989 break;
990 case iso:
991 mr_asprintf(type_of_backup, "ISO");
992 break;
993 case netfs:
994 mr_asprintf(type_of_backup, "%s", bkpinfo->netfs_proto);
995 break;
996 case usb:
997 mr_asprintf(type_of_backup, "USB");
998 break;
[3893]999 case none:
[3878]1000 default:
[3893]1001 fatal_error("Unknown backup_media_type");
1002 break;
[3878]1003 }
1004 return (type_of_backup);
1005}
[1]1006
1007
[3878]1008
1009
[1]1010/**
1011 * Generate a line intended to be passed to update_evalcall_form(), indicating
1012 * the current media fill percentage (or number of kilobytes if size is not known).
1013 * @param bkpinfo The backup media structure. Fields used:
1014 * - @c bkpinfo->backup_media_type
1015 * - @c bkpinfo->media_size
1016 * - @c bkpinfo->scratchdir
1017 * @return The string indicating media fill.
1018 * @note The returned string points to static storage that will be overwritten with each call.
1019 */
[1645]1020char *percent_media_full_comment()
[1]1021{
[128]1022 /*@ int *********************************************** */
1023 int percentage;
[3290]1024 int j;
[1]1025
[128]1026 /*@ buffers ******************************************* */
[3288]1027 char * outstr = NULL;
[3191]1028 char *pos_w_commas = NULL;
[2242]1029 char *mds = NULL;
[1]1030
[128]1031 assert(bkpinfo != NULL);
[1]1032
[3191]1033 if (bkpinfo->media_size <= 0) {
[3611]1034 mr_asprintf(pos_w_commas, "%lld", g_tape_posK);
[3288]1035 mr_asprintf(outstr, "Volume %d: %s kilobytes archived so far", g_current_media_number, pos_w_commas);
[3191]1036 mr_free(pos_w_commas);
[128]1037 return (outstr);
1038 }
[1]1039
[3191]1040 /* update screen */
[128]1041 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
[3150]1042 percentage = (int) (g_tape_posK / 10 / bkpinfo->media_size);
[128]1043 if (percentage > 100) {
1044 percentage = 100;
1045 }
[3288]1046 mr_asprintf(outstr, "Volume %d: [", g_current_media_number);
[128]1047 } else {
[3191]1048 percentage = (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 / bkpinfo->media_size);
[2242]1049 mds = media_descriptor_string(bkpinfo->backup_media_type);
[3288]1050 mr_asprintf(outstr, "%s %d: [", mds, g_current_media_number);
[2242]1051 mr_free(mds);
[1]1052 }
[128]1053 for (j = 0; j < percentage; j += 5) {
[3288]1054 mr_strcat(outstr, "*");
[128]1055 }
1056 for (; j < 100; j += 5) {
[3288]1057 mr_strcat(outstr, ".");
[128]1058 }
[3288]1059 mr_strcat(outstr, "] %d%% used", percentage);
[128]1060 return (outstr);
[1]1061}
1062
[3288]1063
Note: See TracBrowser for help on using the repository browser.