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

Last change on this file was 3878, checked in by Bruno Cornec, 2 months ago

Fix compiler errors

  • Property svn:keywords set to Id
File size: 19.7 KB
RevLine 
[1]1/* libmondo-fork.c
[128]2 $Id: libmondo-fork.c 3878 2024-03-08 11:15:10Z 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 3878 2024-03-08 11:15:10Z 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
[3878]89void *run_prog_in_bkgd_then_exit(void *info)
90{
91 char *sz_command;
92 static int res = 4444;
[3871]93
[3878]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
[3871]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
[1]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
[1645]186eval_call_to_make_ISO(char *basic_call, char *isofile,
[3849]187 int cd_no, char *what_i_am_doing)
[1]188{
189
[128]190 /*@ int's *** */
191 int retval = 0;
[1]192
193
[128]194 /*@ buffers *** */
[3191]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;
[2224]202 char *tmp1 = NULL;
[3191]203 char *tmp2 = NULL;
[1]204
205/*@*********** End Variables ***************************************/
206
[128]207 log_msg(3, "Starting");
208 assert(bkpinfo != NULL);
209 assert_string_is_neither_NULL_nor_zerolength(isofile);
[3191]210
[128]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 }
[1]220
[2380]221 if ((bkpinfo->netfs_user) && (strstr(bkpinfo->netfs_proto,"nfs"))) {
[3185]222 mr_asprintf(tmp1, "su - %s -c \"%s\"", bkpinfo->netfs_user, basic_call);
[2224]223 } else {
[3185]224 mr_asprintf(tmp1, "%s", basic_call);
[2224]225 }
226
[3191]227 mr_asprintf(cd_number_str, "%d", cd_no);
[2224]228 resolve_naff_tokens(midway_call, tmp1, isofile, "_ISO_");
[3191]229 log_msg(4, "basic call = '%s'", tmp1);
230 mr_free(tmp1);
231
[128]232 resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
[3191]233 log_msg(4, "midway_call = '%s'", midway_call);
234 mr_free(cd_number_str);
235
[128]236 resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
237 log_msg(4, "ultimate call = '%s'", ultimate_call);
[3191]238 mr_asprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
[1]239
[3560]240 /* Copy the current logfile - of course incomplete to the media */
[3567]241 mr_system("gzip -c9 %s > ./archives/%d.%s", MONDO_LOGFILE, cd_number_str, MONDO_LOGFILENAME);
[3560]242
[3191]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);
[128]245 if (bkpinfo->manual_cd_tray) {
[3191]246 mr_asprintf(tmp2, "%s", command);
247 p = strstr(tmp2, "2>>");
[128]248 if (p) {
[3863]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>> */
[3191]256 for (; (*p != ' ') && (*p != '\0'); p++) {
[128]257 *p = ' ';
258 }
259 }
[3191]260 mr_free(command);
261 command = tmp2;
[128]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) {
[3191]271 popup_and_OK("mkisofs and/or cdrecord returned an error. CD was not created");
[128]272 }
[1]273 }
[128]274 /* if text mode then do the above & RETURN; if not text mode, do this... */
275 else {
276 log_msg(3, "command = '%s'", command);
[3191]277 retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
[128]278 }
[3191]279 mr_free(command);
[128]280 paranoid_free(midway_call);
281 paranoid_free(ultimate_call);
282 paranoid_free(tmp);
283 return (retval);
[1]284}
285
286
[1688]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) {
[1]295
[1688]296 /*@ int's *** */
297 int retval = 0;
[1]298
[1688]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) {
[3191]315 retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
[1688]316 } else {
317 retval += system(command);
318 }
319 if (!g_text_mode) {
320 newtResume();
321 }
322
323 return (retval);
324}
325
326
327
328
[1]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 */
[128]335int run_program_and_log_output(char *program, int debug_level)
[1]336{
[128]337 /*@ buffer ****************************************************** */
[3034]338 char *callstr = NULL;
[3641]339 char *incoming = NULL;
[3034]340 char *tmp1 = NULL;
[3641]341 char *tmp2 = NULL;
[1]342
[128]343 /*@ int ********************************************************* */
344 int res;
345 bool log_if_failure = FALSE;
346 bool log_if_success = FALSE;
[1]347
[128]348 /*@ pointers *************************************************** */
349 FILE *fin;
350 char *p;
[1]351
[128]352 /*@ end vars *************************************************** */
[1]353
[128]354 assert(program != NULL);
355 if (!program[0]) {
356 log_msg(2, "Warning - asked to run zerolength program");
[3195]357 return(1);
[128]358 }
[1]359
[128]360 if (debug_level <= g_loglevel) {
361 log_if_success = TRUE;
362 log_if_failure = TRUE;
363 }
[3185]364 mr_asprintf(callstr, "%s > %s/mondo-run-prog-thing.tmp 2> %s/mondo-run-prog-thing.err", program, bkpinfo->tmpdir, bkpinfo->tmpdir);
[128]365 while ((p = strchr(callstr, '\r'))) {
366 *p = ' ';
367 }
368 while ((p = strchr(callstr, '\n'))) {
369 *p = ' ';
370 } /* single '=' is intentional */
[1]371
372
[128]373 res = system(callstr);
374 if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
375 log_msg(0, "running: %s", callstr);
[3034]376 log_msg(0, "--------------------------------start of output-----------------------------");
[128]377 }
[3034]378 mr_free(callstr);
379
[3185]380 mr_asprintf(callstr, "cat %s/mondo-run-prog-thing.err >> %s/mondo-run-prog-thing.tmp 2> /dev/null", bkpinfo->tmpdir, bkpinfo->tmpdir);
[1644]381 if (log_if_failure && system(callstr)) {
[128]382 log_OS_error("Command failed");
383 }
[3034]384 mr_free(callstr);
385
[3185]386 mr_asprintf(tmp1, "%s/mondo-run-prog-thing.err", bkpinfo->tmpdir);
[3034]387 unlink(tmp1);
388 mr_free(tmp1);
389
[3185]390 mr_asprintf(tmp1, "%s/mondo-run-prog-thing.tmp", bkpinfo->tmpdir);
[3034]391 fin = fopen(tmp1, "r");
[128]392 if (fin) {
[3641]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;
[3034]403 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
[128]404 log_msg(0, incoming);
405 }
[3641]406 mr_free(incoming);
[1]407 }
[3643]408 mr_free(incoming);
[128]409 paranoid_fclose(fin);
[1]410 }
[3034]411 unlink(tmp1);
412 mr_free(tmp1);
413
[128]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 }
[3195]423 return(res);
[1]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 */
[128]436int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
[1]437{
[128]438 /*@ int ******************************************************** */
439 int retval = 0;
440 int res = 0;
441 int i;
[1]442
[128]443 /*@ pointers **************************************************** */
444 FILE *fin;
[1]445
[128]446 /*@ buffers **************************************************** */
[2700]447 char *command = NULL;
[3191]448 char *lockfile = NULL;
[1]449
[128]450 /*@ end vars *************************************************** */
[1]451
[128]452 assert_string_is_neither_NULL_nor_zerolength(basic_call);
[1]453
[3191]454 mr_asprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
[1644]455
[3191]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);
[128]457 open_evalcall_form(what_i_am_doing);
[3191]458 log_msg(2, "Executing %s", basic_call);
[2700]459
[128]460 if (!(fin = popen(command, "r"))) {
461 log_OS_error("Unable to popen-in command");
[3191]462 log_to_screen("Failed utterly to call '%s'", command);
[2700]463 mr_free(command);
[3191]464 mr_free(lockfile);
[128]465 return (1);
466 }
[3191]467
[128]468 if (!does_file_exist(lockfile)) {
[2656]469 log_to_screen("Waiting for '%s' to start",command);
[128]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 }
[2700]474 mr_free(command);
[3191]475
[128]476 for (; does_file_exist(lockfile); sleep(1)) {
477 log_file_end_to_screen(MONDO_LOGFILE, "");
478 update_evalcall_form(1);
479 }
[3191]480
[154]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 }
[128]493 close_evalcall_form();
494 unlink(lockfile);
[3191]495 mr_free(lockfile);
496
[128]497 return (retval);
[1]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
[296]511
[128]512int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
[1]513{
514// if dir=='w' then copy from orig to archived
515// if dir=='r' then copy from archived to orig
[3191]516 char *tmp = NULL;
517 char *tmp1 = NULL;
518 char *buf = NULL;
519 char *filestr = NULL;
[128]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;
[2178]526 int tmpcap = 512;
[1]527
[128]528 log_msg(5, "Opening.");
[3191]529
[128]530 bufcap = 256L * 1024L;
531 if (!(buf = malloc(bufcap))) {
532 fatal_error("Failed to malloc() buf");
533 }
[1]534
[128]535 if (direction == 'w') {
536 fin = f_orig;
537 fout = f_archived;
[3191]538 mr_asprintf(tmp, "%-64s", PIMP_START_SZ);
[128]539 if (fwrite(tmp, 1, 64, fout) != 64) {
[3191]540 mr_free(tmp);
[128]541 fatal_error("Can't write the introductory block");
542 }
[3191]543 mr_free(tmp);
544
[128]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 }
[3191]550 mr_asprintf(tmp, "%-64ld", bytes_read_in);
[128]551 if (fwrite(tmp, 1, 64, fout) != 64) {
[3191]552 mr_free(tmp);
[128]553 fatal_error("Cannot write introductory block");
554 }
[3191]555 mr_free(tmp);
556
[128]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);
[3191]561 mr_asprintf(tmp, "%-64ld", subsliceno);
[128]562 if (fwrite(tmp, 1, 64, fout) != 64) {
[3191]563 mr_free(tmp);
[128]564 fatal_error("Cannot write post-thingy block");
565 }
[3191]566 mr_free(tmp);
[128]567 log_msg(7, "Subslice #%d written OK", subsliceno);
568 subsliceno++;
569 }
[3191]570 mr_asprintf(tmp, "%-64ld", 0L);
[128]571 if (fwrite(tmp, 1, 64L, fout) != 64L) {
[3191]572 mr_free(tmp);
[128]573 fatal_error("Cannot write final introductory block");
574 }
[3191]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);
[128]583 } else {
584 fin = f_archived;
585 fout = f_orig;
[3191]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);
[128]591 fatal_error("Cannot read the introductory block");
592 }
[3191]593 log_msg(5, "tmp1 is %s", tmp1);
594 if (!strstr(tmp1, PIMP_START_SZ)) {
595 mr_free(tmp1);
[128]596 fatal_error("Can't find intro blk");
597 }
[3191]598 if (fread(tmp1, 1, 64L, fin) != 64L) {
599 mr_free(tmp1);
[128]600 fatal_error("Cannot read introductory blk");
601 }
[3191]602 bytes_to_be_read = atol(tmp1);
[128]603 while (bytes_to_be_read > 0) {
[3191]604 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, bytes_to_be_read);
[128]605 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
606 if (bytes_read_in != bytes_to_be_read) {
[3191]607 mr_free(tmp1);
608 fatal_error("Danger, WIll Robinson. Failed to read whole subvol from archives.");
[128]609 }
610 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
[3191]611 if (fread(tmp1, 1, 64, fin) != 64) {
612 mr_free(tmp1);
[128]613 fatal_error("Cannot read post-thingy block");
614 }
[3191]615 if (atol(tmp1) != subsliceno) {
616 log_msg(1, "Wanted subslice %ld but got %ld ('%s')", subsliceno, atol(tmp1), tmp1);
[128]617 }
618 log_msg(7, "Subslice #%ld read OK", subsliceno);
619 subsliceno++;
[3191]620 if (fread(tmp1, 1, 64, fin) != 64) {
621 mr_free(tmp1);
[128]622 fatal_error("Cannot read introductory block");
623 }
[3191]624 bytes_to_be_read = atol(tmp1);
[128]625 }
626 }
[1]627
[128]628 if (direction == 'w') {
[3191]629 mr_asprintf(tmp, "%-64s", PIMP_END_SZ);
[128]630 if (fwrite(tmp, 1, 64, fout) != 64) {
[3191]631 mr_free(tmp);
[128]632 fatal_error("Can't write the final block");
633 }
[3191]634 mr_free(tmp);
[128]635 } else {
[3191]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);
[128]640 fatal_error("Can't read the final block");
641 }
[3191]642 log_msg(5, "tmpB is %s", tmp1);
643 if (!strstr(tmp1, PIMP_END_SZ)) {
644 mr_asprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
[1644]645 ftmp = fopen(filestr, "w");
[3191]646 mr_free(filestr);
647
648 bytes_read_in = fread(tmp1, 1, 64, fin);
[128]649 log_msg(1, "bytes_read_in = %ld", bytes_read_in);
[3191]650
[3060]651 if (fwrite(tmp, 1, bytes_read_in, ftmp)) {
652 fatal_error("Can't fwrite here");
653 }
[3191]654
655 mr_asprintf(tmp, "I am here - %lld", (long long)ftello(fin));
[3060]656 if (fread(tmp, 1, tmpcap, fin)) {
[3191]657 mr_free(tmp);
[3060]658 fatal_error("Can't fread here");
659 }
[128]660 log_msg(0, "tmp = '%s'", tmp);
[3060]661 if (fwrite(tmp, 1, tmpcap, ftmp)) {
[3191]662 mr_free(tmp);
[3060]663 fatal_error("Can't fwrite there");
664 }
[128]665 fclose(ftmp);
[3191]666 mr_free(tmp);
[128]667 fatal_error("Missing terminating block");
668 }
669 }
670 }
[3191]671 mr_free(tmp1);
[1]672
[128]673 paranoid_free(buf);
674 log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
675 return (retval);
[1]676}
677
678
679
680
681/**
[296]682 * Feed @p input_device through ntfsclone to @p output_fname.
[1]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 */
[296]687int feed_into_ntfsprog(char *input_device, char *output_fname)
[1]688{
689// BACKUP
[296]690 int res = -1;
[3191]691 char *command = NULL;
[3866]692 char *tmp = NULL;
[1]693
[128]694 if (!does_file_exist(input_device)) {
695 fatal_error("input device does not exist");
696 }
[3866]697 if ((tmp = find_home_of_exe("ntfsclone")) == NULL) {
[296]698 fatal_error("ntfsclone not found");
[128]699 }
[3866]700 mr_asprintf(command, "%s --rescue --force --save-image --overwrite %s %s", tmp, output_fname, input_device);
701 mr_free(tmp);
[296]702 res = run_program_and_log_output(command, 5);
[3191]703 mr_free(command);
704
[411]705 unlink(output_fname);
[128]706 return (res);
[1]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.