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