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