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