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