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