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