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