source: MondoRescue/branches/3.3/mondo/src/common/libmondo-fork.c@ 3863

Last change on this file since 3863 was 3863, checked in by Bruno Cornec, 3 months ago

eliminate the last sprintf from libmondo-fork.c

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