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