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