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