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