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