[1113] | 1 | /* libmondo-fork.c - subroutines for handling forking/pthreads/etc.
|
---|
| 2 | * $Id: libmondo-fork.c 1173 2007-02-16 11:25:24Z bruno $
|
---|
| 3 | */
|
---|
[1] | 4 | #include "my-stuff.h"
|
---|
[1080] | 5 | #include "mr_mem.h"
|
---|
[1119] | 6 | #include "mr_msg.h"
|
---|
[1168] | 7 | #include "mr_str.h"
|
---|
| 8 |
|
---|
[1] | 9 | #include "mondostructures.h"
|
---|
| 10 | #include "libmondo-fork.h"
|
---|
| 11 | #include "libmondo-string-EXT.h"
|
---|
[1113] | 12 | #include "newt-specific-EXT.h"
|
---|
[1] | 13 | #include "libmondo-files-EXT.h"
|
---|
| 14 | #include "libmondo-tools-EXT.h"
|
---|
| 15 |
|
---|
| 16 | /*@unused@*/
|
---|
[128] | 17 | //static char cvsid[] = "$Id: libmondo-fork.c 1173 2007-02-16 11:25:24Z 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);
|
---|
[1173] | 48 |
|
---|
[128] | 49 | if ((fin = popen(call, "r"))) {
|
---|
| 50 | for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
|
---|
| 51 | fgets(tmp, MAX_STR_LEN, fin)) {
|
---|
| 52 | if (strlen(tmp) > 1) {
|
---|
| 53 | strcpy(result, tmp);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | paranoid_pclose(fin);
|
---|
| 57 | } else {
|
---|
| 58 | log_OS_error("Unable to popen call");
|
---|
[1] | 59 | }
|
---|
[1168] | 60 | mr_strip_spaces(result);
|
---|
[1151] | 61 | mr_free(tmp);
|
---|
[128] | 62 | return (result);
|
---|
[1] | 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 |
|
---|
[541] | 70 | #define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
|
---|
[1] | 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Call mkisofs to create an ISO image.
|
---|
| 74 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 75 | * - @c bkpinfo->manual_cd_tray
|
---|
| 76 | * - @c bkpinfo->backup_media_type
|
---|
| 77 | * - @c bkpinfo->please_dont_eject_when_restoring
|
---|
| 78 | * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
|
---|
| 79 | * - @c _ISO_ will become the ISO file (@p isofile)
|
---|
| 80 | * - @c _CD#_ becomes the CD number (@p cd_no)
|
---|
| 81 | * - @c _ERR_ becomes the logfile (@p g_logfile)
|
---|
| 82 | * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
|
---|
| 83 | * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
|
---|
| 84 | * @param logstub Unused.
|
---|
| 85 | * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
|
---|
| 86 | * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
|
---|
| 87 | * @bug @p logstub is unused.
|
---|
| 88 | */
|
---|
| 89 | int
|
---|
[128] | 90 | eval_call_to_make_ISO(struct s_bkpinfo *bkpinfo,
|
---|
| 91 | char *basic_call, char *isofile,
|
---|
| 92 | int cd_no, char *logstub, char *what_i_am_doing)
|
---|
[1] | 93 | {
|
---|
| 94 |
|
---|
[128] | 95 | /*@ int's *** */
|
---|
| 96 | int retval = 0;
|
---|
[1] | 97 |
|
---|
| 98 |
|
---|
[128] | 99 | /*@ buffers *** */
|
---|
[1113] | 100 | char *midway_call, *ultimate_call, *tmp;
|
---|
| 101 | char *p = NULL;
|
---|
| 102 | char *cd_number_str = NULL;
|
---|
| 103 | char *command = NULL;
|
---|
| 104 |
|
---|
[1] | 105 |
|
---|
| 106 | /*@*********** End Variables ***************************************/
|
---|
| 107 |
|
---|
[1107] | 108 | mr_msg(3, "Starting");
|
---|
[128] | 109 | assert(bkpinfo != NULL);
|
---|
| 110 | assert_string_is_neither_NULL_nor_zerolength(basic_call);
|
---|
| 111 | assert_string_is_neither_NULL_nor_zerolength(isofile);
|
---|
| 112 | assert_string_is_neither_NULL_nor_zerolength(logstub);
|
---|
[1113] | 113 |
|
---|
[1080] | 114 | midway_call = mr_malloc(1200);
|
---|
| 115 | ultimate_call = mr_malloc(1200);
|
---|
| 116 | tmp = mr_malloc(1200);
|
---|
[1] | 117 |
|
---|
[1113] | 118 | mr_asprintf(&cd_number_str, "%d", cd_no);
|
---|
[128] | 119 | resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
|
---|
| 120 | resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
|
---|
[1113] | 121 | mr_free(cd_number_str);
|
---|
| 122 |
|
---|
[128] | 123 | resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
|
---|
[1107] | 124 | mr_msg(4, "basic call = '%s'", basic_call);
|
---|
| 125 | mr_msg(4, "midway_call = '%s'", midway_call);
|
---|
| 126 | mr_msg(4, "tmp = '%s'", tmp);
|
---|
| 127 | mr_msg(4, "ultimate call = '%s'", ultimate_call);
|
---|
[1113] | 128 | mr_asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
|
---|
[1] | 129 |
|
---|
[128] | 130 | log_to_screen
|
---|
[1113] | 131 | (_("Please be patient. Do not be alarmed by on-screen inactivity."));
|
---|
[1107] | 132 | mr_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
|
---|
[128] | 133 | what_i_am_doing);
|
---|
| 134 | if (bkpinfo->manual_cd_tray) {
|
---|
[1113] | 135 | /* Find everything after a 2>> and remove it */
|
---|
| 136 | p = strstr(command, "2>>");
|
---|
[128] | 137 | if (p) {
|
---|
[1113] | 138 | for (; *p != ' ' && *p != '\0'; p++) {
|
---|
[128] | 139 | *p = ' ';
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
[1] | 142 | #ifndef _XWIN
|
---|
[128] | 143 | if (!g_text_mode) {
|
---|
| 144 | newtSuspend();
|
---|
| 145 | }
|
---|
[1] | 146 | #endif
|
---|
[1107] | 147 | mr_msg(1, "command = '%s'", command);
|
---|
[128] | 148 | retval += system(command);
|
---|
| 149 | if (!g_text_mode) {
|
---|
| 150 | newtResume();
|
---|
| 151 | }
|
---|
| 152 | if (retval) {
|
---|
[1107] | 153 | mr_msg(2, "Basic call '%s' returned an error.", basic_call);
|
---|
[1113] | 154 | popup_and_OK(_("Press ENTER to continue."));
|
---|
[128] | 155 | popup_and_OK
|
---|
[1113] | 156 | (_("mkisofs and/or cdrecord returned an error. CD was not created"));
|
---|
[128] | 157 | }
|
---|
[1] | 158 | }
|
---|
[128] | 159 | /* if text mode then do the above & RETURN; if not text mode, do this... */
|
---|
| 160 | else {
|
---|
[1107] | 161 | mr_msg(3, "command = '%s'", command);
|
---|
[1] | 162 | // yes_this_is_a_goto:
|
---|
[128] | 163 | retval =
|
---|
| 164 | run_external_binary_with_percentage_indicator_NEW
|
---|
| 165 | (what_i_am_doing, command);
|
---|
| 166 | }
|
---|
[1113] | 167 | mr_free(command);
|
---|
[1] | 168 |
|
---|
[1080] | 169 | mr_free(midway_call);
|
---|
| 170 | mr_free(ultimate_call);
|
---|
| 171 | mr_free(tmp);
|
---|
[128] | 172 | return (retval);
|
---|
[1] | 173 | }
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * Run a program and log its output (stdout and stderr) to the logfile.
|
---|
| 178 | * @param program The program to run. Passed to the shell, so you can use pipes etc.
|
---|
| 179 | * @param debug_level If @p g_loglevel is higher than this, do not log the output.
|
---|
| 180 | * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
|
---|
| 181 | */
|
---|
[128] | 182 | int run_program_and_log_output(char *program, int debug_level)
|
---|
[1] | 183 | {
|
---|
[128] | 184 | /*@ buffer ****************************************************** */
|
---|
[1113] | 185 | char *callstr = NULL;
|
---|
| 186 | char *incoming = NULL;
|
---|
[1] | 187 |
|
---|
[128] | 188 | /*@ int ********************************************************* */
|
---|
[1113] | 189 | int res = 0;
|
---|
| 190 | size_t n = 0;
|
---|
[128] | 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 */
|
---|
[1168] | 248 | mr_strip_spaces(incoming);
|
---|
[128] | 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 | }
|
---|
| 325 | /* This works on Newt, and it gives quicker updates. */
|
---|
| 326 | for (; does_file_exist(lockfile); sleep(1)) {
|
---|
| 327 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 328 | update_evalcall_form(1);
|
---|
| 329 | }
|
---|
[154] | 330 | /* Evaluate the status returned by pclose to get the exit code of the called program. */
|
---|
| 331 | errno = 0;
|
---|
| 332 | res = pclose(fin);
|
---|
| 333 | /* Log actual pclose errors. */
|
---|
[1113] | 334 | if (errno) {
|
---|
| 335 | mr_msg(5, "pclose err: %d", errno);
|
---|
| 336 | }
|
---|
[154] | 337 | /* Check if we have a valid status. If we do, extract the called program's exit code. */
|
---|
| 338 | /* If we don't, highlight this fact by returning -1. */
|
---|
| 339 | if (WIFEXITED(res)) {
|
---|
| 340 | retval = WEXITSTATUS(res);
|
---|
| 341 | } else {
|
---|
| 342 | retval = -1;
|
---|
| 343 | }
|
---|
[128] | 344 | close_evalcall_form();
|
---|
| 345 | unlink(lockfile);
|
---|
[1113] | 346 | mr_free(lockfile);
|
---|
| 347 |
|
---|
[128] | 348 | return (retval);
|
---|
[1] | 349 | }
|
---|
| 350 |
|
---|
| 351 |
|
---|
| 352 | /**
|
---|
[1113] | 353 | * Apparently used. @bug This has a purpose, but what?
|
---|
[1] | 354 | */
|
---|
| 355 | #define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
|
---|
| 356 | #define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
|
---|
| 357 |
|
---|
| 358 |
|
---|
| 359 |
|
---|
[296] | 360 |
|
---|
[128] | 361 | int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
|
---|
[1] | 362 | {
|
---|
| 363 | // if dir=='w' then copy from orig to archived
|
---|
| 364 | // if dir=='r' then copy from archived to orig
|
---|
[1113] | 365 | char *tmp = NULL;
|
---|
[1173] | 366 | char *tmp1 = NULL;
|
---|
[1113] | 367 | char *buf = NULL;
|
---|
[128] | 368 | long int bytes_to_be_read, bytes_read_in, bytes_written_out =
|
---|
| 369 | 0, bufcap, subsliceno = 0;
|
---|
| 370 | int retval = 0;
|
---|
[1113] | 371 | FILE *fin = NULL;
|
---|
| 372 | FILE *fout = NULL;
|
---|
| 373 | FILE *ftmp = NULL;
|
---|
[1] | 374 |
|
---|
[1107] | 375 | mr_msg(5, "Opening.");
|
---|
[128] | 376 | malloc_string(tmp);
|
---|
| 377 | tmp[0] = '\0';
|
---|
| 378 | bufcap = 256L * 1024L;
|
---|
[1080] | 379 | buf = mr_malloc(bufcap);
|
---|
[1] | 380 |
|
---|
[128] | 381 | if (direction == 'w') {
|
---|
| 382 | fin = f_orig;
|
---|
| 383 | fout = f_archived;
|
---|
[1173] | 384 | mr_asprintf(&tmp1, "%-64s", PIMP_START_SZ);
|
---|
| 385 | if (fwrite(tmp1, 1, 64, fout) != 64) {
|
---|
[128] | 386 | fatal_error("Can't write the introductory block");
|
---|
| 387 | }
|
---|
[1173] | 388 | mr_free(tmp1);
|
---|
| 389 |
|
---|
[128] | 390 | while (1) {
|
---|
| 391 | bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
|
---|
| 392 | if (bytes_read_in == 0) {
|
---|
| 393 | break;
|
---|
| 394 | }
|
---|
[1173] | 395 | mr_asprintf(&tmp1, "%-64ld", bytes_read_in);
|
---|
| 396 | if (fwrite(tmp1, 1, 64, fout) != 64) {
|
---|
[128] | 397 | fatal_error("Cannot write introductory block");
|
---|
| 398 | }
|
---|
[1173] | 399 | mr_free(tmp1);
|
---|
| 400 |
|
---|
[1107] | 401 | mr_msg(7,
|
---|
[128] | 402 | "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
|
---|
| 403 | subsliceno, bytes_read_in, bytes_to_be_read);
|
---|
| 404 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
|
---|
[1173] | 405 | mr_asprintf(&tmp1, "%-64ld", subsliceno);
|
---|
| 406 | if (fwrite(tmp1, 1, 64, fout) != 64) {
|
---|
[128] | 407 | fatal_error("Cannot write post-thingy block");
|
---|
| 408 | }
|
---|
[1173] | 409 | mr_free(tmp1);
|
---|
| 410 |
|
---|
[1107] | 411 | mr_msg(7, "Subslice #%d written OK", subsliceno);
|
---|
[128] | 412 | subsliceno++;
|
---|
| 413 | }
|
---|
| 414 | sprintf(tmp, "%-64ld", 0L);
|
---|
| 415 | if (fwrite(tmp, 1, 64L, fout) != 64L) {
|
---|
| 416 | fatal_error("Cannot write final introductory block");
|
---|
| 417 | }
|
---|
| 418 | } else {
|
---|
| 419 | fin = f_archived;
|
---|
| 420 | fout = f_orig;
|
---|
[1173] | 421 | tmp = mr_malloc(64L);
|
---|
[128] | 422 | if (fread(tmp, 1, 64L, fin) != 64L) {
|
---|
| 423 | fatal_error("Cannot read the introductory block");
|
---|
| 424 | }
|
---|
[1107] | 425 | mr_msg(5, "tmp is %s", tmp);
|
---|
[128] | 426 | if (!strstr(tmp, PIMP_START_SZ)) {
|
---|
| 427 | fatal_error("Can't find intro blk");
|
---|
| 428 | }
|
---|
| 429 | if (fread(tmp, 1, 64L, fin) != 64L) {
|
---|
| 430 | fatal_error("Cannot read introductory blk");
|
---|
| 431 | }
|
---|
| 432 | bytes_to_be_read = atol(tmp);
|
---|
| 433 | while (bytes_to_be_read > 0) {
|
---|
[1107] | 434 | mr_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
|
---|
[128] | 435 | bytes_to_be_read);
|
---|
| 436 | bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
|
---|
| 437 | if (bytes_read_in != bytes_to_be_read) {
|
---|
| 438 | fatal_error
|
---|
| 439 | ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
|
---|
| 440 | }
|
---|
| 441 | bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
|
---|
[1173] | 442 | if (fread(tmp, 1, 64L, fin) != 64L) {
|
---|
[128] | 443 | fatal_error("Cannot read post-thingy block");
|
---|
| 444 | }
|
---|
| 445 | if (atol(tmp) != subsliceno) {
|
---|
[1107] | 446 | mr_msg(1, "Wanted subslice %ld but got %ld ('%s')",
|
---|
[128] | 447 | subsliceno, atol(tmp), tmp);
|
---|
| 448 | }
|
---|
[1107] | 449 | mr_msg(7, "Subslice #%ld read OK", subsliceno);
|
---|
[128] | 450 | subsliceno++;
|
---|
[1173] | 451 | if (fread(tmp, 1, 64L, fin) != 64L) {
|
---|
[128] | 452 | fatal_error("Cannot read introductory block");
|
---|
| 453 | }
|
---|
| 454 | bytes_to_be_read = atol(tmp);
|
---|
| 455 | }
|
---|
| 456 | }
|
---|
[1] | 457 |
|
---|
[1107] | 458 | // mr_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
|
---|
[1] | 459 |
|
---|
[128] | 460 | if (direction == 'w') {
|
---|
[1173] | 461 | mr_asprintf(&tmp1, "%-64s", PIMP_END_SZ);
|
---|
| 462 | if (fwrite(tmp1, 1, 64L, fout) != 64L) {
|
---|
[128] | 463 | fatal_error("Can't write the final block");
|
---|
| 464 | }
|
---|
[1173] | 465 | mr_free(tmp1);
|
---|
[128] | 466 | } else {
|
---|
[1107] | 467 | mr_msg(1, "tmpA is %s", tmp);
|
---|
[128] | 468 | if (!strstr(tmp, PIMP_END_SZ)) {
|
---|
[1173] | 469 | if (fread(tmp, 1, 64L, fin) != 64L) {
|
---|
[128] | 470 | fatal_error("Can't read the final block");
|
---|
| 471 | }
|
---|
[1107] | 472 | mr_msg(5, "tmpB is %s", tmp);
|
---|
[128] | 473 | if (!strstr(tmp, PIMP_END_SZ)) {
|
---|
| 474 | ftmp = fopen("/tmp/out.leftover", "w");
|
---|
[1173] | 475 | bytes_read_in = fread(tmp, 1, 64L, fin);
|
---|
[1107] | 476 | mr_msg(1, "bytes_read_in = %ld", bytes_read_in);
|
---|
[1] | 477 | // if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
|
---|
[128] | 478 | fwrite(tmp, 1, bytes_read_in, ftmp);
|
---|
[797] | 479 | sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
|
---|
[1107] | 480 | // mr_msg(0, tmp);
|
---|
[128] | 481 | fread(tmp, 1, 512, fin);
|
---|
[1107] | 482 | mr_msg(0, "tmp = '%s'", tmp);
|
---|
[128] | 483 | fwrite(tmp, 1, 512, ftmp);
|
---|
| 484 | fclose(ftmp);
|
---|
| 485 | fatal_error("Missing terminating block");
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
[1] | 489 |
|
---|
[1080] | 490 | mr_free(buf);
|
---|
| 491 | mr_free(tmp);
|
---|
[1107] | 492 | mr_msg(3, "Successfully copied %ld bytes", bytes_written_out);
|
---|
[128] | 493 | return (retval);
|
---|
[1] | 494 | }
|
---|
| 495 |
|
---|
| 496 | /**
|
---|
[296] | 497 | * Feed @p input_device through ntfsclone to @p output_fname.
|
---|
[1] | 498 | * @param input_device The device to image.
|
---|
| 499 | * @param output_fname The file to write.
|
---|
| 500 | * @return 0 for success, nonzero for failure.
|
---|
| 501 | */
|
---|
[296] | 502 | int feed_into_ntfsprog(char *input_device, char *output_fname)
|
---|
[1] | 503 | {
|
---|
| 504 | // BACKUP
|
---|
[296] | 505 | int res = -1;
|
---|
[1113] | 506 | char *command = NULL;
|
---|
[1] | 507 |
|
---|
[128] | 508 | if (!does_file_exist(input_device)) {
|
---|
| 509 | fatal_error("input device does not exist");
|
---|
| 510 | }
|
---|
[296] | 511 | if ( !find_home_of_exe("ntfsclone")) {
|
---|
| 512 | fatal_error("ntfsclone not found");
|
---|
[128] | 513 | }
|
---|
[1113] | 514 | mr_asprintf(&command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
|
---|
[296] | 515 | res = run_program_and_log_output(command, 5);
|
---|
[1080] | 516 | mr_free(command);
|
---|
[1113] | 517 |
|
---|
[411] | 518 | unlink(output_fname);
|
---|
[128] | 519 | return (res);
|
---|
[1] | 520 | }
|
---|
| 521 |
|
---|
| 522 |
|
---|
[128] | 523 | int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
|
---|
[1] | 524 | {
|
---|
| 525 |
|
---|
[128] | 526 | int res = 0;
|
---|
[1] | 527 | int percentage = 0;
|
---|
| 528 | int maxpc = 0;
|
---|
| 529 | int pcno = 0;
|
---|
| 530 | int last_pcno = 0;
|
---|
| 531 |
|
---|
[1113] | 532 | char *command = NULL;
|
---|
[128] | 533 | char *tempfile;
|
---|
| 534 | char *title;
|
---|
| 535 | /*@ pointers ********************************************************** */
|
---|
| 536 | FILE *pin;
|
---|
[1] | 537 |
|
---|
[128] | 538 | malloc_string(title);
|
---|
| 539 | malloc_string(tempfile);
|
---|
| 540 | assert_string_is_neither_NULL_nor_zerolength(cmd);
|
---|
| 541 | assert_string_is_neither_NULL_nor_zerolength(title);
|
---|
[1] | 542 |
|
---|
[128] | 543 | strcpy(title, tt);
|
---|
| 544 | strcpy(tempfile,
|
---|
| 545 | call_program_and_get_last_line_of_output
|
---|
| 546 | ("mktemp -q /tmp/mondo.XXXXXXXX"));
|
---|
[1113] | 547 | mr_asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
|
---|
[128] | 548 | tempfile);
|
---|
[1107] | 549 | mr_msg(3, command);
|
---|
[128] | 550 | open_evalcall_form(title);
|
---|
| 551 | if (!(pin = popen(command, "r"))) {
|
---|
| 552 | log_OS_error("fmt err");
|
---|
[1113] | 553 | mr_free(command);
|
---|
[128] | 554 | return (1);
|
---|
| 555 | }
|
---|
[1113] | 556 | mr_free(command);
|
---|
| 557 |
|
---|
[128] | 558 | maxpc = 100;
|
---|
[1] | 559 | // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
|
---|
[128] | 560 | for (sleep(1); does_file_exist(tempfile); sleep(1)) {
|
---|
| 561 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
|
---|
| 562 | if (pcno < 0 || pcno > 100) {
|
---|
[1107] | 563 | mr_msg(5, "Weird pc#");
|
---|
[128] | 564 | continue;
|
---|
| 565 | }
|
---|
| 566 | percentage = pcno * 100 / maxpc;
|
---|
| 567 | if (pcno <= 5 && last_pcno > 40) {
|
---|
| 568 | close_evalcall_form();
|
---|
[541] | 569 | strcpy(title, "Verifying...");
|
---|
[128] | 570 | open_evalcall_form(title);
|
---|
| 571 | }
|
---|
| 572 | last_pcno = pcno;
|
---|
| 573 | update_evalcall_form(percentage);
|
---|
[1] | 574 | }
|
---|
[128] | 575 | // OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
|
---|
| 576 | close_evalcall_form();
|
---|
| 577 | if (pclose(pin)) {
|
---|
| 578 | res++;
|
---|
| 579 | log_OS_error("Unable to pclose");
|
---|
[1] | 580 | }
|
---|
[128] | 581 | unlink(tempfile);
|
---|
[1080] | 582 | mr_free(tempfile);
|
---|
| 583 | mr_free(title);
|
---|
[128] | 584 | return (res);
|
---|
[1] | 585 | }
|
---|
| 586 |
|
---|
| 587 |
|
---|
[128] | 588 | void *run_prog_in_bkgd_then_exit(void *info)
|
---|
[1] | 589 | {
|
---|
[128] | 590 | char *sz_command;
|
---|
| 591 | static int res = 4444;
|
---|
[1] | 592 |
|
---|
[128] | 593 | res = 999;
|
---|
| 594 | sz_command = (char *) info;
|
---|
[1107] | 595 | mr_msg(4, "sz_command = '%s'", sz_command);
|
---|
[128] | 596 | res = system(sz_command);
|
---|
| 597 | if (res > 256 && res != 4444) {
|
---|
| 598 | res = res / 256;
|
---|
| 599 | }
|
---|
[1107] | 600 | mr_msg(4, "child res = %d", res);
|
---|
[128] | 601 | sz_command[0] = '\0';
|
---|
| 602 | pthread_exit((void *) (&res));
|
---|
[1] | 603 | }
|
---|
| 604 |
|
---|
| 605 |
|
---|
[128] | 606 | int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
|
---|
[1] | 607 | {
|
---|
| 608 |
|
---|
[128] | 609 | /*@ int *************************************************************** */
|
---|
| 610 | int res = 0;
|
---|
[1] | 611 | int percentage = 0;
|
---|
| 612 | int maxpc = 100;
|
---|
| 613 | int pcno = 0;
|
---|
| 614 | int last_pcno = 0;
|
---|
[128] | 615 | int counter = 0;
|
---|
[1] | 616 |
|
---|
[128] | 617 | /*@ buffers *********************************************************** */
|
---|
[1113] | 618 | char *command = NULL;
|
---|
[128] | 619 | char *title;
|
---|
| 620 | /*@ pointers ********************************************************** */
|
---|
| 621 | static int chldres = 0;
|
---|
| 622 | int *pchild_result;
|
---|
| 623 | pthread_t childthread;
|
---|
[1] | 624 |
|
---|
[128] | 625 | pchild_result = &chldres;
|
---|
| 626 | assert_string_is_neither_NULL_nor_zerolength(cmd);
|
---|
| 627 | assert_string_is_neither_NULL_nor_zerolength(tt);
|
---|
| 628 | *pchild_result = 999;
|
---|
[1] | 629 |
|
---|
[128] | 630 | malloc_string(title);
|
---|
| 631 | strcpy(title, tt);
|
---|
[1113] | 632 | mr_asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
|
---|
[1107] | 633 | mr_msg(3, "command = '%s'", command);
|
---|
[128] | 634 | if ((res =
|
---|
| 635 | pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
|
---|
| 636 | (void *) command))) {
|
---|
| 637 | fatal_error("Unable to create an archival thread");
|
---|
| 638 | }
|
---|
[1] | 639 |
|
---|
[1107] | 640 | mr_msg(8, "Parent running");
|
---|
[128] | 641 | open_evalcall_form(title);
|
---|
| 642 | for (sleep(1); command[0] != '\0'; sleep(1)) {
|
---|
| 643 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
|
---|
| 644 | if (pcno <= 0 || pcno > 100) {
|
---|
[1107] | 645 | mr_msg(8, "Weird pc#");
|
---|
[128] | 646 | continue;
|
---|
| 647 | }
|
---|
| 648 | percentage = pcno * 100 / maxpc;
|
---|
| 649 | if (pcno <= 5 && last_pcno >= 40) {
|
---|
| 650 | close_evalcall_form();
|
---|
[541] | 651 | strcpy(title, "Verifying...");
|
---|
[128] | 652 | open_evalcall_form(title);
|
---|
| 653 | }
|
---|
| 654 | if (counter++ >= 5) {
|
---|
| 655 | counter = 0;
|
---|
| 656 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 657 | }
|
---|
| 658 | last_pcno = pcno;
|
---|
| 659 | update_evalcall_form(percentage);
|
---|
[1] | 660 | }
|
---|
[1113] | 661 | mr_free(command);
|
---|
| 662 |
|
---|
[128] | 663 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 664 | close_evalcall_form();
|
---|
| 665 | pthread_join(childthread, (void *) (&pchild_result));
|
---|
| 666 | if (pchild_result) {
|
---|
| 667 | res = *pchild_result;
|
---|
| 668 | } else {
|
---|
| 669 | res = 666;
|
---|
[1] | 670 | }
|
---|
[1107] | 671 | mr_msg(3, "Parent res = %d", res);
|
---|
[1080] | 672 | mr_free(title);
|
---|
[128] | 673 | return (res);
|
---|
[1] | 674 | }
|
---|
| 675 |
|
---|
| 676 |
|
---|
| 677 | /**
|
---|
[296] | 678 | * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
|
---|
| 679 | * @param input_fifo The ntfsclone file to read.
|
---|
[1] | 680 | * @param output_device Where to put the output.
|
---|
[296] | 681 | * @return The return value of ntfsclone (0 for success).
|
---|
[1] | 682 | */
|
---|
[296] | 683 | int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
|
---|
[1] | 684 | {
|
---|
| 685 | // RESTORE
|
---|
[296] | 686 | int res = -1;
|
---|
[1113] | 687 | char *command = NULL;
|
---|
[1] | 688 |
|
---|
[296] | 689 | if ( !find_home_of_exe("ntfsclone")) {
|
---|
| 690 | fatal_error("ntfsclone not found");
|
---|
[128] | 691 | }
|
---|
[1113] | 692 | mr_asprintf(&command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
|
---|
[296] | 693 | res = run_program_and_log_output(command, 5);
|
---|
[1080] | 694 | mr_free(command);
|
---|
[128] | 695 | return (res);
|
---|
[1] | 696 | }
|
---|