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