source: MondoRescue/branches/3.2/mondo/src/common/libmondo-string.c@ 3612

Last change on this file since 3612 was 3611, checked in by Bruno Cornec, 8 years ago

Remove more static allocation

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