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