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