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