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