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