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