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