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

Last change on this file since 2316 was 2316, checked in by Bruno Cornec, 15 years ago

r3327@localhost: bruno | 2009-08-04 00:17:46 +0200
popup_and_get_string and build_partition_name now return an allocated string

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