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