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