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