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