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