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