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