source: MondoRescue/branches/stable/mondo/src/common/libmondo-fork.c@ 1168

Last change on this file since 1168 was 1168, checked in by Bruno Cornec, 17 years ago

strip_spaces => mr_strip_spaces in mr_str.c and corrected at the same time :-)

  • Property svn:keywords set to Id
File size: 19.0 KB
RevLine 
[1113]1/* libmondo-fork.c - subroutines for handling forking/pthreads/etc.
2 * $Id: libmondo-fork.c 1168 2007-02-14 23:27:39Z bruno $
3 */
[1]4#include "my-stuff.h"
[1080]5#include "mr_mem.h"
[1119]6#include "mr_msg.h"
[1168]7#include "mr_str.h"
8
[1]9#include "mondostructures.h"
10#include "libmondo-fork.h"
11#include "libmondo-string-EXT.h"
[1113]12#include "newt-specific-EXT.h"
[1]13#include "libmondo-files-EXT.h"
14#include "libmondo-tools-EXT.h"
15
16/*@unused@*/
[128]17//static char cvsid[] = "$Id: libmondo-fork.c 1168 2007-02-14 23:27:39Z bruno $";
[1]18
19extern char *g_tmpfs_mountpt;
20extern t_bkptype g_backup_media_type;
21extern bool g_text_mode;
[128]22pid_t g_buffer_pid = 0;
[1]23
24
25/**
26 * Call a program and retrieve its last line of output.
27 * @param call The program to run.
28 * @return The last line of its output.
29 * @note The returned value points to static storage that will be overwritten with each call.
30 */
[128]31char *call_program_and_get_last_line_of_output(char *call)
[1]32{
[128]33 /*@ buffers ***************************************************** */
34 static char result[512];
35 char *tmp;
[1]36
[128]37 /*@ pointers **************************************************** */
38 FILE *fin;
[1]39
[128]40 /*@ initialize data ********************************************* */
41 malloc_string(tmp);
42 result[0] = '\0';
43 tmp[0] = '\0';
[1]44
[128]45 /*@******************************************************************** */
[1]46
[128]47 assert_string_is_neither_NULL_nor_zerolength(call);
48 if ((fin = popen(call, "r"))) {
49 for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
50 fgets(tmp, MAX_STR_LEN, fin)) {
51 if (strlen(tmp) > 1) {
52 strcpy(result, tmp);
53 }
54 }
55 paranoid_pclose(fin);
56 } else {
57 log_OS_error("Unable to popen call");
[1]58 }
[1168]59 mr_strip_spaces(result);
[1151]60 mr_free(tmp);
[128]61 return (result);
[1]62}
63
64
65
66
67
68
[541]69#define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
[1]70
71/**
72 * Call mkisofs to create an ISO image.
73 * @param bkpinfo The backup information structure. Fields used:
74 * - @c bkpinfo->manual_cd_tray
75 * - @c bkpinfo->backup_media_type
76 * - @c bkpinfo->please_dont_eject_when_restoring
77 * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
78 * - @c _ISO_ will become the ISO file (@p isofile)
79 * - @c _CD#_ becomes the CD number (@p cd_no)
80 * - @c _ERR_ becomes the logfile (@p g_logfile)
81 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
82 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
83 * @param logstub Unused.
84 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
85 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
86 * @bug @p logstub is unused.
87 */
88int
[128]89eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo,
90 char *basic_call, char *isofile,
91 int cd_no, char *logstub, char *what_i_am_doing)
[1]92{
93
[128]94 /*@ int's *** */
95 int retval = 0;
[1]96
97
[128]98 /*@ buffers *** */
[1113]99 char *midway_call, *ultimate_call, *tmp;
100 char *p = NULL;
101 char *cd_number_str = NULL;
102 char *command = NULL;
103
[1]104
105/*@*********** End Variables ***************************************/
106
[1107]107 mr_msg(3, "Starting");
[128]108 assert(bkpinfo != NULL);
109 assert_string_is_neither_NULL_nor_zerolength(basic_call);
110 assert_string_is_neither_NULL_nor_zerolength(isofile);
111 assert_string_is_neither_NULL_nor_zerolength(logstub);
[1113]112
[1080]113 midway_call = mr_malloc(1200);
114 ultimate_call = mr_malloc(1200);
115 tmp = mr_malloc(1200);
[1]116
[1113]117 mr_asprintf(&cd_number_str, "%d", cd_no);
[128]118 resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
119 resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
[1113]120 mr_free(cd_number_str);
121
[128]122 resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
[1107]123 mr_msg(4, "basic call = '%s'", basic_call);
124 mr_msg(4, "midway_call = '%s'", midway_call);
125 mr_msg(4, "tmp = '%s'", tmp);
126 mr_msg(4, "ultimate call = '%s'", ultimate_call);
[1113]127 mr_asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
[1]128
[128]129 log_to_screen
[1113]130 (_("Please be patient. Do not be alarmed by on-screen inactivity."));
[1107]131 mr_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
[128]132 what_i_am_doing);
133 if (bkpinfo->manual_cd_tray) {
[1113]134 /* Find everything after a 2>> and remove it */
135 p = strstr(command, "2>>");
[128]136 if (p) {
[1113]137 for (; *p != ' ' && *p != '\0'; p++) {
[128]138 *p = ' ';
139 }
140 }
[1]141#ifndef _XWIN
[128]142 if (!g_text_mode) {
143 newtSuspend();
144 }
[1]145#endif
[1107]146 mr_msg(1, "command = '%s'", command);
[128]147 retval += system(command);
148 if (!g_text_mode) {
149 newtResume();
150 }
151 if (retval) {
[1107]152 mr_msg(2, "Basic call '%s' returned an error.", basic_call);
[1113]153 popup_and_OK(_("Press ENTER to continue."));
[128]154 popup_and_OK
[1113]155 (_("mkisofs and/or cdrecord returned an error. CD was not created"));
[128]156 }
[1]157 }
[128]158 /* if text mode then do the above & RETURN; if not text mode, do this... */
159 else {
[1107]160 mr_msg(3, "command = '%s'", command);
[1]161// yes_this_is_a_goto:
[128]162 retval =
163 run_external_binary_with_percentage_indicator_NEW
164 (what_i_am_doing, command);
165 }
[1113]166 mr_free(command);
[1]167
[1080]168 mr_free(midway_call);
169 mr_free(ultimate_call);
170 mr_free(tmp);
[128]171 return (retval);
[1]172}
173
174
175/**
176 * Run a program and log its output (stdout and stderr) to the logfile.
177 * @param program The program to run. Passed to the shell, so you can use pipes etc.
178 * @param debug_level If @p g_loglevel is higher than this, do not log the output.
179 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
180 */
[128]181int run_program_and_log_output(char *program, int debug_level)
[1]182{
[128]183 /*@ buffer ****************************************************** */
[1113]184 char *callstr = NULL;
185 char *incoming = NULL;
[1115]186 char *tmp = NULL;
[1]187
[128]188 /*@ int ********************************************************* */
[1113]189 int res = 0;
190 size_t n = 0;
[128]191 int i;
192 int len;
193 bool log_if_failure = FALSE;
194 bool log_if_success = FALSE;
[1]195
[128]196 /*@ pointers *************************************************** */
[1138]197 FILE *fin = NULL;
198 char *p = NULL;
[1]199
[128]200 /*@ end vars *************************************************** */
[1]201
[128]202 assert(program != NULL);
203 if (!program[0]) {
[1107]204 mr_msg(2, "Warning - asked to run zerolength program");
[128]205 return (1);
206 }
[1]207
[128]208 if (debug_level <= g_loglevel) {
209 log_if_success = TRUE;
210 log_if_failure = TRUE;
211 }
[1113]212 mr_asprintf(&callstr,
[128]213 "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
214 program);
215 while ((p = strchr(callstr, '\r'))) {
216 *p = ' ';
217 }
218 while ((p = strchr(callstr, '\n'))) {
219 *p = ' ';
220 } /* single '=' is intentional */
[1]221
[128]222 res = system(callstr);
223 if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
[1107]224 mr_msg(0, "running: %s", callstr);
225 mr_msg(0,
[128]226 "--------------------------------start of output-----------------------------");
227 }
[1113]228 mr_free(callstr);
229
[128]230 if (log_if_failure
231 &&
232 system
233 ("cat /tmp/mondo-run-prog-thing.err >> /tmp/mondo-run-prog-thing.tmp 2> /dev/null"))
[1]234 {
[128]235 log_OS_error("Command failed");
236 }
237 unlink("/tmp/mondo-run-prog-thing.err");
238 fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
239 if (fin) {
[1140]240 for (mr_getline(&incoming, &n, fin); !feof(fin); mr_getline(&incoming, &n, fin)) {
[128]241 /* patch by Heiko Schlittermann */
242 p = incoming;
243 while (p && *p) {
244 if ((p = strchr(p, '%'))) {
245 memmove(p, p + 1, strlen(p) + 1);
246 p += 2;
247 }
248 }
249 /* end of patch */
[1168]250 mr_strip_spaces(incoming);
[128]251 if ((res == 0 && log_if_success)
252 || (res != 0 && log_if_failure)) {
[1107]253 mr_msg(0, incoming);
[128]254 }
[1]255 }
[1113]256 mr_free(incoming);
[128]257 paranoid_fclose(fin);
[1]258 }
[128]259 unlink("/tmp/mondo-run-prog-thing.tmp");
260 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
[1107]261 mr_msg(0,
[128]262 "--------------------------------end of output------------------------------");
263 if (res) {
[1107]264 mr_msg(0, "...ran with res=%d", res);
[128]265 } else {
[1107]266 mr_msg(0, "...ran just fine. :-)");
[128]267 }
268 }
269 return (res);
[1]270}
271
272
273/**
274 * Run a program and log its output to the screen.
275 * @param basic_call The program to run.
276 * @param what_i_am_doing The title of the evalcall form.
277 * @return The return value of the command (varies, but 0 almost always means success).
278 * @see run_program_and_log_output
279 * @see log_to_screen
280 */
[128]281int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
[1]282{
[128]283 /*@ int ******************************************************** */
284 int retval = 0;
285 int res = 0;
286 int i;
[1]287
[128]288 /*@ pointers **************************************************** */
289 FILE *fin;
[1]290
[128]291 /*@ buffers **************************************************** */
[1113]292 char *tmp = NULL;
293 char *command = NULL;
294 char *lockfile = NULL;
[1]295
[128]296 /*@ end vars *************************************************** */
[1]297
[128]298 assert_string_is_neither_NULL_nor_zerolength(basic_call);
[1]299
[1113]300 mr_asprintf(&lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
[128]301 mkstemp(lockfile);
[1113]302 mr_asprintf(&command,
[128]303 "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
304 lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
305 open_evalcall_form(what_i_am_doing);
[1113]306 mr_asprintf(&tmp, "Executing %s", basic_call);
[1107]307 mr_msg(2, tmp);
[1113]308 mr_free(tmp);
309
[128]310 if (!(fin = popen(command, "r"))) {
311 log_OS_error("Unable to popen-in command");
[1113]312 mr_asprintf(&tmp, _("Failed utterly to call '%s'"), command);
[128]313 log_to_screen(tmp);
[1113]314 mr_free(tmp);
315 mr_free(lockfile);
316 mr_free(command);
[128]317 return (1);
318 }
[1113]319 mr_free(command);
320
[128]321 if (!does_file_exist(lockfile)) {
[1113]322 log_to_screen(_("Waiting for external binary to start"));
[128]323 for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
[1107]324 mr_msg(3, "Waiting for lockfile %s to exist", lockfile);
[128]325 }
326 }
[1]327#ifdef _XWIN
[128]328 /* This only can update when newline goes into the file,
329 but it's *much* prettier/faster on Qt. */
330 while (does_file_exist(lockfile)) {
331 while (!feof(fin)) {
332 if (!fgets(tmp, 512, fin))
333 break;
334 log_to_screen(tmp);
335 }
336 usleep(500000);
337 }
[1]338#else
[128]339 /* This works on Newt, and it gives quicker updates. */
340 for (; does_file_exist(lockfile); sleep(1)) {
341 log_file_end_to_screen(MONDO_LOGFILE, "");
342 update_evalcall_form(1);
343 }
[1]344#endif
[154]345 /* Evaluate the status returned by pclose to get the exit code of the called program. */
346 errno = 0;
347 res = pclose(fin);
348 /* Log actual pclose errors. */
[1113]349 if (errno) {
350 mr_msg(5, "pclose err: %d", errno);
351 }
[154]352 /* Check if we have a valid status. If we do, extract the called program's exit code. */
353 /* If we don't, highlight this fact by returning -1. */
354 if (WIFEXITED(res)) {
355 retval = WEXITSTATUS(res);
356 } else {
357 retval = -1;
358 }
[128]359 close_evalcall_form();
360 unlink(lockfile);
[1113]361 mr_free(lockfile);
362
[128]363 return (retval);
[1]364}
365
366
367/**
[1113]368 * Apparently used. @bug This has a purpose, but what?
[1]369 */
370#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
371#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
372
373
374
[296]375
[128]376int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
[1]377{
378// if dir=='w' then copy from orig to archived
379// if dir=='r' then copy from archived to orig
[1113]380 char *tmp = NULL;
381 char *buf = NULL;
[128]382 long int bytes_to_be_read, bytes_read_in, bytes_written_out =
383 0, bufcap, subsliceno = 0;
384 int retval = 0;
[1113]385 FILE *fin = NULL;
386 FILE *fout = NULL;
387 FILE *ftmp = NULL;
[1]388
[1107]389 mr_msg(5, "Opening.");
[128]390 malloc_string(tmp);
391 tmp[0] = '\0';
392 bufcap = 256L * 1024L;
[1080]393 buf = mr_malloc(bufcap);
[1]394
[128]395 if (direction == 'w') {
396 fin = f_orig;
397 fout = f_archived;
398 sprintf(tmp, "%-64s", PIMP_START_SZ);
399 if (fwrite(tmp, 1, 64, fout) != 64) {
400 fatal_error("Can't write the introductory block");
401 }
402 while (1) {
403 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
404 if (bytes_read_in == 0) {
405 break;
406 }
407 sprintf(tmp, "%-64ld", bytes_read_in);
408 if (fwrite(tmp, 1, 64, fout) != 64) {
409 fatal_error("Cannot write introductory block");
410 }
[1107]411 mr_msg(7,
[128]412 "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
413 subsliceno, bytes_read_in, bytes_to_be_read);
414 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
415 sprintf(tmp, "%-64ld", subsliceno);
416 if (fwrite(tmp, 1, 64, fout) != 64) {
417 fatal_error("Cannot write post-thingy block");
418 }
[1107]419 mr_msg(7, "Subslice #%d written OK", subsliceno);
[128]420 subsliceno++;
421 }
422 sprintf(tmp, "%-64ld", 0L);
423 if (fwrite(tmp, 1, 64L, fout) != 64L) {
424 fatal_error("Cannot write final introductory block");
425 }
426 } else {
427 fin = f_archived;
428 fout = f_orig;
429 if (fread(tmp, 1, 64L, fin) != 64L) {
430 fatal_error("Cannot read the introductory block");
431 }
[1107]432 mr_msg(5, "tmp is %s", tmp);
[128]433 if (!strstr(tmp, PIMP_START_SZ)) {
434 fatal_error("Can't find intro blk");
435 }
436 if (fread(tmp, 1, 64L, fin) != 64L) {
437 fatal_error("Cannot read introductory blk");
438 }
439 bytes_to_be_read = atol(tmp);
440 while (bytes_to_be_read > 0) {
[1107]441 mr_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
[128]442 bytes_to_be_read);
443 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
444 if (bytes_read_in != bytes_to_be_read) {
445 fatal_error
446 ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
447 }
448 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
449 if (fread(tmp, 1, 64, fin) != 64) {
450 fatal_error("Cannot read post-thingy block");
451 }
452 if (atol(tmp) != subsliceno) {
[1107]453 mr_msg(1, "Wanted subslice %ld but got %ld ('%s')",
[128]454 subsliceno, atol(tmp), tmp);
455 }
[1107]456 mr_msg(7, "Subslice #%ld read OK", subsliceno);
[128]457 subsliceno++;
458 if (fread(tmp, 1, 64, fin) != 64) {
459 fatal_error("Cannot read introductory block");
460 }
461 bytes_to_be_read = atol(tmp);
462 }
463 }
[1]464
[1107]465// mr_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
[1]466
[128]467 if (direction == 'w') {
468 sprintf(tmp, "%-64s", PIMP_END_SZ);
469 if (fwrite(tmp, 1, 64, fout) != 64) {
470 fatal_error("Can't write the final block");
471 }
472 } else {
[1107]473 mr_msg(1, "tmpA is %s", tmp);
[128]474 if (!strstr(tmp, PIMP_END_SZ)) {
475 if (fread(tmp, 1, 64, fin) != 64) {
476 fatal_error("Can't read the final block");
477 }
[1107]478 mr_msg(5, "tmpB is %s", tmp);
[128]479 if (!strstr(tmp, PIMP_END_SZ)) {
480 ftmp = fopen("/tmp/out.leftover", "w");
481 bytes_read_in = fread(tmp, 1, 64, fin);
[1107]482 mr_msg(1, "bytes_read_in = %ld", bytes_read_in);
[1]483// if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
[128]484 fwrite(tmp, 1, bytes_read_in, ftmp);
[797]485 sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
[1107]486// mr_msg(0, tmp);
[128]487 fread(tmp, 1, 512, fin);
[1107]488 mr_msg(0, "tmp = '%s'", tmp);
[128]489 fwrite(tmp, 1, 512, ftmp);
490 fclose(ftmp);
491 fatal_error("Missing terminating block");
492 }
493 }
494 }
[1]495
[1080]496 mr_free(buf);
497 mr_free(tmp);
[1107]498 mr_msg(3, "Successfully copied %ld bytes", bytes_written_out);
[128]499 return (retval);
[1]500}
501
502/**
[296]503 * Feed @p input_device through ntfsclone to @p output_fname.
[1]504 * @param input_device The device to image.
505 * @param output_fname The file to write.
506 * @return 0 for success, nonzero for failure.
507 */
[296]508int feed_into_ntfsprog(char *input_device, char *output_fname)
[1]509{
510// BACKUP
[296]511 int res = -1;
[1113]512 char *command = NULL;
[1]513
[128]514 if (!does_file_exist(input_device)) {
515 fatal_error("input device does not exist");
516 }
[296]517 if ( !find_home_of_exe("ntfsclone")) {
518 fatal_error("ntfsclone not found");
[128]519 }
[1113]520 mr_asprintf(&command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
[296]521 res = run_program_and_log_output(command, 5);
[1080]522 mr_free(command);
[1113]523
[411]524 unlink(output_fname);
[128]525 return (res);
[1]526}
527
528
[128]529int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
[1]530{
531
[128]532 int res = 0;
[1]533 int percentage = 0;
534 int maxpc = 0;
535 int pcno = 0;
536 int last_pcno = 0;
537
[1113]538 char *command = NULL;
[128]539 char *tempfile;
540 char *title;
541 /*@ pointers ********************************************************** */
542 FILE *pin;
[1]543
[128]544 malloc_string(title);
545 malloc_string(tempfile);
546 assert_string_is_neither_NULL_nor_zerolength(cmd);
547 assert_string_is_neither_NULL_nor_zerolength(title);
[1]548
[128]549 strcpy(title, tt);
550 strcpy(tempfile,
551 call_program_and_get_last_line_of_output
552 ("mktemp -q /tmp/mondo.XXXXXXXX"));
[1113]553 mr_asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
[128]554 tempfile);
[1107]555 mr_msg(3, command);
[128]556 open_evalcall_form(title);
557 if (!(pin = popen(command, "r"))) {
558 log_OS_error("fmt err");
[1113]559 mr_free(command);
[128]560 return (1);
561 }
[1113]562 mr_free(command);
563
[128]564 maxpc = 100;
[1]565// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
[128]566 for (sleep(1); does_file_exist(tempfile); sleep(1)) {
567 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
568 if (pcno < 0 || pcno > 100) {
[1107]569 mr_msg(5, "Weird pc#");
[128]570 continue;
571 }
572 percentage = pcno * 100 / maxpc;
573 if (pcno <= 5 && last_pcno > 40) {
574 close_evalcall_form();
[541]575 strcpy(title, "Verifying...");
[128]576 open_evalcall_form(title);
577 }
578 last_pcno = pcno;
579 update_evalcall_form(percentage);
[1]580 }
[128]581// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
582 close_evalcall_form();
583 if (pclose(pin)) {
584 res++;
585 log_OS_error("Unable to pclose");
[1]586 }
[128]587 unlink(tempfile);
[1080]588 mr_free(tempfile);
589 mr_free(title);
[128]590 return (res);
[1]591}
592
593
[128]594void *run_prog_in_bkgd_then_exit(void *info)
[1]595{
[128]596 char *sz_command;
597 static int res = 4444;
[1]598
[128]599 res = 999;
600 sz_command = (char *) info;
[1107]601 mr_msg(4, "sz_command = '%s'", sz_command);
[128]602 res = system(sz_command);
603 if (res > 256 && res != 4444) {
604 res = res / 256;
605 }
[1107]606 mr_msg(4, "child res = %d", res);
[128]607 sz_command[0] = '\0';
608 pthread_exit((void *) (&res));
[1]609}
610
611
[128]612int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
[1]613{
614
[128]615 /*@ int *************************************************************** */
616 int res = 0;
[1]617 int percentage = 0;
618 int maxpc = 100;
619 int pcno = 0;
620 int last_pcno = 0;
[128]621 int counter = 0;
[1]622
[128]623 /*@ buffers *********************************************************** */
[1113]624 char *command = NULL;
[128]625 char *title;
626 /*@ pointers ********************************************************** */
627 static int chldres = 0;
628 int *pchild_result;
629 pthread_t childthread;
[1]630
[128]631 pchild_result = &chldres;
632 assert_string_is_neither_NULL_nor_zerolength(cmd);
633 assert_string_is_neither_NULL_nor_zerolength(tt);
634 *pchild_result = 999;
[1]635
[128]636 malloc_string(title);
637 strcpy(title, tt);
[1113]638 mr_asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
[1107]639 mr_msg(3, "command = '%s'", command);
[128]640 if ((res =
641 pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
642 (void *) command))) {
643 fatal_error("Unable to create an archival thread");
644 }
[1]645
[1107]646 mr_msg(8, "Parent running");
[128]647 open_evalcall_form(title);
648 for (sleep(1); command[0] != '\0'; sleep(1)) {
649 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
650 if (pcno <= 0 || pcno > 100) {
[1107]651 mr_msg(8, "Weird pc#");
[128]652 continue;
653 }
654 percentage = pcno * 100 / maxpc;
655 if (pcno <= 5 && last_pcno >= 40) {
656 close_evalcall_form();
[541]657 strcpy(title, "Verifying...");
[128]658 open_evalcall_form(title);
659 }
660 if (counter++ >= 5) {
661 counter = 0;
662 log_file_end_to_screen(MONDO_LOGFILE, "");
663 }
664 last_pcno = pcno;
665 update_evalcall_form(percentage);
[1]666 }
[1113]667 mr_free(command);
668
[128]669 log_file_end_to_screen(MONDO_LOGFILE, "");
670 close_evalcall_form();
671 pthread_join(childthread, (void *) (&pchild_result));
672 if (pchild_result) {
673 res = *pchild_result;
674 } else {
675 res = 666;
[1]676 }
[1107]677 mr_msg(3, "Parent res = %d", res);
[1080]678 mr_free(title);
[128]679 return (res);
[1]680}
681
682
683/**
[296]684 * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
685 * @param input_fifo The ntfsclone file to read.
[1]686 * @param output_device Where to put the output.
[296]687 * @return The return value of ntfsclone (0 for success).
[1]688 */
[296]689int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
[1]690{
691// RESTORE
[296]692 int res = -1;
[1113]693 char *command = NULL;
[1]694
[296]695 if ( !find_home_of_exe("ntfsclone")) {
696 fatal_error("ntfsclone not found");
[128]697 }
[1113]698 mr_asprintf(&command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
[296]699 res = run_program_and_log_output(command, 5);
[1080]700 mr_free(command);
[128]701 return (res);
[1]702}
Note: See TracBrowser for help on using the repository browser.