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