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