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