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