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