Changeset 1113 in MondoRescue for branches/stable/mondo/src/common/libmondo-files.c
- Timestamp:
- Feb 8, 2007, 3:08:10 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mondo/src/common/libmondo-files.c
r1107 r1113 13 13 #include "libmondo-files.h" 14 14 15 #include "lib-common-externs.h"16 17 15 #include "libmondo-tools-EXT.h" 18 #include " libmondo-gui-EXT.h"16 #include "newt-specific-EXT.h" 19 17 #include "libmondo-devices-EXT.h" 20 18 #include "libmondo-fork-EXT.h" 21 19 #include "libmondo-string-EXT.h" 20 #include "mr_mem.h" 22 21 23 22 #include "mr_file.h" … … 45 44 /*@ buffers ***************************************************** */ 46 45 static char output[MAX_STR_LEN]; 47 char command[MAX_STR_LEN * 2]; 48 char tmp[MAX_STR_LEN]; 46 47 char *command = NULL; 48 char *tmp = NULL; 49 size_t n = 0; 49 50 50 51 /*@ pointers **************************************************** */ … … 59 60 60 61 assert_string_is_neither_NULL_nor_zerolength(filename); 62 61 63 if (does_file_exist(filename)) { 62 sprintf(command, "md5sum \"%s\"", filename);64 mr_asprintf(&command, "md5sum \"%s\"", filename); 63 65 fin = popen(command, "r"); 66 mr_free(command); 67 64 68 if (fin) { 65 69 (void) fgets(output, MAX_STR_LEN, fin); … … 68 72 } 69 73 } else { 70 sprintf(tmp, "File '%s' not found; cannot calc checksum",74 mr_asprintf(&tmp, "File '%s' not found; cannot calc checksum", 71 75 filename); 72 76 log_it(tmp); 77 mr_free(tmp); 73 78 } 74 79 if (p) { … … 124 129 125 130 /*@ buffers ***************************************************** */ 126 char command[MAX_STR_LEN * 2];127 char incoming[MAX_STR_LEN];128 char tmp[MAX_STR_LEN];131 char *command = NULL; 132 char *incoming = NULL; 133 char *tmp = NULL; 129 134 130 135 /*@ long ******************************************************** */ 131 136 long noof_lines = -1L; 132 137 138 /*@ int ******************************************************** */ 139 size_t n = 0; 140 133 141 /*@ pointers **************************************************** */ 134 142 FILE *fin; 135 143 136 /*@ initialize [0] to null ******************************************** */137 incoming[0] = '\0';138 139 144 assert_string_is_neither_NULL_nor_zerolength(filename); 140 145 if (!does_file_exist(filename)) { 141 sprintf(tmp,146 mr_asprintf(&tmp, 142 147 "%s does not exist, so I cannot found the number of lines in it", 143 148 filename); 144 149 log_it(tmp); 150 mr_free(tmp); 145 151 return (0); 146 152 } 147 sprintf(command, "cat %s | wc -l", filename);153 mr_asprintf(&command, "cat %s | wc -l", filename); 148 154 if (!does_file_exist(filename)) { 149 155 return (-1); 150 156 } 151 157 fin = popen(command, "r"); 158 mr_free(command); 159 152 160 if (fin) { 153 161 if (feof(fin)) { 154 162 noof_lines = 0; 155 163 } else { 156 (void) fgets(incoming, MAX_STR_LEN - 1, fin);164 mr_getline(&incoming, &n, fin); 157 165 while (strlen(incoming) > 0 158 166 && incoming[strlen(incoming) - 1] < 32) { … … 160 168 } 161 169 noof_lines = atol(incoming); 170 mr_free(incoming); 162 171 } 163 172 paranoid_pclose(fin); … … 181 190 182 191 assert(filename != NULL); 183 // assert_string_is_neither_NULL_nor_zerolength(filename); 192 184 193 if (lstat(filename, &buf)) { 185 194 mr_msg(20, "%s does not exist", filename); … … 192 201 193 202 194 195 196 197 198 203 /** 199 204 * Modify @p inout (a file containing a list of files) to only contain files … … 212 217 /*@ int ********************************************************* */ 213 218 int i; 219 size_t n = 0; 214 220 215 221 /*@ pointers **************************************************** */ … … 220 226 221 227 assert_string_is_neither_NULL_nor_zerolength(inout); 222 sprintf(infname, "%s.in", inout); 223 sprintf(outfname, "%s", inout); 224 sprintf(tmp, "cp -f %s %s", inout, infname); 228 229 mr_asprintf(&infname, "%s.in", inout); 230 231 mr_asprintf(&tmp, "cp -f %s %s", inout, infname); 225 232 run_program_and_log_output(tmp, FALSE); 233 mr_free(tmp); 234 226 235 if (!(fin = fopen(infname, "r"))) { 227 236 log_OS_error("Unable to openin infname"); 237 mr_free(infname); 228 238 return; 229 239 } 240 241 mr_asprintf(&outfname, "%s", inout); 230 242 if (!(fout = fopen(outfname, "w"))) { 231 243 log_OS_error("Unable to openout outfname"); 244 mr_free(infname); 245 mr_free(outfname); 232 246 return; 233 247 } 234 for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin); 235 fgets(incoming, MAX_STR_LEN, fin)) { 248 mr_free(outfname); 249 250 for (mr_getline(&incoming, &n, fin); !feof(fin); 251 mr_getline(&incoming, &n, fin)) { 236 252 i = strlen(incoming) - 1; 237 253 if (i >= 0 && incoming[i] < 32) { … … 241 257 fprintf(fout, "%s\n", incoming); 242 258 } else { 243 sprintf(tmp, "Excluding '%s'-nonexistent\n", incoming);259 mr_asprintf(&tmp, "Excluding '%s'-nonexistent\n", incoming); 244 260 log_it(tmp); 245 } 246 } 261 mr_free(tmp); 262 } 263 } 264 mr_free(incoming); 247 265 paranoid_fclose(fout); 248 266 paranoid_fclose(fin); 249 267 unlink(infname); 250 } 251 252 253 254 255 256 257 268 mr_free(infname); 269 } 258 270 259 271 … … 267 279 int figure_out_kernel_path_interactively_if_necessary(char *kernel) 268 280 { 269 char tmp[MAX_STR_LEN]; 270 char *command; 271 281 char *tmp = NULL; 282 char *command = NULL; 283 284 malloc_string(tmp); 272 285 if (!kernel[0]) { 273 286 strcpy(kernel, … … 277 290 // If we didn't get anything back, check whether mindi raised a fatal error 278 291 if (!kernel[0]) { 279 malloc_string(command); 280 strcpy(command, "grep 'Fatal error' /var/log/mindi.log"); 292 mr_asprintf(&command, "grep 'Fatal error' /var/log/mindi.log"); 281 293 strcpy(tmp, call_program_and_get_last_line_of_output(command)); 282 294 if (strlen(tmp) > 1) { … … 299 311 ("Kernel not found. Please specify with the '-k' flag."); 300 312 } 301 sprintf(tmp, "User says kernel is at %s", kernel); 302 log_it(tmp); 313 log_it("User says kernel is at %s", kernel); 303 314 } 304 315 return (0); 305 316 } 306 307 308 309 310 317 311 318 … … 324 331 static char output[MAX_STR_LEN]; 325 332 char *incoming; 326 char *command ;333 char *command = NULL; 327 334 328 335 malloc_string(incoming); 329 malloc_string(command);330 336 incoming[0] = '\0'; 331 337 /*@******************************* */ 332 338 333 339 assert_string_is_neither_NULL_nor_zerolength(fname); 334 sprintf(command, "which %s 2> /dev/null", fname); 340 341 mr_asprintf(&command, "which %s 2> /dev/null", fname); 335 342 strcpy(incoming, call_program_and_get_last_line_of_output(command)); 343 mr_free(command); 344 336 345 if (incoming[0] == '\0') { 337 346 if (system("which file > /dev/null 2> /dev/null")) { … … 341 350 return (NULL); // forget it :) 342 351 } 343 sprintf(command,352 mr_asprintf(&command, 344 353 "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null", 345 354 incoming); 346 355 strcpy(incoming, 347 356 call_program_and_get_last_line_of_output(command)); 357 mr_free(command); 348 358 } 349 359 if (incoming[0] == '\0') // yes, it is == '\0' twice, not once :) 350 360 { 351 sprintf(command, "dirname %s 2> /dev/null", incoming);361 mr_asprintf(&command, "dirname %s 2> /dev/null", incoming); 352 362 strcpy(incoming, 353 363 call_program_and_get_last_line_of_output(command)); 364 mr_free(command); 354 365 } 355 366 strcpy(output, incoming); … … 362 373 } 363 374 mr_free(incoming); 364 mr_free(command);365 375 if (!output[0]) { 366 376 return (NULL); … … 371 381 372 382 373 374 375 376 377 378 379 383 /** 380 384 * Get the last sequence of digits surrounded by non-digits in the first 32k of … … 397 401 398 402 assert_string_is_neither_NULL_nor_zerolength(logfile); 403 399 404 if (!(fin = fopen(logfile, "r"))) { 400 405 log_OS_error("Unable to open logfile"); … … 410 415 for (; len > 0 && isdigit(datablock[len - 1]); len--); 411 416 trackno = atoi(datablock + len); 412 /*413 sprintf(tmp,"datablock=%s; trackno=%d",datablock+len, trackno);414 log_it(tmp);415 */416 417 return (trackno); 417 418 } 418 419 420 421 422 423 419 424 420 … … 432 428 { 433 429 434 /*@ buffers ***************************************************** */ 435 char tmp[MAX_STR_LEN]; 436 char lastline[MAX_STR_LEN]; 437 char command[MAX_STR_LEN]; 438 /*@ pointers **************************************************** */ 439 char *p; 440 441 /*@ int's ******************************************************* */ 430 char *lastline = NULL; 431 char *command = NULL; 432 char *p = NULL; 442 433 int i; 443 434 435 malloc_string(lastline); 444 436 for (i = NOOF_ERR_LINES - 1; 445 437 i >= 0 && !strstr(err_log_lines[i], "% Done") 446 438 && !strstr(err_log_lines[i], "% done"); i--); 447 439 if (i < 0) { 448 sprintf(command,440 mr_asprintf(&command, 449 441 "tail -n3 %s | grep -Fi \"%c\" | tail -n1 | awk '{print $0;}'", 450 442 filename, '%'); 451 443 strcpy(lastline, 452 444 call_program_and_get_last_line_of_output(command)); 445 mr_free(command); 453 446 if (!lastline[0]) { 454 447 return (0); … … 462 455 *p = '\0'; 463 456 } 464 // mr_msg(2, "lastline='%s', ", p, lastline);465 457 if (!p) { 466 458 return (0); … … 473 465 i = atoi(p); 474 466 475 sprintf(tmp, "'%s' --> %d", p, i);476 // log_to_screen(tmp);477 478 467 return (i); 479 468 } 480 481 482 483 469 484 470 … … 493 479 /*@ buffers ***************************************************** */ 494 480 static char output[MAX_STR_LEN]; 495 static char command[MAX_STR_LEN * 2];496 static char tmp[MAX_STR_LEN];481 char *command = NULL; 482 char *tmp = NULL; 497 483 498 484 /*@ pointers **************************************************** */ 499 485 FILE *fin; 486 size_t n = 0; 500 487 501 488 /*@ end vars **************************************************** */ 502 489 503 490 if (!does_file_exist(filename)) { 504 sprintf(tmp, "Tring to get last line of nonexistent file (%s)",491 mr_asprintf(&tmp, _("Tring to get last line of nonexistent file (%s)"), 505 492 filename); 506 493 log_it(tmp); 494 mr_free(tmp); 507 495 output[0] = '\0'; 508 496 return (output); 509 497 } 510 sprintf(command, "tail -n1 %s", filename);498 mr_asprintf(&command, "tail -n1 %s", filename); 511 499 fin = popen(command, "r"); 500 mr_free(command); 501 512 502 (void) fgets(output, MAX_STR_LEN, fin); 513 503 paranoid_pclose(fin); … … 517 507 return (output); 518 508 } 509 519 510 520 511 /** … … 544 535 545 536 546 547 /**548 * ?????549 * @bug I don't know what this function does. However, it seems orphaned, so it should probably be removed.550 */551 int552 make_checksum_list_file(char *filelist, char *cksumlist, char *comppath)553 {554 /*@ pointers **************************************************** */555 FILE *fin;556 FILE *fout;557 558 /*@ int ******************************************************* */559 int percentage;560 int i;561 int counter = 0;562 563 /*@ buffer ****************************************************** */564 char stub_fname[1000];565 char curr_fname[1000];566 char curr_cksum[1000];567 char tmp[1000];568 569 /*@ long [long] ************************************************* */570 off_t filelist_length;571 off_t curr_pos;572 long start_time;573 long current_time;574 long time_taken;575 long time_remaining;576 577 /*@ end vars *************************************************** */578 579 start_time = get_time();580 filelist_length = length_of_file(filelist);581 sprintf(tmp, "filelist = %s; cksumlist = %s", filelist, cksumlist);582 log_it(tmp);583 fin = fopen(filelist, "r");584 if (fin == NULL) {585 log_OS_error("Unable to fopen-in filelist");586 log_to_screen("Can't open filelist");587 return (1);588 }589 fout = fopen(cksumlist, "w");590 if (fout == NULL) {591 log_OS_error("Unable to openout cksumlist");592 paranoid_fclose(fin);593 log_to_screen("Can't open checksum list");594 return (1);595 }596 for (fgets(stub_fname, 999, fin); !feof(fin);597 fgets(stub_fname, 999, fin)) {598 if (stub_fname[(i = strlen(stub_fname) - 1)] < 32) {599 stub_fname[i] = '\0';600 }601 sprintf(tmp, "%s%s", comppath, stub_fname);602 strcpy(curr_fname, tmp + 1);603 strcpy(curr_cksum, calc_file_ugly_minichecksum(curr_fname));604 fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum);605 if (counter++ > 12) {606 current_time = get_time();607 counter = 0;608 curr_fname[37] = '\0';609 curr_pos = ftello(fin) / 1024;610 percentage = (int) (curr_pos * 100 / filelist_length);611 time_taken = current_time - start_time;612 if (percentage == 0) {613 /* printf("%0d%% done \r",percentage); */614 } else {615 time_remaining =616 time_taken * 100 / (long) (percentage) - time_taken;617 sprintf(tmp,618 "%02d%% done %02d:%02d taken %02d:%02d remaining %-37s\r",619 percentage, (int) (time_taken / 60),620 (int) (time_taken % 60),621 (int) (time_remaining / 60),622 (int) (time_remaining % 60), curr_fname);623 log_to_screen(tmp);624 }625 sync();626 }627 }628 paranoid_fclose(fout);629 paranoid_fclose(fin);630 log_it("Done.");631 return (0);632 }633 634 635 537 /** 636 538 * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>. … … 638 540 * @return The return value of @c mkdir. 639 541 */ 542 /* BERLIOS: This function shouldn't call system at all */ 640 543 int make_hole_for_dir(char *outdir_fname) 641 544 { 642 char tmp[MAX_STR_LEN * 2];545 char *tmp; 643 546 int res = 0; 644 547 645 548 assert_string_is_neither_NULL_nor_zerolength(outdir_fname); 646 sprintf(tmp, "mkdir -p %s", outdir_fname);549 mr_asprintf(&tmp, "mkdir -p %s", outdir_fname); 647 550 res = system(tmp); 551 mr_free(tmp); 648 552 return (res); 649 553 } … … 656 560 * @bug Return value unnecessary. 657 561 */ 562 /* BERLIOS: This function shouldn't call system at all */ 658 563 int make_hole_for_file(char *outfile_fname) 659 564 { 660 565 /*@ buffer ****************************************************** */ 661 char command[MAX_STR_LEN * 2];566 char *command; 662 567 663 568 /*@ int ******************************************************** */ … … 669 574 assert(!strstr(outfile_fname, MNT_CDROM)); 670 575 assert(!strstr(outfile_fname, "/dev/cdrom")); 671 sprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname); 576 577 mr_asprintf(&command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname); 672 578 res += system(command); 673 sprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname); 579 mr_free(command); 580 581 mr_asprintf(&command, "rmdir \"%s\" 2> /dev/null", outfile_fname); 674 582 res += system(command); 675 sprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname); 583 mr_free(command); 584 585 mr_asprintf(&command, "rm -f \"%s\" 2> /dev/null", outfile_fname); 676 586 res += system(command); 587 mr_free(command); 677 588 unlink(outfile_fname); 678 589 return (0); 679 590 } 680 681 682 591 683 592 … … 697 606 698 607 /*@ buffers **************************************************** */ 699 char incoming[MAX_STR_LEN]; 700 608 char *incoming = NULL; 609 610 size_t n = 0; 701 611 /*@ end vars *************************************************** */ 702 612 … … 708 618 return (0); 709 619 } 710 (void) fgets(incoming, MAX_STR_LEN - 1, fin);620 mr_getline(&incoming, &n, fin); 711 621 while (!feof(fin)) { 712 622 if (strstr(incoming, wildcard)) { 713 623 matches++; 714 624 } 715 (void) fgets(incoming, MAX_STR_LEN - 1, fin);625 mr_getline(&incoming, &n, fin); 716 626 } 717 627 paranoid_fclose(fin); 628 mr_free(incoming); 718 629 return (matches); 719 630 } 720 721 722 631 723 632 … … 731 640 void register_pid(pid_t pid, char *name_str) 732 641 { 733 char tmp[MAX_STR_LEN + 1], lockfile_fname[MAX_STR_LEN + 1]; 642 char *tmp = NULL; 643 char *lockfile_fname = NULL; 734 644 int res; 645 size_t n = 0; 735 646 FILE *fin; 736 647 737 sprintf(lockfile_fname, "/var/run/monitas-%s.pid", name_str);648 mr_asprintf(&lockfile_fname, "/var/run/monitas-%s.pid", name_str); 738 649 if (!pid) { 739 650 log_it("Unregistering PID"); … … 741 652 log_it("Error unregistering PID"); 742 653 } 654 mr_free(lockfile_fname); 743 655 return; 744 656 } 745 657 if (does_file_exist(lockfile_fname)) { 746 tmp[0] = '\0';747 658 if ((fin = fopen(lockfile_fname, "r"))) { 748 (void) fgets(tmp, MAX_STR_LEN, fin);659 mr_getline(&tmp, &n, fin); 749 660 paranoid_fclose(fin); 750 661 } else { … … 752 663 } 753 664 pid = (pid_t) atol(tmp); 754 sprintf(tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid); 665 mr_free(tmp); 666 667 mr_asprintf(&tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid); 755 668 res = system(tmp); 669 mr_free(tmp); 756 670 if (!res) { 757 671 log_it … … 760 674 } 761 675 } 762 sprintf(tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(),676 mr_asprintf(&tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(), 763 677 lockfile_fname); 678 mr_free(lockfile_fname); 679 764 680 if (system(tmp)) { 765 681 fatal_error("Cannot register PID"); 766 682 } 767 } 768 683 mr_free(tmp); 684 return; 685 } 769 686 770 687 … … 777 694 long size_of_partition_in_mountlist_K(char *tmpdir, char *dev) 778 695 { 779 char command[MAX_STR_LEN]; 780 char mountlist[MAX_STR_LEN]; 781 char sz_res[MAX_STR_LEN]; 782 long file_len_K; 783 784 sprintf(mountlist, "%s/mountlist.txt", tmpdir); 785 sprintf(command, 786 "grep \"%s \" %s/mountlist.txt | head -n1 | awk '{print $4}'", 696 char *command = NULL; 697 char *sz_res = NULL; 698 long file_len_K = 0L; 699 700 malloc_string(sz_res); 701 mr_asprintf(&command, 702 "grep '%s ' %s/mountlist.txt | head -n1 | awk '{print $4;}'", 787 703 dev, tmpdir); 788 704 log_it(command); … … 790 706 file_len_K = atol(sz_res); 791 707 mr_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K); 708 mr_free(command); 709 mr_free(sz_res); 792 710 return (file_len_K); 793 711 } 712 794 713 795 714 /** … … 800 719 long size_of_all_biggiefiles_K(struct s_bkpinfo *bkpinfo) 801 720 { 802 /*@ buffers ***************************************************** */ 803 char *fname; 804 char *biggielist; 805 char *comment; 806 char *tmp; 807 char *command; 721 char *fname = NULL; 722 char *biggielist = NULL; 723 char *comment = NULL; 724 char *tmp = NULL; 725 char *command = NULL; 808 726 809 727 /*@ long ******************************************************** */ 810 long scratchL = 0 ;811 long file_len_K ;728 long scratchL = 0L; 729 long file_len_K = 0L; 812 730 813 731 /*@ pointers *************************************************** */ 814 732 FILE *fin = NULL; 733 size_t n = 0; 815 734 816 735 /*@ end vars *************************************************** */ 817 736 818 malloc_string(fname);819 malloc_string(biggielist);820 malloc_string(comment);821 737 malloc_string(tmp); 822 malloc_string(command);823 738 log_it("Calculating size of all biggiefiles (in total)"); 824 sprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);739 mr_asprintf(&biggielist, "%s/biggielist.txt", bkpinfo->tmpdir); 825 740 log_it("biggielist = %s", biggielist); 826 741 if (!(fin = fopen(biggielist, "r"))) { … … 829 744 } else { 830 745 mr_msg(4, "Reading it..."); 831 for ( fgets(fname, MAX_STR_LEN, fin); !feof(fin);832 fgets(fname, MAX_STR_LEN, fin)) {746 for (mr_getline(&fname, &n, fin); !feof(fin); 747 mr_getline(&fname, &n, fin)) { 833 748 if (fname[strlen(fname) - 1] <= 32) { 834 749 fname[strlen(fname) - 1] = '\0'; … … 839 754 fatal_error("ntfsresize not found"); 840 755 } 841 sprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);756 mr_asprintf(&command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname); 842 757 log_it("command = %s", command); 843 758 strcpy (tmp, call_program_and_get_last_line_of_output(command)); 759 mr_free(command); 760 844 761 log_it("res of it = %s", tmp); 845 762 file_len_K = atoll(tmp) / 1024L; … … 855 772 mr_msg(4, "%s --> %ld K", fname, file_len_K); 856 773 } 857 sprintf(comment,774 mr_asprintf(&comment, 858 775 "After adding %s, scratchL+%ld now equals %ld", fname, 859 776 file_len_K, scratchL); 860 777 mr_msg(4, comment); 778 mr_free(comment); 779 861 780 if (feof(fin)) { 862 781 break; 863 782 } 864 783 } 865 } 784 mr_free(fname); 785 } 786 mr_free(biggielist); 787 866 788 log_it("Closing..."); 867 789 paranoid_fclose(fin); 868 790 log_it("Finished calculating total size of all biggiefiles"); 869 mr_free(fname);870 mr_free(biggielist);871 mr_free(comment);872 791 mr_free(tmp); 873 mr_free(command);874 792 return (scratchL); 875 793 } … … 884 802 { 885 803 /*@ buffer ****************************************************** */ 886 char tmp[MAX_STR_LEN];887 char command[MAX_STR_LEN * 2];804 char *tmp = NULL; 805 char *command = NULL; 888 806 long long llres; 807 size_t n = 0; 889 808 /*@ pointers **************************************************** */ 890 char *p ;891 FILE *fin ;809 char *p = NULL; 810 FILE *fin = NULL; 892 811 893 812 /*@ end vars *************************************************** */ 894 813 895 sprintf(command, "du -sk %s", mountpt);814 mr_asprintf(&command, "du -sk %s", mountpt); 896 815 errno = 0; 897 816 fin = popen(command, "r"); … … 900 819 llres = 0; 901 820 } else { 902 (void) fgets(tmp, MAX_STR_LEN, fin); 903 paranoid_pclose(fin); 904 p = strchr(tmp, '\t'); 905 if (p) { 906 *p = '\0'; 907 } 908 for (p = tmp, llres = 0; *p != '\0'; p++) { 909 llres *= 10; 910 llres += (int) (*p - '0'); 911 } 912 } 913 821 mr_getline(&tmp, &n, fin); 822 paranoid_pclose(fin); 823 p = strchr(tmp, '\t'); 824 if (p) { 825 *p = '\0'; 826 } 827 for (p = tmp, llres = 0; *p != '\0'; p++) { 828 llres *= 10; 829 llres += (int) (*p - '0'); 830 } 831 } 832 833 mr_free(command); 834 mr_free(tmp); 914 835 return (llres); 915 836 } … … 931 852 } 932 853 854 933 855 /** 934 856 * Update a reverse CRC checksum to include another character. … … 947 869 948 870 949 950 951 871 /** 952 872 * Check for an executable on the user's system; write a message to the … … 958 878 { 959 879 /*@ buffers *** */ 960 char command[MAX_STR_LEN * 2]; 961 char errorstr[MAX_STR_LEN]; 962 963 964 sprintf(command, "which %s > /dev/null 2> /dev/null", fname); 965 sprintf(errorstr, 966 "Please install '%s'. I cannot find it on your system.", 880 char *command; 881 char *errorstr; 882 int res = 0; 883 884 885 mr_asprintf(&command, "which %s > /dev/null 2> /dev/null", fname); 886 res = system(command); 887 mr_free(command); 888 889 if (res) { 890 mr_asprintf(&errorstr, 891 _("Please install '%s'. I cannot find it on your system."), 967 892 fname); 968 if (system(command)) {969 893 log_to_screen(errorstr); 894 mr_free(errorstr); 970 895 log_to_screen 971 ( "There may be hyperlink at http://www.mondorescue.com which");972 log_to_screen( "will take you to the relevant (missing) package.");896 (_("There may be an hyperlink at http://www.mondorescue.org which")); 897 log_to_screen(_("will take you to the relevant (missing) package.")); 973 898 return (1); 974 899 } else { … … 976 901 } 977 902 } 978 979 980 981 982 903 983 904 … … 1012 933 1013 934 1014 1015 935 /** 1016 936 * Read @p fname into @p contents. … … 1045 965 return (res); 1046 966 } 1047 1048 1049 1050 1051 1052 1053 1054 967 1055 968 … … 1067 980 { 1068 981 /*@ Char buffers ** */ 1069 char command[MAX_STR_LEN * 2];982 char *command = NULL; 1070 983 char tmp[MAX_STR_LEN]; 1071 984 char old_pwd[MAX_STR_LEN]; … … 1074 987 "Copying Mondo's core files to the scratch directory"); 1075 988 989 /* BERLIOS: Why do we need to do it here as well ? */ 1076 990 mr_msg(4, "g_mondo_home='%s'", g_mondo_home); 1077 991 if (strlen(g_mondo_home) < 2) { 1078 992 find_and_store_mondoarchives_home(g_mondo_home); 1079 993 } 1080 sprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home,994 mr_asprintf(&command, CP_BIN " --parents -pRdf %s %s", g_mondo_home, 1081 995 bkpinfo->scratchdir); 1082 996 … … 1085 999 fatal_error("Failed to copy Mondo's stuff to scratchdir"); 1086 1000 } 1001 mr_free(command); 1002 1003 /* i18n */ 1004 mr_asprintf(&command, CP_BIN " --parents /usr/share/locale/*/LC_MESSAGES/mondo.mo %s",bkpinfo->scratchdir); 1005 mr_msg(4, "command = %s", command); 1006 run_program_and_log_output(command, 1); 1007 mr_free(command); 1087 1008 1088 1009 sprintf(tmp, "%s/payload.tgz", g_mondo_home); … … 1092 1013 (void) getcwd(old_pwd, MAX_STR_LEN - 1); 1093 1014 chdir(bkpinfo->scratchdir); 1094 sprintf(command, "tar -zxvf %s", tmp);1015 mr_asprintf(&command, "tar -zxvf %s", tmp); 1095 1016 if (run_program_and_log_output(command, FALSE)) { 1096 1017 fatal_error("Failed to untar payload"); 1097 1018 } 1019 mr_free(command); 1098 1020 chdir(old_pwd); 1099 1021 } 1100 1022 1101 sprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir,1023 mr_asprintf(&command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir, 1102 1024 bkpinfo->scratchdir); 1103 1025 … … 1105 1027 fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir"); 1106 1028 } 1107 1108 strcpy(tmp, 1109 call_program_and_get_last_line_of_output("which mondorestore"));1110 if (!tmp [0]) {1029 mr_free(command); 1030 1031 tmp = call_program_and_get_last_line_of_output("which mondorestore"); 1032 if (!tmp) { 1111 1033 fatal_error 1112 1034 ("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?"); 1113 1035 } 1114 sprintf(command, "cp -f %s %s", tmp, bkpinfo->tmpdir);1036 mr_asprintf(&command, "cp -f %s %s", tmp, bkpinfo->tmpdir); 1115 1037 if (run_program_and_log_output(command, FALSE)) { 1116 1038 fatal_error("Failed to copy mondorestore to tmpdir"); 1117 1039 } 1118 1119 sprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir); 1040 mr_free(command); 1041 1042 mr_asprintf(&command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir); 1120 1043 paranoid_system(command); 1044 mr_free(command); 1121 1045 1122 1046 if (bkpinfo->postnuke_tarball[0]) { 1123 sprintf(command, "cp -f %s %s/post-nuke.tgz",1047 mr_asprintf(&command, "cp -f %s %s/post-nuke.tgz", 1124 1048 bkpinfo->postnuke_tarball, bkpinfo->tmpdir); 1125 1049 if (run_program_and_log_output(command, FALSE)) { 1126 1050 fatal_error("Unable to copy post-nuke tarball to tmpdir"); 1127 1051 } 1128 }1129 1052 mr_free(command); 1053 } 1130 1054 1131 1055 mvaddstr_and_log_it(g_currentY++, 74, "Done."); 1132 1056 } 1133 1134 1135 1136 1057 1137 1058 … … 1156 1077 char nfs_client_defgw[MAX_STR_LEN]; 1157 1078 char nfs_server_ipaddr[MAX_STR_LEN]; 1158 char tmp[MAX_STR_LEN];1159 char command[MAX_STR_LEN * 2];1079 char *tmp = NULL; 1080 char *command = NULL; 1160 1081 1161 1082 FILE *fd1 = NULL; … … 1165 1086 1166 1087 log_it("Storing NFS configuration"); 1167 strcpy(tmp, bkpinfo->nfs_mount);1088 mr_asprintf(&tmp, bkpinfo->nfs_mount); 1168 1089 p = strchr(tmp, ':'); 1169 1090 if (!p) { … … 1173 1094 *(p++) = '\0'; 1174 1095 strcpy(nfs_server_ipaddr, tmp); 1096 mr_free(tmp); 1097 1175 1098 strcpy(nfs_mount, p); 1176 1099 /* BERLIOS : there is a bug #67 here as it only considers the first NIC */ 1177 sprintf(command,1100 mr_asprintf(&command, 1178 1101 "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1"); 1179 1102 strcpy(nfs_dev, call_program_and_get_last_line_of_output(command)); 1180 sprintf(command, 1103 mr_free(command); 1104 1105 mr_asprintf(&command, 1181 1106 "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2"); 1182 1107 strcpy(nfs_client_ipaddr, 1183 1108 call_program_and_get_last_line_of_output(command)); 1184 sprintf(command, 1109 mr_free(command); 1110 1111 mr_asprintf(&command, 1185 1112 "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2"); 1186 1113 strcpy(nfs_client_netmask, 1187 1114 call_program_and_get_last_line_of_output(command)); 1188 sprintf(command, 1115 mr_free(command); 1116 1117 mr_asprintf(&command, 1189 1118 "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f8 | cut -d':' -f2"); 1190 1119 strcpy(nfs_client_broadcast, 1191 1120 call_program_and_get_last_line_of_output(command)); 1192 sprintf(command, 1121 mr_free(command); 1122 1123 mr_asprintf(&command, 1193 1124 "route -n | grep '^0.0.0.0' | awk '{print $2}'"); 1194 1125 strcpy(nfs_client_defgw, 1195 1126 call_program_and_get_last_line_of_output(command)); 1196 sprintf(tmp, 1197 "nfs_client_ipaddr=%s; nfs_server_ipaddr=%s; nfs_mount=%s", 1198 nfs_client_ipaddr, nfs_server_ipaddr, nfs_mount); 1127 mr_free(command); 1128 1129 mr_asprintf(&tmp, 1130 "nfs_client_ipaddr=%s; nfs_client_netmask=%s; nfs_server_ipaddr=%s; nfs_mount=%s; nfs_client_defgw=%s; ", 1131 nfs_client_ipaddr, nfs_client_netmask, nfs_server_ipaddr, nfs_mount, nfs_client_defgw); 1132 log_it(tmp); 1133 mr_free(tmp); 1134 1199 1135 if (strlen(nfs_dev) < 2) { 1200 1136 fatal_error … … 1212 1148 if (!strncmp(nfs_dev, "bond", 4)) { 1213 1149 log_to_screen("Found bonding device %s; looking for corresponding ethN slave device\n", nfs_dev); 1214 sprintf(command,1150 mr_asprintf(&command, 1215 1151 "ifconfig %s | awk '{print $5}' | head -n1", nfs_dev); 1216 1152 strcpy(mac_addr, call_program_and_get_last_line_of_output(command)); 1217 sprintf(command, 1153 mr_free(command); 1154 1155 mr_asprintf(&command, 1218 1156 "ifconfig | grep -E '%s' | grep -v '%s' | head -n1 | cut -d' ' -f1", mac_addr,nfs_dev); 1219 1157 strcpy(nfs_dev, call_program_and_get_last_line_of_output(command)); 1158 mr_free(command); 1159 1220 1160 log_to_screen("Replacing it with %s\n", nfs_dev); 1221 1161 } 1222 1162 1223 1163 fd1 = mr_fopen(MONDORESTORECFG, "a"); 1224 1225 1164 mr_fprintf(fd1, "nfs-dev %s\n", nfs_dev); 1226 1165 mr_fprintf(fd1, "nfs-client-ipaddr %s\n", nfs_client_ipaddr); … … 1236 1175 log_it("Finished storing NFS configuration"); 1237 1176 } 1238 1239 1240 1241 1242 1177 1243 1178 … … 1262 1197 { 1263 1198 /*@ buffers *************** */ 1264 char tmp[MAX_STR_LEN];1199 char *tmp = NULL; 1265 1200 1266 1201 /*@ long long ************* */ … … 1289 1224 } 1290 1225 if (scratchLL <= 1) { 1291 sprintf(tmp, 1292 "Your backup will probably occupy a single %s. Maybe two.", 1293 media_descriptor_string(bkpinfo->backup_media_type)); 1294 } else if (scratchLL > 4) { 1295 sprintf(tmp, 1296 "Your backup will occupy one meeeeellion media! (maybe %s)", 1297 number_to_text((int) (scratchLL + 1))); 1226 mr_asprintf(&tmp, 1227 _("Your backup will probably occupy a single %s. Maybe two."), 1228 bkpinfo->backup_media_string); 1298 1229 } else { 1299 sprintf(tmp, "Your backup will occupy approximately %s media.",1230 mr_asprintf(&tmp, _("Your backup will occupy approximately %s media."), 1300 1231 number_to_text((int) (scratchLL + 1))); 1301 1232 } … … 1303 1234 log_to_screen(tmp); 1304 1235 } 1305 } 1306 1307 1308 /** 1309 * Get the last suffix of @p instr. 1310 * If @p instr was "httpd.log.gz", we would return "gz". 1311 * @param instr The filename to get the suffix of. 1312 * @return The suffix (without a dot), or "" if none. 1313 * @note The returned string points to static storage that will be overwritten with each call. 1314 */ 1315 char *sz_last_suffix(char *instr) 1316 { 1317 static char outstr[MAX_STR_LEN]; 1318 char *p; 1319 1320 p = strrchr(instr, '.'); 1321 if (!p) { 1322 outstr[0] = '\0'; 1323 } else { 1324 strcpy(outstr, p); 1325 } 1326 return (outstr); 1236 mr_free(tmp); 1237 return; 1327 1238 } 1328 1239 … … 1336 1247 bool is_this_file_compressed(char *filename) 1337 1248 { 1338 char do_not_compress_these[MAX_STR_LEN]; 1339 char tmp[MAX_STR_LEN]; 1340 char *p; 1341 1342 sprintf(tmp, "%s/do-not-compress-these", g_mondo_home); 1249 char *do_not_compress_these = NULL; 1250 char *tmp = NULL; 1251 char *p = NULL; 1252 1253 malloc_string(do_not_compress_these); 1254 mr_asprintf(&tmp, "%s/do-not-compress-these", g_mondo_home); 1343 1255 if (!does_file_exist(tmp)) { 1256 mr_free(tmp); 1344 1257 return (FALSE); 1345 1258 } 1346 strcpy(do_not_compress_these, last_line_of_file(tmp)); 1259 mr_free(tmp); 1260 1261 strcpy(do_not_compress_these,last_line_of_file(tmp)); 1347 1262 for (p = do_not_compress_these; p != NULL; p++) { 1348 strcpy(tmp, p);1263 mr_asprintf(&tmp, p); 1349 1264 if (strchr(tmp, ' ')) { 1350 1265 *(strchr(tmp, ' ')) = '\0'; 1351 1266 } 1352 if (!strcmp(sz_last_suffix(filename), tmp)) { /*printf("MATCH\n"); */ 1267 if (!strcmp(sz_last_suffix(filename), tmp)) { 1268 mr_free(do_not_compress_these); 1269 mr_free(tmp); 1353 1270 return (TRUE); 1354 1271 } 1272 mr_free(tmp); 1273 1355 1274 if (!(p = strchr(p, ' '))) { 1356 1275 break; 1357 1276 } 1358 1277 } 1278 mr_free(do_not_compress_these); 1359 1279 return (FALSE); 1360 1280 } 1361 1362 1281 1363 1282 … … 1375 1294 1376 1295 1377 1378 1379 1296 /** 1380 1297 * Create a small script that mounts /boot, calls @c grub-install, and syncs the disks. … … 1384 1301 int make_grub_install_scriptlet(char *outfile) 1385 1302 { 1386 FILE *fout ;1387 char *tmp ;1303 FILE *fout = NULL; 1304 char *tmp = NULL; 1388 1305 int retval = 0; 1389 1306 1390 malloc_string(tmp);1391 1307 if ((fout = fopen(outfile, "w"))) { 1392 1308 fprintf(fout, … … 1394 1310 paranoid_fclose(fout); 1395 1311 mr_msg(2, "Created %s", outfile); 1396 sprintf(tmp, "chmod +x %s", outfile);1312 mr_asprintf(&tmp, "chmod +x %s", outfile); 1397 1313 paranoid_system(tmp); 1314 mr_free(tmp); 1315 1398 1316 retval = 0; 1399 1317 } else { 1400 1318 retval = 1; 1401 1319 } 1402 mr_free(tmp);1403 1320 return (retval); 1404 1321 }
Note:
See TracChangeset
for help on using the changeset viewer.