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