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