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