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