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