Changeset 171 in MondoRescue for trunk/mondo/mondo/common/libmondo-files.c
- Timestamp:
- Dec 8, 2005, 5:20:29 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/mondo/mondo/common/libmondo-files.c
r149 r171 1 /* libmondo-files.c file manipulation 2 $Id$ 3 . 4 5 6 04/16/04 7 - find_home_of_exe() really does return NULL now if file not found 8 9 03/22/04 10 - added mode_of_file() 11 12 10/02/03 13 - cleaned up grab_percentage_from_last_line_of_file() 14 15 09/18 16 - added int make_grub_install_scriptlet() 17 18 09/16 19 - cleaned up mkisofs feedback window 20 21 09/12 22 - fixed Teuton-hostile bug in size_of_all_biggiefiles_K() 23 24 09/05 25 - added size_of_partition_in_mountlist_K() 26 - size_of_all_biggiefiles_K() now calls get_phys_size_of_drive(fname) 27 28 07/02 29 - fixed calls to popup_and_get_string() 30 31 05/19 32 - added CP_BIN 33 34 05/05 35 - added Joshua Oreman's FreeBSD patches 36 37 05/04 38 - find_home_of_exe() now returns NULL if file not found 39 40 04/26 41 - if >4 media est'd, say one meeeellion 42 43 04/25 44 - fixed minor bug in find_home_of_exe() 45 46 04/24 47 - added lots of assert()'s and log_OS_error()'s 48 49 04/07 50 - fix find_home_of_exe() 51 - cleaned up code a bit 52 53 03/27 54 - copy_mondo_and_mindi_stuff --- if _homedir_/payload.tgz exists then untar it to CD 55 56 01/14/2003 57 - if backup media type == nfs then don't estimate no. of media reqd 58 59 11/25/2002 60 - don't log/echo estimated # of media required if >=50 61 62 10/01 - 11/09 63 - chmod uses 0x, not decimal :) 64 - added is_this_file_compressed() 65 - replace convoluted grep with wc (KP) 66 67 09/01 - 09/30 68 - only show "number of media" estimate if no -x 69 - run_program_and_log_output() now takes boolean operator to specify 70 whether it will log its activities in the event of _success_ 71 72 08/01 - 08/31 73 - handle unknown media sizes 74 - cleaned up some log_it() calls 75 76 07/24 77 - created 1 /* $Id$ 2 * file manipulation 78 3 */ 79 4 … … 117 42 { 118 43 /*@ buffers ***************************************************** */ 119 static char output[MAX_STR_LEN]; 120 char command[MAX_STR_LEN * 2]; 121 char tmp[MAX_STR_LEN]; 44 static char *output = NULL; 45 char *command; 46 char *tmp; 47 size_t n = 0; 122 48 123 49 /*@ pointers **************************************************** */ … … 132 58 133 59 assert_string_is_neither_NULL_nor_zerolength(filename); 60 134 61 if (does_file_exist(filename)) { 135 sprintf(command, "md5sum \"%s\"", filename);62 asprintf(&command, "md5sum \"%s\"", filename); 136 63 fin = popen(command, "r"); 64 paranoid_free(command); 65 137 66 if (fin) { 138 (void) fgets(output, MAX_STR_LEN, fin);67 (void) getline(&output, &n, fin); 139 68 p = strchr(output, ' '); 140 69 paranoid_pclose(fin); 141 70 } 142 71 } else { 143 sprintf(tmp, "File '%s' not found; cannot calc checksum",72 asprintf(&tmp, "File '%s' not found; cannot calc checksum", 144 73 filename); 145 74 log_it(tmp); 75 paranoid_free(tmp); 146 76 } 147 77 if (p) { … … 163 93 164 94 /*@ buffers ***************************************************** */ 165 static char curr_cksum[1000];95 char *curr_cksum; 166 96 167 97 /*@ pointers **************************************************** */ … … 170 100 struct stat buf; 171 101 172 /*@ initialize data *************************************************** */173 curr_cksum[0] = '\0';174 175 102 /*@************************************************************** */ 176 103 177 104 assert_string_is_neither_NULL_nor_zerolength(curr_fname); 178 105 if (lstat(curr_fname, &buf)) { 179 return (curr_cksum); // empty 180 } 181 182 sprintf(curr_cksum, "%ld-%ld-%ld", (long) (buf.st_size), 106 asprintf(&curr_cksum, ""); 107 } else { 108 asprintf(&curr_cksum, "%ld-%ld-%ld", (long) (buf.st_size), 183 109 (long) (buf.st_mtime), (long) (buf.st_ctime)); 110 } 184 111 return (curr_cksum); 185 112 } 186 187 113 188 114 … … 197 123 198 124 /*@ buffers ***************************************************** */ 199 char command[MAX_STR_LEN * 2];200 char incoming[MAX_STR_LEN];201 char tmp[MAX_STR_LEN];125 char *command; 126 char *incoming = NULL; 127 char *tmp; 202 128 203 129 /*@ long ******************************************************** */ 204 130 long noof_lines = -1L; 205 131 132 /*@ int ******************************************************** */ 133 size_t n = 0; 134 206 135 /*@ pointers **************************************************** */ 207 136 FILE *fin; 208 137 209 /*@ initialize [0] to null ******************************************** */210 incoming[0] = '\0';211 212 138 assert_string_is_neither_NULL_nor_zerolength(filename); 213 139 if (!does_file_exist(filename)) { 214 sprintf(tmp,140 asprintf(&tmp, 215 141 "%s does not exist, so I cannot found the number of lines in it", 216 142 filename); 217 143 log_it(tmp); 144 paranoid_free(tmp); 218 145 return (0); 219 146 } 220 sprintf(command, "cat %s | wc -l", filename);147 asprintf(&command, "cat %s | wc -l", filename); 221 148 if (!does_file_exist(filename)) { 222 149 return (-1); 223 150 } 224 151 fin = popen(command, "r"); 152 paranoid_free(command); 153 225 154 if (fin) { 226 155 if (feof(fin)) { 227 156 noof_lines = 0; 228 157 } else { 229 (void) fgets(incoming, MAX_STR_LEN - 1, fin);158 (void) getline(&incoming, &n, fin); 230 159 while (strlen(incoming) > 0 231 160 && incoming[strlen(incoming) - 1] < 32) { … … 233 162 } 234 163 noof_lines = atol(incoming); 164 paranoid_free(incoming); 235 165 } 236 166 paranoid_pclose(fin); … … 254 184 255 185 assert(filename != NULL); 256 // assert_string_is_neither_NULL_nor_zerolength(filename); 186 257 187 if (lstat(filename, &buf)) { 258 188 log_msg(20, "%s does not exist", filename); … … 265 195 266 196 267 268 269 270 271 197 /** 272 198 * Modify @p inout (a file containing a list of files) to only contain files … … 278 204 void exclude_nonexistent_files(char *inout) 279 205 { 280 char infname[MAX_STR_LEN];281 char outfname[MAX_STR_LEN];282 char tmp[MAX_STR_LEN];283 char incoming[MAX_STR_LEN];206 char *infname; 207 char *outfname; 208 char *tmp; 209 char *incoming = NULL; 284 210 285 211 /*@ int ********************************************************* */ 286 212 int i; 213 size_t n = 0; 287 214 288 215 /*@ pointers **************************************************** */ … … 293 220 294 221 assert_string_is_neither_NULL_nor_zerolength(inout); 295 sprintf(infname, "%s.in", inout); 296 sprintf(outfname, "%s", inout); 297 sprintf(tmp, "cp -f %s %s", inout, infname); 222 223 asprintf(&infname, "%s.in", inout); 224 225 asprintf(&tmp, "cp -f %s %s", inout, infname); 298 226 run_program_and_log_output(tmp, FALSE); 227 paranoid_free(tmp); 228 299 229 if (!(fin = fopen(infname, "r"))) { 300 230 log_OS_error("Unable to openin infname"); 231 paranoid_free(infname); 301 232 return; 302 233 } 234 235 asprintf(&outfname, "%s", inout); 303 236 if (!(fout = fopen(outfname, "w"))) { 304 237 log_OS_error("Unable to openout outfname"); 238 paranoid_free(outfname); 305 239 return; 306 240 } 307 for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin); 308 fgets(incoming, MAX_STR_LEN, fin)) { 241 paranoid_free(outfname); 242 243 for (getline(&incoming, &n, fin); !feof(fin); 244 getline(&incoming, &n, fin)) { 309 245 i = strlen(incoming) - 1; 310 246 if (i >= 0 && incoming[i] < 32) { … … 314 250 fprintf(fout, "%s\n", incoming); 315 251 } else { 316 sprintf(tmp, "Excluding '%s'-nonexistent\n", incoming);252 asprintf(&tmp, "Excluding '%s'-nonexistent\n", incoming); 317 253 log_it(tmp); 318 } 319 } 254 paranoid_free(tmp); 255 } 256 } 257 paranoid_free(incoming); 320 258 paranoid_fclose(fout); 321 259 paranoid_fclose(fin); 322 260 unlink(infname); 323 } 324 325 326 327 328 329 330 261 paranoid_free(infname); 262 } 331 263 332 264 … … 340 272 int figure_out_kernel_path_interactively_if_necessary(char *kernel) 341 273 { 342 char tmp[MAX_STR_LEN];274 char *tmp; 343 275 344 276 if (!kernel[0]) { … … 360 292 ("Kernel not found. Please specify with the '-k' flag."); 361 293 } 362 sprintf(tmp, "User says kernel is at %s", kernel);294 asprintf(&tmp, "User says kernel is at %s", kernel); 363 295 log_it(tmp); 296 paranoid_free(tmp); 364 297 } 365 298 return (0); 366 299 } 367 368 369 370 371 300 372 301 … … 387 316 char *command; 388 317 389 malloc_string(incoming);390 malloc_string(command);391 incoming[0] = '\0';392 318 /*@******************************* */ 393 319 394 320 assert_string_is_neither_NULL_nor_zerolength(fname); 395 sprintf(command, "which %s 2> /dev/null", fname); 396 strcpy(incoming, call_program_and_get_last_line_of_output(command)); 321 asprintf(&command, "which %s 2> /dev/null", fname); 322 asprintf(&incoming, call_program_and_get_last_line_of_output(command)); 323 paranoid_free(command); 324 397 325 if (incoming[0] == '\0') { 398 326 if (system("which file > /dev/null 2> /dev/null")) { 399 327 paranoid_free(incoming); 400 paranoid_free(command);401 output[0] = '\0';402 328 return (NULL); // forget it :) 403 329 } 404 sprintf(command,330 asprintf(&command, 405 331 "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null", 406 332 incoming); 407 strcpy(incoming, 333 paranoid_free(incoming); 334 335 asprintf(&incoming, 408 336 call_program_and_get_last_line_of_output(command)); 337 paranoid_free(command); 409 338 } 410 339 if (incoming[0] == '\0') // yes, it is == '\0' twice, not once :) 411 340 { 412 sprintf(command, "dirname %s 2> /dev/null", incoming); 413 strcpy(incoming, 341 asprintf(&command, "dirname %s 2> /dev/null", incoming); 342 paranoid_free(incoming); 343 344 asprintf(&incoming, 414 345 call_program_and_get_last_line_of_output(command)); 346 paranoid_free(command); 415 347 } 416 348 strcpy(output, incoming); 349 paranoid_free(incoming); 350 417 351 if (output[0] != '\0' && does_file_exist(output)) { 418 352 log_msg(4, "find_home_of_exe () --- Found %s at %s", fname, 419 incoming);353 output); 420 354 } else { 421 355 output[0] = '\0'; 422 356 log_msg(4, "find_home_of_exe() --- Could not find %s", fname); 423 357 } 424 paranoid_free(incoming);425 paranoid_free(command);426 358 if (!output[0]) { 427 359 return (NULL); … … 432 364 433 365 434 435 436 437 438 439 440 366 /** 441 367 * Get the last sequence of digits surrounded by non-digits in the first 32k of … … 458 384 459 385 assert_string_is_neither_NULL_nor_zerolength(logfile); 386 460 387 if (!(fin = fopen(logfile, "r"))) { 461 388 log_OS_error("Unable to open logfile"); … … 471 398 for (; len > 0 && isdigit(datablock[len - 1]); len--); 472 399 trackno = atoi(datablock + len); 473 /*474 sprintf(tmp,"datablock=%s; trackno=%d",datablock+len, trackno);475 log_it(tmp);476 */477 400 return (trackno); 478 401 } 479 480 481 482 483 484 402 485 403 … … 494 412 495 413 /*@ buffers ***************************************************** */ 496 char tmp[MAX_STR_LEN]; 497 char lastline[MAX_STR_LEN]; 498 char command[MAX_STR_LEN]; 414 char *lastline; 415 char *command; 499 416 /*@ pointers **************************************************** */ 500 417 char *p; … … 507 424 && !strstr(err_log_lines[i], "% done"); i--); 508 425 if (i < 0) { 509 sprintf(command,426 asprintf(&command, 510 427 "tail -n3 %s | fgrep -i \"%c\" | tail -n1 | awk '{print $0;}'", 511 428 filename, '%'); 512 strcpy(lastline,429 asprintf(&lastline, 513 430 call_program_and_get_last_line_of_output(command)); 431 paranoid_free(command); 514 432 if (!lastline[0]) { 433 paranoid_free(lastline); 515 434 return (0); 516 435 } 517 436 } else { 518 strcpy(lastline, err_log_lines[i]);437 asprintf(&lastline, err_log_lines[i]); 519 438 } 520 439 … … 523 442 *p = '\0'; 524 443 } 525 // log_msg(2, "lastline='%s', ", p, lastline);526 444 if (!p) { 445 paranoid_free(lastline); 527 446 return (0); 528 447 } … … 533 452 } 534 453 i = atoi(p); 535 536 sprintf(tmp, "'%s' --> %d", p, i); 537 // log_to_screen(tmp); 454 paranoid_free(lastline); 538 455 539 456 return (i); 540 457 } 541 542 543 544 458 545 459 … … 554 468 /*@ buffers ***************************************************** */ 555 469 static char output[MAX_STR_LEN]; 556 static char command[MAX_STR_LEN * 2];557 static char tmp[MAX_STR_LEN];470 static char *command; 471 static char *tmp; 558 472 559 473 /*@ pointers **************************************************** */ … … 563 477 564 478 if (!does_file_exist(filename)) { 565 sprintf(tmp, "Tring to get last line of nonexistent file (%s)",479 asprintf(&tmp, "Tring to get last line of nonexistent file (%s)", 566 480 filename); 567 481 log_it(tmp); 482 paranoid_free(tmp); 483 568 484 output[0] = '\0'; 569 485 return (output); 570 486 } 571 sprintf(command, "tail -n1 %s", filename);487 asprintf(&command, "tail -n1 %s", filename); 572 488 fin = popen(command, "r"); 489 paranoid_free(command); 490 573 491 (void) fgets(output, MAX_STR_LEN, fin); 574 492 paranoid_pclose(fin); … … 578 496 return (output); 579 497 } 498 580 499 581 500 /** … … 605 524 606 525 607 608 /**609 * ?????610 * @bug I don't know what this function does. However, it seems orphaned, so it should probably be removed.611 */612 int613 make_checksum_list_file(char *filelist, char *cksumlist, char *comppath)614 {615 /*@ pointers **************************************************** */616 FILE *fin;617 FILE *fout;618 619 /*@ int ******************************************************* */620 int percentage;621 int i;622 int counter = 0;623 624 /*@ buffer ****************************************************** */625 char stub_fname[1000];626 char curr_fname[1000];627 char curr_cksum[1000];628 char tmp[1000];629 630 /*@ long [long] ************************************************* */631 long long filelist_length;632 long curr_pos;633 long start_time;634 long current_time;635 long time_taken;636 long time_remaining;637 638 /*@ end vars *************************************************** */639 640 start_time = get_time();641 filelist_length = length_of_file(filelist);642 sprintf(tmp, "filelist = %s; cksumlist = %s", filelist, cksumlist);643 log_it(tmp);644 fin = fopen(filelist, "r");645 if (fin == NULL) {646 log_OS_error("Unable to fopen-in filelist");647 log_to_screen("Can't open filelist");648 return (1);649 }650 fout = fopen(cksumlist, "w");651 if (fout == NULL) {652 log_OS_error("Unable to openout cksumlist");653 paranoid_fclose(fin);654 log_to_screen("Can't open checksum list");655 return (1);656 }657 for (fgets(stub_fname, 999, fin); !feof(fin);658 fgets(stub_fname, 999, fin)) {659 if (stub_fname[(i = strlen(stub_fname) - 1)] < 32) {660 stub_fname[i] = '\0';661 }662 sprintf(tmp, "%s%s", comppath, stub_fname);663 strcpy(curr_fname, tmp + 1);664 strcpy(curr_cksum, calc_file_ugly_minichecksum(curr_fname));665 fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum);666 if (counter++ > 12) {667 current_time = get_time();668 counter = 0;669 curr_fname[37] = '\0';670 curr_pos = ftell(fin) / 1024;671 percentage = (int) (curr_pos * 100 / filelist_length);672 time_taken = current_time - start_time;673 if (percentage == 0) {674 /* printf("%0d%% done \r",percentage); */675 } else {676 time_remaining =677 time_taken * 100 / (long) (percentage) - time_taken;678 sprintf(tmp,679 "%02d%% done %02d:%02d taken %02d:%02d remaining %-37s\r",680 percentage, (int) (time_taken / 60),681 (int) (time_taken % 60),682 (int) (time_remaining / 60),683 (int) (time_remaining % 60), curr_fname);684 log_to_screen(tmp);685 }686 sync();687 }688 }689 paranoid_fclose(fout);690 paranoid_fclose(fin);691 log_it("Done.");692 return (0);693 }694 695 696 526 /** 697 527 * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>. … … 702 532 int make_hole_for_dir(char *outdir_fname) 703 533 { 704 char tmp[MAX_STR_LEN * 2];534 char *tmp; 705 535 int res = 0; 706 536 707 537 assert_string_is_neither_NULL_nor_zerolength(outdir_fname); 708 sprintf(tmp, "mkdir -p %s", outdir_fname);538 asprintf(&tmp, "mkdir -p %s", outdir_fname); 709 539 res = system(tmp); 540 paranoid_free(tmp); 710 541 return (res); 711 542 } … … 722 553 { 723 554 /*@ buffer ****************************************************** */ 724 char command[MAX_STR_LEN * 2];555 char *command; 725 556 726 557 /*@ int ******************************************************** */ … … 732 563 assert(!strstr(outfile_fname, MNT_CDROM)); 733 564 assert(!strstr(outfile_fname, "/dev/cdrom")); 734 sprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname); 565 566 asprintf(&command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname); 735 567 res += system(command); 736 sprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname); 568 paranoid_free(command); 569 570 asprintf(&command, "rmdir \"%s\" 2> /dev/null", outfile_fname); 737 571 res += system(command); 738 sprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname); 572 paranoid_free(command); 573 574 asprintf(&command, "rm -f \"%s\" 2> /dev/null", outfile_fname); 739 575 res += system(command); 576 paranoid_free(command); 740 577 unlink(outfile_fname); 741 578 return (0); 742 579 } 743 744 745 580 746 581 … … 760 595 761 596 /*@ buffers **************************************************** */ 762 char incoming[MAX_STR_LEN]; 763 597 char *incoming = NULL; 598 599 size_t n = 0; 764 600 /*@ end vars *************************************************** */ 765 601 … … 771 607 return (0); 772 608 } 773 (void) fgets(incoming, MAX_STR_LEN - 1, fin);609 (void) getline(&incoming, &n, fin); 774 610 while (!feof(fin)) { 775 611 if (strstr(incoming, wildcard)) { 776 612 matches++; 777 613 } 778 (void) fgets(incoming, MAX_STR_LEN - 1, fin);614 (void) getline(&incoming, &n, fin); 779 615 } 780 616 paranoid_fclose(fin); 617 paranoid_free(incoming); 781 618 return (matches); 782 619 } 783 784 785 620 786 621 … … 794 629 void register_pid(pid_t pid, char *name_str) 795 630 { 796 char tmp[MAX_STR_LEN + 1], lockfile_fname[MAX_STR_LEN + 1]; 631 char *tmp; 632 char *lockfile_fname; 797 633 int res; 634 size_t n = 0; 798 635 FILE *fin; 799 636 800 sprintf(lockfile_fname, "/var/run/monitas-%s.pid", name_str);637 asprintf(&lockfile_fname, "/var/run/monitas-%s.pid", name_str); 801 638 if (!pid) { 802 639 log_it("Unregistering PID"); … … 804 641 log_it("Error unregistering PID"); 805 642 } 643 paranoid_free(lockfile_fname); 806 644 return; 807 645 } 808 646 if (does_file_exist(lockfile_fname)) { 809 tmp[0] = '\0';810 647 if ((fin = fopen(lockfile_fname, "r"))) { 811 (void) fgets(tmp, MAX_STR_LEN, fin);648 (void) getline(&tmp, &n, fin); 812 649 paranoid_fclose(fin); 813 650 } else { … … 815 652 } 816 653 pid = (pid_t) atol(tmp); 817 sprintf(tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid); 654 paranoid_free(tmp); 655 656 asprintf(&tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid); 818 657 res = system(tmp); 658 paranoid_free(tmp); 819 659 if (!res) { 820 660 log_it … … 823 663 } 824 664 } 825 sprintf(tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(),665 asprintf(&tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(), 826 666 lockfile_fname); 667 paranoid_free(lockfile_fname); 668 827 669 if (system(tmp)) { 828 670 fatal_error("Cannot register PID"); 829 671 } 830 } 831 672 paranoid_free(tmp); 673 return; 674 } 832 675 833 676 … … 840 683 long size_of_partition_in_mountlist_K(char *tmpdir, char *dev) 841 684 { 842 char command[MAX_STR_LEN]; 843 char mountlist[MAX_STR_LEN]; 844 char sz_res[MAX_STR_LEN]; 685 char *command; 686 char *sz_res; 845 687 long file_len_K; 846 688 847 sprintf(mountlist, "%s/mountlist.txt", tmpdir); 848 sprintf(command, 849 "cat %s/mountlist.txt | grep \"%s \" | head -n1 | awk '{print $4;}'", 850 tmpdir, dev); 689 asprintf(&command, 690 "grep '%s ' %s/mountlist.txt | head -n1 | awk '{print $4;}'", 691 dev, tmpdir); 851 692 log_it(command); 852 strcpy(sz_res, call_program_and_get_last_line_of_output(command));693 asprintf(&sz_res, call_program_and_get_last_line_of_output(command)); 853 694 file_len_K = atol(sz_res); 854 695 log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K); 696 paranoid_free(command); 697 paranoid_free(sz_res); 855 698 return (file_len_K); 856 699 } 700 857 701 858 702 /** … … 874 718 /*@ pointers *************************************************** */ 875 719 FILE *fin = NULL; 720 size_t n = 0; 876 721 877 722 /*@ end vars *************************************************** */ 878 723 879 malloc_string(fname);880 malloc_string(biggielist);881 malloc_string(comment);882 724 log_it("Calculating size of all biggiefiles (in total)"); 883 sprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);725 asprintf(&biggielist, "%s/biggielist.txt", bkpinfo->tmpdir); 884 726 log_it("biggielist = %s", biggielist); 885 727 if (!(fin = fopen(biggielist, "r"))) { … … 888 730 } else { 889 731 log_msg(4, "Reading it..."); 890 for ( fgets(fname, MAX_STR_LEN, fin); !feof(fin);891 fgets(fname, MAX_STR_LEN, fin)) {732 for (getline(&fname, &n, fin); !feof(fin); 733 getline(&fname, &n, fin)) { 892 734 if (fname[strlen(fname) - 1] <= 32) { 893 735 fname[strlen(fname) - 1] = '\0'; … … 902 744 log_msg(4, "%s --> %ld K", fname, file_len_K); 903 745 } 904 sprintf(comment,746 asprintf(&comment, 905 747 "After adding %s, scratchL+%ld now equals %ld", fname, 906 748 file_len_K, scratchL); 907 749 log_msg(4, comment); 750 paranoid_free(comment); 751 908 752 if (feof(fin)) { 909 753 break; 910 754 } 911 755 } 912 } 756 paranoid_free(fname); 757 } 758 paranoid_free(biggielist); 759 913 760 log_it("Closing..."); 914 761 paranoid_fclose(fin); 915 762 log_it("Finished calculating total size of all biggiefiles"); 916 paranoid_free(fname);917 paranoid_free(biggielist);918 paranoid_free(comment);919 763 return (scratchL); 920 764 } 765 921 766 922 767 /** … … 929 774 { 930 775 /*@ buffer ****************************************************** */ 931 char tmp[MAX_STR_LEN];932 char command[MAX_STR_LEN * 2];776 char *tmp = NULL; 777 char *command; 933 778 long long llres; 779 size_t n = 0; 934 780 /*@ pointers **************************************************** */ 935 781 char *p; … … 938 784 /*@ end vars *************************************************** */ 939 785 940 sprintf(command, "du -sk %s", mountpt);786 asprintf(&command, "du -sk %s", mountpt); 941 787 fin = popen(command, "r"); 942 (void) fgets(tmp, MAX_STR_LEN, fin); 788 paranoid_free(command); 789 790 (void) getline(&tmp, &n, fin); 943 791 paranoid_pclose(fin); 944 792 p = strchr(tmp, '\t'); … … 950 798 llres += (int) (*p - '0'); 951 799 } 800 paranoid_free(tmp); 952 801 return (llres); 953 802 } … … 969 818 } 970 819 820 971 821 /** 972 822 * Update a reverse CRC checksum to include another character. … … 985 835 986 836 987 988 989 837 /** 990 838 * Check for an executable on the user's system; write a message to the … … 996 844 { 997 845 /*@ buffers *** */ 998 char command[MAX_STR_LEN * 2]; 999 char errorstr[MAX_STR_LEN]; 1000 1001 1002 sprintf(command, "which %s > /dev/null 2> /dev/null", fname); 1003 sprintf(errorstr, 846 char *command; 847 char *errorstr; 848 int res = 0; 849 850 851 asprintf(&command, "which %s > /dev/null 2> /dev/null", fname); 852 res = system(command); 853 paranoid_free(command); 854 855 if (res) { 856 asprintf(&errorstr, 1004 857 "Please install '%s'. I cannot find it on your system.", 1005 858 fname); 1006 if (system(command)) {1007 859 log_to_screen(errorstr); 860 paranoid_free(errorstr); 1008 861 log_to_screen 1009 ("There may be hyperlink at http://www.mondorescue.comwhich");862 ("There may be an hyperlink at http://www.mondorescue.org which"); 1010 863 log_to_screen("will take you to the relevant (missing) package."); 1011 864 return (1); … … 1014 867 } 1015 868 } 1016 1017 1018 1019 1020 869 1021 870 … … 1050 899 1051 900 1052 1053 901 /** 1054 902 * Read @p fname into @p contents. … … 1083 931 return (res); 1084 932 } 1085 1086 1087 1088 1089 1090 1091 1092 933 1093 934 … … 1105 946 { 1106 947 /*@ Char buffers ** */ 1107 char command[MAX_STR_LEN * 2];1108 char tmp[MAX_STR_LEN];948 char *command; 949 char *tmp; 1109 950 char old_pwd[MAX_STR_LEN]; 1110 951 … … 1118 959 g_mondo_home = find_and_store_mondoarchives_home(); 1119 960 } 1120 sprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home,961 asprintf(&command, CP_BIN " --parents -pRdf %s %s", g_mondo_home, 1121 962 bkpinfo->scratchdir); 1122 963 … … 1125 966 fatal_error("Failed to copy Mondo's stuff to scratchdir"); 1126 967 } 1127 1128 sprintf(tmp, "%s/payload.tgz", g_mondo_home); 968 paranoid_free(command); 969 970 asprintf(&tmp, "%s/payload.tgz", g_mondo_home); 1129 971 if (does_file_exist(tmp)) { 1130 972 log_it("Untarring payload %s to scratchdir %s", tmp, … … 1132 974 (void) getcwd(old_pwd, MAX_STR_LEN - 1); 1133 975 chdir(bkpinfo->scratchdir); 1134 sprintf(command, "tar -zxvf %s", tmp);976 asprintf(&command, "tar -zxvf %s", tmp); 1135 977 if (run_program_and_log_output(command, FALSE)) { 1136 978 fatal_error("Failed to untar payload"); 1137 979 } 980 paranoid_free(command); 1138 981 chdir(old_pwd); 1139 982 } 1140 1141 sprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir, 983 paranoid_free(tmp); 984 985 asprintf(&command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir, 1142 986 bkpinfo->scratchdir); 1143 1144 987 if (run_program_and_log_output(command, FALSE)) { 1145 988 fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir"); 1146 989 } 1147 1148 strcpy(tmp, 990 paranoid_free(command); 991 992 asprintf(&tmp, 1149 993 call_program_and_get_last_line_of_output("which mondorestore")); 1150 994 if (!tmp[0]) { … … 1152 996 ("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?"); 1153 997 } 1154 sprintf(command, "cp -f %s %s", tmp, bkpinfo->tmpdir); 998 asprintf(&command, "cp -f %s %s", tmp, bkpinfo->tmpdir); 999 paranoid_free(tmp); 1000 1155 1001 if (run_program_and_log_output(command, FALSE)) { 1156 1002 fatal_error("Failed to copy mondorestore to tmpdir"); 1157 1003 } 1158 1159 sprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir); 1004 paranoid_free(command); 1005 1006 asprintf(&command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir); 1160 1007 paranoid_system(command); 1008 paranoid_free(command); 1161 1009 1162 1010 if (bkpinfo->postnuke_tarball[0]) { 1163 sprintf(command, "cp -f %s %s/post-nuke.tgz",1011 asprintf(&command, "cp -f %s %s/post-nuke.tgz", 1164 1012 bkpinfo->postnuke_tarball, bkpinfo->tmpdir); 1165 1013 if (run_program_and_log_output(command, FALSE)) { 1166 1014 fatal_error("Unable to copy post-nuke tarball to tmpdir"); 1167 1015 } 1168 }1169 1016 paranoid_free(command); 1017 } 1170 1018 1171 1019 mvaddstr_and_log_it(g_currentY++, 74, "Done."); 1172 1020 } 1173 1174 1175 1176 1021 1177 1022 … … 1188 1033 1189 1034 /*@ buffers ******** */ 1190 char outfile[MAX_STR_LEN];1191 char nfs_dev[MAX_STR_LEN];1192 char nfs_mount[MAX_STR_LEN];1193 char nfs_client_ipaddr[MAX_STR_LEN];1194 char nfs_client_netmask[MAX_STR_LEN];1195 char nfs_client_defgw[MAX_STR_LEN];1196 char nfs_server_ipaddr[MAX_STR_LEN];1197 char tmp[MAX_STR_LEN];1198 char command[MAX_STR_LEN * 2];1035 char *outfile; 1036 char *nfs_dev; 1037 char *nfs_mount; 1038 char *nfs_client_ipaddr; 1039 char *nfs_client_netmask; 1040 char *nfs_client_defgw; 1041 char *nfs_server_ipaddr; 1042 char *tmp; 1043 char *command; 1199 1044 1200 1045 /*@ pointers ***** */ … … 1205 1050 1206 1051 log_it("Storing NFS configuration"); 1207 strcpy(tmp, bkpinfo->nfs_mount);1052 asprintf(&tmp, bkpinfo->nfs_mount); 1208 1053 p = strchr(tmp, ':'); 1209 1054 if (!p) { … … 1212 1057 } 1213 1058 *(p++) = '\0'; 1214 strcpy(nfs_server_ipaddr, tmp); 1215 strcpy(nfs_mount, p); 1216 sprintf(command, 1059 asprintf(&nfs_server_ipaddr, tmp); 1060 paranoid_free(tmp); 1061 1062 asprintf(&nfs_mount, p); 1063 asprintf(&command, 1217 1064 "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1"); 1218 strcpy(nfs_dev, call_program_and_get_last_line_of_output(command)); 1219 sprintf(command, 1065 asprintf(&nfs_dev, call_program_and_get_last_line_of_output(command)); 1066 paranoid_free(command); 1067 1068 asprintf(&command, 1220 1069 "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2"); 1221 strcpy(nfs_client_ipaddr,1070 asprintf(&nfs_client_ipaddr, 1222 1071 call_program_and_get_last_line_of_output(command)); 1223 sprintf(command, 1072 paranoid_free(command); 1073 1074 asprintf(&command, 1224 1075 "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2"); 1225 strcpy(nfs_client_netmask,1076 asprintf(&nfs_client_netmask, 1226 1077 call_program_and_get_last_line_of_output(command)); 1227 sprintf(command, 1078 paranoid_free(command); 1079 1080 asprintf(&command, 1228 1081 "route | egrep '^default' | awk '{printf $2}'"); 1229 strcpy(nfs_client_defgw,1082 asprintf(&nfs_client_defgw, 1230 1083 call_program_and_get_last_line_of_output(command)); 1231 sprintf(tmp, 1232 "nfs_client_ipaddr=%s; nfs_server_ipaddr=%s; nfs_mount=%s", 1233 nfs_client_ipaddr, nfs_server_ipaddr, nfs_mount); 1084 paranoid_free(command); 1085 1086 asprintf(&tmp, 1087 "nfs_client_ipaddr=%s; nfs_client_netmask=%s; nfs_server_ipaddr=%s; nfs_mount=%s; nfs_client_defgw=%s; ", 1088 nfs_client_ipaddr, nfs_client_netmask, nfs_server_ipaddr, nfs_mount, nfs_client_defgw); 1089 paranoid_free(nfs_mount); 1090 log_it(tmp); 1091 paranoid_free(tmp); 1092 1234 1093 if (strlen(nfs_dev) < 2) { 1235 1094 fatal_error 1236 1095 ("Unable to find ethN (eth0, eth1, ...) adapter via NFS mount you specified."); 1237 1096 } 1238 sprintf(outfile, "%s/start-nfs", bkpinfo->tmpdir);1239 sprintf(tmp, "outfile = %s", outfile);1097 asprintf(&outfile, "%s/start-nfs", bkpinfo->tmpdir); 1098 asprintf(&tmp, "outfile = %s", outfile); 1240 1099 log_it(tmp); 1100 paranoid_free(tmp); 1101 1241 1102 if (!(fout = fopen(outfile, "w"))) { 1242 1103 fatal_error("Cannot store NFS config"); … … 1256 1117 // paranoid_system ("mkdir -p /var/cache/mondo-archive 2> /dev/null"); 1257 1118 1258 sprintf(tmp, "cp -f %s /var/cache/mondo-archive", outfile); 1119 asprintf(&tmp, "cp -f %s /var/cache/mondo-archive", outfile); 1120 paranoid_free(outfile); 1121 1259 1122 run_program_and_log_output(tmp, FALSE); 1260 1261 sprintf(tmp, "%s/NFS-DEV", bkpinfo->tmpdir); 1123 paranoid_free(tmp); 1124 1125 asprintf(&tmp, "%s/NFS-DEV", bkpinfo->tmpdir); 1262 1126 write_one_liner_data_file(tmp, nfs_dev); 1263 1264 sprintf(tmp, "%s/NFS-CLIENT-IPADDR", bkpinfo->tmpdir); 1127 paranoid_free(nfs_dev); 1128 paranoid_free(tmp); 1129 1130 asprintf(&tmp, "%s/NFS-CLIENT-IPADDR", bkpinfo->tmpdir); 1265 1131 write_one_liner_data_file(tmp, nfs_client_ipaddr); 1266 sprintf(tmp, "%s/NFS-CLIENT-NETMASK", bkpinfo->tmpdir); 1132 paranoid_free(nfs_client_ipaddr); 1133 paranoid_free(tmp); 1134 1135 asprintf(&tmp, "%s/NFS-CLIENT-NETMASK", bkpinfo->tmpdir); 1267 1136 write_one_liner_data_file(tmp, nfs_client_netmask); 1268 sprintf(tmp, "%s/NFS-CLIENT-DEFGW", bkpinfo->tmpdir); 1137 paranoid_free(nfs_client_netmask); 1138 paranoid_free(tmp); 1139 1140 asprintf(&tmp, "%s/NFS-CLIENT-DEFGW", bkpinfo->tmpdir); 1269 1141 write_one_liner_data_file(tmp, nfs_client_defgw); 1270 sprintf(tmp, "%s/NFS-SERVER-IPADDR", bkpinfo->tmpdir); 1142 paranoid_free(nfs_client_defgw); 1143 paranoid_free(tmp); 1144 1145 asprintf(&tmp, "%s/NFS-SERVER-IPADDR", bkpinfo->tmpdir); 1271 1146 write_one_liner_data_file(tmp, nfs_server_ipaddr); 1272 sprintf(tmp, "%s/NFS-SERVER-MOUNT", bkpinfo->tmpdir); 1147 paranoid_free(nfs_server_ipaddr); 1148 paranoid_free(tmp); 1149 1150 asprintf(&tmp, "%s/NFS-SERVER-MOUNT", bkpinfo->tmpdir); 1273 1151 write_one_liner_data_file(tmp, bkpinfo->nfs_mount); 1274 sprintf(tmp, "%s/NFS-SERVER-PATH", bkpinfo->tmpdir); 1152 paranoid_free(tmp); 1153 1154 asprintf(&tmp, "%s/NFS-SERVER-PATH", bkpinfo->tmpdir); 1275 1155 write_one_liner_data_file(tmp, bkpinfo->nfs_remote_dir); 1276 sprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir); 1156 paranoid_free(tmp); 1157 1158 asprintf(&tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir); 1277 1159 write_one_liner_data_file(tmp, bkpinfo->prefix); 1160 paranoid_free(tmp); 1161 1278 1162 log_it("Finished storing NFS configuration"); 1279 1163 } 1280 1281 1282 1283 1284 1164 1285 1165 … … 1304 1184 { 1305 1185 /*@ buffers *************** */ 1306 char tmp[MAX_STR_LEN];1186 char *tmp; 1307 1187 1308 1188 /*@ long long ************* */ … … 1329 1209 } 1330 1210 if (scratchLL <= 1) { 1331 sprintf(tmp,1211 asprintf(&tmp, 1332 1212 "Your backup will probably occupy a single %s. Maybe two.", 1333 1213 media_descriptor_string(bkpinfo->backup_media_type)); 1334 1214 } else { 1335 sprintf(tmp, "Your backup will occupy approximately %s media.",1215 asprintf(&tmp, "Your backup will occupy approximately %s media.", 1336 1216 number_to_text((int) (scratchLL + 1))); 1337 1217 } … … 1339 1219 log_to_screen(tmp); 1340 1220 } 1341 } 1342 1343 1344 /** 1345 * Get the last suffix of @p instr. 1346 * If @p instr was "httpd.log.gz", we would return "gz". 1347 * @param instr The filename to get the suffix of. 1348 * @return The suffix (without a dot), or "" if none. 1349 * @note The returned string points to static storage that will be overwritten with each call. 1350 */ 1351 char *sz_last_suffix(char *instr) 1352 { 1353 static char outstr[MAX_STR_LEN]; 1354 char *p; 1355 1356 p = strrchr(instr, '.'); 1357 if (!p) { 1358 outstr[0] = '\0'; 1359 } else { 1360 strcpy(outstr, p); 1361 } 1362 return (outstr); 1221 paranoid_free(tmp); 1222 return; 1363 1223 } 1364 1224 … … 1372 1232 bool is_this_file_compressed(char *filename) 1373 1233 { 1374 char do_not_compress_these[MAX_STR_LEN];1375 char tmp[MAX_STR_LEN];1234 char *do_not_compress_these; 1235 char *tmp; 1376 1236 char *p; 1377 1237 1378 sprintf(tmp, "%s/do-not-compress-these", g_mondo_home);1238 asprintf(&tmp, "%s/do-not-compress-these", g_mondo_home); 1379 1239 if (!does_file_exist(tmp)) { 1240 paranoid_free(tmp); 1380 1241 return (FALSE); 1381 1242 } 1382 strcpy(do_not_compress_these, last_line_of_file(tmp)); 1243 paranoid_free(tmp); 1244 1245 asprintf(&do_not_compress_these, last_line_of_file(tmp)); 1383 1246 for (p = do_not_compress_these; p != NULL; p++) { 1384 strcpy(tmp, p);1247 asprintf(&tmp, p); 1385 1248 if (strchr(tmp, ' ')) { 1386 1249 *(strchr(tmp, ' ')) = '\0'; 1387 1250 } 1388 if (!strcmp(sz_last_suffix(filename), tmp)) { /*printf("MATCH\n"); */ 1251 if (!strcmp(strrchr(filename, '.'), tmp)) { 1252 paranoid_free(do_not_compress_these); 1253 paranoid_free(tmp); 1389 1254 return (TRUE); 1390 1255 } 1256 paranoid_free(tmp); 1257 1391 1258 if (!(p = strchr(p, ' '))) { 1392 1259 break; 1393 1260 } 1394 1261 } 1262 paranoid_free(do_not_compress_these); 1395 1263 return (FALSE); 1396 1264 } 1397 1398 1265 1399 1266 … … 1411 1278 1412 1279 1413 1414 1415 1280 /** 1416 1281 * Create a small script that mounts /boot, calls @c grub-install, and syncs the disks. … … 1424 1289 int retval = 0; 1425 1290 1426 malloc_string(tmp);1427 1291 if ((fout = fopen(outfile, "w"))) { 1428 1292 fprintf(fout, … … 1430 1294 paranoid_fclose(fout); 1431 1295 log_msg(2, "Created %s", outfile); 1432 sprintf(tmp, "chmod +x %s", outfile);1296 asprintf(&tmp, "chmod +x %s", outfile); 1433 1297 paranoid_system(tmp); 1298 paranoid_free(tmp); 1299 1434 1300 retval = 0; 1435 1301 } else { 1436 1302 retval = 1; 1437 1303 } 1438 paranoid_free(tmp);1439 1304 return (retval); 1440 1305 } 1441 1306 1442 1307 /* @} - end fileGroup */ 1308 1309 void paranoid_alloc(char *alloc, char *orig) 1310 { 1311 paranoid_free(alloc); 1312 asprintf(&alloc, orig); 1313 } 1314
Note:
See TracChangeset
for help on using the changeset viewer.