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