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