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

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

Fix syntax issues

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