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