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