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