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