[1] | 1 | /* libmondo-fork.c
|
---|
[128] | 2 | $Id: libmondo-fork.c 1080 2007-01-28 18:04:41Z bruno $
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | #include "my-stuff.h"
|
---|
[1080] | 7 | #include "mr_mem.h"
|
---|
[1] | 8 | #include "mondostructures.h"
|
---|
| 9 | #include "libmondo-fork.h"
|
---|
| 10 | #include "libmondo-string-EXT.h"
|
---|
[541] | 11 | #include "libmondo-gui-EXT.h"
|
---|
[1] | 12 | #include "libmondo-files-EXT.h"
|
---|
| 13 | #include "libmondo-tools-EXT.h"
|
---|
[541] | 14 | #include "lib-common-externs.h"
|
---|
[1] | 15 |
|
---|
| 16 | /*@unused@*/
|
---|
[128] | 17 | //static char cvsid[] = "$Id: libmondo-fork.c 1080 2007-01-28 18:04:41Z bruno $";
|
---|
[1] | 18 |
|
---|
| 19 | extern char *g_tmpfs_mountpt;
|
---|
| 20 | extern t_bkptype g_backup_media_type;
|
---|
| 21 | extern bool g_text_mode;
|
---|
[128] | 22 | pid_t g_buffer_pid = 0;
|
---|
[1] | 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 | */
|
---|
[128] | 31 | char *call_program_and_get_last_line_of_output(char *call)
|
---|
[1] | 32 | {
|
---|
[128] | 33 | /*@ buffers ***************************************************** */
|
---|
| 34 | static char result[512];
|
---|
| 35 | char *tmp;
|
---|
[1] | 36 |
|
---|
[128] | 37 | /*@ pointers **************************************************** */
|
---|
| 38 | FILE *fin;
|
---|
[1] | 39 |
|
---|
[128] | 40 | /*@ initialize data ********************************************* */
|
---|
| 41 | malloc_string(tmp);
|
---|
| 42 | result[0] = '\0';
|
---|
| 43 | tmp[0] = '\0';
|
---|
[1] | 44 |
|
---|
[128] | 45 | /*@******************************************************************** */
|
---|
[1] | 46 |
|
---|
[128] | 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");
|
---|
[1] | 58 | }
|
---|
[128] | 59 | strip_spaces(result);
|
---|
| 60 | return (result);
|
---|
[1] | 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 |
|
---|
[541] | 68 | #define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
|
---|
[1] | 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
|
---|
[128] | 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)
|
---|
[1] | 91 | {
|
---|
| 92 |
|
---|
[128] | 93 | /*@ int's *** */
|
---|
| 94 | int retval = 0;
|
---|
[1] | 95 |
|
---|
| 96 |
|
---|
[128] | 97 | /*@ buffers *** */
|
---|
| 98 | char *midway_call, *ultimate_call, *tmp, *command, *incoming,
|
---|
| 99 | *old_stderr, *cd_number_str;
|
---|
| 100 | char *p;
|
---|
[1] | 101 |
|
---|
| 102 | /*@*********** End Variables ***************************************/
|
---|
| 103 |
|
---|
[128] | 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);
|
---|
[1080] | 109 | midway_call = mr_malloc(1200);
|
---|
| 110 | ultimate_call = mr_malloc(1200);
|
---|
| 111 | tmp = mr_malloc(1200);
|
---|
| 112 | command = mr_malloc(1200);
|
---|
[128] | 113 | malloc_string(incoming);
|
---|
| 114 | malloc_string(old_stderr);
|
---|
| 115 | malloc_string(cd_number_str);
|
---|
[1] | 116 |
|
---|
[128] | 117 | incoming[0] = '\0';
|
---|
| 118 | old_stderr[0] = '\0';
|
---|
[1] | 119 |
|
---|
[128] | 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);
|
---|
[1] | 129 |
|
---|
[128] | 130 | log_to_screen
|
---|
[541] | 131 | ("Please be patient. Do not be alarmed by on-screen inactivity.");
|
---|
[128] | 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);
|
---|
[1] | 147 | #ifndef _XWIN
|
---|
[128] | 148 | if (!g_text_mode) {
|
---|
| 149 | newtSuspend();
|
---|
| 150 | }
|
---|
[1] | 151 | #endif
|
---|
[128] | 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);
|
---|
[541] | 159 | popup_and_OK("Press ENTER to continue.");
|
---|
[128] | 160 | popup_and_OK
|
---|
[541] | 161 | ("mkisofs and/or cdrecord returned an error. CD was not created");
|
---|
[128] | 162 | }
|
---|
[1] | 163 | }
|
---|
[128] | 164 | /* if text mode then do the above & RETURN; if not text mode, do this... */
|
---|
| 165 | else {
|
---|
| 166 | log_msg(3, "command = '%s'", command);
|
---|
[1] | 167 | // yes_this_is_a_goto:
|
---|
[128] | 168 | retval =
|
---|
| 169 | run_external_binary_with_percentage_indicator_NEW
|
---|
| 170 | (what_i_am_doing, command);
|
---|
| 171 | }
|
---|
[1] | 172 |
|
---|
[1080] | 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);
|
---|
[1] | 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 | */
|
---|
[128] | 187 | return (retval);
|
---|
[1] | 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 | */
|
---|
[128] | 199 | int run_program_and_log_output(char *program, int debug_level)
|
---|
[1] | 200 | {
|
---|
[128] | 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];
|
---|
[1] | 206 |
|
---|
[128] | 207 | /*@ int ********************************************************* */
|
---|
| 208 | int res;
|
---|
| 209 | int i;
|
---|
| 210 | int len;
|
---|
| 211 | bool log_if_failure = FALSE;
|
---|
| 212 | bool log_if_success = FALSE;
|
---|
[1] | 213 |
|
---|
[128] | 214 | /*@ pointers *************************************************** */
|
---|
| 215 | FILE *fin;
|
---|
| 216 | char *p;
|
---|
[1] | 217 |
|
---|
[128] | 218 | /*@ end vars *************************************************** */
|
---|
[1] | 219 |
|
---|
[128] | 220 | assert(program != NULL);
|
---|
| 221 | if (!program[0]) {
|
---|
| 222 | log_msg(2, "Warning - asked to run zerolength program");
|
---|
| 223 | return (1);
|
---|
| 224 | }
|
---|
[1] | 225 | // if (debug_level == TRUE) { debug_level=5; }
|
---|
| 226 |
|
---|
[128] | 227 | // assert_string_is_neither_NULL_nor_zerolength(program);
|
---|
[1] | 228 |
|
---|
[128] | 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 */
|
---|
[1] | 242 |
|
---|
| 243 |
|
---|
[128] | 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"))
|
---|
[1] | 266 | {
|
---|
[128] | 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 | }
|
---|
[1] | 288 | }
|
---|
[128] | 289 | paranoid_fclose(fin);
|
---|
[1] | 290 | }
|
---|
[128] | 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 | }
|
---|
[1] | 301 | // else
|
---|
| 302 | // { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
|
---|
[128] | 303 | return (res);
|
---|
[1] | 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 | */
|
---|
[128] | 316 | int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
|
---|
[1] | 317 | {
|
---|
[128] | 318 | /*@ int ******************************************************** */
|
---|
| 319 | int retval = 0;
|
---|
| 320 | int res = 0;
|
---|
| 321 | int i;
|
---|
[1] | 322 |
|
---|
[128] | 323 | /*@ pointers **************************************************** */
|
---|
| 324 | FILE *fin;
|
---|
[1] | 325 |
|
---|
[128] | 326 | /*@ buffers **************************************************** */
|
---|
| 327 | char tmp[MAX_STR_LEN * 2];
|
---|
| 328 | char command[MAX_STR_LEN * 2];
|
---|
| 329 | char lockfile[MAX_STR_LEN];
|
---|
[1] | 330 |
|
---|
[128] | 331 | /*@ end vars *************************************************** */
|
---|
[1] | 332 |
|
---|
[128] | 333 | assert_string_is_neither_NULL_nor_zerolength(basic_call);
|
---|
[1] | 334 |
|
---|
[128] | 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");
|
---|
[541] | 345 | sprintf(tmp, "Failed utterly to call '%s'", command);
|
---|
[128] | 346 | log_to_screen(tmp);
|
---|
| 347 | return (1);
|
---|
| 348 | }
|
---|
| 349 | if (!does_file_exist(lockfile)) {
|
---|
[541] | 350 | log_to_screen("Waiting for external binary to start");
|
---|
[128] | 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 | }
|
---|
[1] | 355 | #ifdef _XWIN
|
---|
[128] | 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 | }
|
---|
[1] | 366 | #else
|
---|
[128] | 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 | }
|
---|
[1] | 372 | #endif
|
---|
[154] | 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 | }
|
---|
[128] | 385 | close_evalcall_form();
|
---|
| 386 | unlink(lockfile);
|
---|
| 387 | return (retval);
|
---|
[1] | 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 |
|
---|
[296] | 401 |
|
---|
[128] | 402 | int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
|
---|
[1] | 403 | {
|
---|
| 404 | // if dir=='w' then copy from orig to archived
|
---|
| 405 | // if dir=='r' then copy from archived to orig
|
---|
[128] | 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;
|
---|
[1] | 414 |
|
---|
[128] | 415 | log_msg(5, "Opening.");
|
---|
| 416 | malloc_string(tmp);
|
---|
| 417 | tmp[0] = '\0';
|
---|
| 418 | bufcap = 256L * 1024L;
|
---|
[1080] | 419 | buf = mr_malloc(bufcap);
|
---|
[1] | 420 |
|
---|
[128] | 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 | }
|
---|
[1] | 490 |
|
---|
| 491 | // log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
|
---|
| 492 |
|
---|
[128] | 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);
|
---|
[1] | 509 | // if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
|
---|
[128] | 510 | fwrite(tmp, 1, bytes_read_in, ftmp);
|
---|
[797] | 511 | sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
|
---|
[128] | 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 | }
|
---|
[1] | 521 |
|
---|
[1080] | 522 | mr_free(buf);
|
---|
| 523 | mr_free(tmp);
|
---|
[128] | 524 | log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
|
---|
| 525 | return (retval);
|
---|
[1] | 526 | }
|
---|
| 527 |
|
---|
| 528 |
|
---|
| 529 |
|
---|
| 530 |
|
---|
| 531 | /**
|
---|
[296] | 532 | * Feed @p input_device through ntfsclone to @p output_fname.
|
---|
[1] | 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 | */
|
---|
[296] | 537 | int feed_into_ntfsprog(char *input_device, char *output_fname)
|
---|
[1] | 538 | {
|
---|
| 539 | // BACKUP
|
---|
[296] | 540 | int res = -1;
|
---|
| 541 | char*command;
|
---|
[1] | 542 |
|
---|
[128] | 543 | if (!does_file_exist(input_device)) {
|
---|
| 544 | fatal_error("input device does not exist");
|
---|
| 545 | }
|
---|
[296] | 546 | if ( !find_home_of_exe("ntfsclone")) {
|
---|
| 547 | fatal_error("ntfsclone not found");
|
---|
[128] | 548 | }
|
---|
[296] | 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);
|
---|
[1080] | 552 | mr_free(command);
|
---|
[411] | 553 | unlink(output_fname);
|
---|
[128] | 554 | return (res);
|
---|
[1] | 555 | }
|
---|
| 556 |
|
---|
| 557 |
|
---|
| 558 |
|
---|
| 559 |
|
---|
| 560 |
|
---|
[128] | 561 | int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
|
---|
[1] | 562 | {
|
---|
| 563 |
|
---|
[128] | 564 | /*@ int *************************************************************** */
|
---|
| 565 | int res = 0;
|
---|
[1] | 566 | int percentage = 0;
|
---|
| 567 | int maxpc = 0;
|
---|
| 568 | int pcno = 0;
|
---|
| 569 | int last_pcno = 0;
|
---|
| 570 |
|
---|
[128] | 571 | /*@ buffers *********************************************************** */
|
---|
| 572 | char *command;
|
---|
| 573 | char *tempfile;
|
---|
| 574 | char *title;
|
---|
| 575 | /*@ pointers ********************************************************** */
|
---|
| 576 | FILE *pin;
|
---|
[1] | 577 |
|
---|
[128] | 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);
|
---|
[1] | 583 |
|
---|
[128] | 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;
|
---|
[1] | 597 | // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
|
---|
[128] | 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();
|
---|
[541] | 607 | strcpy(title, "Verifying...");
|
---|
[128] | 608 | open_evalcall_form(title);
|
---|
| 609 | }
|
---|
| 610 | last_pcno = pcno;
|
---|
| 611 | update_evalcall_form(percentage);
|
---|
[1] | 612 | }
|
---|
[128] | 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");
|
---|
[1] | 618 | }
|
---|
[128] | 619 | unlink(tempfile);
|
---|
[1080] | 620 | mr_free(command);
|
---|
| 621 | mr_free(tempfile);
|
---|
| 622 | mr_free(title);
|
---|
[128] | 623 | return (res);
|
---|
[1] | 624 | }
|
---|
| 625 |
|
---|
| 626 |
|
---|
| 627 |
|
---|
| 628 |
|
---|
[128] | 629 | void *run_prog_in_bkgd_then_exit(void *info)
|
---|
[1] | 630 | {
|
---|
[128] | 631 | char *sz_command;
|
---|
| 632 | static int res = 4444;
|
---|
[1] | 633 |
|
---|
[128] | 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));
|
---|
[1] | 644 | }
|
---|
| 645 |
|
---|
| 646 |
|
---|
| 647 |
|
---|
| 648 |
|
---|
[128] | 649 | int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
|
---|
[1] | 650 | {
|
---|
| 651 |
|
---|
[128] | 652 | /*@ int *************************************************************** */
|
---|
| 653 | int res = 0;
|
---|
[1] | 654 | int percentage = 0;
|
---|
| 655 | int maxpc = 100;
|
---|
| 656 | int pcno = 0;
|
---|
| 657 | int last_pcno = 0;
|
---|
[128] | 658 | int counter = 0;
|
---|
[1] | 659 |
|
---|
[128] | 660 | /*@ buffers *********************************************************** */
|
---|
| 661 | char *command;
|
---|
| 662 | char *title;
|
---|
| 663 | /*@ pointers ********************************************************** */
|
---|
| 664 | static int chldres = 0;
|
---|
| 665 | int *pchild_result;
|
---|
| 666 | pthread_t childthread;
|
---|
[1] | 667 |
|
---|
[128] | 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;
|
---|
[1] | 672 |
|
---|
[128] | 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 | }
|
---|
[1] | 683 |
|
---|
[128] | 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();
|
---|
[541] | 695 | strcpy(title, "Verifying...");
|
---|
[128] | 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);
|
---|
[1] | 704 | }
|
---|
[128] | 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;
|
---|
[1] | 712 | }
|
---|
[128] | 713 | log_msg(3, "Parent res = %d", res);
|
---|
[1080] | 714 | mr_free(command);
|
---|
| 715 | mr_free(title);
|
---|
[128] | 716 | return (res);
|
---|
[1] | 717 | }
|
---|
| 718 |
|
---|
| 719 |
|
---|
| 720 |
|
---|
| 721 |
|
---|
| 722 | /**
|
---|
[296] | 723 | * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
|
---|
| 724 | * @param input_fifo The ntfsclone file to read.
|
---|
[1] | 725 | * @param output_device Where to put the output.
|
---|
[296] | 726 | * @return The return value of ntfsclone (0 for success).
|
---|
[1] | 727 | */
|
---|
[296] | 728 | int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
|
---|
[1] | 729 | {
|
---|
| 730 | // RESTORE
|
---|
[296] | 731 | int res = -1;
|
---|
| 732 | char *command;
|
---|
[1] | 733 |
|
---|
[296] | 734 | if ( !find_home_of_exe("ntfsclone")) {
|
---|
| 735 | fatal_error("ntfsclone not found");
|
---|
[128] | 736 | }
|
---|
[296] | 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);
|
---|
[1080] | 740 | mr_free(command);
|
---|
[128] | 741 | return (res);
|
---|
[1] | 742 | }
|
---|