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

Last change on this file was 3878, checked in by Bruno Cornec, 7 weeks ago

Fix compiler errors

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