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