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