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