Changeset 3191 in MondoRescue for branches/3.2/mondo/src/common/libmondo-fork.c
- Timestamp:
- Sep 25, 2013, 8:55:45 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mondo/src/common/libmondo-fork.c
r3185 r3191 8 8 #include "my-stuff.h" 9 9 #include "mr_mem.h" 10 #include "mr_str.h" 10 11 #include "mondostructures.h" 11 12 #include "libmondo-fork.h" … … 38 39 /*@ buffers ***************************************************** */ 39 40 static char result[MAX_STR_LEN]; 40 char *tmp; 41 char *p; 41 char *tmp = NULL; 42 42 43 43 /*@ pointers **************************************************** */ 44 FILE *fin; 44 FILE *fin = NULL; 45 int res = 0; 45 46 46 47 /*@ initialize data ********************************************* */ 47 malloc_string(tmp);48 48 result[0] = '\0'; 49 tmp[0] = '\0';50 49 51 50 /*@******************************************************************** */ … … 53 52 assert_string_is_neither_NULL_nor_zerolength(call); 54 53 if ((fin = popen(call, "r"))) { 55 for (p = fgets(tmp, MAX_STR_LEN, fin); !feof(fin) && (p != NULL); 56 p = fgets(tmp, MAX_STR_LEN, fin)) { 54 for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) { 57 55 if (strlen(tmp) > 1) { 58 56 strcpy(result, tmp); 59 57 } 58 mr_free(tmp); 60 59 } 61 60 paranoid_pclose(fin); 62 61 } else { 63 log_OS_error("Unable to popen call");62 log_OS_error("Unable to open resulting file"); 64 63 } 65 64 strip_spaces(result); 66 paranoid_free(tmp); 67 return (result); 68 } 69 70 71 72 65 mr_free(tmp); 66 return(result); 67 } 73 68 74 69 … … 86 81 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs). 87 82 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number. 88 * @param logstub Unused.89 83 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog. 90 84 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure). 91 * @bug @p logstub is unused.92 85 */ 93 86 int … … 101 94 102 95 /*@ buffers *** */ 103 char *midway_call, *ultimate_call, *tmp, *command, *incoming, 104 *old_stderr, *cd_number_str; 105 char *p; 96 char *midway_call = NULL; 97 char *ultimate_call = NULL; 98 char *tmp = NULL; 99 100 char *cd_number_str = NULL; 101 char *command = NULL; 102 char *p= NULL; 106 103 char *tmp1 = NULL; 104 char *tmp2 = NULL; 107 105 108 106 /*@*********** End Variables ***************************************/ … … 110 108 log_msg(3, "Starting"); 111 109 assert(bkpinfo != NULL); 112 // BERLIOS: doesn't work even if the string is correct !113 //assert_string_is_neither_NULL_nor_zerolength(basic_call);114 110 assert_string_is_neither_NULL_nor_zerolength(isofile); 115 assert_string_is_neither_NULL_nor_zerolength(logstub); 111 116 112 if (!(midway_call = malloc(1200))) { 117 113 fatal_error("Cannot malloc midway_call"); … … 123 119 fatal_error("Cannot malloc tmp"); 124 120 } 125 if (!(command = malloc(1200))) {126 fatal_error("Cannot malloc command");127 }128 malloc_string(incoming);129 malloc_string(old_stderr);130 malloc_string(cd_number_str);131 132 incoming[0] = '\0';133 old_stderr[0] = '\0';134 121 135 122 if ((bkpinfo->netfs_user) && (strstr(bkpinfo->netfs_proto,"nfs"))) { … … 139 126 } 140 127 141 sprintf(cd_number_str, "%d", cd_no);128 mr_asprintf(cd_number_str, "%d", cd_no); 142 129 resolve_naff_tokens(midway_call, tmp1, isofile, "_ISO_"); 130 log_msg(4, "basic call = '%s'", tmp1); 131 mr_free(tmp1); 132 143 133 resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_"); 134 log_msg(4, "midway_call = '%s'", midway_call); 135 mr_free(cd_number_str); 136 144 137 resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_"); 145 log_msg(4, "basic call = '%s'", tmp1);146 log_msg(4, "midway_call = '%s'", midway_call);147 log_msg(4, "tmp = '%s'", tmp);148 138 log_msg(4, "ultimate call = '%s'", ultimate_call); 149 sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE); 150 151 log_to_screen 152 ("Please be patient. Do not be alarmed by on-screen inactivity."); 153 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'", 154 what_i_am_doing); 155 strcpy(tmp, command); 139 mr_asprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE); 140 141 log_to_screen("Please be patient. Do not be alarmed by on-screen inactivity."); 142 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'", what_i_am_doing); 156 143 if (bkpinfo->manual_cd_tray) { 157 p = strstr(tmp, "2>>"); 144 mr_asprintf(tmp2, "%s", command); 145 p = strstr(tmp2, "2>>"); 158 146 if (p) { 159 147 sprintf(p, " "); … … 161 149 p++; 162 150 } 163 for (; *p != ' '; p++) {151 for (; (*p != ' ') && (*p != '\0'); p++) { 164 152 *p = ' '; 165 153 } 166 154 } 167 strcpy(command, tmp);168 #ifndef _XWIN 155 mr_free(command); 156 command = tmp2; 169 157 if (!g_text_mode) { 170 158 newtSuspend(); 171 159 } 172 #endif173 160 log_msg(1, "command = '%s'", command); 174 161 retval += system(command); … … 177 164 } 178 165 if (retval) { 179 log_msg(2, "Basic call '%s' returned an error.", tmp1); 180 popup_and_OK("Press ENTER to continue."); 181 popup_and_OK 182 ("mkisofs and/or cdrecord returned an error. CD was not created"); 166 popup_and_OK("mkisofs and/or cdrecord returned an error. CD was not created"); 183 167 } 184 168 } … … 186 170 else { 187 171 log_msg(3, "command = '%s'", command); 188 // yes_this_is_a_goto: 189 retval = 190 run_external_binary_with_percentage_indicator_NEW 191 (what_i_am_doing, command); 192 } 193 194 mr_free(tmp1); 172 retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command); 173 } 174 mr_free(command); 195 175 paranoid_free(midway_call); 196 176 paranoid_free(ultimate_call); 197 177 paranoid_free(tmp); 198 paranoid_free(command);199 paranoid_free(incoming);200 paranoid_free(old_stderr);201 paranoid_free(cd_number_str);202 178 return (retval); 203 179 } … … 232 208 log_msg(1, "command = '%s'", command); 233 209 if (!g_text_mode) { 234 retval = run_external_binary_with_percentage_indicator_NEW 235 (what_i_am_doing, command); 210 retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command); 236 211 } else { 237 212 retval += system(command); … … 277 252 return (1); 278 253 } 279 // if (debug_level == TRUE) { debug_level=5; }280 281 // assert_string_is_neither_NULL_nor_zerolength(program);282 254 283 255 if (debug_level <= g_loglevel) { … … 322 294 } 323 295 } 324 /* end of patch */325 296 strip_spaces(incoming); 326 297 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) { … … 342 313 } 343 314 } 344 // else345 // { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }346 315 return (res); 347 316 } … … 368 337 369 338 /*@ buffers **************************************************** */ 370 char *tmp = NULL;371 339 char *command = NULL; 372 char lockfile[MAX_STR_LEN];340 char *lockfile = NULL; 373 341 374 342 /*@ end vars *************************************************** */ … … 376 344 assert_string_is_neither_NULL_nor_zerolength(basic_call); 377 345 378 sprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir); 379 380 mr_asprintf(command, 381 "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res", 382 lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile); 346 mr_asprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir); 347 348 mr_asprintf(command, "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res", lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile); 383 349 open_evalcall_form(what_i_am_doing); 384 mr_asprintf(tmp, "Executing %s", basic_call); 385 log_msg(2, tmp); 386 mr_free(tmp); 350 log_msg(2, "Executing %s", basic_call); 387 351 388 352 if (!(fin = popen(command, "r"))) { 389 353 log_OS_error("Unable to popen-in command"); 390 mr_asprintf(tmp, "Failed utterly to call '%s'", command); 391 log_to_screen(tmp); 392 mr_free(tmp); 354 log_to_screen("Failed utterly to call '%s'", command); 393 355 mr_free(command); 356 mr_free(lockfile); 394 357 return (1); 395 358 } 359 396 360 if (!does_file_exist(lockfile)) { 397 361 log_to_screen("Waiting for '%s' to start",command); … … 401 365 } 402 366 mr_free(command); 403 #ifdef _XWIN 404 /* This only can update when newline goes into the file, 405 but it's *much* prettier/faster on Qt. */ 406 while (does_file_exist(lockfile)) { 407 while (!feof(fin)) { 408 /* TODO: Dead and wrong code */ 409 if (!fgets(tmp, 512, fin)) 410 break; 411 log_to_screen(tmp); 412 } 413 usleep(500000); 414 } 415 #else 416 /* This works on Newt, and it gives quicker updates. */ 367 417 368 for (; does_file_exist(lockfile); sleep(1)) { 418 369 log_file_end_to_screen(MONDO_LOGFILE, ""); 419 370 update_evalcall_form(1); 420 371 } 421 #endif 372 422 373 /* Evaluate the status returned by pclose to get the exit code of the called program. */ 423 374 errno = 0; … … 434 385 close_evalcall_form(); 435 386 unlink(lockfile); 387 mr_free(lockfile); 388 436 389 return (retval); 437 390 } … … 453 406 // if dir=='w' then copy from orig to archived 454 407 // if dir=='r' then copy from archived to orig 455 char *tmp; 456 char *buf; 457 char filestr[MAX_STR_LEN]; 408 char *tmp = NULL; 409 char *tmp1 = NULL; 410 char *buf = NULL; 411 char *filestr = NULL; 458 412 long int bytes_to_be_read, bytes_read_in, bytes_written_out = 459 413 0, bufcap, subsliceno = 0; … … 465 419 466 420 log_msg(5, "Opening."); 467 if (!(tmp = malloc(tmpcap))) { 468 fatal_error("Failed to malloc() tmp"); 469 } 470 tmp[0] = '\0'; 421 471 422 bufcap = 256L * 1024L; 472 423 if (!(buf = malloc(bufcap))) { … … 477 428 fin = f_orig; 478 429 fout = f_archived; 479 sprintf(tmp, "%-64s", PIMP_START_SZ);430 mr_asprintf(tmp, "%-64s", PIMP_START_SZ); 480 431 if (fwrite(tmp, 1, 64, fout) != 64) { 432 mr_free(tmp); 481 433 fatal_error("Can't write the introductory block"); 482 434 } 435 mr_free(tmp); 436 483 437 while (1) { 484 438 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin); … … 486 440 break; 487 441 } 488 sprintf(tmp, "%-64ld", bytes_read_in);442 mr_asprintf(tmp, "%-64ld", bytes_read_in); 489 443 if (fwrite(tmp, 1, 64, fout) != 64) { 444 mr_free(tmp); 490 445 fatal_error("Cannot write introductory block"); 491 446 } 447 mr_free(tmp); 448 492 449 log_msg(7, 493 450 "subslice #%ld --- I have read %ld of %ld bytes in from f_orig", 494 451 subsliceno, bytes_read_in, bytes_to_be_read); 495 452 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout); 496 sprintf(tmp, "%-64ld", subsliceno);453 mr_asprintf(tmp, "%-64ld", subsliceno); 497 454 if (fwrite(tmp, 1, 64, fout) != 64) { 455 mr_free(tmp); 498 456 fatal_error("Cannot write post-thingy block"); 499 457 } 458 mr_free(tmp); 500 459 log_msg(7, "Subslice #%d written OK", subsliceno); 501 460 subsliceno++; 502 461 } 503 sprintf(tmp, "%-64ld", 0L);462 mr_asprintf(tmp, "%-64ld", 0L); 504 463 if (fwrite(tmp, 1, 64L, fout) != 64L) { 464 mr_free(tmp); 505 465 fatal_error("Cannot write final introductory block"); 506 466 } 467 mr_free(tmp); 468 469 mr_asprintf(tmp, "%-64s", PIMP_END_SZ); 470 if (fwrite(tmp, 1, 64, fout) != 64) { 471 mr_free(tmp); 472 fatal_error("Can't write the final block"); 473 } 474 mr_free(tmp); 507 475 } else { 508 476 fin = f_archived; 509 477 fout = f_orig; 510 if (fread(tmp, 1, 64L, fin) != 64L) { 478 if (!(tmp1 = malloc(tmpcap))) { 479 fatal_error("Failed to malloc() tmp"); 480 } 481 if (fread(tmp1, 1, 64L, fin) != 64L) { 482 mr_free(tmp1); 511 483 fatal_error("Cannot read the introductory block"); 512 484 } 513 log_msg(5, "tmp is %s", tmp); 514 if (!strstr(tmp, PIMP_START_SZ)) { 485 log_msg(5, "tmp1 is %s", tmp1); 486 if (!strstr(tmp1, PIMP_START_SZ)) { 487 mr_free(tmp1); 515 488 fatal_error("Can't find intro blk"); 516 489 } 517 if (fread(tmp, 1, 64L, fin) != 64L) { 490 if (fread(tmp1, 1, 64L, fin) != 64L) { 491 mr_free(tmp1); 518 492 fatal_error("Cannot read introductory blk"); 519 493 } 520 bytes_to_be_read = atol(tmp );494 bytes_to_be_read = atol(tmp1); 521 495 while (bytes_to_be_read > 0) { 522 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, 523 bytes_to_be_read); 496 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, bytes_to_be_read); 524 497 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin); 525 498 if (bytes_read_in != bytes_to_be_read) { 526 fatal_error527 499 mr_free(tmp1); 500 fatal_error("Danger, WIll Robinson. Failed to read whole subvol from archives."); 528 501 } 529 502 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout); 530 if (fread(tmp, 1, 64, fin) != 64) { 503 if (fread(tmp1, 1, 64, fin) != 64) { 504 mr_free(tmp1); 531 505 fatal_error("Cannot read post-thingy block"); 532 506 } 533 if (atol(tmp) != subsliceno) { 534 log_msg(1, "Wanted subslice %ld but got %ld ('%s')", 535 subsliceno, atol(tmp), tmp); 507 if (atol(tmp1) != subsliceno) { 508 log_msg(1, "Wanted subslice %ld but got %ld ('%s')", subsliceno, atol(tmp1), tmp1); 536 509 } 537 510 log_msg(7, "Subslice #%ld read OK", subsliceno); 538 511 subsliceno++; 539 if (fread(tmp, 1, 64, fin) != 64) { 512 if (fread(tmp1, 1, 64, fin) != 64) { 513 mr_free(tmp1); 540 514 fatal_error("Cannot read introductory block"); 541 515 } 542 bytes_to_be_read = atol(tmp); 543 } 544 } 545 546 // log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in); 516 bytes_to_be_read = atol(tmp1); 517 } 518 } 547 519 548 520 if (direction == 'w') { 549 sprintf(tmp, "%-64s", PIMP_END_SZ);521 mr_asprintf(tmp, "%-64s", PIMP_END_SZ); 550 522 if (fwrite(tmp, 1, 64, fout) != 64) { 523 mr_free(tmp); 551 524 fatal_error("Can't write the final block"); 552 525 } 526 mr_free(tmp); 553 527 } else { 554 log_msg(1, "tmpA is %s", tmp); 555 if (!strstr(tmp, PIMP_END_SZ)) { 556 if (fread(tmp, 1, 64, fin) != 64) { 528 log_msg(1, "tmpA is %s", tmp1); 529 if (!strstr(tmp1, PIMP_END_SZ)) { 530 if (fread(tmp1, 1, 64, fin) != 64) { 531 mr_free(tmp1); 557 532 fatal_error("Can't read the final block"); 558 533 } 559 log_msg(5, "tmpB is %s", tmp );560 if (!strstr(tmp , PIMP_END_SZ)) {561 sprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);534 log_msg(5, "tmpB is %s", tmp1); 535 if (!strstr(tmp1, PIMP_END_SZ)) { 536 mr_asprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir); 562 537 ftmp = fopen(filestr, "w"); 563 bytes_read_in = fread(tmp, 1, 64, fin); 538 mr_free(filestr); 539 540 bytes_read_in = fread(tmp1, 1, 64, fin); 564 541 log_msg(1, "bytes_read_in = %ld", bytes_read_in); 565 // if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); } 542 566 543 if (fwrite(tmp, 1, bytes_read_in, ftmp)) { 567 544 fatal_error("Can't fwrite here"); 568 545 } 569 sprintf(tmp, "I am here - %lld", (long long)ftello(fin)); 570 // log_msg(0, tmp);546 547 mr_asprintf(tmp, "I am here - %lld", (long long)ftello(fin)); 571 548 if (fread(tmp, 1, tmpcap, fin)) { 549 mr_free(tmp); 572 550 fatal_error("Can't fread here"); 573 551 } 574 552 log_msg(0, "tmp = '%s'", tmp); 575 553 if (fwrite(tmp, 1, tmpcap, ftmp)) { 554 mr_free(tmp); 576 555 fatal_error("Can't fwrite there"); 577 556 } 578 557 fclose(ftmp); 558 mr_free(tmp); 579 559 fatal_error("Missing terminating block"); 580 560 } 581 561 } 582 562 } 563 mr_free(tmp1); 583 564 584 565 paranoid_free(buf); 585 paranoid_free(tmp);586 566 log_msg(3, "Successfully copied %ld bytes", bytes_written_out); 587 567 return (retval); … … 601 581 // BACKUP 602 582 int res = -1; 603 char *command;583 char *command = NULL; 604 584 605 585 if (!does_file_exist(input_device)) { … … 609 589 fatal_error("ntfsclone not found"); 610 590 } 611 malloc_string(command); 612 sprintf(command, "ntfsclone --rescue --force --save-image --overwrite %s %s", output_fname, input_device); 591 mr_asprintf(command, "ntfsclone --rescue --force --save-image --overwrite %s %s", output_fname, input_device); 613 592 res = run_program_and_log_output(command, 5); 614 paranoid_free(command); 593 mr_free(command); 594 615 595 unlink(output_fname); 616 596 return (res); … … 638 618 639 619 640 int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd) 641 { 620 int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd) { 642 621 643 622 /*@ int *************************************************************** */ … … 650 629 651 630 /*@ buffers *********************************************************** */ 652 char *command; 653 char *title; 631 char *command = NULL; 654 632 /*@ pointers ********************************************************** */ 655 633 static int chldres = 0; … … 662 640 *pchild_result = 999; 663 641 664 malloc_string(title); 665 malloc_string(command); 666 strcpy(title, tt); 667 sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE); 642 mr_asprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE); 668 643 log_msg(3, "command = '%s'", command); 669 if ((res = 670 pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, 671 (void *) command))) { 644 if ((res = pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, (void *) command))) { 672 645 fatal_error("Unable to create an archival thread"); 673 646 } 674 647 675 648 log_msg(8, "Parent running"); 676 open_evalcall_form(title); 649 open_evalcall_form(tt); 650 677 651 for (sleep(1); command[0] != '\0'; sleep(1)) { 678 652 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE); 679 if (pcno < =0 || pcno > 100) {680 log_msg(8, "Weird pc# ");653 if (pcno < 0 || pcno > 100) { 654 log_msg(8, "Weird pc# %d", pcno); 681 655 continue; 682 656 } … … 684 658 if (pcno <= 5 && last_pcno >= 40) { 685 659 close_evalcall_form(); 686 strcpy(title, "Verifying..."); 687 open_evalcall_form(title); 660 open_evalcall_form("Verifying..."); 688 661 } 689 662 if (counter++ >= 5) { … … 694 667 update_evalcall_form(percentage); 695 668 } 669 mr_free(command); 670 696 671 log_file_end_to_screen(MONDO_LOGFILE, ""); 697 672 close_evalcall_form(); … … 703 678 } 704 679 log_msg(3, "Parent res = %d", res); 705 paranoid_free(command);706 paranoid_free(title);707 680 return (res); 708 681 } 709 710 711 682 712 683 … … 721 692 // RESTORE 722 693 int res = -1; 723 char *command ;694 char *command = NULL; 724 695 725 696 if ( !find_home_of_exe("ntfsclone")) { 726 697 fatal_error("ntfsclone not found"); 727 698 } 728 malloc_string(command); 729 sprintf(command, "ntfsclone --rescue --force --restore-image --overwrite %s %s", output_device, input_fifo); 699 mr_asprintf(command, "ntfsclone --rescue --force --restore-image --overwrite %s %s", output_device, input_fifo); 730 700 res = run_program_and_log_output(command, 5); 731 paranoid_free(command);701 mr_free(command); 732 702 return (res); 733 703 }
Note:
See TracChangeset
for help on using the changeset viewer.