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