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