[1] | 1 | /* libmondo-fork.c
|
---|
[128] | 2 | $Id: libmondo-fork.c 2420 2009-09-24 09:48:15Z 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 2420 2009-09-24 09:48:15Z 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.
|
---|
[2392] | 34 | * @note The returned value points to an allocated string that the caller needs to free
|
---|
[1] | 35 | */
|
---|
[128] | 36 | char *call_program_and_get_last_line_of_output(char *call)
|
---|
[1] | 37 | {
|
---|
[128] | 38 | /*@ buffers ***************************************************** */
|
---|
[2383] | 39 | char *result = NULL;
|
---|
[2420] | 40 | char *tmpf = NULL;
|
---|
[1] | 41 |
|
---|
[128] | 42 | /*@ pointers **************************************************** */
|
---|
| 43 | FILE *fin;
|
---|
[1] | 44 |
|
---|
[128] | 45 | /*@******************************************************************** */
|
---|
[1] | 46 |
|
---|
[128] | 47 | assert_string_is_neither_NULL_nor_zerolength(call);
|
---|
[2392] | 48 |
|
---|
[2420] | 49 | mr_asprintf(tmpf, "%s/cpgll.out", bkpinfo->scratchdir);
|
---|
| 50 | mr_strcat(call, " > %s", tmpf);
|
---|
[2392] | 51 | log_msg(4, "Calling command: %s", call);
|
---|
| 52 | /* By default return an empty string in any case */
|
---|
| 53 | mr_asprintf(result, "");
|
---|
| 54 |
|
---|
[2420] | 55 | system(call);
|
---|
| 56 | if ((fin = fopen(ftmp, "r"))) {
|
---|
[2392] | 57 | while (!feof(fin)) {
|
---|
[2420] | 58 | mr_getline(result, fin);
|
---|
| 59 | log_msg(9, "Loop result: %s", result);
|
---|
[2392] | 60 | }
|
---|
[2420] | 61 | log_msg(4, "Final Result: %s", result);
|
---|
[2392] | 62 | mr_strip_spaces(result);
|
---|
[128] | 63 | paranoid_pclose(fin);
|
---|
| 64 | } else {
|
---|
| 65 | log_OS_error("Unable to popen call");
|
---|
[1] | 66 | }
|
---|
[2420] | 67 | mr_free(ftmp)
|
---|
[2392] | 68 | log_msg(4, "Returns: %s", result);
|
---|
[2383] | 69 | return(result);
|
---|
[1] | 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 |
|
---|
[541] | 77 | #define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
|
---|
[1] | 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Call mkisofs to create an ISO image.
|
---|
| 81 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 82 | * - @c bkpinfo->manual_cd_tray
|
---|
| 83 | * - @c bkpinfo->backup_media_type
|
---|
| 84 | * - @c bkpinfo->please_dont_eject_when_restoring
|
---|
| 85 | * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
|
---|
| 86 | * - @c _ISO_ will become the ISO file (@p isofile)
|
---|
| 87 | * - @c _CD#_ becomes the CD number (@p cd_no)
|
---|
| 88 | * - @c _ERR_ becomes the logfile (@p g_logfile)
|
---|
| 89 | * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
|
---|
| 90 | * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
|
---|
| 91 | * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
|
---|
| 92 | * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
|
---|
| 93 | */
|
---|
[2405] | 94 | int eval_call_to_make_ISO(char *basic_call, char *isofile, int cd_no, char *what_i_am_doing) {
|
---|
[1] | 95 |
|
---|
[128] | 96 | /*@ int's *** */
|
---|
| 97 | int retval = 0;
|
---|
[1] | 98 |
|
---|
| 99 |
|
---|
[128] | 100 | /*@ buffers *** */
|
---|
[2315] | 101 | char *midway_call = NULL;
|
---|
| 102 | char *ultimate_call = NULL;
|
---|
| 103 | char *tmp = NULL;
|
---|
[2275] | 104 |
|
---|
| 105 | char *cd_number_str = NULL;
|
---|
| 106 | char *command = NULL;
|
---|
[128] | 107 | char *p;
|
---|
[2224] | 108 | char *tmp1 = NULL;
|
---|
[2275] | 109 | char *tmp2 = NULL;
|
---|
[1] | 110 |
|
---|
| 111 | /*@*********** End Variables ***************************************/
|
---|
| 112 |
|
---|
[128] | 113 | log_msg(3, "Starting");
|
---|
| 114 | assert(bkpinfo != NULL);
|
---|
| 115 | assert_string_is_neither_NULL_nor_zerolength(isofile);
|
---|
[1] | 116 |
|
---|
[2382] | 117 | if ((bkpinfo->netfs_user) && (strstr(bkpinfo->netfs_proto,"nfs"))) {
|
---|
| 118 | mr_asprintf(tmp1, "su - %s -c \"%s\"", bkpinfo->netfs_user, basic_call);
|
---|
[2224] | 119 | } else {
|
---|
[2323] | 120 | mr_asprintf(tmp1, "%s", basic_call);
|
---|
[2224] | 121 | }
|
---|
| 122 |
|
---|
[2323] | 123 | mr_asprintf(cd_number_str, "%d", cd_no);
|
---|
[2315] | 124 | log_msg(4, "basic call = '%s'", tmp1);
|
---|
| 125 | midway_call = resolve_naff_tokens(tmp1, isofile, "_ISO_");
|
---|
| 126 | mr_free(tmp1);
|
---|
| 127 |
|
---|
| 128 | log_msg(4, "midway_call = '%s'", midway_call);
|
---|
| 129 | tmp = resolve_naff_tokens(midway_call, cd_number_str, "_CD#_");
|
---|
[2275] | 130 | mr_free(cd_number_str);
|
---|
[2315] | 131 | mr_free(midway_call);
|
---|
[2275] | 132 |
|
---|
[128] | 133 | log_msg(4, "tmp = '%s'", tmp);
|
---|
[2315] | 134 | ultimate_call = resolve_naff_tokens(tmp, MONDO_LOGFILE, "_ERR_");
|
---|
| 135 | mr_free(tmp);
|
---|
| 136 |
|
---|
[128] | 137 | log_msg(4, "ultimate call = '%s'", ultimate_call);
|
---|
[2323] | 138 | mr_asprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
|
---|
[2315] | 139 | mr_free(ultimate_call);
|
---|
[1] | 140 |
|
---|
[128] | 141 | log_to_screen
|
---|
[541] | 142 | ("Please be patient. Do not be alarmed by on-screen inactivity.");
|
---|
[128] | 143 | log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
|
---|
| 144 | what_i_am_doing);
|
---|
| 145 | if (bkpinfo->manual_cd_tray) {
|
---|
[2323] | 146 | mr_asprintf(tmp2, "%s", command);
|
---|
[2275] | 147 | p = strstr(tmp2, "2>>");
|
---|
[128] | 148 | if (p) {
|
---|
[2275] | 149 | *p++ = ' ';
|
---|
| 150 | *p++ = ' ';
|
---|
| 151 | *p++ = ' ';
|
---|
[128] | 152 | while (*p == ' ') {
|
---|
| 153 | p++;
|
---|
| 154 | }
|
---|
[2275] | 155 | for (; (*p != ' ') && (*p != '\0'); p++) {
|
---|
[128] | 156 | *p = ' ';
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
[2275] | 159 | mr_free(command);
|
---|
| 160 | command = tmp2;
|
---|
[128] | 161 | if (!g_text_mode) {
|
---|
| 162 | newtSuspend();
|
---|
| 163 | }
|
---|
| 164 | log_msg(1, "command = '%s'", command);
|
---|
| 165 | retval += system(command);
|
---|
| 166 | if (!g_text_mode) {
|
---|
| 167 | newtResume();
|
---|
| 168 | }
|
---|
| 169 | if (retval) {
|
---|
[2275] | 170 | popup_and_OK("mkisofs and/or cdrecord returned an error. CD was not created");
|
---|
[128] | 171 | }
|
---|
[1] | 172 | }
|
---|
[128] | 173 | /* if text mode then do the above & RETURN; if not text mode, do this... */
|
---|
| 174 | else {
|
---|
| 175 | log_msg(3, "command = '%s'", command);
|
---|
[2275] | 176 | retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
|
---|
[128] | 177 | }
|
---|
[2275] | 178 | mr_free(command);
|
---|
[128] | 179 | return (retval);
|
---|
[1] | 180 | }
|
---|
| 181 |
|
---|
| 182 |
|
---|
[1688] | 183 | /**
|
---|
| 184 | * Call copy of data to create an USB image.
|
---|
| 185 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 186 | * - @c bkpinfo->backup_media_type
|
---|
| 187 | * @return Exit code of @c copy (0 is success, anything else indicates failure).
|
---|
| 188 | */
|
---|
| 189 | int
|
---|
| 190 | eval_call_to_make_USB(char *command, char *what_i_am_doing) {
|
---|
[1] | 191 |
|
---|
[1688] | 192 | /*@ int's *** */
|
---|
| 193 | int retval = 0;
|
---|
[1] | 194 |
|
---|
[1688] | 195 |
|
---|
| 196 | /*@*********** End Variables ***************************************/
|
---|
| 197 |
|
---|
| 198 | log_msg(3, "Starting");
|
---|
| 199 | assert(bkpinfo != NULL);
|
---|
| 200 |
|
---|
| 201 | log_to_screen
|
---|
| 202 | ("Please be patient. Do not be alarmed by on-screen inactivity.");
|
---|
| 203 | log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
|
---|
| 204 | what_i_am_doing);
|
---|
| 205 |
|
---|
| 206 | if (!g_text_mode) {
|
---|
| 207 | newtSuspend();
|
---|
| 208 | }
|
---|
| 209 | log_msg(1, "command = '%s'", command);
|
---|
| 210 | if (!g_text_mode) {
|
---|
[2339] | 211 | retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
|
---|
[1688] | 212 | } else {
|
---|
| 213 | retval += system(command);
|
---|
| 214 | }
|
---|
| 215 | if (!g_text_mode) {
|
---|
| 216 | newtResume();
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | return (retval);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 |
|
---|
[1] | 225 | /**
|
---|
| 226 | * Run a program and log its output (stdout and stderr) to the logfile.
|
---|
| 227 | * @param program The program to run. Passed to the shell, so you can use pipes etc.
|
---|
| 228 | * @param debug_level If @p g_loglevel is higher than this, do not log the output.
|
---|
| 229 | * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
|
---|
| 230 | */
|
---|
[128] | 231 | int run_program_and_log_output(char *program, int debug_level)
|
---|
[1] | 232 | {
|
---|
[128] | 233 | /*@ buffer ****************************************************** */
|
---|
[2275] | 234 | char *callstr = NULL;
|
---|
[128] | 235 | char incoming[MAX_STR_LEN * 2];
|
---|
[2275] | 236 | char *tmp1 = NULL;
|
---|
[1] | 237 |
|
---|
[128] | 238 | /*@ int ********************************************************* */
|
---|
| 239 | int res;
|
---|
| 240 | bool log_if_failure = FALSE;
|
---|
| 241 | bool log_if_success = FALSE;
|
---|
[1] | 242 |
|
---|
[128] | 243 | /*@ pointers *************************************************** */
|
---|
| 244 | FILE *fin;
|
---|
| 245 | char *p;
|
---|
[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 | }
|
---|
[2323] | 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);
|
---|
[2316] | 271 | log_msg(0, "--------------------------------start of output-----------------------------");
|
---|
[128] | 272 | }
|
---|
[2275] | 273 | mr_free(callstr);
|
---|
| 274 |
|
---|
[2323] | 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 | }
|
---|
[2275] | 279 | mr_free(callstr);
|
---|
| 280 |
|
---|
[2323] | 281 | mr_asprintf(tmp1, "%s/mondo-run-prog-thing.err", bkpinfo->tmpdir);
|
---|
[2275] | 282 | unlink(tmp1);
|
---|
| 283 | mr_free(tmp1);
|
---|
| 284 |
|
---|
[2323] | 285 | mr_asprintf(tmp1, "%s/mondo-run-prog-thing.tmp", bkpinfo->tmpdir);
|
---|
[2275] | 286 | fin = fopen(tmp1, "r");
|
---|
[128] | 287 | if (fin) {
|
---|
[2357] | 288 | for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin); 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);
|
---|
[2357] | 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 | }
|
---|
[2275] | 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 **************************************************** */
|
---|
[2275] | 339 | char *command = NULL;
|
---|
| 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 |
|
---|
[2323] | 346 | mr_asprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
|
---|
[1644] | 347 |
|
---|
[2323] | 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);
|
---|
[2324] | 350 | log_msg(2, "Executing %s", basic_call);
|
---|
[2275] | 351 |
|
---|
[128] | 352 | if (!(fin = popen(command, "r"))) {
|
---|
| 353 | log_OS_error("Unable to popen-in command");
|
---|
[2324] | 354 | log_to_screen("Failed utterly to call '%s'", command);
|
---|
[2275] | 355 | mr_free(command);
|
---|
| 356 | mr_free(lockfile);
|
---|
[128] | 357 | return (1);
|
---|
| 358 | }
|
---|
[2275] | 359 | mr_free(command);
|
---|
| 360 |
|
---|
[128] | 361 | if (!does_file_exist(lockfile)) {
|
---|
[541] | 362 | log_to_screen("Waiting for external binary to start");
|
---|
[128] | 363 | for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
|
---|
| 364 | log_msg(3, "Waiting for lockfile %s to exist", lockfile);
|
---|
| 365 | }
|
---|
| 366 | }
|
---|
[2357] | 367 |
|
---|
[128] | 368 | for (; does_file_exist(lockfile); sleep(1)) {
|
---|
| 369 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 370 | update_evalcall_form(1);
|
---|
| 371 | }
|
---|
[2357] | 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);
|
---|
[2275] | 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
|
---|
[2275] | 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.");
|
---|
[2275] | 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;
|
---|
[2323] | 430 | mr_asprintf(tmp, "%-64s", PIMP_START_SZ);
|
---|
[128] | 431 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
[2275] | 432 | mr_free(tmp);
|
---|
[128] | 433 | fatal_error("Can't write the introductory block");
|
---|
| 434 | }
|
---|
[2275] | 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 | }
|
---|
[2323] | 442 | mr_asprintf(tmp, "%-64ld", bytes_read_in);
|
---|
[128] | 443 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
[2275] | 444 | mr_free(tmp);
|
---|
[128] | 445 | fatal_error("Cannot write introductory block");
|
---|
| 446 | }
|
---|
[2275] | 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);
|
---|
[2323] | 453 | mr_asprintf(tmp, "%-64ld", subsliceno);
|
---|
[128] | 454 | if (fwrite(tmp, 1, 64, fout) != 64) {
|
---|
[2275] | 455 | mr_free(tmp);
|
---|
[128] | 456 | fatal_error("Cannot write post-thingy block");
|
---|
| 457 | }
|
---|
[2275] | 458 | mr_free(tmp);
|
---|
[128] | 459 | log_msg(7, "Subslice #%d written OK", subsliceno);
|
---|
| 460 | subsliceno++;
|
---|
| 461 | }
|
---|
[2323] | 462 | mr_asprintf(tmp, "%-64ld", 0L);
|
---|
[128] | 463 | if (fwrite(tmp, 1, 64L, fout) != 64L) {
|
---|
[2275] | 464 | mr_free(tmp);
|
---|
[128] | 465 | fatal_error("Cannot write final introductory block");
|
---|
| 466 | }
|
---|
[2275] | 467 | mr_free(tmp);
|
---|
| 468 |
|
---|
[2323] | 469 | mr_asprintf(tmp, "%-64s", PIMP_END_SZ);
|
---|
[2275] | 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;
|
---|
[2275] | 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 | }
|
---|
[2275] | 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 | }
|
---|
[2275] | 490 | if (fread(tmp1, 1, 64L, fin) != 64L) {
|
---|
| 491 | mr_free(tmp1);
|
---|
[128] | 492 | fatal_error("Cannot read introductory blk");
|
---|
| 493 | }
|
---|
[2275] | 494 | bytes_to_be_read = atol(tmp1);
|
---|
[128] | 495 | while (bytes_to_be_read > 0) {
|
---|
[2275] | 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) {
|
---|
[2275] | 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);
|
---|
[2275] | 503 | if (fread(tmp1, 1, 64, fin) != 64) {
|
---|
| 504 | mr_free(tmp1);
|
---|
[128] | 505 | fatal_error("Cannot read post-thingy block");
|
---|
| 506 | }
|
---|
[2275] | 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++;
|
---|
[2275] | 512 | if (fread(tmp1, 1, 64, fin) != 64) {
|
---|
| 513 | mr_free(tmp1);
|
---|
[128] | 514 | fatal_error("Cannot read introductory block");
|
---|
| 515 | }
|
---|
[2275] | 516 | bytes_to_be_read = atol(tmp1);
|
---|
[128] | 517 | }
|
---|
[2275] | 518 | log_msg(1, "tmpA is %s", tmp1);
|
---|
| 519 | if (!strstr(tmp1, PIMP_END_SZ)) {
|
---|
| 520 | if (fread(tmp1, 1, 64, fin) != 64) {
|
---|
| 521 | mr_free(tmp1);
|
---|
[128] | 522 | fatal_error("Can't read the final block");
|
---|
| 523 | }
|
---|
[2275] | 524 | log_msg(5, "tmpB is %s", tmp1);
|
---|
| 525 | if (!strstr(tmp1, PIMP_END_SZ)) {
|
---|
[2323] | 526 | mr_asprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
|
---|
[1644] | 527 | ftmp = fopen(filestr, "w");
|
---|
[2275] | 528 | mr_free(filestr);
|
---|
| 529 |
|
---|
| 530 | bytes_read_in = fread(tmp1, 1, 64, fin);
|
---|
[128] | 531 | log_msg(1, "bytes_read_in = %ld", bytes_read_in);
|
---|
[2275] | 532 |
|
---|
| 533 | fwrite(tmp1, 1, bytes_read_in, ftmp);
|
---|
| 534 | fread(tmp1, 1, tmpcap, fin);
|
---|
| 535 | log_msg(0, "tmp1 = '%s'", tmp1);
|
---|
| 536 | fwrite(tmp1, 1, tmpcap, ftmp);
|
---|
[128] | 537 | fclose(ftmp);
|
---|
[2275] | 538 | mr_free(tmp1);
|
---|
[128] | 539 | fatal_error("Missing terminating block");
|
---|
| 540 | }
|
---|
| 541 | }
|
---|
[2275] | 542 | mr_free(tmp1);
|
---|
[128] | 543 | }
|
---|
[1] | 544 |
|
---|
[128] | 545 | paranoid_free(buf);
|
---|
| 546 | log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
|
---|
| 547 | return (retval);
|
---|
[1] | 548 | }
|
---|
| 549 |
|
---|
| 550 |
|
---|
| 551 |
|
---|
| 552 |
|
---|
| 553 | /**
|
---|
[296] | 554 | * Feed @p input_device through ntfsclone to @p output_fname.
|
---|
[1] | 555 | * @param input_device The device to image.
|
---|
| 556 | * @param output_fname The file to write.
|
---|
| 557 | * @return 0 for success, nonzero for failure.
|
---|
| 558 | */
|
---|
[296] | 559 | int feed_into_ntfsprog(char *input_device, char *output_fname)
|
---|
[1] | 560 | {
|
---|
| 561 | // BACKUP
|
---|
[296] | 562 | int res = -1;
|
---|
[2331] | 563 | char *command = NULL;
|
---|
[1] | 564 |
|
---|
[128] | 565 | if (!does_file_exist(input_device)) {
|
---|
| 566 | fatal_error("input device does not exist");
|
---|
| 567 | }
|
---|
[2331] | 568 | command = find_home_of_exe("ntfsclone");
|
---|
| 569 | if (!command) {
|
---|
| 570 | mr_free(command);
|
---|
[296] | 571 | fatal_error("ntfsclone not found");
|
---|
[128] | 572 | }
|
---|
[2331] | 573 | mr_free(command);
|
---|
| 574 |
|
---|
[2323] | 575 | mr_asprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
|
---|
[296] | 576 | res = run_program_and_log_output(command, 5);
|
---|
[2275] | 577 | mr_free(command);
|
---|
| 578 |
|
---|
[411] | 579 | unlink(output_fname);
|
---|
[128] | 580 | return (res);
|
---|
[1] | 581 | }
|
---|
| 582 |
|
---|
| 583 |
|
---|
[128] | 584 | void *run_prog_in_bkgd_then_exit(void *info)
|
---|
[1] | 585 | {
|
---|
[128] | 586 | char *sz_command;
|
---|
| 587 | static int res = 4444;
|
---|
[1] | 588 |
|
---|
[128] | 589 | res = 999;
|
---|
| 590 | sz_command = (char *) info;
|
---|
| 591 | log_msg(4, "sz_command = '%s'", sz_command);
|
---|
| 592 | res = system(sz_command);
|
---|
| 593 | if (res > 256 && res != 4444) {
|
---|
| 594 | res = res / 256;
|
---|
| 595 | }
|
---|
| 596 | log_msg(4, "child res = %d", res);
|
---|
| 597 | sz_command[0] = '\0';
|
---|
| 598 | pthread_exit((void *) (&res));
|
---|
[1] | 599 | }
|
---|
| 600 |
|
---|
| 601 |
|
---|
| 602 |
|
---|
| 603 |
|
---|
[2339] | 604 | int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd) {
|
---|
[1] | 605 |
|
---|
[128] | 606 | /*@ int *************************************************************** */
|
---|
| 607 | int res = 0;
|
---|
[1] | 608 | int percentage = 0;
|
---|
| 609 | int maxpc = 100;
|
---|
| 610 | int pcno = 0;
|
---|
| 611 | int last_pcno = 0;
|
---|
[128] | 612 | int counter = 0;
|
---|
[1] | 613 |
|
---|
[128] | 614 | /*@ buffers *********************************************************** */
|
---|
[2275] | 615 | char *command = NULL;
|
---|
[2331] | 616 | char *title = NULL;
|
---|
[128] | 617 | /*@ pointers ********************************************************** */
|
---|
| 618 | static int chldres = 0;
|
---|
| 619 | int *pchild_result;
|
---|
| 620 | pthread_t childthread;
|
---|
[1] | 621 |
|
---|
[128] | 622 | pchild_result = &chldres;
|
---|
| 623 | assert_string_is_neither_NULL_nor_zerolength(cmd);
|
---|
| 624 | assert_string_is_neither_NULL_nor_zerolength(tt);
|
---|
| 625 | *pchild_result = 999;
|
---|
[1] | 626 |
|
---|
[2323] | 627 | mr_asprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
|
---|
[128] | 628 | log_msg(3, "command = '%s'", command);
|
---|
[2275] | 629 | if ((res = pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, (void *) command))) {
|
---|
[128] | 630 | fatal_error("Unable to create an archival thread");
|
---|
| 631 | }
|
---|
[1] | 632 |
|
---|
[128] | 633 | log_msg(8, "Parent running");
|
---|
[2331] | 634 | mr_asprintf(title, "%s", tt);
|
---|
[128] | 635 | open_evalcall_form(title);
|
---|
[2331] | 636 | mr_free(title);
|
---|
| 637 |
|
---|
[128] | 638 | for (sleep(1); command[0] != '\0'; sleep(1)) {
|
---|
| 639 | pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
|
---|
[2339] | 640 | if (pcno < 0 || pcno > 100) {
|
---|
| 641 | log_msg(8, "Weird pc# %d", pcno);
|
---|
[128] | 642 | continue;
|
---|
| 643 | }
|
---|
| 644 | percentage = pcno * 100 / maxpc;
|
---|
| 645 | if (pcno <= 5 && last_pcno >= 40) {
|
---|
| 646 | close_evalcall_form();
|
---|
[2331] | 647 | mr_asprintf(title, "Verifying...");
|
---|
[128] | 648 | open_evalcall_form(title);
|
---|
[2331] | 649 | mr_free(title);
|
---|
[128] | 650 | }
|
---|
| 651 | if (counter++ >= 5) {
|
---|
| 652 | counter = 0;
|
---|
| 653 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 654 | }
|
---|
| 655 | last_pcno = pcno;
|
---|
| 656 | update_evalcall_form(percentage);
|
---|
[1] | 657 | }
|
---|
[2275] | 658 | mr_free(command);
|
---|
| 659 |
|
---|
[128] | 660 | log_file_end_to_screen(MONDO_LOGFILE, "");
|
---|
| 661 | close_evalcall_form();
|
---|
| 662 | pthread_join(childthread, (void *) (&pchild_result));
|
---|
| 663 | if (pchild_result) {
|
---|
| 664 | res = *pchild_result;
|
---|
| 665 | } else {
|
---|
| 666 | res = 666;
|
---|
[1] | 667 | }
|
---|
[128] | 668 | log_msg(3, "Parent res = %d", res);
|
---|
| 669 | return (res);
|
---|
[1] | 670 | }
|
---|
| 671 |
|
---|
| 672 |
|
---|
| 673 | /**
|
---|
[296] | 674 | * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
|
---|
| 675 | * @param input_fifo The ntfsclone file to read.
|
---|
[1] | 676 | * @param output_device Where to put the output.
|
---|
[296] | 677 | * @return The return value of ntfsclone (0 for success).
|
---|
[1] | 678 | */
|
---|
[296] | 679 | int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
|
---|
[1] | 680 | {
|
---|
| 681 | // RESTORE
|
---|
[296] | 682 | int res = -1;
|
---|
[2275] | 683 | char *command = NULL;
|
---|
[1] | 684 |
|
---|
[2331] | 685 | command = find_home_of_exe("ntfsclone");
|
---|
| 686 | if (!command) {
|
---|
| 687 | mr_free(command);
|
---|
[296] | 688 | fatal_error("ntfsclone not found");
|
---|
[128] | 689 | }
|
---|
[2331] | 690 | mr_free(command);
|
---|
| 691 |
|
---|
[2323] | 692 | mr_asprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
|
---|
[296] | 693 | res = run_program_and_log_output(command, 5);
|
---|
[2275] | 694 | mr_free(command);
|
---|
[128] | 695 | return (res);
|
---|
[1] | 696 | }
|
---|