[2053] | 1 | /* file manipulation
|
---|
[116] | 2 | $Id: libmondo-files.c 3147 2013-06-19 06:34:46Z bruno $
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * @file
|
---|
| 7 | * Functions to manipulate files.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | #include "my-stuff.h"
|
---|
[2242] | 12 | #include "mr_mem.h"
|
---|
[1] | 13 | #include "mondostructures.h"
|
---|
| 14 | #include "libmondo-files.h"
|
---|
| 15 |
|
---|
[541] | 16 | #include "lib-common-externs.h"
|
---|
| 17 |
|
---|
[1] | 18 | #include "libmondo-tools-EXT.h"
|
---|
[541] | 19 | #include "libmondo-gui-EXT.h"
|
---|
[1] | 20 | #include "libmondo-devices-EXT.h"
|
---|
| 21 | #include "libmondo-fork-EXT.h"
|
---|
| 22 | #include "libmondo-string-EXT.h"
|
---|
| 23 |
|
---|
| 24 | /*@unused@*/
|
---|
[116] | 25 | //static char cvsid[] = "$Id: libmondo-files.c 3147 2013-06-19 06:34:46Z bruno $";
|
---|
[1] | 26 |
|
---|
| 27 | extern char err_log_lines[NOOF_ERR_LINES][MAX_STR_LEN];
|
---|
| 28 |
|
---|
| 29 | extern int g_currentY;
|
---|
| 30 | extern char *g_mondo_home;
|
---|
| 31 |
|
---|
[1645] | 32 | /* Reference to global bkpinfo */
|
---|
| 33 | extern struct s_bkpinfo *bkpinfo;
|
---|
| 34 |
|
---|
[1] | 35 | /**
|
---|
| 36 | * @addtogroup fileGroup
|
---|
| 37 | * @{
|
---|
| 38 | */
|
---|
| 39 | /**
|
---|
| 40 | * Get an md5 checksum of the specified file.
|
---|
| 41 | * @param filename The file to checksum.
|
---|
| 42 | * @return The 32-character ASCII representation of the 128-bit checksum.
|
---|
| 43 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 44 | */
|
---|
[116] | 45 | char *calc_checksum_of_file(char *filename)
|
---|
[1] | 46 | {
|
---|
[116] | 47 | /*@ buffers ***************************************************** */
|
---|
[2357] | 48 | char *output = NULL;
|
---|
[2272] | 49 | char *command = NULL;
|
---|
[1] | 50 |
|
---|
[116] | 51 | /*@ pointers **************************************************** */
|
---|
[2357] | 52 | char *p = NULL;
|
---|
[3147] | 53 | char *q;
|
---|
[116] | 54 | FILE *fin;
|
---|
[1] | 55 |
|
---|
[116] | 56 | /*@************************************************************** */
|
---|
[1] | 57 |
|
---|
[116] | 58 | assert_string_is_neither_NULL_nor_zerolength(filename);
|
---|
| 59 | if (does_file_exist(filename)) {
|
---|
[2323] | 60 | mr_asprintf(command, "md5sum \"%s\"", filename);
|
---|
[116] | 61 | fin = popen(command, "r");
|
---|
| 62 | if (fin) {
|
---|
[2357] | 63 | mr_getline(output, fin);
|
---|
[116] | 64 | p = strchr(output, ' ');
|
---|
[2357] | 65 | if (p != NULL) {
|
---|
| 66 | *p = '\0';
|
---|
| 67 | } else {
|
---|
| 68 | mr_free(output);
|
---|
| 69 | mr_asprintf(output, "");
|
---|
| 70 | }
|
---|
[116] | 71 | paranoid_pclose(fin);
|
---|
| 72 | }
|
---|
[2272] | 73 | mr_free(command);
|
---|
[116] | 74 | } else {
|
---|
[2324] | 75 | log_it("File '%s' not found; cannot calc checksum", filename);
|
---|
[2357] | 76 | mr_asprintf(output, "");
|
---|
[1] | 77 | }
|
---|
[116] | 78 | return (output);
|
---|
[1] | 79 | }
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Get a not-quite-unique representation of some of the file's @c stat properties.
|
---|
| 84 | * The returned string has the form <tt>size-mtime-ctime</tt>.
|
---|
| 85 | * @param curr_fname The file to generate the "checksum" for.
|
---|
| 86 | * @return The "checksum".
|
---|
| 87 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 88 | */
|
---|
[116] | 89 | char *calc_file_ugly_minichecksum(char *curr_fname)
|
---|
[1] | 90 | {
|
---|
| 91 |
|
---|
[116] | 92 | /*@ buffers ***************************************************** */
|
---|
| 93 | static char curr_cksum[1000];
|
---|
[1] | 94 |
|
---|
[116] | 95 | /*@ pointers **************************************************** */
|
---|
[1] | 96 |
|
---|
[116] | 97 | /*@ structures ************************************************** */
|
---|
| 98 | struct stat buf;
|
---|
[1] | 99 |
|
---|
[116] | 100 | /*@ initialize data *************************************************** */
|
---|
| 101 | curr_cksum[0] = '\0';
|
---|
[1] | 102 |
|
---|
[116] | 103 | /*@************************************************************** */
|
---|
[1] | 104 |
|
---|
[116] | 105 | assert_string_is_neither_NULL_nor_zerolength(curr_fname);
|
---|
| 106 | if (lstat(curr_fname, &buf)) {
|
---|
| 107 | return (curr_cksum); // empty
|
---|
| 108 | }
|
---|
[1] | 109 |
|
---|
[116] | 110 | sprintf(curr_cksum, "%ld-%ld-%ld", (long) (buf.st_size),
|
---|
| 111 | (long) (buf.st_mtime), (long) (buf.st_ctime));
|
---|
| 112 | return (curr_cksum);
|
---|
[1] | 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Get the number of lines in @p filename.
|
---|
| 119 | * @param filename The file to count lines in.
|
---|
| 120 | * @return The number of lines in @p filename.
|
---|
| 121 | * @bug This function uses the shell and "wc -l"; it should probably be rewritten in C.
|
---|
| 122 | */
|
---|
[116] | 123 | long count_lines_in_file(char *filename)
|
---|
[1] | 124 | {
|
---|
| 125 |
|
---|
[116] | 126 | /*@ buffers ***************************************************** */
|
---|
[2272] | 127 | char *command = NULL;
|
---|
[2357] | 128 | char *incoming = NULL;
|
---|
[3147] | 129 | char *q;
|
---|
[1] | 130 |
|
---|
[116] | 131 | /*@ long ******************************************************** */
|
---|
| 132 | long noof_lines = -1L;
|
---|
[1] | 133 |
|
---|
[116] | 134 | /*@ pointers **************************************************** */
|
---|
| 135 | FILE *fin;
|
---|
[1] | 136 |
|
---|
[116] | 137 | assert_string_is_neither_NULL_nor_zerolength(filename);
|
---|
| 138 | if (!does_file_exist(filename)) {
|
---|
[2324] | 139 | log_it("%s does not exist, so I cannot found the number of lines in it", filename);
|
---|
[116] | 140 | return (0);
|
---|
[1] | 141 | }
|
---|
[2323] | 142 | mr_asprintf(command, "cat %s | wc -l", filename);
|
---|
[116] | 143 | if (!does_file_exist(filename)) {
|
---|
[2272] | 144 | mr_free(command);
|
---|
[116] | 145 | return (-1);
|
---|
[1] | 146 | }
|
---|
[116] | 147 | fin = popen(command, "r");
|
---|
[2272] | 148 | mr_free(command);
|
---|
[116] | 149 | if (fin) {
|
---|
| 150 | if (feof(fin)) {
|
---|
| 151 | noof_lines = 0;
|
---|
| 152 | } else {
|
---|
[2357] | 153 | mr_getline(incoming, fin);
|
---|
| 154 | while (strlen(incoming) > 0 && incoming[strlen(incoming) - 1] < 32) {
|
---|
[116] | 155 | incoming[strlen(incoming) - 1] = '\0';
|
---|
| 156 | }
|
---|
| 157 | noof_lines = atol(incoming);
|
---|
[2357] | 158 | mr_free(incoming);
|
---|
[116] | 159 | }
|
---|
| 160 | paranoid_pclose(fin);
|
---|
| 161 | }
|
---|
| 162 | return (noof_lines);
|
---|
[1] | 163 | }
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 | /**
|
---|
| 167 | * Check for existence of given @p filename.
|
---|
| 168 | * @param filename The file to check for.
|
---|
| 169 | * @return TRUE if it exists, FALSE otherwise.
|
---|
| 170 | */
|
---|
[116] | 171 | bool does_file_exist(char *filename)
|
---|
[1] | 172 | {
|
---|
| 173 |
|
---|
[116] | 174 | /*@ structures ************************************************** */
|
---|
| 175 | struct stat buf;
|
---|
[1] | 176 |
|
---|
[116] | 177 | /*@************************************************************** */
|
---|
[1] | 178 |
|
---|
[116] | 179 | assert(filename != NULL);
|
---|
| 180 | // assert_string_is_neither_NULL_nor_zerolength(filename);
|
---|
| 181 | if (lstat(filename, &buf)) {
|
---|
| 182 | log_msg(20, "%s does not exist", filename);
|
---|
| 183 | return (FALSE);
|
---|
| 184 | } else {
|
---|
| 185 | log_msg(20, "%s exists", filename);
|
---|
| 186 | return (TRUE);
|
---|
| 187 | }
|
---|
[1] | 188 | }
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Modify @p inout (a file containing a list of files) to only contain files
|
---|
| 197 | * that exist.
|
---|
| 198 | * @param inout The filelist to operate on.
|
---|
| 199 | * @note The original file is renamed beforehand, so it will not be accessible
|
---|
| 200 | * while the modification is in progress.
|
---|
| 201 | */
|
---|
[116] | 202 | void exclude_nonexistent_files(char *inout)
|
---|
[1] | 203 | {
|
---|
[2272] | 204 | char *infname = NULL;
|
---|
| 205 | char *outfname = NULL;
|
---|
[2273] | 206 | char *tmp = NULL;
|
---|
[2357] | 207 | char *incoming = NULL;
|
---|
[3147] | 208 | char *q;
|
---|
[1] | 209 |
|
---|
[116] | 210 | /*@ int ********************************************************* */
|
---|
| 211 | int i;
|
---|
[1] | 212 |
|
---|
[116] | 213 | /*@ pointers **************************************************** */
|
---|
| 214 | FILE *fin, *fout;
|
---|
[1] | 215 |
|
---|
| 216 |
|
---|
[116] | 217 | /*@ end vars *********************************************************** */
|
---|
[1] | 218 |
|
---|
[116] | 219 | assert_string_is_neither_NULL_nor_zerolength(inout);
|
---|
[2323] | 220 | mr_asprintf(infname, "%s.in", inout);
|
---|
| 221 | mr_asprintf(outfname, "%s", inout);
|
---|
| 222 | mr_asprintf(tmp, "cp -f %s %s", inout, infname);
|
---|
[116] | 223 | run_program_and_log_output(tmp, FALSE);
|
---|
[2272] | 224 | mr_free(tmp);
|
---|
| 225 |
|
---|
[116] | 226 | if (!(fin = fopen(infname, "r"))) {
|
---|
| 227 | log_OS_error("Unable to openin infname");
|
---|
[2272] | 228 | mr_free(infname);
|
---|
[116] | 229 | return;
|
---|
[1] | 230 | }
|
---|
[116] | 231 | if (!(fout = fopen(outfname, "w"))) {
|
---|
| 232 | log_OS_error("Unable to openout outfname");
|
---|
[2272] | 233 | mr_free(outfname);
|
---|
[116] | 234 | return;
|
---|
[1] | 235 | }
|
---|
[2272] | 236 | mr_free(outfname);
|
---|
| 237 |
|
---|
[2357] | 238 | for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
|
---|
[116] | 239 | i = strlen(incoming) - 1;
|
---|
| 240 | if (i >= 0 && incoming[i] < 32) {
|
---|
| 241 | incoming[i] = '\0';
|
---|
| 242 | }
|
---|
| 243 | if (does_file_exist(incoming)) {
|
---|
| 244 | fprintf(fout, "%s\n", incoming);
|
---|
| 245 | } else {
|
---|
[2324] | 246 | log_it("Excluding '%s'-nonexistent\n", incoming);
|
---|
[116] | 247 | }
|
---|
[2357] | 248 | mr_free(incoming);
|
---|
[1] | 249 | }
|
---|
[2357] | 250 | mr_free(incoming);
|
---|
| 251 |
|
---|
[116] | 252 | paranoid_fclose(fout);
|
---|
| 253 | paranoid_fclose(fin);
|
---|
| 254 | unlink(infname);
|
---|
[2272] | 255 | mr_free(infname);
|
---|
[1] | 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 |
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | /**
|
---|
| 267 | * Attempt to find the user's kernel by calling Mindi.
|
---|
| 268 | * If Mindi can't find the kernel, ask user. If @p kernel is not empty,
|
---|
| 269 | * don't do anything.
|
---|
| 270 | * @param kernel Where to put the found kernel.
|
---|
| 271 | * @return 0 for success, 1 for failure.
|
---|
| 272 | */
|
---|
[2334] | 273 | char *figure_out_kernel_path_interactively_if_necessary(void) {
|
---|
| 274 |
|
---|
[2272] | 275 | char *tmp = NULL;
|
---|
| 276 | char *command = NULL;;
|
---|
[2334] | 277 | char *kernel = NULL;;
|
---|
[1] | 278 |
|
---|
[2704] | 279 | kernel = call_program_and_get_last_line_of_output("mindi --findkernel 2> /dev/null",TRUE);
|
---|
[2334] | 280 |
|
---|
[392] | 281 | // If we didn't get anything back, check whether mindi raised a fatal error
|
---|
| 282 | if (!kernel[0]) {
|
---|
[2323] | 283 | mr_asprintf(command, "%s", "grep 'Fatal error' /var/log/mindi.log");
|
---|
[2704] | 284 | tmp = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[392] | 285 | if (strlen(tmp) > 1) {
|
---|
| 286 | popup_and_OK(tmp);
|
---|
[2272] | 287 | mr_free(tmp);
|
---|
| 288 | mr_free(command);
|
---|
[2334] | 289 | mr_free(kernel);
|
---|
[392] | 290 | fatal_error("Mindi gave a fatal error. Please check '/var/log/mindi.log'.");
|
---|
| 291 | }
|
---|
[2272] | 292 | mr_free(tmp);
|
---|
| 293 | mr_free(command);
|
---|
[392] | 294 | }
|
---|
[116] | 295 | log_it("Calling Mindi with kernel path of '%s'", kernel);
|
---|
| 296 | while (!kernel[0]) {
|
---|
[2328] | 297 | if (!ask_me_yes_or_no("Kernel not found or invalid. Choose another?")) {
|
---|
[2334] | 298 | return (NULL);
|
---|
[116] | 299 | }
|
---|
[2316] | 300 | tmp = popup_and_get_string("Kernel path", "What is the full path and filename of your kernel, please?", kernel);
|
---|
| 301 | if (tmp == NULL) {
|
---|
| 302 | fatal_error("Kernel not found. Please specify with the '-k' flag.");
|
---|
[116] | 303 | }
|
---|
[2328] | 304 | mr_free(kernel);
|
---|
| 305 | kernel = tmp;
|
---|
[2316] | 306 |
|
---|
| 307 | log_it("User says kernel is at %s", kernel);
|
---|
[116] | 308 | }
|
---|
[2334] | 309 | return (kernel);
|
---|
[1] | 310 | }
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 |
|
---|
| 314 |
|
---|
| 315 |
|
---|
| 316 |
|
---|
| 317 | /**
|
---|
| 318 | * Find location of specified executable in user's PATH.
|
---|
| 319 | * @param fname The basename of the executable to search for (e.g. @c afio).
|
---|
| 320 | * @return The full path to the executable, or "" if it does not exist, or NULL if @c file could not be found.
|
---|
| 321 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
[2331] | 322 | * @bug The checks with @c file and @c dirname seem pointless. If @c output is "", then you're calling
|
---|
[1] | 323 | * <tt>dirname 2\>/dev/null</tt> or <tt>file 2\>/dev/null | cut -d':' -f1 2\>/dev/null</tt>, which basically amounts
|
---|
| 324 | * to nothing.
|
---|
| 325 | */
|
---|
[116] | 326 | char *find_home_of_exe(char *fname)
|
---|
[1] | 327 | {
|
---|
[116] | 328 | /*@ buffers ********************* */
|
---|
[2331] | 329 | char *output = NULL;
|
---|
[2272] | 330 | char *command = NULL;
|
---|
[1] | 331 |
|
---|
[116] | 332 | /*@******************************* */
|
---|
[1] | 333 |
|
---|
[116] | 334 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
[2323] | 335 | mr_asprintf(command, "which %s 2> /dev/null", fname);
|
---|
[2704] | 336 | output = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2272] | 337 | mr_free(command);
|
---|
[2331] | 338 | if (output[0] == '\0') {
|
---|
[116] | 339 | if (system("which file > /dev/null 2> /dev/null")) {
|
---|
[2331] | 340 | mr_free(output);
|
---|
[116] | 341 | return (NULL); // forget it :)
|
---|
| 342 | }
|
---|
[2331] | 343 | mr_asprintf(command, "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null", output);
|
---|
| 344 | mr_free(output);
|
---|
[2704] | 345 | output = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2272] | 346 | mr_free(command);
|
---|
[116] | 347 | }
|
---|
[2331] | 348 | if (output[0] == '\0') {
|
---|
| 349 | mr_asprintf(command, "dirname %s 2> /dev/null", output);
|
---|
| 350 | mr_free(output);
|
---|
[2704] | 351 | output = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2272] | 352 | mr_free(command);
|
---|
[1] | 353 | }
|
---|
[2331] | 354 |
|
---|
[116] | 355 | if (output[0] != '\0' && does_file_exist(output)) {
|
---|
[2331] | 356 | log_msg(4, "find_home_of_exe () --- Found %s at %s", fname, output);
|
---|
[116] | 357 | } else {
|
---|
[2331] | 358 | mr_free(output);
|
---|
[116] | 359 | log_msg(4, "find_home_of_exe() --- Could not find %s", fname);
|
---|
| 360 | }
|
---|
[2331] | 361 | return (output);
|
---|
[1] | 362 | }
|
---|
| 363 |
|
---|
| 364 |
|
---|
| 365 |
|
---|
| 366 |
|
---|
| 367 |
|
---|
| 368 |
|
---|
| 369 |
|
---|
| 370 |
|
---|
| 371 | /**
|
---|
| 372 | * Get the last sequence of digits surrounded by non-digits in the first 32k of
|
---|
| 373 | * a file.
|
---|
| 374 | * @param logfile The file to look in.
|
---|
| 375 | * @return The number found, or 0 if none.
|
---|
| 376 | */
|
---|
[116] | 377 | int get_trackno_from_logfile(char *logfile)
|
---|
[1] | 378 | {
|
---|
| 379 |
|
---|
[116] | 380 | /*@ pointers ********************************************************* */
|
---|
| 381 | FILE *fin;
|
---|
[1] | 382 |
|
---|
[116] | 383 | /*@ int ************************************************************** */
|
---|
| 384 | int trackno = 0;
|
---|
| 385 | size_t len = 0;
|
---|
[1] | 386 |
|
---|
[116] | 387 | /*@ buffer ************************************************************ */
|
---|
| 388 | char datablock[32701];
|
---|
[1] | 389 |
|
---|
[116] | 390 | assert_string_is_neither_NULL_nor_zerolength(logfile);
|
---|
| 391 | if (!(fin = fopen(logfile, "r"))) {
|
---|
| 392 | log_OS_error("Unable to open logfile");
|
---|
| 393 | fatal_error("Unable to open logfile to read trackno");
|
---|
| 394 | }
|
---|
| 395 | len = fread(datablock, 1, 32700, fin);
|
---|
| 396 | paranoid_fclose(fin);
|
---|
| 397 | if (len <= 0) {
|
---|
| 398 | return (0);
|
---|
| 399 | }
|
---|
| 400 | for (; len > 0 && !isdigit(datablock[len - 1]); len--);
|
---|
| 401 | datablock[len--] = '\0';
|
---|
| 402 | for (; len > 0 && isdigit(datablock[len - 1]); len--);
|
---|
| 403 | trackno = atoi(datablock + len);
|
---|
| 404 | return (trackno);
|
---|
[1] | 405 | }
|
---|
| 406 |
|
---|
| 407 |
|
---|
| 408 |
|
---|
| 409 |
|
---|
| 410 |
|
---|
| 411 |
|
---|
| 412 |
|
---|
| 413 | /**
|
---|
| 414 | * Get a percentage from the last line of @p filename. We look for the string
|
---|
| 415 | * "% done" on the last line and, if we find it, grab the number before the last % sign.
|
---|
| 416 | * @param filename The file to get the percentage from.
|
---|
| 417 | * @return The percentage found, or 0 for error.
|
---|
| 418 | */
|
---|
[116] | 419 | int grab_percentage_from_last_line_of_file(char *filename)
|
---|
[1] | 420 | {
|
---|
| 421 |
|
---|
[116] | 422 | /*@ buffers ***************************************************** */
|
---|
[2331] | 423 | char *lastline = NULL;
|
---|
[2272] | 424 | char *command = NULL;
|
---|
[116] | 425 | /*@ pointers **************************************************** */
|
---|
[2338] | 426 | char *p = NULL;
|
---|
[1] | 427 |
|
---|
[116] | 428 | /*@ int's ******************************************************* */
|
---|
[2338] | 429 | int i = 0;
|
---|
[1] | 430 |
|
---|
[2338] | 431 | for (i = NOOF_ERR_LINES - 1; i >= 0 && !strstr(err_log_lines[i], "% Done") && !strstr(err_log_lines[i], "% done"); i--);
|
---|
[116] | 432 | if (i < 0) {
|
---|
[2383] | 433 | mr_asprintf(command, "tail -n3 %s | grep -Fi %% | tail -n1 | awk '{print $0;}'", filename);
|
---|
[2704] | 434 | lastline = call_program_and_get_last_line_of_output(command,FALSE);
|
---|
[2272] | 435 | mr_free(command);
|
---|
[116] | 436 | if (!lastline[0]) {
|
---|
[2331] | 437 | mr_free(lastline);
|
---|
[116] | 438 | return (0);
|
---|
| 439 | }
|
---|
| 440 | } else {
|
---|
[2331] | 441 | mr_asprintf(lastline, "%s", err_log_lines[i]);
|
---|
[1] | 442 | }
|
---|
| 443 |
|
---|
[116] | 444 | p = strrchr(lastline, '%');
|
---|
| 445 | if (p) {
|
---|
| 446 | *p = '\0';
|
---|
[2338] | 447 | } else {
|
---|
[2331] | 448 | mr_free(lastline);
|
---|
[116] | 449 | return (0);
|
---|
| 450 | }
|
---|
[2338] | 451 |
|
---|
[2341] | 452 | for (p--; isdigit(*p) && p != lastline; p--);
|
---|
[116] | 453 | if (p != lastline) {
|
---|
| 454 | p++;
|
---|
| 455 | }
|
---|
[2338] | 456 | i = atoi(p);
|
---|
[2331] | 457 | mr_free(lastline);
|
---|
[116] | 458 | return (i);
|
---|
[1] | 459 | }
|
---|
| 460 |
|
---|
| 461 |
|
---|
| 462 |
|
---|
| 463 |
|
---|
| 464 |
|
---|
| 465 | /**
|
---|
| 466 | * Return the last line of @p filename.
|
---|
| 467 | * @param filename The file to get the last line of.
|
---|
| 468 | * @return The last line of the file.
|
---|
| 469 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 470 | */
|
---|
[116] | 471 | char *last_line_of_file(char *filename)
|
---|
[1] | 472 | {
|
---|
[116] | 473 | /*@ buffers ***************************************************** */
|
---|
[2357] | 474 | char *output = NULL;
|
---|
[2273] | 475 | char *command = NULL;
|
---|
[3147] | 476 | char *p = NULL;
|
---|
[1] | 477 |
|
---|
[116] | 478 | /*@ pointers **************************************************** */
|
---|
| 479 | FILE *fin;
|
---|
[1] | 480 |
|
---|
[116] | 481 | /*@ end vars **************************************************** */
|
---|
[1] | 482 |
|
---|
[116] | 483 | if (!does_file_exist(filename)) {
|
---|
[2324] | 484 | log_it("Tring to get last line of nonexistent file (%s)", filename);
|
---|
[2357] | 485 | mr_asprintf(output, "");
|
---|
[116] | 486 | return (output);
|
---|
| 487 | }
|
---|
[2323] | 488 | mr_asprintf(command, "tail -n1 %s", filename);
|
---|
[116] | 489 | fin = popen(command, "r");
|
---|
[2272] | 490 | mr_free(command);
|
---|
| 491 |
|
---|
[2357] | 492 | mr_getline(output, fin);
|
---|
[116] | 493 | paranoid_pclose(fin);
|
---|
| 494 | while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
|
---|
| 495 | output[strlen(output) - 1] = '\0';
|
---|
| 496 | }
|
---|
| 497 | return (output);
|
---|
[1] | 498 | }
|
---|
| 499 |
|
---|
| 500 | /**
|
---|
| 501 | * Get the length of @p filename in bytes.
|
---|
| 502 | * @param filename The file to get the length of.
|
---|
| 503 | * @return The length of the file, or -1 for error.
|
---|
| 504 | */
|
---|
[684] | 505 | off_t length_of_file(char *filename)
|
---|
[1] | 506 | {
|
---|
[116] | 507 | /*@ pointers *************************************************** */
|
---|
| 508 | FILE *fin;
|
---|
[1] | 509 |
|
---|
[116] | 510 | /*@ long long ************************************************* */
|
---|
[684] | 511 | off_t length;
|
---|
[1] | 512 |
|
---|
[116] | 513 | fin = fopen(filename, "r");
|
---|
| 514 | if (!fin) {
|
---|
| 515 | log_it("filename=%s", filename);
|
---|
| 516 | log_OS_error("Unable to openin filename");
|
---|
| 517 | return (-1);
|
---|
| 518 | }
|
---|
[415] | 519 | fseeko(fin, 0, SEEK_END);
|
---|
[684] | 520 | length = ftello(fin);
|
---|
[116] | 521 | paranoid_fclose(fin);
|
---|
| 522 | return (length);
|
---|
[1] | 523 | }
|
---|
| 524 |
|
---|
| 525 |
|
---|
| 526 |
|
---|
| 527 | /**
|
---|
| 528 | * ?????
|
---|
| 529 | * @bug I don't know what this function does. However, it seems orphaned, so it should probably be removed.
|
---|
| 530 | */
|
---|
[2357] | 531 | int make_checksum_list_file(char *filelist, char *cksumlist, char *comppath) {
|
---|
[116] | 532 | /*@ pointers **************************************************** */
|
---|
| 533 | FILE *fin;
|
---|
| 534 | FILE *fout;
|
---|
[1] | 535 |
|
---|
[116] | 536 | /*@ int ******************************************************* */
|
---|
| 537 | int percentage;
|
---|
| 538 | int i;
|
---|
| 539 | int counter = 0;
|
---|
[1] | 540 |
|
---|
[116] | 541 | /*@ buffer ****************************************************** */
|
---|
[2357] | 542 | char *stub_fname = NULL;
|
---|
| 543 | char *curr_fname = NULL;
|
---|
[2331] | 544 | char *curr_cksum = NULL;
|
---|
[2272] | 545 | char *tmp = NULL;
|
---|
[3147] | 546 | char *q;
|
---|
[1] | 547 |
|
---|
[116] | 548 | /*@ long [long] ************************************************* */
|
---|
[684] | 549 | off_t filelist_length;
|
---|
| 550 | off_t curr_pos;
|
---|
[116] | 551 | long start_time;
|
---|
| 552 | long current_time;
|
---|
| 553 | long time_taken;
|
---|
| 554 | long time_remaining;
|
---|
[1] | 555 |
|
---|
[116] | 556 | /*@ end vars *************************************************** */
|
---|
[1] | 557 |
|
---|
[116] | 558 | start_time = get_time();
|
---|
| 559 | filelist_length = length_of_file(filelist);
|
---|
[2272] | 560 | log_it("filelist = %s; cksumlist = %s", filelist, cksumlist);
|
---|
| 561 |
|
---|
[116] | 562 | fin = fopen(filelist, "r");
|
---|
| 563 | if (fin == NULL) {
|
---|
| 564 | log_OS_error("Unable to fopen-in filelist");
|
---|
| 565 | log_to_screen("Can't open filelist");
|
---|
| 566 | return (1);
|
---|
[1] | 567 | }
|
---|
[116] | 568 | fout = fopen(cksumlist, "w");
|
---|
| 569 | if (fout == NULL) {
|
---|
| 570 | log_OS_error("Unable to openout cksumlist");
|
---|
| 571 | paranoid_fclose(fin);
|
---|
[541] | 572 | log_to_screen("Can't open checksum list");
|
---|
[116] | 573 | return (1);
|
---|
[1] | 574 | }
|
---|
[2357] | 575 | for (mr_getline(stub_fname, fin); !feof(fin); mr_getline(stub_fname, fin)) {
|
---|
[116] | 576 | if (stub_fname[(i = strlen(stub_fname) - 1)] < 32) {
|
---|
| 577 | stub_fname[i] = '\0';
|
---|
| 578 | }
|
---|
[2323] | 579 | mr_asprintf(tmp, "%s%s", comppath, stub_fname);
|
---|
[2357] | 580 | mr_free(stub_fname);
|
---|
| 581 |
|
---|
| 582 | mr_asprintf(curr_fname, "%s", tmp + 1);
|
---|
[2272] | 583 | mr_free(tmp);
|
---|
| 584 |
|
---|
[2331] | 585 | mr_asprintf(curr_cksum, "%s", calc_file_ugly_minichecksum(curr_fname));
|
---|
[116] | 586 | fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum);
|
---|
[2331] | 587 | mr_free(curr_cksum);
|
---|
| 588 |
|
---|
[116] | 589 | if (counter++ > 12) {
|
---|
| 590 | current_time = get_time();
|
---|
| 591 | counter = 0;
|
---|
[2357] | 592 | /* BERLIOS: 37 really ? */
|
---|
[116] | 593 | curr_fname[37] = '\0';
|
---|
[684] | 594 | curr_pos = ftello(fin) / 1024;
|
---|
[116] | 595 | percentage = (int) (curr_pos * 100 / filelist_length);
|
---|
| 596 | time_taken = current_time - start_time;
|
---|
[2357] | 597 | if (percentage != 0) {
|
---|
[2324] | 598 | time_remaining = time_taken * 100 / (long) (percentage) - time_taken;
|
---|
| 599 | log_to_screen("%02d%% done %02d:%02d taken %02d:%02d remaining %-37s\r", percentage, (int) (time_taken / 60), (int) (time_taken % 60), (int) (time_remaining / 60), (int) (time_remaining % 60), curr_fname);
|
---|
[116] | 600 | }
|
---|
| 601 | sync();
|
---|
| 602 | }
|
---|
[2357] | 603 | mr_free(curr_fname);
|
---|
[116] | 604 | }
|
---|
[2357] | 605 | mr_free(stub_fname);
|
---|
| 606 |
|
---|
[116] | 607 | paranoid_fclose(fout);
|
---|
| 608 | paranoid_fclose(fin);
|
---|
| 609 | log_it("Done.");
|
---|
| 610 | return (0);
|
---|
[1] | 611 | }
|
---|
| 612 |
|
---|
| 613 |
|
---|
| 614 | /**
|
---|
| 615 | * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>.
|
---|
| 616 | * @param outdir_fname The directory to create.
|
---|
| 617 | * @return The return value of @c mkdir.
|
---|
| 618 | */
|
---|
[2325] | 619 | int make_hole_for_dir(const char *outdir_fname)
|
---|
[1] | 620 | {
|
---|
[2272] | 621 | char *tmp = NULL;
|
---|
[116] | 622 | int res = 0;
|
---|
[1] | 623 |
|
---|
[116] | 624 | assert_string_is_neither_NULL_nor_zerolength(outdir_fname);
|
---|
[2323] | 625 | mr_asprintf(tmp, "mkdir -p %s", outdir_fname);
|
---|
[116] | 626 | res = system(tmp);
|
---|
[2272] | 627 | mr_free(tmp);
|
---|
[116] | 628 | return (res);
|
---|
[1] | 629 | }
|
---|
| 630 |
|
---|
| 631 |
|
---|
| 632 | /**
|
---|
| 633 | * Create the parent directories of @p outfile_fname.
|
---|
| 634 | * @param outfile_fname The file to make a "hole" for.
|
---|
| 635 | * @return 0, always.
|
---|
| 636 | * @bug Return value unnecessary.
|
---|
| 637 | */
|
---|
[116] | 638 | int make_hole_for_file(char *outfile_fname)
|
---|
[1] | 639 | {
|
---|
[116] | 640 | /*@ buffer ****************************************************** */
|
---|
[2272] | 641 | char *command = NULL;
|
---|
[1] | 642 |
|
---|
[116] | 643 | /*@ int ******************************************************** */
|
---|
| 644 | int res = 0;
|
---|
[1] | 645 |
|
---|
[116] | 646 | /*@ end vars *************************************************** */
|
---|
[1] | 647 |
|
---|
[116] | 648 | assert_string_is_neither_NULL_nor_zerolength(outfile_fname);
|
---|
| 649 | assert(!strstr(outfile_fname, MNT_CDROM));
|
---|
| 650 | assert(!strstr(outfile_fname, "/dev/cdrom"));
|
---|
[2323] | 651 | mr_asprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
|
---|
[116] | 652 | res += system(command);
|
---|
[2272] | 653 | mr_free(command);
|
---|
| 654 |
|
---|
[2323] | 655 | mr_asprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
|
---|
[116] | 656 | res += system(command);
|
---|
[2272] | 657 | mr_free(command);
|
---|
| 658 |
|
---|
[2323] | 659 | mr_asprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
|
---|
[116] | 660 | res += system(command);
|
---|
[2272] | 661 | mr_free(command);
|
---|
| 662 |
|
---|
[116] | 663 | unlink(outfile_fname);
|
---|
| 664 | return (0);
|
---|
[1] | 665 | }
|
---|
| 666 |
|
---|
| 667 |
|
---|
| 668 |
|
---|
| 669 |
|
---|
| 670 | /**
|
---|
| 671 | * Get the number of lines in @p filelist_fname that contain the string @p wildcard.
|
---|
| 672 | * @param filelist_fname The file to search through.
|
---|
| 673 | * @param wildcard The string to search for. This is @e not a shell glob or a regular expression.
|
---|
| 674 | * @return The number of lines matched.
|
---|
| 675 | */
|
---|
[116] | 676 | long noof_lines_that_match_wildcard(char *filelist_fname, char *wildcard)
|
---|
[1] | 677 | {
|
---|
[116] | 678 | /*@ long ******************************************************* */
|
---|
| 679 | long matches = 0;
|
---|
[1] | 680 |
|
---|
[116] | 681 | /*@ pointers *************************************************** */
|
---|
| 682 | FILE *fin;
|
---|
[1] | 683 |
|
---|
[116] | 684 | /*@ buffers **************************************************** */
|
---|
[2357] | 685 | char *incoming = NULL;
|
---|
[3147] | 686 | char *p = NULL;
|
---|
[1] | 687 |
|
---|
[116] | 688 | /*@ end vars *************************************************** */
|
---|
[1] | 689 |
|
---|
| 690 |
|
---|
[116] | 691 | fin = fopen(filelist_fname, "r");
|
---|
[1] | 692 |
|
---|
[116] | 693 | if (!fin) {
|
---|
| 694 | log_OS_error("Unable to openin filelist_fname");
|
---|
| 695 | return (0);
|
---|
[1] | 696 | }
|
---|
[2357] | 697 | mr_getline(incoming, fin);
|
---|
[116] | 698 | while (!feof(fin)) {
|
---|
| 699 | if (strstr(incoming, wildcard)) {
|
---|
| 700 | matches++;
|
---|
| 701 | }
|
---|
[2357] | 702 | mr_free(incoming);
|
---|
| 703 | mr_getline(incoming, fin);
|
---|
[116] | 704 | }
|
---|
[2357] | 705 | mr_free(incoming);
|
---|
[116] | 706 | paranoid_fclose(fin);
|
---|
| 707 | return (matches);
|
---|
[1] | 708 | }
|
---|
| 709 |
|
---|
| 710 |
|
---|
| 711 |
|
---|
| 712 | /**
|
---|
[2462] | 713 | * Determine the size (in KB) of @p dev in the mountlist in mountlist.txt.
|
---|
[1] | 714 | * @param tmpdir The tempdir where the mountlist is stored.
|
---|
| 715 | * @param dev The device to search for.
|
---|
| 716 | * @return The size of the partition in KB.
|
---|
| 717 | */
|
---|
[116] | 718 | long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
|
---|
[1] | 719 | {
|
---|
[2272] | 720 | char *command = NULL;
|
---|
| 721 | char *mountlist = NULL;
|
---|
[2331] | 722 | char *sz_res = NULL;
|
---|
[116] | 723 | long file_len_K;
|
---|
| 724 |
|
---|
[2462] | 725 | mr_asprintf(mountlist, "%s/mountlist.txt", MINDI_CACHE);
|
---|
| 726 | mr_asprintf(command, "grep \"%s \" %s/mountlist.txt | head -n1 | awk '{print $4}'", dev, MINDI_CACHE);
|
---|
[2272] | 727 | mr_free(mountlist);
|
---|
| 728 |
|
---|
[116] | 729 | log_it(command);
|
---|
[2704] | 730 | sz_res = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[116] | 731 | file_len_K = atol(sz_res);
|
---|
| 732 | log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
|
---|
[2331] | 733 | mr_free(sz_res);
|
---|
[2272] | 734 | mr_free(command);
|
---|
| 735 |
|
---|
[116] | 736 | return (file_len_K);
|
---|
[1] | 737 | }
|
---|
| 738 |
|
---|
| 739 | /**
|
---|
| 740 | * Calculate the total size (in KB) of all the biggiefiles in this backup.
|
---|
| 741 | * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
|
---|
| 742 | * @return The total size of all biggiefiles in KB.
|
---|
| 743 | */
|
---|
[1645] | 744 | long size_of_all_biggiefiles_K()
|
---|
[1] | 745 | {
|
---|
[116] | 746 | /*@ buffers ***************************************************** */
|
---|
[2357] | 747 | char *fname = NULL;
|
---|
[2272] | 748 | char *biggielist = NULL;
|
---|
| 749 | char *tmp = NULL;
|
---|
| 750 | char *command = NULL;
|
---|
[3147] | 751 | char *comment;
|
---|
[1] | 752 |
|
---|
[116] | 753 | /*@ long ******************************************************** */
|
---|
| 754 | long scratchL = 0;
|
---|
| 755 | long file_len_K;
|
---|
[1] | 756 |
|
---|
[116] | 757 | /*@ pointers *************************************************** */
|
---|
| 758 | FILE *fin = NULL;
|
---|
[1] | 759 |
|
---|
[116] | 760 | /*@ end vars *************************************************** */
|
---|
[1] | 761 |
|
---|
[116] | 762 | log_it("Calculating size of all biggiefiles (in total)");
|
---|
[2623] | 763 | mr_asprintf(biggielist, "%s/biggielist.txt", bkpinfo->scratchdir);
|
---|
[116] | 764 | log_it("biggielist = %s", biggielist);
|
---|
[2272] | 765 | fin = fopen(biggielist, "r");
|
---|
| 766 | mr_free(biggielist);
|
---|
| 767 |
|
---|
| 768 | if (!(fin)) {
|
---|
| 769 | log_OS_error("Cannot open biggielist. OK, so estimate is based on filesets only.");
|
---|
[116] | 770 | } else {
|
---|
| 771 | log_msg(4, "Reading it...");
|
---|
[2357] | 772 | for (mr_getline(fname, fin); !feof(fin); mr_getline(fname, fin)) {
|
---|
[116] | 773 | if (fname[strlen(fname) - 1] <= 32) {
|
---|
| 774 | fname[strlen(fname) - 1] = '\0';
|
---|
| 775 | }
|
---|
| 776 | if (0 == strncmp(fname, "/dev/", 5)) {
|
---|
[296] | 777 | if (is_dev_an_NTFS_dev(fname)) {
|
---|
[2331] | 778 | tmp = find_home_of_exe("ntfsresize");
|
---|
| 779 | if (!tmp) {
|
---|
| 780 | mr_free(tmp);
|
---|
[2357] | 781 | mr_free(fname);
|
---|
[296] | 782 | fatal_error("ntfsresize not found");
|
---|
| 783 | }
|
---|
[2331] | 784 | mr_free(tmp);
|
---|
| 785 |
|
---|
[2323] | 786 | mr_asprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
|
---|
[296] | 787 | log_it("command = %s", command);
|
---|
[2704] | 788 | tmp = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2272] | 789 | mr_free(command);
|
---|
| 790 |
|
---|
[296] | 791 | log_it("res of it = %s", tmp);
|
---|
| 792 | file_len_K = atoll(tmp) / 1024L;
|
---|
[2272] | 793 | mr_free(tmp);
|
---|
[296] | 794 | } else {
|
---|
| 795 | file_len_K = get_phys_size_of_drive(fname) * 1024L;
|
---|
| 796 | }
|
---|
[116] | 797 | } else {
|
---|
[684] | 798 | /* BERLIOS: more than long here ??? */
|
---|
[116] | 799 | file_len_K = (long) (length_of_file(fname) / 1024);
|
---|
| 800 | }
|
---|
| 801 | if (file_len_K > 0) {
|
---|
| 802 | scratchL += file_len_K;
|
---|
| 803 | log_msg(4, "%s --> %ld K", fname, file_len_K);
|
---|
| 804 | }
|
---|
[2324] | 805 | log_msg(4, "After adding %s, scratchL+%ld now equals %ld", fname, file_len_K, scratchL);
|
---|
[116] | 806 | if (feof(fin)) {
|
---|
| 807 | break;
|
---|
| 808 | }
|
---|
[2357] | 809 | mr_free(fname);
|
---|
[116] | 810 | }
|
---|
[2357] | 811 | mr_free(fname);
|
---|
[1] | 812 | }
|
---|
[116] | 813 | log_it("Closing...");
|
---|
| 814 | paranoid_fclose(fin);
|
---|
| 815 | log_it("Finished calculating total size of all biggiefiles");
|
---|
| 816 | return (scratchL);
|
---|
[1] | 817 | }
|
---|
| 818 |
|
---|
| 819 | /**
|
---|
| 820 | * Determine the amount of space (in KB) occupied by a mounted CD.
|
---|
| 821 | * This can also be used to find the space used for other directories.
|
---|
| 822 | * @param mountpt The mountpoint/directory to check.
|
---|
| 823 | * @return The amount of space occupied in KB.
|
---|
| 824 | */
|
---|
[116] | 825 | long long space_occupied_by_cd(char *mountpt)
|
---|
[1] | 826 | {
|
---|
[116] | 827 | /*@ buffer ****************************************************** */
|
---|
[2357] | 828 | char *tmp = NULL;
|
---|
[2272] | 829 | char *command = NULL;
|
---|
[116] | 830 | long long llres;
|
---|
| 831 | /*@ pointers **************************************************** */
|
---|
| 832 | char *p;
|
---|
| 833 | FILE *fin;
|
---|
[1] | 834 |
|
---|
[116] | 835 | /*@ end vars *************************************************** */
|
---|
[1] | 836 |
|
---|
[2323] | 837 | mr_asprintf(command, "du -sk %s", mountpt);
|
---|
[816] | 838 | errno = 0;
|
---|
[116] | 839 | fin = popen(command, "r");
|
---|
[816] | 840 | if (errno) {
|
---|
| 841 | log_it("popen() FAILED: command=%s, mountpt=%s, fin=%d, errno=%d, strerror=%s", command, mountpt, fin, errno, strerror(errno));
|
---|
| 842 | llres = 0;
|
---|
| 843 | } else {
|
---|
[2357] | 844 | mr_getline(tmp, fin);
|
---|
[816] | 845 | paranoid_pclose(fin);
|
---|
| 846 | p = strchr(tmp, '\t');
|
---|
| 847 | if (p) {
|
---|
| 848 | *p = '\0';
|
---|
| 849 | }
|
---|
| 850 | for (p = tmp, llres = 0; *p != '\0'; p++) {
|
---|
| 851 | llres *= 10;
|
---|
| 852 | llres += (int) (*p - '0');
|
---|
| 853 | }
|
---|
[2357] | 854 | mr_free(tmp);
|
---|
[116] | 855 | }
|
---|
[2272] | 856 | mr_free(command);
|
---|
[816] | 857 |
|
---|
[116] | 858 | return (llres);
|
---|
[1] | 859 | }
|
---|
| 860 |
|
---|
| 861 |
|
---|
| 862 | /**
|
---|
| 863 | * Update a CRC checksum to include another character.
|
---|
| 864 | * @param crc The original CRC checksum.
|
---|
| 865 | * @param c The character to add.
|
---|
| 866 | * @return The new CRC checksum.
|
---|
| 867 | * @ingroup utilityGroup
|
---|
| 868 | */
|
---|
[116] | 869 | unsigned int updcrc(unsigned int crc, unsigned int c)
|
---|
[1] | 870 | {
|
---|
[116] | 871 | unsigned int tmp;
|
---|
| 872 | tmp = (crc >> 8) ^ c;
|
---|
| 873 | crc = (crc << 8) ^ crctttab[tmp & 255];
|
---|
| 874 | return crc;
|
---|
[1] | 875 | }
|
---|
| 876 |
|
---|
| 877 | /**
|
---|
| 878 | * Update a reverse CRC checksum to include another character.
|
---|
| 879 | * @param crc The original CRC checksum.
|
---|
| 880 | * @param c The character to add.
|
---|
| 881 | * @return The new CRC checksum.
|
---|
| 882 | * @ingroup utilityGroup
|
---|
| 883 | */
|
---|
[116] | 884 | unsigned int updcrcr(unsigned int crc, unsigned int c)
|
---|
[1] | 885 | {
|
---|
[116] | 886 | unsigned int tmp;
|
---|
| 887 | tmp = crc ^ c;
|
---|
| 888 | crc = (crc >> 8) ^ crc16tab[tmp & 0xff];
|
---|
| 889 | return crc;
|
---|
[1] | 890 | }
|
---|
| 891 |
|
---|
| 892 |
|
---|
| 893 |
|
---|
| 894 |
|
---|
| 895 | /**
|
---|
| 896 | * Check for an executable on the user's system; write a message to the
|
---|
| 897 | * screen and the log if we can't find it.
|
---|
| 898 | * @param fname The executable basename to look for.
|
---|
| 899 | * @return 0 if it's found, nonzero if not.
|
---|
| 900 | */
|
---|
[116] | 901 | int whine_if_not_found(char *fname)
|
---|
[1] | 902 | {
|
---|
[116] | 903 | /*@ buffers *** */
|
---|
[2272] | 904 | char *command = NULL;
|
---|
| 905 | int res = 0;
|
---|
[1] | 906 |
|
---|
[2323] | 907 | mr_asprintf(command, "which %s > /dev/null 2> /dev/null", fname);
|
---|
[2272] | 908 | res = system(command);
|
---|
| 909 | mr_free(command);
|
---|
[1] | 910 |
|
---|
[2272] | 911 | if (res) {
|
---|
[2324] | 912 | log_to_screen("Please install '%s'. I cannot find it on your system.", fname);
|
---|
[2937] | 913 | log_to_screen("There may be hyperlink at http://www.mondorescue.org which");
|
---|
[541] | 914 | log_to_screen("will take you to the relevant (missing) package.");
|
---|
[116] | 915 | return (1);
|
---|
| 916 | } else {
|
---|
| 917 | return (0);
|
---|
| 918 | }
|
---|
[1] | 919 | }
|
---|
| 920 |
|
---|
| 921 |
|
---|
| 922 |
|
---|
| 923 |
|
---|
| 924 |
|
---|
| 925 |
|
---|
| 926 | /**
|
---|
| 927 | * Create a data file at @p fname containing @p contents.
|
---|
| 928 | * The data actually can be multiple lines, despite the name.
|
---|
| 929 | * @param fname The file to create.
|
---|
| 930 | * @param contents The data to put in it.
|
---|
| 931 | * @return 0 for success, 1 for failure.
|
---|
| 932 | */
|
---|
[116] | 933 | int write_one_liner_data_file(char *fname, char *contents)
|
---|
[1] | 934 | {
|
---|
[116] | 935 | /*@ pointers *************************************************** */
|
---|
| 936 | FILE *fout;
|
---|
| 937 | int res = 0;
|
---|
[1] | 938 |
|
---|
[116] | 939 | /*@ end vars *************************************************** */
|
---|
[1] | 940 |
|
---|
[116] | 941 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 942 | if (!contents) {
|
---|
| 943 | log_it("%d: Warning - writing NULL to %s", __LINE__, fname);
|
---|
| 944 | }
|
---|
| 945 | if (!(fout = fopen(fname, "w"))) {
|
---|
| 946 | log_it("fname=%s");
|
---|
| 947 | log_OS_error("Unable to openout fname");
|
---|
| 948 | return (1);
|
---|
| 949 | }
|
---|
| 950 | fprintf(fout, "%s\n", contents);
|
---|
| 951 | paranoid_fclose(fout);
|
---|
| 952 | return (res);
|
---|
[1] | 953 | }
|
---|
| 954 |
|
---|
| 955 |
|
---|
| 956 |
|
---|
| 957 | /**
|
---|
| 958 | * Read @p fname into @p contents.
|
---|
| 959 | * @param fname The file to read.
|
---|
| 960 | * @param contents Where to put its contents.
|
---|
| 961 | * @return 0 for success, nonzero for failure.
|
---|
| 962 | */
|
---|
[116] | 963 | int read_one_liner_data_file(char *fname, char *contents)
|
---|
[1] | 964 | {
|
---|
[116] | 965 | /*@ pointers *************************************************** */
|
---|
| 966 | FILE *fin;
|
---|
| 967 | int res = 0;
|
---|
| 968 | int i;
|
---|
[1] | 969 |
|
---|
[116] | 970 | /*@ end vars *************************************************** */
|
---|
[1] | 971 |
|
---|
[116] | 972 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 973 | if (!contents) {
|
---|
| 974 | log_it("%d: Warning - reading NULL from %s", __LINE__, fname);
|
---|
| 975 | }
|
---|
| 976 | if (!(fin = fopen(fname, "r"))) {
|
---|
| 977 | log_it("fname=%s", fname);
|
---|
| 978 | log_OS_error("Unable to openin fname");
|
---|
| 979 | return (1);
|
---|
| 980 | }
|
---|
[3147] | 981 | res = fscanf(fin, "%s\n", contents);
|
---|
[116] | 982 | i = strlen(contents);
|
---|
| 983 | if (i > 0 && contents[i - 1] < 32) {
|
---|
| 984 | contents[i - 1] = '\0';
|
---|
| 985 | }
|
---|
| 986 | paranoid_fclose(fin);
|
---|
[3147] | 987 | res = 0;
|
---|
[116] | 988 | return (res);
|
---|
[1] | 989 | }
|
---|
| 990 |
|
---|
| 991 |
|
---|
| 992 |
|
---|
| 993 |
|
---|
| 994 |
|
---|
| 995 |
|
---|
| 996 |
|
---|
| 997 |
|
---|
| 998 |
|
---|
| 999 | /**
|
---|
| 1000 | * Copy the files that Mondo/Mindi need to run to the scratchdir or tempdir.
|
---|
[2289] | 1001 | * Currently this includes: copy Mondo's home directory to scratchdir,
|
---|
[2462] | 1002 | * copy post-nuke.tgz (if it exists) to tmpdir,
|
---|
[2379] | 1003 | * and run "hostname > scratchdir/HOSTNAME".
|
---|
[1] | 1004 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1005 | * - @c bkpinfo->postnuke_tarball
|
---|
| 1006 | * - @c bkpinfo->scratchdir
|
---|
| 1007 | * - @c bkpinfo->tmpdir
|
---|
| 1008 | */
|
---|
[1645] | 1009 | void copy_mondo_and_mindi_stuff_to_scratchdir()
|
---|
[1] | 1010 | {
|
---|
[116] | 1011 | /*@ Char buffers ** */
|
---|
[2272] | 1012 | char *command = NULL;
|
---|
[2331] | 1013 | char *tmp = NULL;
|
---|
[2272] | 1014 | int res = 0;
|
---|
[1] | 1015 |
|
---|
[2331] | 1016 | mvaddstr_and_log_it(g_currentY, 0, "Copying Mondo's core files to the scratch directory");
|
---|
[1] | 1017 |
|
---|
[116] | 1018 | log_msg(4, "g_mondo_home='%s'", g_mondo_home);
|
---|
| 1019 | if (strlen(g_mondo_home) < 2) {
|
---|
| 1020 | find_and_store_mondoarchives_home(g_mondo_home);
|
---|
| 1021 | }
|
---|
[2323] | 1022 | mr_asprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home, bkpinfo->scratchdir);
|
---|
[1] | 1023 |
|
---|
[116] | 1024 | log_msg(4, "command = %s", command);
|
---|
[2272] | 1025 | res = run_program_and_log_output(command, 1);
|
---|
| 1026 | mr_free(command);
|
---|
| 1027 |
|
---|
| 1028 | if (res) {
|
---|
[116] | 1029 | fatal_error("Failed to copy Mondo's stuff to scratchdir");
|
---|
| 1030 | }
|
---|
[1] | 1031 |
|
---|
[3147] | 1032 | mr_asprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
|
---|
| 1033 |
|
---|
| 1034 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1035 | fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir");
|
---|
| 1036 | }
|
---|
| 1037 |
|
---|
[2704] | 1038 | tmp = call_program_and_get_last_line_of_output("which mondorestore",TRUE);
|
---|
[116] | 1039 | if (!tmp[0]) {
|
---|
[2331] | 1040 | mr_free(tmp);
|
---|
| 1041 | fatal_error("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?");
|
---|
[116] | 1042 | }
|
---|
[2331] | 1043 | mr_free(tmp);
|
---|
| 1044 |
|
---|
[2323] | 1045 | mr_asprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
|
---|
[116] | 1046 | paranoid_system(command);
|
---|
[2272] | 1047 | mr_free(command);
|
---|
[1] | 1048 |
|
---|
[2328] | 1049 | if (bkpinfo->postnuke_tarball) {
|
---|
[2323] | 1050 | mr_asprintf(command, "cp -f %s %s/post-nuke.tgz", bkpinfo->postnuke_tarball, bkpinfo->tmpdir);
|
---|
[2272] | 1051 | res = run_program_and_log_output(command, FALSE);
|
---|
| 1052 | mr_free(command);
|
---|
| 1053 |
|
---|
| 1054 | if (res) {
|
---|
[116] | 1055 | fatal_error("Unable to copy post-nuke tarball to tmpdir");
|
---|
| 1056 | }
|
---|
| 1057 | }
|
---|
[1] | 1058 |
|
---|
[116] | 1059 | mvaddstr_and_log_it(g_currentY++, 74, "Done.");
|
---|
[1] | 1060 | }
|
---|
| 1061 |
|
---|
| 1062 |
|
---|
| 1063 |
|
---|
| 1064 |
|
---|
| 1065 |
|
---|
| 1066 | /**
|
---|
[2382] | 1067 | * Store the client's NETFS configuration in files to be restored at restore-time.
|
---|
| 1068 | * Assumes that @c bkpinfo->media_type = netfs, but does not check for this.
|
---|
[1] | 1069 | * @param bkpinfo The backup information structure. Fields used:
|
---|
[2382] | 1070 | * - @c netfs_mount
|
---|
| 1071 | * - @c netfs_remote_dir
|
---|
[1] | 1072 | * - @c tmpdir
|
---|
| 1073 | */
|
---|
[2382] | 1074 | void store_netfs_config()
|
---|
[1] | 1075 | {
|
---|
| 1076 |
|
---|
[116] | 1077 | /*@ buffers ******** */
|
---|
[2382] | 1078 | char *netfs_dev = NULL;
|
---|
[2937] | 1079 | char *netfs_client_hwaddr = NULL;
|
---|
[2382] | 1080 | char *netfs_mount = NULL;
|
---|
| 1081 | char *netfs_client_ipaddr = NULL;
|
---|
| 1082 | char *netfs_client_netmask = NULL;
|
---|
| 1083 | char *netfs_client_broadcast = NULL;
|
---|
| 1084 | char *netfs_client_defgw = NULL;
|
---|
| 1085 | char *netfs_server_ipaddr = NULL;
|
---|
[2272] | 1086 | char *tmp = NULL;
|
---|
[2273] | 1087 | char *command = NULL;
|
---|
[1] | 1088 |
|
---|
[116] | 1089 | /*@ pointers ***** */
|
---|
| 1090 | char *p;
|
---|
[1] | 1091 |
|
---|
[2382] | 1092 | if (! bkpinfo->netfs_mount) {
|
---|
| 1093 | fatal_error("No netfs_mount found !");
|
---|
[2325] | 1094 | }
|
---|
| 1095 |
|
---|
[2382] | 1096 | log_it("Storing Network configuration");
|
---|
| 1097 | mr_asprintf(tmp, "%s", bkpinfo->netfs_mount);
|
---|
[116] | 1098 | p = strchr(tmp, ':');
|
---|
| 1099 | if (!p) {
|
---|
[2382] | 1100 | fatal_error("Network mount doesn't have a colon in it, e.g. 192.168.1.4:/home/nfs");
|
---|
[116] | 1101 | }
|
---|
| 1102 | *(p++) = '\0';
|
---|
[2382] | 1103 | mr_asprintf(netfs_server_ipaddr, "%s", tmp);
|
---|
| 1104 | mr_asprintf(netfs_mount, "%s", p);
|
---|
[2272] | 1105 | mr_free(tmp);
|
---|
[1236] | 1106 |
|
---|
[802] | 1107 | /* BERLIOS : there is a bug #67 here as it only considers the first NIC */
|
---|
[2323] | 1108 | mr_asprintf(command, "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1");
|
---|
[2704] | 1109 | netfs_dev = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2273] | 1110 | mr_free(command);
|
---|
[2272] | 1111 |
|
---|
[2323] | 1112 | mr_asprintf(command, "%s", "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2");
|
---|
[2704] | 1113 | netfs_client_ipaddr = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2273] | 1114 | mr_free(command);
|
---|
[2272] | 1115 |
|
---|
[2323] | 1116 | mr_asprintf(command, "%s", "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2");
|
---|
[2704] | 1117 | netfs_client_netmask = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2273] | 1118 | mr_free(command);
|
---|
[2272] | 1119 |
|
---|
[2323] | 1120 | mr_asprintf(command, "%s", "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f8 | cut -d':' -f2");
|
---|
[2704] | 1121 | netfs_client_broadcast = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2273] | 1122 | mr_free(command);
|
---|
[2272] | 1123 |
|
---|
[2323] | 1124 | mr_asprintf(command, "%s", "route -n | grep '^0.0.0.0' | awk '{print $2}'");
|
---|
[2704] | 1125 | netfs_client_defgw = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2273] | 1126 | mr_free(command);
|
---|
[2272] | 1127 |
|
---|
[2382] | 1128 | if (strlen(netfs_dev) < 2) {
|
---|
| 1129 | fatal_error("Unable to find ethN (eth0, eth1, ...) adapter via Network mount you specified.");
|
---|
[116] | 1130 | }
|
---|
[2382] | 1131 |
|
---|
[802] | 1132 | /********
|
---|
[2382] | 1133 | * If the Network device that found above is a bonded device,
|
---|
[802] | 1134 | * we need to replace it with an ethN device or the
|
---|
[2382] | 1135 | * networking will not start during an Network restore.
|
---|
[802] | 1136 | *
|
---|
[2382] | 1137 | * If the Network device in netfs_dev begins with the word "bond", or alb or aft
|
---|
| 1138 | * look for the corresponding slave ethN device and copy it to netfs_dev.
|
---|
[802] | 1139 | * Using the common MAC address
|
---|
| 1140 | ********/
|
---|
[2382] | 1141 | if (!strncmp(netfs_dev, "bond", 4) || !strncmp(netfs_dev, "alb", 3) || !strncmp(netfs_dev, "aft", 3)) {
|
---|
| 1142 | log_to_screen("Found bonding device %s; looking for corresponding ethN slave device\n", netfs_dev);
|
---|
[2937] | 1143 |
|
---|
| 1144 | mr_asprintf(command, "ifconfig %s | awk '{print $5}' | head -n1", netfs_dev);
|
---|
| 1145 | netfs_client_hwaddr = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2273] | 1146 | mr_free(command);
|
---|
[2272] | 1147 |
|
---|
[2937] | 1148 | mr_asprintf(command, "ifconfig | grep -E '%s' | grep -v '%s' | head -n1 | cut -d' ' -f1", netfs_client_hwaddr, netfs_dev);
|
---|
[2382] | 1149 | mr_free(netfs_dev);
|
---|
[2272] | 1150 |
|
---|
[2704] | 1151 | netfs_dev = call_program_and_get_last_line_of_output(command,TRUE);
|
---|
[2273] | 1152 | mr_free(command);
|
---|
[2272] | 1153 |
|
---|
[2382] | 1154 | log_to_screen("Replacing it with %s\n", netfs_dev);
|
---|
[802] | 1155 | }
|
---|
[1] | 1156 |
|
---|
[2382] | 1157 | mr_asprintf(tmp, "%s/NETFS-DEV", bkpinfo->tmpdir);
|
---|
| 1158 | write_one_liner_data_file(tmp, netfs_dev);
|
---|
| 1159 | mr_free(netfs_dev);
|
---|
[2272] | 1160 | mr_free(tmp);
|
---|
[1] | 1161 |
|
---|
[2382] | 1162 | mr_asprintf(tmp, "%s/NETFS-SERVER-MOUNT", bkpinfo->tmpdir);
|
---|
| 1163 | write_one_liner_data_file(tmp, bkpinfo->netfs_mount);
|
---|
[2272] | 1164 | mr_free(tmp);
|
---|
[2382] | 1165 | mr_free(netfs_mount);
|
---|
[2272] | 1166 |
|
---|
[2705] | 1167 | mr_asprintf(tmp, "%s/NETFS-CLIENT-IPADDR", bkpinfo->tmpdir);
|
---|
| 1168 | write_one_liner_data_file(tmp, netfs_client_ipaddr);
|
---|
| 1169 | mr_free(tmp);
|
---|
| 1170 |
|
---|
[2937] | 1171 | mr_asprintf(tmp, "%s/NETFS-CLIENT-HWADDR", bkpinfo->tmpdir);
|
---|
| 1172 | write_one_liner_data_file(tmp, netfs_client_hwaddr);
|
---|
| 1173 | mr_free(tmp);
|
---|
| 1174 |
|
---|
[2705] | 1175 | mr_asprintf(tmp, "%s/NETFS-CLIENT-NETMASK", bkpinfo->tmpdir);
|
---|
| 1176 | write_one_liner_data_file(tmp, netfs_client_netmask);
|
---|
| 1177 | mr_free(tmp);
|
---|
| 1178 |
|
---|
| 1179 | mr_asprintf(tmp, "%s/NETFS-CLIENT-BROADCAST", bkpinfo->tmpdir);
|
---|
| 1180 | write_one_liner_data_file(tmp, netfs_client_broadcast);
|
---|
| 1181 | mr_free(tmp);
|
---|
| 1182 |
|
---|
| 1183 | mr_asprintf(tmp, "%s/NETFS-CLIENT-DEFGW", bkpinfo->tmpdir);
|
---|
| 1184 | write_one_liner_data_file(tmp, netfs_client_defgw);
|
---|
| 1185 | mr_free(tmp);
|
---|
| 1186 |
|
---|
[2850] | 1187 | mr_asprintf(tmp, "%s/NETFS-SERVER-IPADDR", bkpinfo->tmpdir);
|
---|
| 1188 | write_one_liner_data_file(tmp, netfs_server_ipaddr);
|
---|
| 1189 | mr_free(tmp);
|
---|
| 1190 |
|
---|
| 1191 | mr_asprintf(tmp, "%s/NETFS-SERVER-MOUNT", bkpinfo->tmpdir);
|
---|
| 1192 | write_one_liner_data_file(tmp, bkpinfo->netfs_mount);
|
---|
| 1193 | mr_free(tmp);
|
---|
| 1194 |
|
---|
| 1195 | if (bkpinfo->netfs_user) {
|
---|
| 1196 | mr_asprintf(tmp, "%s/NETFS-SERVER-USER", bkpinfo->tmpdir);
|
---|
| 1197 | write_one_liner_data_file(tmp, bkpinfo->netfs_user);
|
---|
| 1198 | mr_free(tmp);
|
---|
| 1199 | }
|
---|
| 1200 | mr_asprintf(tmp, "%s/NETFS-SERVER-PATH", bkpinfo->tmpdir);
|
---|
| 1201 | write_one_liner_data_file(tmp, bkpinfo->netfs_remote_dir);
|
---|
| 1202 | mr_free(tmp);
|
---|
| 1203 |
|
---|
| 1204 | mr_asprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
|
---|
| 1205 | write_one_liner_data_file(tmp, bkpinfo->prefix);
|
---|
| 1206 | mr_free(tmp);
|
---|
| 1207 |
|
---|
[2382] | 1208 | log_it("Finished storing Network configuration");
|
---|
[1] | 1209 | }
|
---|
| 1210 |
|
---|
| 1211 |
|
---|
| 1212 | /**
|
---|
| 1213 | * Determine the approximate number of media that the backup will take up,
|
---|
| 1214 | * and tell the user. The uncompressed size is estimated as size_of_all_biggiefiles_K()
|
---|
| 1215 | * plus (noof_sets x bkpinfo->optimal_set_size). The compression factor is estimated as
|
---|
| 1216 | * 2/3 for LZO and 1/2 for bzip2. The data is not saved anywhere. If there are any
|
---|
| 1217 | * "imagedevs", the estimate is not shown as it will be wildly inaccurate.
|
---|
| 1218 | * If there are more than 50 media estimated, the estimate will not be shown.
|
---|
| 1219 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1220 | * - @c bkpinfo->backup_media_type
|
---|
| 1221 | * - @c bkpinfo->media_size
|
---|
| 1222 | * - @c bkpinfo->optimal_set_size
|
---|
| 1223 | * - @c bkpinfo->use_lzo
|
---|
| 1224 | * @param noof_sets The number of filesets created.
|
---|
| 1225 | * @ingroup archiveGroup
|
---|
| 1226 | */
|
---|
| 1227 | void
|
---|
[1645] | 1228 | estimate_noof_media_required(long noof_sets)
|
---|
[1] | 1229 | {
|
---|
[116] | 1230 | /*@ buffers *************** */
|
---|
[2272] | 1231 | char *tmp = NULL;
|
---|
[2242] | 1232 | char *mds = NULL;
|
---|
[1] | 1233 |
|
---|
[116] | 1234 | /*@ long long ************* */
|
---|
| 1235 | long long scratchLL;
|
---|
[1] | 1236 |
|
---|
[1505] | 1237 | if (bkpinfo->media_size[1] <= 0) {
|
---|
[116] | 1238 | log_to_screen("Number of media required: UNKNOWN");
|
---|
| 1239 | return;
|
---|
| 1240 | }
|
---|
[1] | 1241 |
|
---|
[116] | 1242 | log_it("Estimating number of media required...");
|
---|
| 1243 | scratchLL =
|
---|
| 1244 | (long long) (noof_sets) * (long long) (bkpinfo->optimal_set_size)
|
---|
[1645] | 1245 | + (long long) (size_of_all_biggiefiles_K());
|
---|
[116] | 1246 | scratchLL = (scratchLL / 1024) / bkpinfo->media_size[1];
|
---|
| 1247 | scratchLL++;
|
---|
| 1248 | if (bkpinfo->use_lzo) {
|
---|
| 1249 | scratchLL = (scratchLL * 2) / 3;
|
---|
[1002] | 1250 | } else if (bkpinfo->use_gzip) {
|
---|
[998] | 1251 | scratchLL = (scratchLL * 2) / 3;
|
---|
[2338] | 1252 | } else if (bkpinfo->use_lzma) {
|
---|
| 1253 | scratchLL = (scratchLL * 2) / 3;
|
---|
[116] | 1254 | } else {
|
---|
| 1255 | scratchLL = scratchLL / 2;
|
---|
| 1256 | }
|
---|
| 1257 | if (!scratchLL) {
|
---|
| 1258 | scratchLL++;
|
---|
| 1259 | }
|
---|
| 1260 | if (scratchLL <= 1) {
|
---|
[2242] | 1261 | mds = media_descriptor_string(bkpinfo->backup_media_type);
|
---|
[2323] | 1262 | mr_asprintf(tmp, "Your backup will probably occupy a single %s. Maybe two.", mds);
|
---|
[2242] | 1263 | mr_free(mds);
|
---|
[116] | 1264 | } else if (scratchLL > 4) {
|
---|
[2323] | 1265 | mr_asprintf(tmp, "Your backup will occupy one meeeeellion media! (maybe %s)", number_to_text((int) (scratchLL + 1)));
|
---|
[116] | 1266 | } else {
|
---|
[2323] | 1267 | mr_asprintf(tmp, "Your backup will occupy approximately %s media.", number_to_text((int) (scratchLL + 1)));
|
---|
[116] | 1268 | }
|
---|
[2327] | 1269 | if (scratchLL < 50) {
|
---|
[116] | 1270 | log_to_screen(tmp);
|
---|
| 1271 | }
|
---|
[2272] | 1272 | mr_free(tmp);
|
---|
[1] | 1273 | }
|
---|
| 1274 |
|
---|
| 1275 |
|
---|
| 1276 | /**
|
---|
| 1277 | * Determine whether a file is compressed. This is done
|
---|
| 1278 | * by reading through the "do-not-compress-these" file distributed with Mondo.
|
---|
| 1279 | * @param filename The file to check.
|
---|
| 1280 | * @return TRUE if it's compressed, FALSE if not.
|
---|
| 1281 | */
|
---|
[116] | 1282 | bool is_this_file_compressed(char *filename)
|
---|
[1] | 1283 | {
|
---|
[2331] | 1284 | char *do_not_compress_these = NULL;
|
---|
[2272] | 1285 | char *tmp = NULL;
|
---|
[116] | 1286 | char *p;
|
---|
[1236] | 1287 | char *q = NULL;
|
---|
[1] | 1288 |
|
---|
[1236] | 1289 | q = strrchr(filename, '.');
|
---|
| 1290 | if (q == NULL) {
|
---|
| 1291 | return (FALSE);
|
---|
| 1292 | }
|
---|
| 1293 |
|
---|
[2323] | 1294 | mr_asprintf(tmp, "%s/do-not-compress-these", g_mondo_home);
|
---|
[116] | 1295 | if (!does_file_exist(tmp)) {
|
---|
[2272] | 1296 | mr_free(tmp);
|
---|
[116] | 1297 | return (FALSE);
|
---|
| 1298 | }
|
---|
[1236] | 1299 | /* BERLIOS: This is just plain WRONG !! */
|
---|
[2357] | 1300 | do_not_compress_these = last_line_of_file(tmp);
|
---|
[2272] | 1301 | mr_free(tmp);
|
---|
[1236] | 1302 |
|
---|
[116] | 1303 | for (p = do_not_compress_these; p != NULL; p++) {
|
---|
[2323] | 1304 | mr_asprintf(tmp, "%s", p);
|
---|
[116] | 1305 | if (strchr(tmp, ' ')) {
|
---|
| 1306 | *(strchr(tmp, ' ')) = '\0';
|
---|
| 1307 | }
|
---|
[1236] | 1308 | if (!strcmp(q, tmp)) {
|
---|
[2272] | 1309 | mr_free(tmp);
|
---|
[2331] | 1310 | mr_free(do_not_compress_these);
|
---|
[116] | 1311 | return (TRUE);
|
---|
| 1312 | }
|
---|
| 1313 | if (!(p = strchr(p, ' '))) {
|
---|
| 1314 | break;
|
---|
| 1315 | }
|
---|
[2272] | 1316 | mr_free(tmp);
|
---|
[116] | 1317 | }
|
---|
[2331] | 1318 | mr_free(do_not_compress_these);
|
---|
[116] | 1319 | return (FALSE);
|
---|
[1] | 1320 | }
|
---|
| 1321 |
|
---|
| 1322 | /* @} - end fileGroup */
|
---|