source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-fork.c@ 2392

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