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