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