source: branches/3.2/mondo/src/mondorestore/mondorestore.c @ 3469

Last change on this file since 3469 was 3469, checked in by Bruno Cornec, 5 years ago

Remove usage of a local use_star variable and use the one in bkpinfo instead

  • Property svn:keywords set to Id
File size: 94.9 KB
Line 
1/***************************************************************************
2$Id: mondorestore.c 3469 2015-09-11 15:34:21Z bruno $
3restores mondoarchive data
4The main file for mondorestore.
5***************************************************************************/
6
7/**************************************************************************
8 * #include statements                                                    *
9 **************************************************************************/
10#include <pthread.h>
11#include "my-stuff.h"
12#include "mr_mem.h"
13#include "../common/mondostructures.h"
14#include "../common/libmondo.h"
15#include "mr-externs.h"
16#include "mondorestore.h"
17#include "mondo-rstr-compare-EXT.h"
18#include "mondo-rstr-tools-EXT.h"
19#include <utime.h>
20
21extern void wait_until_software_raids_are_prepped(char *, int);
22
23extern void twenty_seconds_til_yikes(void);
24
25/* We don't have a cleanup function yet */
26void (*mr_cleanup)(void) = NULL;
27
28/* Reference to global bkpinfo */
29struct s_bkpinfo *bkpinfo;
30
31char which_restore_mode(void);
32
33/* For use in other programs (ex. XMondo) */
34#ifdef MONDORESTORE_MODULE
35#define main __mondorestore_main
36#define g_ISO_restore_mode __mondorestore_g_ISO_restore_mode
37#endif
38
39//static char cvsid[] = "$Id: mondorestore.c 3469 2015-09-11 15:34:21Z bruno $";
40
41/**************************************************************************
42 * Globals                                                                *
43 **************************************************************************/
44/*
45extern char *g_tmpfs_mountpt;   // declared in libmondo-tools.c
46*/
47extern bool g_text_mode;
48extern FILE *g_fprep;
49extern double g_kernel_version;
50extern int g_partition_table_locked_up;
51extern int g_noof_rows;
52
53extern int partition_everything(struct mountlist_itself *mountlist);
54extern int handle_incoming_parameters(int argc, char *argv[]);
55extern int mount_media();
56
57/**
58 * @name Restore-Time Globals
59 * @ingroup globalGroup
60 * @{
61 */
62
63/**
64 * If TRUE, then we're restoring from ISOs or an NFS server.
65 * If FALSE, then we're restoring from some kind of real media (tape, CD, etc.)
66 */
67bool g_ISO_restore_mode = FALSE;    /* are we in Iso Mode? */
68
69/* Whether we should fail immediately at first error */
70bool g_fail_immediately = FALSE;
71
72/**
73 * If TRUE, then we have had a successful "nuke" restore.
74 */
75bool g_I_have_just_nuked = FALSE;
76
77/**
78 * The location of 'biggielist.txt', containing the biggiefiles on the current archive set.
79 */
80char *g_biggielist_txt;
81
82/**
83 * The location of 'filelist.full', containing all files (<em>including biggiefiles</em>) on
84 * the current archive set.
85 */
86char *g_filelist_full;
87
88/**
89 * The location of a file containing a list of the devices that were archived
90 * as images, not as individual files.
91 */
92char *g_filelist_imagedevs;
93
94/**
95 * The location of a file containing a list of imagedevs to actually restore.
96 * @see g_filelist_imagedevs
97 */
98char *g_imagedevs_restthese;
99
100/**
101 * The location of 'mondorestore.cfg', containing the metadata
102 * information for this backup.
103 */
104char *g_mondo_cfg_file;
105
106/**
107 * The location of 'mountlist.txt', containing the information on the
108 * user's partitions and hard drives.
109 */
110char *g_mountlist_fname;
111
112/**
113 * Mondo's home directory during backup. Unused in mondorestore; included
114 * to avoid link errors.
115 */
116char *g_mondo_home;
117
118extern char *g_getfacl;
119extern char *g_getfattr;
120
121/* @} - end of "Restore-Time Globals" in globalGroup */
122
123
124
125extern int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived,
126                                 char direction);
127
128
129/**************************************************************************
130 * COMPAQ ProLiant Stuff:  needs some special help                        *
131**************************************************************************/
132
133/**
134 * The message to display if we detect that the user is using a Compaq ProLiant.
135 */
136#define COMPAQ_PROLIANTS_SUCK "Partition and format your disk using Compaq's disaster recovery CD. After you've done that, please reboot with your Mondo media in Interactive Mode."
137
138/**
139 * Determine whether @p mountlist contains a Compaq diagnostic partition.
140 * @param mountlist The mountlist to examine.
141 * @return TRUE if there's a Compaq diagnostic partition; FALSE if not.
142 * @ingroup restoreUtilityGroup
143 */
144bool
145partition_table_contains_Compaq_diagnostic_partition(struct mountlist_itself * mountlist) {
146
147int i;
148
149assert(mountlist != NULL);
150
151for (i = 0; i < mountlist->entries; i++) {
152    if (strstr(mountlist->el[i].format, "ompaq")) {
153        log_msg(2, "mountlist[%d] (%s) is %s (Compaq alert!)", i, mountlist->el[i].device, mountlist->el[i].format);
154
155        return (TRUE);
156    }
157}
158return (FALSE);
159}
160
161/**************************************************************************
162 *END_PARTITION_TABLE_CONTAINS_COMPAQ_DIAGNOSTIC_PARTITION                *
163 **************************************************************************/
164
165
166/**
167 * Allow the user to abort the backup if we find that there is a Compaq diagnostic partition.
168 * @note This function does not actually check for the presence of a Compaq partition.
169 * @ingroup restoreUtilityGroup
170 */
171void offer_to_abort_because_Compaq_ProLiants_suck(void)
172{
173    popup_and_OK(COMPAQ_PROLIANTS_SUCK);
174    if (ask_me_yes_or_no("Would you like to reboot and use your Compaq CD to prep your hard drive?"))
175    {
176        fatal_error("Aborting. Please reboot and prep your hard drive with your Compaq CD.");
177    }
178}
179
180/**************************************************************************
181 *END_OFFER_TO_ABORT_BECAUSE_COMPAQ_PROLIANTS_SUCK                        *
182 **************************************************************************/
183
184
185static void clean_blkid() {
186
187    char *tmp1 = NULL;
188
189    /* Clean up blkid cache file if they exist */
190    mr_asprintf(tmp1,"%s/etc/blkid.tab",bkpinfo->restore_path);
191    (void)unlink(tmp1);
192    paranoid_free(tmp1);
193    mr_asprintf(tmp1,"%s/etc/blkid.tab.old",bkpinfo->restore_path);
194    (void)unlink(tmp1);
195    paranoid_free(tmp1);
196}
197
198
199static void clean_multipathconf() {
200
201    char *tmp1 = NULL;
202    char *editor = NULL;
203
204    /* Clean up multiconf cache file if they exist */
205    mr_asprintf(tmp1,"%s/var/lib/multipath/bindings",bkpinfo->restore_path);
206    (void)unlink(tmp1);
207    paranoid_free(tmp1);
208
209    /* Edit multipath.conf if needed to adapt wwid */
210    mr_asprintf(tmp1,"%s/etc/multipath.conf", MNT_RESTORING);
211    if (does_file_exist(tmp1)) {
212        log_msg(2, "We may need to clean /etc/multipath.conf");
213    } else {
214        paranoid_free(tmp1);
215        return;
216    }
217    paranoid_free(tmp1);
218
219    if (bkpinfo->restore_mode != nuke) {
220        mr_asprintf(editor, "%s", find_my_editor());
221        mr_asprintf(tmp1,"chroot %s %s /etc/multipath.conf", MNT_RESTORING, editor);
222        popup_and_OK("You will now edit multipath.conf");
223        if (!g_text_mode) {
224            newtSuspend();
225        }
226        paranoid_system(tmp1);
227        if (!g_text_mode) {
228            newtResume();
229        }
230        paranoid_free(tmp1);
231        paranoid_free(editor);
232    } else {
233        log_to_screen("Non-interactive mode: no way to give you the keyboard so that you edit your multipath.conf. Hope it's OK");
234        log_msg(1,"Non-interactive mode: no way to give you the keyboard so that you edit your multipath.conf. Hope it's OK");
235    }
236}
237
238
239/**
240 * Restore biggiefile @p bigfileno from the currently mounted CD.
241 * @param bkpinfo The backup information structure. Fields used:
242 * - @c bkpinfo->backup_media_type
243 * - @c bkpinfo->restore_path
244 * @param bigfileno The biggiefile number (starting from 0) to restore.
245 * @param filelist The node structure containing the list of files to restore.
246 * If the biggiefile is not in this list, it will be skipped (return value will
247 * still indicate success).
248 * @return 0 for success (or skip), nonzero for failure.
249 */
250int restore_a_biggiefile_from_CD(long bigfileno,
251                             struct s_node *filelist,
252                             char *pathname_of_last_file_restored)
253{
254    FILE *fin;
255    FILE *fout;
256    FILE *fbzip2;
257
258  /** malloc ***/
259    char *checksum = NULL;
260    char *outfile_fname = NULL;
261    char *tmp = NULL;
262    char *bzip2_command = NULL;
263    char *suffix = NULL;
264    char *sz_devfile = NULL;
265    char *bigblk;
266    char *mds = NULL;
267    int retval = 0;
268    int finished = FALSE;
269    long sliceno;
270    long siz;
271    char *ntfsprog_fifo = NULL;
272    char *file_to_openout = NULL;
273    struct s_filename_and_lstat_info biggiestruct;
274    struct utimbuf the_utime_buf, *ubuf;
275    bool use_ntfsprog_hack = FALSE;
276    pid_t pid;
277    int res = 0;
278    int old_loglevel;
279    struct s_node *node;
280
281    old_loglevel = g_loglevel;
282    ubuf = &the_utime_buf;
283    assert(bkpinfo != NULL);
284
285    pathname_of_last_file_restored[0] = '\0';
286    if (!(bigblk = malloc(TAPE_BLOCK_SIZE))) {
287        fatal_error("Cannot malloc bigblk");
288    }
289
290    if (!(fin = fopen(slice_fname(bigfileno, 0, ARCHIVES_PATH, ""), "r"))) {
291        log_to_screen("Cannot even open bigfile's info file");
292        return (1);
293    }
294
295    memset((void *) &biggiestruct, 0, sizeof(biggiestruct));
296    if (fread((void *) &biggiestruct, 1, sizeof(biggiestruct), fin) <
297        sizeof(biggiestruct)) {
298        log_msg(2, "Warning - unable to get biggiestruct of bigfile #%d",
299                bigfileno + 1);
300    }
301    paranoid_fclose(fin);
302
303    mr_asprintf(checksum, "%s", biggiestruct.checksum);
304    if (!checksum[0]) {
305        log_msg(3, "Warning - bigfile %ld does not have a checksum", bigfileno + 1);
306    }
307    mr_free(checksum);
308
309    if (!strncmp(biggiestruct.filename, "/dev/", 5))    // Whether NTFS or not :)
310    {
311        mr_asprintf(outfile_fname, "%s", biggiestruct.filename);
312    } else {
313        mr_asprintf(outfile_fname, "%s/%s", bkpinfo->restore_path, biggiestruct.filename);
314    }
315
316    /* skip file if we have a selective restore subset & it doesn't match */
317    if (filelist != NULL) {
318        node = find_string_at_node(filelist, biggiestruct.filename);
319        if (!node) {
320            log_msg(0, "Skipping %s (name isn't in filelist)", biggiestruct.filename);
321            pathname_of_last_file_restored[0] = '\0';
322            return (0);
323        } else if (!(node->selected)) {
324            log_msg(1, "Skipping %s (name isn't in biggielist subset)", biggiestruct.filename);
325            pathname_of_last_file_restored[0] = '\0';
326            return (0);
327        }
328    }
329
330    /* otherwise, continue */
331    log_msg(1, "DEFINITELY restoring %s", biggiestruct.filename);
332    if (biggiestruct.use_ntfsprog) {
333        if (strncmp(biggiestruct.filename, "/dev/", 5)) {
334            log_msg(1, "I was in error when I set biggiestruct.use_ntfsprog to TRUE.");
335            log_msg(1, "%s isn't even in /dev", biggiestruct.filename);
336            biggiestruct.use_ntfsprog = FALSE;
337        }
338    }
339
340    if (biggiestruct.use_ntfsprog)  // if it's an NTFS device
341    {
342        g_loglevel = 4;
343        use_ntfsprog_hack = TRUE;
344        log_msg(2, "Calling ntfsclone in background because %s is an NTFS /dev entry", outfile_fname);
345        mr_asprintf(sz_devfile, "/tmp/%d.%d.000", (int) (random() % 32768), (int) (random() % 32768));
346        mkfifo(sz_devfile, 0x770);
347        mr_asprintf(ntfsprog_fifo, "%s", sz_devfile);
348        mr_free(sz_devfile);
349        file_to_openout = ntfsprog_fifo;
350        switch (pid = fork()) {
351        case -1:
352            fatal_error("Fork failure");
353        case 0:
354            log_msg(3, "CHILD - fip - calling feed_outfrom_ntfsprog(%s, %s)", biggiestruct.filename, ntfsprog_fifo);
355            res = feed_outfrom_ntfsprog(biggiestruct.filename, ntfsprog_fifo);
356            mr_free(ntfsprog_fifo);
357            exit(res);
358            break;
359        default:
360            log_msg(3, "feed_into_ntfsprog() called in background --- pid=%ld", (long int) (pid));
361        }
362        mr_free(ntfsprog_fifo);
363    } else {
364        use_ntfsprog_hack = FALSE;
365        file_to_openout = outfile_fname;
366        if (!does_file_exist(outfile_fname))    // yes, it looks weird with the '!' but it's correct that way
367        {
368            make_hole_for_file(outfile_fname);
369        }
370    }
371
372    log_msg(2, "Reassembling big file %ld (%s)", bigfileno + 1, outfile_fname);
373
374    /*
375       last slice is zero-length and uncompressed; when we find it, we stop.
376       We DON'T wait until there are no more slices; if we did that,
377       We might stop at end of CD, not at last slice (which is 0-len and uncompd)
378     */
379
380    strncpy(pathname_of_last_file_restored, biggiestruct.filename, MAX_STR_LEN - 1);
381    pathname_of_last_file_restored[MAX_STR_LEN - 1] = '\0';
382
383    log_msg(3, "file_to_openout = %s", file_to_openout);
384    if (!(fout = fopen(file_to_openout, "w"))) {
385        log_to_screen("Cannot openout outfile_fname - hard disk full?");
386        return (1);
387    }
388    log_msg(3, "Opened out to %s", outfile_fname);  // CD/DVD --> mondorestore --> ntfsclone --> hard disk itself
389
390    for (sliceno = 1, finished = FALSE; !finished;) {
391        if (!does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "")) &&
392            !does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "lzo")) &&
393            !does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "gz")) &&
394            !does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "lzma")) &&
395            !does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "bz2"))) {
396            log_msg(3, "Cannot find a data slice or terminator slice on CD %d", g_current_media_number);
397            g_current_media_number++;
398            mds = media_descriptor_string(bkpinfo->backup_media_type);
399            log_msg(2, "Asking for %s #%d so that I may read slice #%ld", mds, g_current_media_number, sliceno);
400            mr_free(mds);
401
402            log_to_screen("Restoring from %s #%d", mds, g_current_media_number);
403
404            insist_on_this_cd_number(g_current_media_number);
405            log_to_screen("Continuing to restore.");
406        } else {
407            mr_asprintf(tmp, "%s", slice_fname(bigfileno, sliceno, ARCHIVES_PATH, ""));
408            if (does_file_exist(tmp) && length_of_file(tmp) == 0) {
409                log_msg(2, "End of bigfile # %ld (slice %ld is the terminator)", bigfileno + 1, sliceno);
410                finished = TRUE;
411                mr_free(tmp);
412                continue;
413            } else {
414                if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "lzo"))) {
415                    mr_asprintf(bzip2_command, "lzop");
416                    mr_asprintf(suffix, "lzo");
417                } else
418                    if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "gz"))) {
419                    mr_asprintf(bzip2_command, "gzip");
420                    mr_asprintf(suffix, "gz");
421                } else
422                    if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "lzma"))) {
423                    mr_asprintf(bzip2_command, "lzma");
424                    mr_asprintf(suffix, "lzma");
425                } else
426                    if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, "bz2"))) {
427                    mr_asprintf(bzip2_command, "bzip2");
428                    mr_asprintf(suffix, "bz2");
429                } else
430                    if (does_file_exist(slice_fname(bigfileno, sliceno, ARCHIVES_PATH, ""))) {
431                    mr_asprintf(bzip2_command, "");
432                    mr_asprintf(suffix, "");
433                } else {
434                    log_to_screen("OK, that's pretty fsck0red...");
435                    mr_free(tmp);
436                    return (1);
437                }
438            }
439            mr_free(tmp);
440            if (bzip2_command != NULL) {
441                mr_strcat(bzip2_command, " -dc %s 2>> %s", slice_fname(bigfileno, sliceno, ARCHIVES_PATH, suffix), MONDO_LOGFILE);
442            } else {
443                mr_asprintf(bzip2_command, "cat %s 2>> %s", slice_fname(bigfileno, sliceno, ARCHIVES_PATH, suffix), MONDO_LOGFILE);
444            }
445            mr_free(suffix);
446
447            mds = media_descriptor_string(bkpinfo->backup_media_type);
448            mr_asprintf(tmp, "Working on %s #%d, file #%ld, slice #%ld    ", mds, g_current_media_number, bigfileno + 1, sliceno);
449            mr_free(mds);
450            log_msg(2, tmp);
451
452            if (!g_text_mode) {
453                newtDrawRootText(0, g_noof_rows - 2, tmp);
454                newtRefresh();
455                strip_spaces(tmp);
456                update_progress_form(tmp);
457            }
458            mr_free(tmp);
459
460            if (!(fbzip2 = popen(bzip2_command, "r"))) {
461                fatal_error("Can't run popen command");
462            }
463            mr_free(bzip2_command);
464
465            while (!feof(fbzip2)) {
466                siz = fread(bigblk, 1, TAPE_BLOCK_SIZE, fbzip2);
467                if (siz > 0) {
468                    siz = fwrite(bigblk, 1, siz, fout);
469                }
470            }
471            paranoid_pclose(fbzip2);
472
473
474            sliceno++;
475            g_current_progress++;
476        }
477    }
478    paranoid_fclose(fout);
479    g_loglevel = old_loglevel;
480
481    if (use_ntfsprog_hack) {
482        log_msg(3, "Waiting for ntfsclone to finish");
483        mr_asprintf(tmp, " ps | grep \" ntfsclone \" | grep -v grep > /dev/null 2> /dev/null");
484        while (system(tmp) == 0) {
485            sleep(1);
486        }
487        mr_free(tmp);
488        log_it("OK, ntfsclone has really finished");
489    }
490
491    if (strcmp(outfile_fname, "/dev/null")) {
492        if (chown(outfile_fname, biggiestruct.properties.st_uid, biggiestruct.properties.st_gid)) {
493            // FIXME
494        }
495        chmod(outfile_fname, biggiestruct.properties.st_mode);
496        ubuf->actime = biggiestruct.properties.st_atime;
497        ubuf->modtime = biggiestruct.properties.st_mtime;
498        utime(outfile_fname, ubuf);
499    }
500    mr_free(outfile_fname);
501    paranoid_free(bigblk);
502
503    return (retval);
504}
505
506/**************************************************************************
507 *END_ RESTORE_A_BIGGIEFILE_FROM_CD                                       *
508 **************************************************************************/
509
510
511/**
512 * Restore all biggiefiles from all media in this CD backup.
513 * The CD with the last afioball should be currently mounted.
514 * @param bkpinfo The backup information structure. @c backup_media_type is the
515 * only field used in this function.
516 * @param filelist The node structure containing the list of files to be
517 * restored. If a prospective biggiefile is not in this list, it will be ignored.
518 * @return 0 for success, nonzero for failure.
519 */
520int restore_all_biggiefiles_from_CD(struct s_node *filelist) {
521
522    int retval = 0;
523    int res = 0;
524    long noof_biggiefiles, bigfileno = 0, total_slices;
525  /** malloc **/
526    char *tmp = NULL;
527    char *tmp1 = NULL;
528    char *mds = NULL;
529    bool just_changed_cds = FALSE;
530    char *xattr_fname = NULL;
531    char *acl_fname = NULL;
532    char *biggies_whose_EXATs_we_should_set = NULL; // EXtended ATtributes
533    char *pathname_of_last_biggie_restored;
534    FILE *fbw = NULL;
535
536    malloc_string(pathname_of_last_biggie_restored);
537    malloc_string(tmp);
538    assert(bkpinfo != NULL);
539
540    mr_asprintf(biggies_whose_EXATs_we_should_set, "%s/biggies-whose-EXATs-we-should-set", bkpinfo->tmpdir);
541    if (!(fbw = fopen(biggies_whose_EXATs_we_should_set, "w"))) {
542        log_msg(1, "Warning - cannot openout %s", biggies_whose_EXATs_we_should_set);
543    }
544
545    read_cfg_var(g_mondo_cfg_file, "total-slices", tmp);
546    total_slices = atol(tmp);
547    mr_free(tmp);
548
549    mr_asprintf(tmp1, "Reassembling large files      ");
550    mvaddstr_and_log_it(g_currentY, 0, tmp1);
551    mr_free(tmp1);
552
553    if (length_of_file(BIGGIELIST) < 6) {
554        log_msg(1, "OK, no biggielist; not restoring biggiefiles");
555        return (0);
556    }
557    noof_biggiefiles = count_lines_in_file(BIGGIELIST);
558    if (noof_biggiefiles <= 0) {
559        log_msg(2, "OK, no biggiefiles in biggielist; not restoring biggiefiles");
560        return (0);
561    }
562    log_msg(2, "OK, there are %ld biggiefiles in the archives", noof_biggiefiles);
563
564    open_progress_form("Reassembling large files",
565                       "I am now reassembling all the large files.",
566                       "Please wait. This may take some time.",
567                       "", total_slices);
568    for (bigfileno = 0 ; bigfileno < noof_biggiefiles ;) {
569        log_msg(2, "Thinking about restoring bigfile %ld", bigfileno + 1);
570        if (!does_file_exist(slice_fname(bigfileno, 0, ARCHIVES_PATH, ""))) {
571            log_msg(3, "...but its first slice isn't on this CD. Perhaps this was a selective restore?");
572            mds = media_descriptor_string(bkpinfo->backup_media_type);
573            log_msg(3, "Cannot find bigfile #%ld 's first slice on %s #%d", bigfileno + 1, mds, g_current_media_number);
574            log_msg(3, "Slicename would have been %s",
575                    slice_fname(bigfileno, 0, ARCHIVES_PATH, ""));
576            // I'm not positive 'just_changed_cds' is even necessary...
577            if (just_changed_cds) {
578                just_changed_cds = FALSE;
579                log_msg(3, "I'll continue to scan this CD for bigfiles to be restored.");
580            } else if (does_file_exist(MNT_CDROM "/archives/NOT-THE-LAST")) {
581                insist_on_this_cd_number(++g_current_media_number);
582                log_to_screen("Restoring from %s #%d", mds, g_current_media_number);
583                just_changed_cds = TRUE;
584            } else {
585                /* That big file doesn't exist, but the followings may */
586                /* So we need to continue looping */
587                log_msg(2, "There was no bigfile #%ld. That's OK.", bigfileno + 1);
588                log_msg(2, "I'm going to stop restoring bigfiles now.");
589                retval++;
590                bigfileno++;
591            }
592            mr_free(mds);
593        } else {
594            just_changed_cds = FALSE;
595            mr_asprintf(tmp1, "Restoring big file %ld", bigfileno + 1);
596            update_progress_form(tmp1);
597            mr_free(tmp1);
598            res = restore_a_biggiefile_from_CD(bigfileno, filelist, pathname_of_last_biggie_restored);
599            log_it("%s",pathname_of_last_biggie_restored);
600            if (fbw && pathname_of_last_biggie_restored[0]) {
601                fprintf(fbw, "%s\n", pathname_of_last_biggie_restored);
602            }
603            retval += res;
604            bigfileno++;
605
606        }
607    }
608
609    if (fbw) {
610        fclose(fbw);
611        if (g_getfattr) {
612            mr_asprintf(xattr_fname, XATTR_BIGGLST_FNAME_RAW_SZ, ARCHIVES_PATH);
613            if (length_of_file(xattr_fname) > 0) {
614                set_fattr_list(biggies_whose_EXATs_we_should_set, xattr_fname);
615            }
616            mr_free(xattr_fname);
617        }
618        if (g_getfacl) {
619            mr_asprintf(acl_fname, ACL_BIGGLST_FNAME_RAW_SZ, ARCHIVES_PATH);
620            if (length_of_file(acl_fname) > 0) {
621                set_acl_list(biggies_whose_EXATs_we_should_set, acl_fname);
622            }
623            mr_free(acl_fname);
624        }
625    }
626    mr_free(biggies_whose_EXATs_we_should_set);
627
628    if (does_file_exist("/PAUSE")) {
629        popup_and_OK("Press ENTER to go on. Delete /PAUSE to stop these pauses.");
630    }
631    close_progress_form();
632    if (retval) {
633        mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
634    } else {
635        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
636    }
637    paranoid_free(pathname_of_last_biggie_restored);
638    return (retval);
639}
640
641/**************************************************************************
642 *END_RESTORE_ALL_BIGGIFILES_FROM_CD                                      *
643 **************************************************************************/
644
645
646
647/**
648 * Restore @p tarball_fname from CD.
649 * @param tarball_fname The filename of the tarball to restore (in /mnt/cdrom).
650 * This will be used unmodified.
651 * @param current_tarball_number The number (starting from 0) of the fileset
652 * we're restoring now.
653 * @param filelist The node structure containing the list of files to be
654 * restored. If no file in the afioball is in this list, afio will still be
655 * called, but nothing will be written.
656 * @return 0 for success, nonzero for failure.
657 */
658int
659restore_a_tarball_from_CD(char *tarball_fname,
660                          long current_tarball_number,
661                          struct s_node *filelist)
662{
663    int retval = 0;
664    int res;
665    char *p;
666
667  /** malloc **/
668    char *command = NULL;
669    char *tmp = NULL;
670    char *filelist_name = NULL;
671    char *filelist_subset_fname = NULL;
672    char *executable = NULL;
673    char *temp_log = NULL;
674    long matches = 0;
675    char *xattr_fname = NULL;
676    char *acl_fname = NULL;
677
678    assert_string_is_neither_NULL_nor_zerolength(tarball_fname);
679    assert(bkpinfo != NULL);
680
681    log_msg(5, "Entering");
682    if (strstr(tarball_fname, ".star")) {
683        bkpinfo->use_star = TRUE;
684    } else {
685        bkpinfo->use_star = FALSE;
686    }
687    mr_asprintf(command, "mkdir -p %s/tmp", MNT_RESTORING);
688    run_program_and_log_output(command, 9);
689    paranoid_free(command);
690
691    mr_asprintf(filelist_name, MNT_CDROM "/archives/filelist.%ld", current_tarball_number);
692    if (length_of_file(filelist_name) <= 2) {
693        log_msg(2, "There are _zero_ files in filelist '%s'", filelist_name);
694        log_msg(2, "This is a bit silly (ask dev-team to fix mondo_makefilelist, please)");
695        log_msg(2, "but it's non-critical. It's cosmetic. Don't worry about it.");
696        retval = 0;
697        mr_free(filelist_name);
698        log_msg(5, "Leaving");
699        return(0);
700    }
701    if (count_lines_in_file(filelist_name) <= 0 || length_of_file(tarball_fname) <= 0) {
702        log_msg(3, "length_of_file(%s) = %llu", tarball_fname, length_of_file(tarball_fname));
703        log_msg(3, "count_lines_in_file(%s) = %llu", tarball_fname, count_lines_in_file(tarball_fname));
704        log_to_screen("Unable to restore fileset #%ld (CD I/O error)", current_tarball_number);
705        mr_free(filelist_name);
706        log_msg(5, "Leaving");
707        return(1);
708    }
709
710    if (filelist) {
711        mr_asprintf(filelist_subset_fname, "/tmp/filelist-subset-%ld.tmp", current_tarball_number);
712        if ((matches =
713             save_filelist_entries_in_common(filelist_name, filelist,
714                                             filelist_subset_fname,
715                                             bkpinfo->use_star)) <= 0) {
716            log_msg(1, "Skipping fileset %ld", current_tarball_number);
717        } else {
718            log_msg(3, "Saved fileset %ld's subset to %s", current_tarball_number, filelist_subset_fname);
719        }
720        log_to_screen("Tarball #%ld --- %ld matches", current_tarball_number, matches);
721    }
722    mr_free(filelist_name);
723
724    if (filelist == NULL || matches > 0) {
725        if (g_getfattr) {
726            mr_asprintf(xattr_fname, XATTR_LIST_FNAME_RAW_SZ, MNT_CDROM "/archives", current_tarball_number);
727        }
728        if (g_getfacl) {
729            mr_asprintf(acl_fname, ACL_LIST_FNAME_RAW_SZ, MNT_CDROM "/archives", current_tarball_number);
730        }
731        if (strstr(tarball_fname, ".bz2")) {
732            mr_asprintf(executable, "bzip2");
733        } else if (strstr(tarball_fname, ".lzma")) {
734            mr_asprintf(executable, "lzma");
735        } else if (strstr(tarball_fname, ".gz")) {
736            mr_asprintf(executable, "gzip");
737        } else if (strstr(tarball_fname, ".lzo")) {
738            mr_asprintf(executable, "lzop");
739        }
740        if (bkpinfo->compression_level == 0) {
741            mr_asprintf(executable, "%s", "");
742        } else {
743            if (executable) {
744                mr_asprintf(tmp, "which %s > /dev/null 2> /dev/null", executable);
745                res = run_program_and_log_output(tmp, FALSE);
746                mr_free(tmp);
747   
748                if (res) {
749                    log_to_screen("(compare_a_tarball) Compression program %s not found - oh no!", executable);
750                    paranoid_MR_finish(1);
751                }
752                tmp = executable;
753                mr_asprintf(executable, "-P %s -Z", tmp);
754                mr_free(tmp);
755            }
756        }
757#ifdef __FreeBSD__
758#define BUFSIZE 512
759#else
760#define BUFSIZE (1024L*1024L)/TAPE_BLOCK_SIZE
761#endif
762
763        if (bkpinfo->use_star) {
764            mr_asprintf(command, "star -x -force-remove -sparse -U " STAR_ACL_SZ " file=%s", tarball_fname);
765            if (strstr(tarball_fname, ".bz2")) {
766                mr_strcat(command, " -bz");
767            }
768        } else {
769            if (! executable) {
770                log_msg(2, "No executable, this shouldn't happen !");
771            } else {
772                if (filelist_subset_fname != NULL) {
773                    mr_asprintf(command, "afio -i -M 8m -b %ld -c %ld %s -w '%s' %s", TAPE_BLOCK_SIZE, BUFSIZE, executable, filelist_subset_fname, tarball_fname);
774                } else {
775                    mr_asprintf(command, "afio -i -b %ld -c %ld -M 8m %s %s", TAPE_BLOCK_SIZE, BUFSIZE, executable, tarball_fname);
776                }
777            }
778        }
779        mr_free(executable);
780
781#undef BUFSIZE
782        mr_asprintf(temp_log, "/tmp/%d.%d", (int) (random() % 32768), (int) (random() % 32768));
783
784        mr_strcat(command, " 2>> %s >> %s", temp_log, temp_log);
785        log_msg(1, "command = '%s'", command);
786        unlink(temp_log);
787        res = system(command);
788        if (res) {
789            p = strstr(command, "-acl ");
790            if (p) {
791                p[0] = p[1] = p[2] = p[3] = ' ';
792                log_msg(1, "new command = '%s'", command);
793                res = system(command);
794            }
795        }
796        paranoid_free(command);
797
798        if (res && length_of_file(temp_log) < 5) {
799            res = 0;
800        }
801
802        if (! bkpinfo->use_star) {
803            if (g_getfattr) {
804                log_msg(1, "Setting fattr list %s", xattr_fname);
805                if (length_of_file(xattr_fname) > 0) {
806                    res = set_fattr_list(filelist_subset_fname, xattr_fname);
807                    if (res) {
808                        log_to_screen("Errors occurred while setting extended attributes");
809                    } else {
810                        log_msg(1, "I set xattr OK");
811                    }
812                    retval += res;
813                }
814            }
815            if (g_getfacl) {
816                log_msg(1, "Setting acl list %s", acl_fname);
817                if (length_of_file(acl_fname) > 0) {
818                    res = set_acl_list(filelist_subset_fname, acl_fname);
819                    if (res) {
820                        log_to_screen("Errors occurred while setting access control lists");
821                    } else {
822                        log_msg(1, "I set ACL OK");
823                    }
824                    retval += res;
825                }
826            }
827        } else {
828            retval = res;
829        }
830        // Be verbose for star
831        if (retval || bkpinfo->use_star) {
832            mr_asprintf(command, "cat %s >> %s", temp_log, MONDO_LOGFILE);
833            paranoid_system(command);
834            paranoid_free(command);
835
836            if (retval) {
837                log_msg(2, "Errors occurred while processing fileset #%d", current_tarball_number);
838            }
839        } else {
840            log_msg(2, "Fileset #%d processed OK", current_tarball_number);
841        }
842        unlink(temp_log);
843        mr_free(temp_log);
844    }
845    if (does_file_exist("/PAUSE")) {
846        popup_and_OK
847            ("Press ENTER to go on. Delete /PAUSE to stop these pauses.");
848    }
849    unlink(filelist_subset_fname);
850    mr_free(filelist_subset_fname);
851    if (g_getfattr) {
852        unlink(xattr_fname);
853        mr_free(xattr_fname);
854    }
855    if (g_getfacl) {
856        unlink(acl_fname);
857        mr_free(acl_fname);
858    }
859
860    log_msg(5, "Leaving");
861    return (retval);
862}
863
864/**************************************************************************
865 *END_RESTORE_A_TARBALL_FROM_CD                                           *
866 **************************************************************************/
867
868
869
870/**
871 * Restore all afioballs from all CDs in the backup.
872 * The first CD should be inserted (if not, it will be asked for).
873 * @param bkpinfo The backup information structure. @c backup_media_type is the
874 * only field used in @e this function.
875 * @param filelist The node structure containing the list of files to be
876 * restored. If no file in some particular afioball is in this list, afio will
877 * still be called for that fileset, but nothing will be written.
878 * @return 0 for success, or the number of filesets that failed.
879 */
880int
881restore_all_tarballs_from_CD(struct s_node *filelist)
882{
883    int retval = 0;
884    int res;
885    int attempts;
886    long current_tarball_number = 0L;
887    long max_val;
888  /**malloc ***/
889    char *mds = NULL;
890    char *tmp = NULL;
891    char *tmp1 = NULL;
892    char *tarball_fname = NULL;
893    char *progress_str = NULL;
894
895    assert(bkpinfo != NULL);
896
897    malloc_string(tmp);
898    mvaddstr_and_log_it(g_currentY, 0, "Restoring from archives");
899    log_msg(2, "Insisting on 1st media, so that I can have a look at LAST-FILELIST-NUMBER");
900    if (g_current_media_number != 1) {
901        log_msg(3, "OK, that's jacked up.");
902        g_current_media_number = 1;
903    }
904    insist_on_this_cd_number(g_current_media_number);
905    read_cfg_var(g_mondo_cfg_file, "last-filelist-number", tmp);
906    max_val = atol(tmp) + 1;
907    paranoid_free(tmp);
908
909    mds = media_descriptor_string(bkpinfo->backup_media_type);
910    mr_asprintf(progress_str, "Restoring from %s #%d", mds, g_current_media_number);
911
912    log_to_screen(progress_str);
913    open_progress_form("Restoring from archives",
914                       "Restoring data from the archives.",
915                       "Please wait. This may take some time.",
916                       progress_str, max_val);
917    for (;;) {
918        insist_on_this_cd_number(g_current_media_number);
919        update_progress_form(progress_str);
920        mr_free(progress_str);
921
922        mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.bz2", current_tarball_number);
923        if (!does_file_exist(tarball_fname)) {
924            mr_free(tarball_fname);
925            mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.gz", current_tarball_number);
926        }
927        if (!does_file_exist(tarball_fname)) {
928            mr_free(tarball_fname);
929            mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.lzma", current_tarball_number);
930        }
931        if (!does_file_exist(tarball_fname)) {
932            mr_free(tarball_fname);
933            mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.lzo", current_tarball_number);
934        }
935        if (!does_file_exist(tarball_fname)) {
936            mr_free(tarball_fname);
937            mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.", current_tarball_number);
938        }
939        if (!does_file_exist(tarball_fname)) {
940            mr_free(tarball_fname);
941            mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.star.bz2", current_tarball_number);
942        }
943        if (!does_file_exist(tarball_fname)) {
944            mr_free(tarball_fname);
945            mr_asprintf(tarball_fname, MNT_CDROM "/archives/%ld.star.", current_tarball_number);
946        }
947        if (!does_file_exist(tarball_fname)) {
948            if (current_tarball_number == 0) {
949                log_to_screen
950                    ("No tarballs. Strange. Maybe you only backed up freakin' big files?");
951                mr_free(tarball_fname);
952                return (0);
953            }
954            if (!does_file_exist(MNT_CDROM "/archives/NOT-THE-LAST")
955                || system("find " MNT_CDROM
956                          "/archives/slice* > /dev/null 2> /dev/null") ==
957                0) {
958                break;
959            }
960            g_current_media_number++;
961            mr_asprintf(progress_str, "Restoring from %s #%d", media_descriptor_string(bkpinfo->backup_media_type), g_current_media_number);
962            log_to_screen(progress_str);
963        } else {
964            mr_asprintf(progress_str, "Restoring from fileset #%ld on %s #%d", current_tarball_number, mds, g_current_media_number);
965            for (res = 999, attempts = 0; attempts < 3 && res != 0; attempts++) {
966                res = restore_a_tarball_from_CD(tarball_fname, current_tarball_number, filelist);
967            }
968            mr_asprintf(tmp1, "%s #%d, fileset #%ld - restore ", mds, g_current_media_number, current_tarball_number);
969            if (res) {
970                mr_strcat(tmp1, "reported errors");
971            } else if (attempts > 1) {
972                mr_strcat(tmp1, "succeeded");
973            } else {
974                mr_strcat(tmp1, "succeeded");
975            }
976            if (attempts > 1) {
977                mr_strcat(tmp1, " (%d attempts) - review logs", attempts);
978            }
979            if (attempts > 1) {
980                log_to_screen(tmp1);
981            }
982            mr_free(tmp1);
983
984            retval += res;
985            current_tarball_number++;
986            g_current_progress++;
987        }
988        mr_free(tarball_fname);
989
990        /* Now we need to umount the current media to have the next mounted by insist_on_this_cd_number */
991        /*   run_program_and_log_output("umount " MNT_CDROM, FALSE); */
992    }
993    mr_free(mds);
994    mr_free(progress_str);
995
996    close_progress_form();
997    if (retval) {
998        mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
999    } else {
1000        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1001    }
1002
1003    return (retval);
1004}
1005
1006/**************************************************************************
1007 *END_RESTORE_ALL_TARBALLS_FROM_CD                                        *
1008 **************************************************************************/
1009
1010
1011/**
1012 * Restore a biggiefile from the currently opened stream.
1013 * @param bkpinfo The backup information structure. Fields used:
1014 * - @c bkpinfo->restore_path
1015 * - @c bkpinfo->zip_exe
1016 * @param orig_bf_fname The original filename of the biggiefile.
1017 * @param biggiefile_number The number of the biggiefile (starting from 0).
1018 * @param orig_checksum Unused.
1019 * @param biggiefile_size Unused.
1020 * @param filelist The node structure containing the list of files to be restored.
1021 * If @p orig_bf_fname is not in the list, it will be ignored.
1022 * @return 0 for success (or skip), nonzero for failure.
1023 * @bug orig_checksum and biggiefile_size are unused (except to check that they are non-NULL).
1024 */
1025int restore_a_biggiefile_from_stream(char *orig_bf_fname, long biggiefile_number, char *orig_checksum,  //UNUSED
1026                                     long long biggiefile_size, //UNUSED
1027                                     struct s_node *filelist,
1028                                     int use_ntfsprog,
1029                                     char *pathname_of_last_file_restored)
1030{
1031    FILE *pout;
1032    FILE *fin;
1033
1034  /** mallocs ********/
1035    char *tmp = NULL;
1036    char *tmp1 = NULL;
1037    char *command = NULL;
1038    char *outfile_fname = NULL;
1039    char *sz_devfile = NULL;
1040    char *ntfsprog_fifo = NULL;
1041    char *file_to_openout = NULL;
1042
1043    struct s_node *node;
1044
1045    int old_loglevel;
1046    long current_slice_number = 0;
1047    int retval = 0;
1048    int res = 0;
1049    int ctrl_chr = '\0';
1050    long long slice_siz;
1051    bool dummy_restore = FALSE;
1052    bool use_ntfsprog_hack = FALSE;
1053    pid_t pid;
1054    struct s_filename_and_lstat_info biggiestruct;
1055    struct utimbuf the_utime_buf, *ubuf;
1056    ubuf = &the_utime_buf;
1057
1058    malloc_string(tmp);
1059    old_loglevel = g_loglevel;
1060    assert(bkpinfo != NULL);
1061    assert(orig_bf_fname != NULL);
1062    assert(orig_checksum != NULL);
1063
1064    pathname_of_last_file_restored[0] = '\0';
1065    if (use_ntfsprog == BLK_START_A_PIHBIGGIE) {
1066        use_ntfsprog = 1;
1067        log_msg(1, "%s --- pih=YES", orig_bf_fname);
1068    } else if (use_ntfsprog == BLK_START_A_NORMBIGGIE) {
1069        use_ntfsprog = 0;
1070        log_msg(1, "%s --- pih=NO", orig_bf_fname);
1071    } else {
1072        use_ntfsprog = 0;
1073        log_msg(1, "%s --- pih=NO (weird marker though)", orig_bf_fname);
1074    }
1075
1076    strncpy(pathname_of_last_file_restored, orig_bf_fname,
1077            MAX_STR_LEN - 1);
1078    pathname_of_last_file_restored[MAX_STR_LEN - 1] = '\0';
1079
1080    /* open out to biggiefile to be restored (or /dev/null if biggiefile is not to be restored) */
1081
1082    if (filelist != NULL) {
1083        node = find_string_at_node(filelist, orig_bf_fname);
1084        if (!node) {
1085            dummy_restore = TRUE;
1086            log_msg(1,
1087                    "Skipping big file %ld (%s) - not in biggielist subset",
1088                    biggiefile_number + 1, orig_bf_fname);
1089            pathname_of_last_file_restored[0] = '\0';
1090        } else if (!(node->selected)) {
1091            dummy_restore = TRUE;
1092            log_msg(1, "Skipping %s (name isn't in biggielist subset)",
1093                    orig_bf_fname);
1094            pathname_of_last_file_restored[0] = '\0';
1095        }
1096    }
1097
1098    if (use_ntfsprog) {
1099        if (strncmp(orig_bf_fname, "/dev/", 5)) {
1100            log_msg(1,
1101                    "I was in error when I set use_ntfsprog to TRUE.");
1102            log_msg(1, "%s isn't even in /dev", orig_bf_fname);
1103            use_ntfsprog = FALSE;
1104        }
1105    }
1106
1107    if (use_ntfsprog) {
1108        g_loglevel = 4;
1109        mr_asprintf(outfile_fname, "%s", orig_bf_fname);
1110        use_ntfsprog_hack = TRUE;
1111        log_msg(2, "Calling ntfsclone in background because %s is a /dev entry", outfile_fname);
1112        mr_asprintf(sz_devfile, "%s/%d.%d.000", bkpinfo->tmpdir, (int) (random() % 32768), (int) (random() % 32768));
1113        mkfifo(sz_devfile, 0x770);
1114        mr_asprintf(ntfsprog_fifo, "%s", sz_devfile);
1115        mr_free(sz_devfile);
1116
1117        file_to_openout = ntfsprog_fifo;
1118        switch (pid = fork()) {
1119        case -1:
1120            fatal_error("Fork failure");
1121        case 0:
1122            log_msg(3, "CHILD - fip - calling feed_outfrom_ntfsprog(%s, %s)", outfile_fname, ntfsprog_fifo);
1123            res = feed_outfrom_ntfsprog(outfile_fname, ntfsprog_fifo);
1124            mr_free(ntfsprog_fifo);
1125            exit(res);
1126            break;
1127        default:
1128            log_msg(3, "feed_into_ntfsprog() called in background --- pid=%ld", (long int) (pid));
1129        }
1130        mr_free(ntfsprog_fifo);
1131    } else {
1132        if (!strncmp(orig_bf_fname, "/dev/", 5))    {
1133            // non-NTFS partition
1134            mr_asprintf(outfile_fname, "%s", orig_bf_fname);
1135        } else {
1136            // biggiefile
1137            mr_asprintf(outfile_fname, "%s/%s", bkpinfo->restore_path, orig_bf_fname);
1138        }
1139        use_ntfsprog_hack = FALSE;
1140        file_to_openout = outfile_fname;
1141        if (!does_file_exist(outfile_fname))    // yes, it looks weird with the '!' but it's correct that way
1142        {
1143            make_hole_for_file(outfile_fname);
1144        }
1145        log_msg(2, "Reassembling big file %ld (%s)", biggiefile_number + 1, orig_bf_fname);
1146    }
1147
1148    if (dummy_restore) {
1149        mr_free(outfile_fname);
1150        mr_asprintf(outfile_fname, "/dev/null");
1151    }
1152
1153    if (!bkpinfo->zip_exe[0]) {
1154        mr_asprintf(command, "cat > \"%s\"", file_to_openout);
1155    } else {
1156        mr_asprintf(command, "%s -dc > \"%s\" 2>> %s", bkpinfo->zip_exe, file_to_openout, MONDO_LOGFILE);
1157        if (strcmp(bkpinfo->zip_exe, "gzip") == 0) {
1158            /* Ignore SIGPIPE for gzip as it causes errors on big files
1159             * Cf: http://trac.mondorescue.org/ticket/244 */
1160            signal(SIGPIPE,SIG_IGN);
1161        }
1162    }
1163    log_msg(3, "Pipe command = '%s'", command);
1164
1165    /* restore biggiefile, one slice at a time */
1166    if (!(pout = popen(command, "w"))) {
1167        fatal_error("Cannot pipe out");
1168    }
1169    mr_free(command);
1170
1171    for (res = read_header_block_from_stream(&slice_siz, tmp, &ctrl_chr);
1172         ctrl_chr != BLK_STOP_A_BIGGIE;
1173         res = read_header_block_from_stream(&slice_siz, tmp, &ctrl_chr)) {
1174        if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1175            wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1176        }
1177        log_msg(2, "Working on file #%ld, slice #%ld    ", biggiefile_number + 1, current_slice_number);
1178        if (!g_text_mode) {
1179            newtDrawRootText(0, g_noof_rows - 2, tmp);
1180            newtRefresh();
1181        }
1182        strip_spaces(tmp);
1183        update_progress_form(tmp);
1184        if (current_slice_number == 0) {
1185            res =
1186                read_file_from_stream_to_file("/tmp/biggie-blah.txt",
1187                                              slice_siz);
1188            if (!(fin = fopen("/tmp/biggie-blah.txt", "r"))) {
1189                log_OS_error("blah blah");
1190            } else {
1191                if (fread
1192                    ((void *) &biggiestruct, 1, sizeof(biggiestruct),
1193                     fin) < sizeof(biggiestruct)) {
1194                    log_msg(2,
1195                            "Warning - unable to get biggiestruct of bigfile #%d",
1196                            biggiefile_number + 1);
1197                }
1198                paranoid_fclose(fin);
1199            }
1200        } else {
1201            res =
1202                read_file_from_stream_to_stream(pout, slice_siz);
1203        }
1204        retval += res;
1205        res = read_header_block_from_stream(&slice_siz, tmp, &ctrl_chr);
1206        if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
1207            wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
1208        }
1209        current_slice_number++;
1210        g_current_progress++;
1211    }
1212    paranoid_pclose(pout);
1213
1214    if (bkpinfo->zip_exe[0]) {
1215        if (strcmp(bkpinfo->zip_exe, "gzip") == 0) {
1216            /* Re-enable SIGPIPE for gzip */
1217            signal(SIGPIPE, terminate_daemon);
1218        }
1219    }
1220
1221    log_msg(1, "pathname_of_last_file_restored is now %s",
1222            pathname_of_last_file_restored);
1223
1224    if (use_ntfsprog_hack) {
1225        log_msg(3, "Waiting for ntfsclone to finish");
1226        mr_asprintf(tmp1, " ps | grep \" ntfsclone \" | grep -v grep > /dev/null 2> /dev/null");
1227        while (system(tmp1) == 0) {
1228            sleep(1);
1229        }
1230        mr_free(tmp1);
1231        log_msg(3, "OK, ntfsclone has really finished");
1232    }
1233
1234    log_msg(3, "biggiestruct.filename = %s", biggiestruct.filename);
1235    log_msg(3, "biggiestruct.checksum = %s", biggiestruct.checksum);
1236    if (strcmp(outfile_fname, "/dev/null")) {
1237        chmod(outfile_fname, biggiestruct.properties.st_mode);
1238        if (chown(outfile_fname, biggiestruct.properties.st_uid, biggiestruct.properties.st_gid)) {
1239            // FIXME
1240        }
1241        ubuf->actime = biggiestruct.properties.st_atime;
1242        ubuf->modtime = biggiestruct.properties.st_mtime;
1243        utime(outfile_fname, ubuf);
1244    }
1245    mr_free(outfile_fname);
1246
1247    paranoid_free(tmp);
1248    g_loglevel = old_loglevel;
1249    return (retval);
1250}
1251
1252/**************************************************************************
1253 *END_RESTORE_A_BIGGIEFILE_FROM_STREAM                                    *
1254 **************************************************************************/
1255
1256
1257
1258/**
1259 * Restore all biggiefiles from the currently opened stream.
1260 * @param bkpinfo The backup information structure. Passed to other functions.
1261 * @param filelist The node structure containing the list of files to be
1262 * restored. If a prospective biggiefile is not in the list, it will be ignored.
1263 * @return 0 for success, or the number of biggiefiles that failed.
1264 */
1265int
1266restore_all_biggiefiles_from_stream(struct s_node *filelist)
1267{
1268    long noof_biggiefiles;
1269    long current_bigfile_number = 0;
1270    long total_slices;
1271
1272    int retval = 0;
1273    int res = 0;
1274    int ctrl_chr;
1275
1276  /** malloc add ****/
1277    char *tmp = NULL;
1278    char *tmp1 = NULL;
1279    char *biggie_fname;
1280    char *biggie_cksum;
1281    char *xattr_fname = NULL;
1282    char *acl_fname = NULL;
1283    char *p;
1284    char *pathname_of_last_biggie_restored;
1285    char *biggies_whose_EXATs_we_should_set = NULL; // EXtended ATtributes
1286    long long biggie_size;
1287    FILE *fbw = NULL;
1288
1289    malloc_string(tmp);
1290    malloc_string(biggie_fname);
1291    malloc_string(biggie_cksum);
1292    malloc_string(pathname_of_last_biggie_restored);
1293    assert(bkpinfo != NULL);
1294
1295    read_cfg_var(g_mondo_cfg_file, "total-slices", tmp);
1296
1297    total_slices = atol(tmp);
1298
1299    if (g_getfattr) {
1300        mr_asprintf(xattr_fname, XATTR_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
1301    }
1302    if (g_getfacl) {
1303        mr_asprintf(acl_fname, ACL_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
1304    }
1305    mr_asprintf(tmp1, "Reassembling large files      ");
1306    mvaddstr_and_log_it(g_currentY, 0, tmp1);
1307    mr_free(tmp1);
1308
1309    mr_asprintf(biggies_whose_EXATs_we_should_set, "%s/biggies-whose-EXATs-we-should-set", bkpinfo->tmpdir);
1310    if (!(fbw = fopen(biggies_whose_EXATs_we_should_set, "w"))) {
1311        log_msg(1, "Warning - cannot openout %s", biggies_whose_EXATs_we_should_set);
1312    }
1313
1314    // get xattr and acl files if they're there
1315    res = read_header_block_from_stream(&biggie_size, biggie_fname, &ctrl_chr);
1316    if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
1317        res = read_EXAT_files_from_tape(&biggie_size, biggie_fname, &ctrl_chr, xattr_fname, acl_fname);
1318    }
1319
1320    noof_biggiefiles = atol(biggie_fname);
1321    log_msg(2, "OK, there are %ld biggiefiles in the archives", noof_biggiefiles);
1322    open_progress_form("Reassembling large files",
1323                       "I am now reassembling all the large files.",
1324                       "Please wait. This may take some time.",
1325                       "", total_slices);
1326
1327    for (res =
1328         read_header_block_from_stream(&biggie_size, biggie_fname,
1329                                       &ctrl_chr);
1330         ctrl_chr != BLK_STOP_BIGGIEFILES;
1331         res =
1332         read_header_block_from_stream(&biggie_size, biggie_fname,
1333                                       &ctrl_chr)) {
1334        if (ctrl_chr != BLK_START_A_NORMBIGGIE
1335            && ctrl_chr != BLK_START_A_PIHBIGGIE) {
1336            wrong_marker(BLK_START_A_NORMBIGGIE, ctrl_chr);
1337        }
1338        p = strrchr(biggie_fname, '/');
1339        if (!p) {
1340            p = biggie_fname;
1341        } else {
1342            p++;
1343        }
1344        mr_asprintf(tmp1, "Restoring big file %ld (%lld K)", current_bigfile_number + 1, biggie_size / 1024);
1345        update_progress_form(tmp1);
1346        mr_free(tmp1);
1347        res = restore_a_biggiefile_from_stream(biggie_fname,
1348                                               current_bigfile_number,
1349                                               biggie_cksum,
1350                                               biggie_size,
1351                                               filelist, ctrl_chr,
1352                                               pathname_of_last_biggie_restored);
1353        log_msg(1, "I believe I have restored %s",
1354                pathname_of_last_biggie_restored);
1355        if (fbw && pathname_of_last_biggie_restored[0]) {
1356            fprintf(fbw, "%s\n", pathname_of_last_biggie_restored);
1357        }
1358        retval += res;
1359        current_bigfile_number++;
1360
1361    }
1362    if (current_bigfile_number != noof_biggiefiles
1363        && noof_biggiefiles != 0) {
1364        log_msg(1, "Warning - bigfileno=%ld but noof_biggiefiles=%ld", current_bigfile_number, noof_biggiefiles);
1365    } else {
1366        log_msg(1, "%ld biggiefiles in biggielist.txt; %ld biggiefiles processed today.", noof_biggiefiles, current_bigfile_number);
1367    }
1368
1369    if (fbw) {
1370        fclose(fbw);
1371        if (length_of_file(biggies_whose_EXATs_we_should_set) > 2) {
1372            log_it("Setting biggie-EXATs");
1373            if (g_getfattr) {
1374                if (length_of_file(xattr_fname) > 0) {
1375                    log_msg(1, "set_fattr_List(%s,%s)", biggies_whose_EXATs_we_should_set, xattr_fname);
1376                    set_fattr_list(biggies_whose_EXATs_we_should_set, xattr_fname);
1377                }
1378            }
1379            if (g_getfacl) {
1380                if (length_of_file(acl_fname) > 0) {
1381                    log_msg(1, "set_acl_list(%s,%s)", biggies_whose_EXATs_we_should_set, acl_fname);
1382                    set_acl_list(biggies_whose_EXATs_we_should_set, acl_fname);
1383                }
1384            }
1385        } else {
1386            log_it("No biggiefiles selected. So, no biggie-EXATs to set.");
1387        }
1388    }
1389    mr_free(xattr_fname);
1390    mr_free(acl_fname);
1391    mr_free(biggies_whose_EXATs_we_should_set);
1392
1393    if (does_file_exist("/PAUSE")) {
1394        popup_and_OK
1395            ("Press ENTER to go on. Delete /PAUSE to stop these pauses.");
1396    }
1397
1398    close_progress_form();
1399    if (retval) {
1400        mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
1401    } else {
1402        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1403    }
1404    paranoid_free(pathname_of_last_biggie_restored);
1405    paranoid_free(biggie_fname);
1406    paranoid_free(biggie_cksum);
1407    paranoid_free(tmp);
1408    return (retval);
1409}
1410
1411/**************************************************************************
1412 *END_RESTORE_ALL_BIGGIEFILES_FROM_STREAM                                 *
1413 **************************************************************************/
1414
1415
1416
1417
1418/**
1419 * Restore a tarball from the currently opened stream.
1420 * @param bkpinfo The backup information structure. Fields used:
1421 * - @c bkpinfo->backup_media_type
1422 * - @c bkpinfo->media_device
1423 * - @c bkpinfo->zip_exe
1424 * @param tarball_fname The filename of the afioball to restore.
1425 * @param current_tarball_number The number (starting from 0) of the fileset
1426 * we're restoring now.
1427 * @param filelist The node structure containing the list of files to be
1428 * restored. If no file in the afioball is in this list, afio will still be
1429 * called, but nothing will be written.
1430 * @param size The size (in @b bytes) of the afioball.
1431 * @return 0 for success, nonzero for failure.
1432 */
1433int
1434restore_a_tarball_from_stream(char *tarball_fname,
1435                              long current_tarball_number,
1436                              struct s_node *filelist,
1437                              long long size, char *xattr_fname,
1438                              char *acl_fname)
1439{
1440    int retval = 0;
1441    int res = 0;
1442
1443  /** malloc add ***/
1444    char *mds = NULL;
1445    char *command = NULL;
1446    char *afio_fname = NULL;
1447    char *filelist_fname = NULL;
1448    char *filelist_subset_fname = NULL;
1449    char *executable = NULL;
1450    long matches = 0;
1451    bool restore_this_fileset = FALSE;
1452
1453    assert(bkpinfo != NULL);
1454    assert_string_is_neither_NULL_nor_zerolength(tarball_fname);
1455
1456    /* to do it with a file... */
1457    mds = media_descriptor_string(bkpinfo->backup_media_type);
1458    log_msg(2, "Restoring from fileset #%ld (%ld KB) on %s #%d",
1459            current_tarball_number, (long) size >> 10, mds, g_current_media_number);
1460    mr_free(mds);
1461
1462    run_program_and_log_output("mkdir -p " MNT_RESTORING "/tmp", FALSE);
1463
1464  /****************************************************************************
1465   * Use RAMDISK's /tmp; saves time; oh wait, it's too small                  *
1466   * Well, pipe from tape to afio, then; oh wait, can't do that either: bug   *
1467   * in afio or someting; oh darn.. OK, use tmpfs :-)                         *
1468   ****************************************************************************/
1469    mr_asprintf(afio_fname, "/tmp/tmpfs/archive.tmp.%ld", current_tarball_number);
1470    mr_asprintf(filelist_fname, "%s/filelist.%ld", bkpinfo->tmpdir, current_tarball_number);
1471    mr_asprintf(filelist_subset_fname, "%s/filelist-subset-%ld.tmp", bkpinfo->tmpdir, current_tarball_number);
1472
1473    res = read_file_from_stream_to_file(afio_fname, size);
1474    if (strstr(tarball_fname, ".star")) {
1475        bkpinfo->use_star = TRUE;
1476    } else {
1477        bkpinfo->use_star = FALSE;
1478    }
1479    if (res) {
1480        log_msg(1, "Warning - error reading afioball from tape");
1481    }
1482    if (bkpinfo->compression_level == 0) {
1483        mr_asprintf(executable, "%s", "");
1484    } else {
1485        if (bkpinfo->use_star) {
1486            mr_asprintf(executable, "%s", " -bz");
1487        } else {
1488            mr_asprintf(executable, "-P %s -Z", bkpinfo->zip_exe);
1489        }
1490    }
1491
1492    if (!filelist)              // if unconditional restore then restore entire fileset
1493    {
1494        restore_this_fileset = TRUE;
1495    } else                      // If restoring selectively then get TOC from tarball
1496    {
1497        if (bkpinfo->use_star) {
1498            mr_asprintf(command, "star -sparse -t file=%s %s", afio_fname, executable);
1499        } else {
1500            mr_asprintf(command, "afio -t -M 8m -b %ld %s %s", TAPE_BLOCK_SIZE, executable, afio_fname);
1501        }
1502        mr_strcat(command, " > %s 2>> %s", filelist_fname, MONDO_LOGFILE);
1503        log_msg(1, "command = %s", command);
1504        if (system(command)) {
1505            log_msg(4, "Warning - error occurred while retrieving TOC");
1506        }
1507        mr_free(command);
1508
1509        if ((matches =
1510             save_filelist_entries_in_common(filelist_fname, filelist,
1511                                             filelist_subset_fname,
1512                                             bkpinfo->use_star))
1513            <= 0 || length_of_file(filelist_subset_fname) < 2) {
1514            if (length_of_file(filelist_subset_fname) < 2) {
1515                log_msg(1, "No matches found in fileset %ld",
1516                        current_tarball_number);
1517            }
1518            log_msg(2, "Skipping fileset %ld", current_tarball_number);
1519            restore_this_fileset = FALSE;
1520        } else {
1521            log_msg(5, "%ld matches. Saved fileset %ld's subset to %s",
1522                    matches, current_tarball_number,
1523                    filelist_subset_fname);
1524            restore_this_fileset = TRUE;
1525        }
1526    }
1527
1528// Concoct the call to star/afio to restore files
1529    if (strstr(tarball_fname, ".star.")) {
1530        // star
1531        mr_asprintf(command, "star -sparse -x file=%s %s", afio_fname, executable);
1532        if (filelist) {
1533            mr_strcat(command, " list=%s", filelist_subset_fname);
1534        }
1535    } else {
1536        // afio
1537        mr_asprintf(command, "afio -i -M 8m -b %ld %s", TAPE_BLOCK_SIZE, executable);
1538        if (filelist) {
1539            mr_strcat(command, " -w %s", filelist_subset_fname);
1540        }
1541        mr_strcat(command, " %s", afio_fname);
1542    }
1543    mr_strcat(command, " 2>> %s", MONDO_LOGFILE);
1544    mr_free(executable);
1545
1546// Call if IF there are files to restore (selectively/unconditionally)
1547    if (restore_this_fileset) {
1548        log_msg(1, "Calling command='%s'", command);
1549        paranoid_system(command);
1550
1551        if (g_getfattr) {
1552            log_it("Restoring xattr stuff");
1553            res = set_fattr_list(filelist_subset_fname, xattr_fname);
1554            if (res) {
1555                log_msg(1, "Errors occurred while setting xattr");
1556            } else {
1557                log_msg(1, "I set xattr OK");
1558            }
1559            retval += res;
1560        }
1561
1562        if (g_getfacl) {
1563            log_it("Restoring acl stuff");
1564            res = set_acl_list(filelist_subset_fname, acl_fname);
1565            if (res) {
1566                log_msg(1, "Errors occurred while setting ACL");
1567            } else {
1568                log_msg(1, "I set ACL OK");
1569            }
1570            retval += res;
1571        }
1572
1573    } else {
1574        log_msg(1, "NOT CALLING '%s'", command);
1575    }
1576    mr_free(command);
1577
1578    if (does_file_exist("/PAUSE") && current_tarball_number >= 50) {
1579        log_to_screen("Paused after set %ld", current_tarball_number);
1580        popup_and_OK("Pausing. Press ENTER to continue.");
1581    }
1582
1583    unlink(filelist_subset_fname);
1584    mr_free(filelist_subset_fname);
1585    unlink(filelist_fname);
1586    mr_free(filelist_fname);
1587    unlink(afio_fname);
1588    mr_free(afio_fname);
1589
1590    return (retval);
1591}
1592
1593/**************************************************************************
1594 *END_RESTORE_A_TARBALL_FROM_STREAM                                       *
1595 **************************************************************************/
1596
1597
1598
1599
1600
1601/**
1602 * Restore all afioballs from the currently opened tape stream.
1603 * @param bkpinfo The backup information structure. Fields used:
1604 * - @c bkpinfo->backup_media_type
1605 * - @c bkpinfo->restore_path
1606 * @param filelist The node structure containing the list of files to be
1607 * restored. If no file in an afioball is in this list, afio will still be
1608 * called for that fileset, but nothing will be written.
1609 * @return 0 for success, or the number of filesets that failed.
1610 */
1611int restore_all_tarballs_from_stream(struct s_node *filelist)
1612{
1613    int retval = 0;
1614    int res;
1615    long current_afioball_number = 0;
1616    int ctrl_chr;
1617    long max_val /*, total_noof_files */ ;
1618
1619  /** malloc **/
1620    char *tmp = NULL;
1621    char *mds = NULL;
1622    char *progress_str = NULL;
1623    char *tmp_fname;
1624    char *xattr_fname = NULL;
1625    char *acl_fname = NULL;
1626
1627    long long tmp_size;
1628
1629    malloc_string(tmp);
1630    malloc_string(tmp_fname);
1631    assert(bkpinfo != NULL);
1632    mvaddstr_and_log_it(g_currentY, 0, "Restoring from archives");
1633    read_cfg_var(g_mondo_cfg_file, "last-filelist-number", tmp);
1634    max_val = atol(tmp) + 1;
1635
1636    if (chdir(bkpinfo->restore_path)) { /* I don't know why this is needed _here_ but it seems to be. -HR, 02/04/2002 */
1637        //FIXME
1638    }
1639
1640    run_program_and_log_output("pwd", 5);
1641
1642    mr_asprintf(progress_str, "Restoring from media #%d", g_current_media_number);
1643    log_to_screen(progress_str);
1644    open_progress_form("Restoring from archives",
1645                       "Restoring data from the archives.",
1646                       "Please wait. This may take some time.",
1647                       progress_str, max_val);
1648
1649    log_msg(3, "hey");
1650
1651    res = read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
1652    if (res) {
1653        log_msg(2, "Warning - error reading afioball from tape");
1654    }
1655    retval += res;
1656    if (ctrl_chr != BLK_START_AFIOBALLS) {
1657        wrong_marker(BLK_START_AFIOBALLS, ctrl_chr);
1658    }
1659    log_msg(2, "ho");
1660    res = read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
1661    while (ctrl_chr != BLK_STOP_AFIOBALLS) {
1662        update_progress_form(progress_str);
1663        if (g_getfattr) {
1664            mr_asprintf(xattr_fname, "%s/xattr-subset-%ld.tmp", bkpinfo->tmpdir, current_afioball_number);
1665            unlink(xattr_fname);
1666        }
1667        if (g_getfacl) {
1668            mr_asprintf(acl_fname, "%s/acl-subset-%ld.tmp", bkpinfo->tmpdir, current_afioball_number);
1669            unlink(acl_fname);
1670        }
1671        if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
1672            log_it("Reading EXAT files from tape");
1673            res = read_EXAT_files_from_tape(&tmp_size, tmp_fname, &ctrl_chr, xattr_fname, acl_fname);
1674        }
1675        if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1676            wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1677        }
1678        log_msg(4, "Restoring from fileset #%ld (name=%s, size=%ld K)", current_afioball_number, tmp_fname, (long) tmp_size >> 10);
1679        res = restore_a_tarball_from_stream(tmp_fname, current_afioball_number, filelist, tmp_size, xattr_fname, acl_fname);
1680        retval += res;
1681        if (res) {
1682            log_to_screen("Fileset %ld - errors occurred", current_afioball_number);
1683        }
1684        res =
1685            read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
1686        if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
1687            wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
1688        }
1689
1690        current_afioball_number++;
1691        g_current_progress++;
1692        mds = media_descriptor_string(bkpinfo->backup_media_type),
1693
1694        mr_free(progress_str);
1695        mr_asprintf(progress_str, "Restoring from fileset #%ld on %s #%d", current_afioball_number, mds, g_current_media_number);
1696        mr_free(mds);
1697        res = read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
1698        if (g_getfattr) {
1699            unlink(xattr_fname);
1700        }
1701        if (g_getfacl) {
1702            unlink(acl_fname);
1703        }
1704    }                           // next
1705    mr_free(progress_str);
1706    if (g_getfattr) {
1707        mr_free(xattr_fname);
1708    }
1709    if (g_getfacl) {
1710        mr_free(acl_fname);
1711    }
1712
1713    log_msg(1, "All done with afioballs");
1714    close_progress_form();
1715    if (retval) {
1716        mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
1717    } else {
1718        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1719    }
1720    paranoid_free(tmp);
1721    paranoid_free(tmp_fname);
1722    return (retval);
1723}
1724
1725/**************************************************************************
1726 *END_ RESTORE_ALL_TARBALLS_FROM_STREAM                                   *
1727 **************************************************************************/
1728
1729/* @} - end of LLrestoreGroup */
1730
1731
1732/**
1733 * Restore all files in @p filelist.
1734 * @param bkpinfo The backup information structure. Most fields are used.
1735 * @param filelist The node structure containing the list of files to be
1736 * restored.
1737 * @return 0 for success, or the number of afioballs and biggiefiles that failed.
1738 * @ingroup restoreGroup
1739 */
1740int restore_everything(struct s_node *filelist)
1741{
1742    int resA;
1743    int resB;
1744
1745  /** mallco ***/
1746    char *cwd;
1747    char *newpath;
1748    char *tmp = NULL;
1749    assert(bkpinfo != NULL);
1750
1751    malloc_string(cwd);
1752    malloc_string(newpath);
1753    log_msg(2, "restore_everything() --- starting");
1754    g_current_media_number = 1;
1755    if (getcwd(cwd, MAX_STR_LEN - 1)) {
1756        // FIXME
1757    }
1758    mr_asprintf(tmp, "mkdir -p %s", bkpinfo->restore_path);
1759    run_program_and_log_output(tmp, FALSE);
1760    mr_free(tmp);
1761
1762    log_msg(1, "Changing dir to %s", bkpinfo->restore_path);
1763    if (chdir(bkpinfo->restore_path)) {
1764        //FIXME
1765    }
1766    if (getcwd(newpath, MAX_STR_LEN - 1)) {
1767        // FIXME
1768    }
1769    log_msg(1, "path is now %s", newpath);
1770    log_msg(1, "restoring everything");
1771    if (!find_home_of_exe("petris") && !g_text_mode) {
1772        newtDrawRootText(0, g_noof_rows - 2,
1773                         "Press ALT-<left cursor> twice to play Petris :-) ");
1774        newtRefresh();
1775    }
1776    mvaddstr_and_log_it(g_currentY, 0, "Preparing to read your archives");
1777    mount_media();
1778    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1779        mvaddstr_and_log_it(g_currentY++, 0,
1780                            "Restoring OS and data from streaming media");
1781        if (bkpinfo->backup_media_type == cdstream) {
1782            openin_cdstream();
1783        } else {
1784            assert_string_is_neither_NULL_nor_zerolength(bkpinfo->media_device);
1785            openin_tape();
1786        }
1787        resA = restore_all_tarballs_from_stream(filelist);
1788        resB = restore_all_biggiefiles_from_stream(filelist);
1789        if (bkpinfo->backup_media_type == cdstream) {
1790            closein_cdstream();
1791        } else {
1792            closein_tape();
1793        }
1794    } else {
1795        mvaddstr_and_log_it(g_currentY++, 0,
1796                            "Restoring OS and data from CD/USB   ");
1797        resA = restore_all_tarballs_from_CD(filelist);
1798        resB = restore_all_biggiefiles_from_CD(filelist);
1799    }
1800    if (chdir(cwd)) {
1801        //FIXME
1802    }
1803    if (resA + resB) {
1804        log_to_screen("Errors occurred while data was being restored.");
1805    }
1806    if (length_of_file("/etc/raidtab") > 0) {
1807        log_msg(2, "Copying local raidtab to restored filesystem");
1808        run_program_and_log_output("cp -f /etc/raidtab " MNT_RESTORING
1809                                   "/etc/raidtab", FALSE);
1810    }
1811    kill_petris();
1812    log_msg(2, "restore_everything() --- leaving");
1813    paranoid_free(cwd);
1814    paranoid_free(newpath);
1815    return (resA + resB);
1816}
1817
1818/**************************************************************************
1819 *END_RESTORE_EVERYTHING                                                  *
1820 **************************************************************************/
1821
1822
1823/**
1824 * @addtogroup restoreGroup
1825 * @{
1826 */
1827/**
1828 * Restore the user's data, in a disaster recovery situation, prompting the
1829 * user about whether or not to do every step.
1830 * The user can edit the mountlist, choose files to restore, etc.
1831 * @param bkpinfo The backup information structure. Most fields are used.
1832 * @param mountlist The mountlist containing information about the user's partitions.
1833 * @param raidlist The raidlist to go with @p mountlist.
1834 * @return 0 for success, or the number of errors encountered.
1835 */
1836int interactive_mode(struct mountlist_itself *mountlist, struct raidlist_itself *raidlist)
1837{
1838    int retval = 0;
1839    int res;
1840    int ptn_errs = 0;
1841    int fmt_errs = 0;
1842
1843    bool done;
1844    bool restore_all;
1845
1846  /** needs malloc **********/
1847    char *tmp;
1848    char *tmp1 = NULL;
1849    char *fstab_fname = NULL;
1850    char *old_restpath = NULL;
1851
1852    struct s_node *filelist;
1853
1854    /* try to partition and format */
1855
1856    log_msg(2, "interactive_mode --- starting (great, assertions OK)");
1857
1858    malloc_string(tmp);
1859    assert(bkpinfo != NULL);
1860    assert(mountlist != NULL);
1861    assert(raidlist != NULL);
1862
1863    log_msg(2, "interactive_mode --- assertions OK");
1864
1865    if (g_text_mode) {
1866        if (!ask_me_yes_or_no("Interactive Mode + textonly = experimental! Proceed anyway?")) {
1867            fatal_error("Wise move.");
1868        }
1869    }
1870
1871    log_it("About to load config file");
1872    get_cfg_file_from_archive_or_bust();
1873    read_cfg_file_into_bkpinfo(g_mondo_cfg_file);
1874    log_it("Done loading config file; resizing ML");
1875
1876    mr_asprintf(tmp1, "%s", call_program_and_get_last_line_of_output("cat "CMDLINE));
1877    if (strstr(tmp1, "noresize")) {
1878        log_msg(1, "Not resizing mountlist.");
1879    } else {
1880        resize_mountlist_proportionately_to_suit_new_drives(mountlist);
1881    }
1882    mr_free(tmp1);
1883
1884    for (done = FALSE; !done;) {
1885        log_it("About to edit mountlist");
1886        if (g_text_mode) {
1887            save_mountlist_to_disk(mountlist, g_mountlist_fname);
1888            mr_asprintf(tmp1, "%s %s", find_my_editor(), g_mountlist_fname);
1889            res = system(tmp1);
1890            mr_free(tmp1);
1891            load_mountlist(mountlist, g_mountlist_fname);
1892        } else {
1893            res = edit_mountlist(g_mountlist_fname, mountlist, raidlist);
1894        }
1895        log_it("Finished editing mountlist");
1896        if (res) {
1897            paranoid_MR_finish(1);
1898        }
1899        log_msg(2, "Proceeding...");
1900        save_mountlist_to_disk(mountlist, g_mountlist_fname);
1901        save_raidlist_to_raidtab(raidlist, RAIDTAB_FNAME);
1902        mvaddstr_and_log_it(1, 30, "Restoring Interactively");
1903        if (bkpinfo->differential) {
1904            log_to_screen("Because this is a differential backup, disk");
1905            log_to_screen
1906                (" partitioning and formatting will not take place.");
1907            done = TRUE;
1908        } else {
1909            if (ask_me_yes_or_no
1910                ("Do you want to erase and partition your hard drives?")) {
1911                if (partition_table_contains_Compaq_diagnostic_partition
1912                    (mountlist)) {
1913                    offer_to_abort_because_Compaq_ProLiants_suck();
1914                    done = TRUE;
1915                } else {
1916                    twenty_seconds_til_yikes();
1917                    g_fprep = fopen("/tmp/prep.sh", "w");
1918                    ptn_errs = partition_everything(mountlist);
1919                    if (ptn_errs) {
1920                        log_to_screen
1921                            ("Warning. Errors occurred during disk partitioning.");
1922                    }
1923
1924                    fmt_errs = format_everything(mountlist, FALSE, raidlist);
1925                    if (!fmt_errs) {
1926                        log_to_screen
1927                            ("Errors during disk partitioning were handled OK.");
1928                        log_to_screen
1929                            ("Partitions were formatted OK despite those errors.");
1930                        ptn_errs = 0;
1931                    }
1932                    if (!ptn_errs && !fmt_errs) {
1933                        done = TRUE;
1934                    }
1935                }
1936                paranoid_fclose(g_fprep);
1937            } else {
1938                mvaddstr_and_log_it(g_currentY++, 0,
1939                                    "User opted not to partition the devices");
1940                if (ask_me_yes_or_no
1941                    ("Do you want to format your hard drives?")) {
1942                    fmt_errs = format_everything(mountlist, TRUE, raidlist);
1943                    if (!fmt_errs) {
1944                        done = TRUE;
1945                    }
1946                } else {
1947                    ptn_errs = fmt_errs = 0;
1948                    done = TRUE;
1949                }
1950            }
1951            if (fmt_errs) {
1952                mvaddstr_and_log_it(g_currentY++,
1953                                    0,
1954                                    "Errors occurred. Please repartition and format drives manually.");
1955                done = FALSE;
1956            }
1957            if (ptn_errs & !fmt_errs) {
1958                mvaddstr_and_log_it(g_currentY++,
1959                                    0,
1960                                    "Errors occurred during partitioning. Formatting, however, went OK.");
1961                done = TRUE;
1962            }
1963            if (!done) {
1964                if (!ask_me_yes_or_no("Re-edit the mountlist?")) {
1965                    retval++;
1966                    goto end_of_func;
1967                }
1968            }
1969        }
1970    }
1971
1972    /* mount */
1973    if (mount_all_devices(mountlist, TRUE)) {
1974        unmount_all_devices(mountlist);
1975        retval++;
1976        goto end_of_func;
1977    }
1978    /* restore */
1979    if ((restore_all = ask_me_yes_or_no("Do you want me to restore all of your data?"))) {
1980        log_msg(1, "Restoring all data");
1981        retval += restore_everything(NULL);
1982    } else if ((restore_all = ask_me_yes_or_no("Do you want me to restore _some_ of your data?"))) {
1983        mr_asprintf(old_restpath, "%s", bkpinfo->restore_path);
1984        for (done = FALSE; !done;) {
1985            unlink("/tmp/filelist.full");
1986            filelist = process_filelist_and_biggielist();
1987            /* Now you have /tmp/tmpfs/filelist.restore-these and /tmp/tmpfs/biggielist.restore-these;
1988               the former is a list of regular files; the latter, biggiefiles and imagedevs.
1989             */
1990            if (filelist) {
1991              gotos_suck:
1992                strcpy(tmp, old_restpath);
1993                // (NB: %s is where your filesystem is mounted now, by default)", MNT_RESTORING);
1994                if (popup_and_get_string("Restore path", "Restore files to where?", tmp, MAX_STR_LEN / 4)) {
1995                    if (!strcmp(tmp, "/")) {
1996                        if (!ask_me_yes_or_no("Are you sure?")) {
1997                            goto gotos_suck;
1998                        }
1999                        tmp[0] = '\0';  // so we restore to [blank]/file/name :)
2000                    }
2001                    strcpy(bkpinfo->restore_path, tmp);
2002                    log_msg(1, "Restoring subset");
2003                    retval += restore_everything(filelist);
2004                    free_filelist(filelist);
2005                } else {
2006                    strcpy(bkpinfo->restore_path, old_restpath);
2007                    free_filelist(filelist);
2008                }
2009                if (!ask_me_yes_or_no("Restore another subset of your backup?")) {
2010                    done = TRUE;
2011                }
2012            } else {
2013                done = TRUE;
2014            }
2015        }
2016        mr_free(old_restpath);
2017    } else {
2018        mvaddstr_and_log_it(g_currentY++, 0, "User opted not to restore any data.                                  ");
2019    }
2020    if (retval) {
2021        mvaddstr_and_log_it(g_currentY++, 0, "Errors occurred during the restore phase.            ");
2022    }
2023
2024    clean_multipathconf();
2025    if (ask_me_yes_or_no("Initialize the boot loader?")) {
2026        run_boot_loader(TRUE);
2027    } else {
2028        mvaddstr_and_log_it(g_currentY++,
2029                            0,
2030                            "User opted not to initialize the boot loader.");
2031    }
2032
2033    clean_blkid();
2034    protect_against_braindead_sysadmins();
2035    retval += unmount_all_devices(mountlist);
2036
2037    /*  if (restore_some || restore_all || */
2038    if (ask_me_yes_or_no
2039        ("Label/Identify your ext2/ext3/ext4 partitions if necessary?")) {
2040        mvaddstr_and_log_it(g_currentY, 0,
2041                            "Using tune2fs/tune4fs to identify your ext2,3,4 partitions");
2042        if (does_file_exist("/tmp/fstab.new")) {
2043            mr_asprintf(fstab_fname, "/tmp/fstab.new");
2044        } else {
2045            mr_asprintf(fstab_fname, "/tmp/fstab");
2046        }
2047        mr_asprintf(tmp1, "label-partitions-as-necessary %s < %s >> %s 2>> %s", g_mountlist_fname, fstab_fname, MONDO_LOGFILE, MONDO_LOGFILE);
2048        mr_free(fstab_fname);
2049
2050        res = system(tmp1);
2051        mr_free(tmp1);
2052        if (res) {
2053            log_to_screen("label-partitions-as-necessary returned an error");
2054            mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
2055        } else {
2056            mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2057        }
2058        retval += res;
2059    }
2060
2061    log_it("About to leave interactive_mode()");
2062    if (retval) {
2063        mvaddstr_and_log_it(g_currentY++,
2064                            0,
2065                            "Warning - errors occurred during the restore phase.");
2066    }
2067  end_of_func:
2068    paranoid_free(tmp);
2069    log_it("Leaving interactive_mode()");
2070    return (retval);
2071}
2072
2073/**************************************************************************
2074 *END_INTERACTIVE_MODE                                                    *
2075 **************************************************************************/
2076
2077
2078
2079/**
2080 * Restore the user's data automatically (no prompts), after a twenty-second
2081 * warning period.
2082 * @param bkpinfo The backup information structure. Most fields are used.
2083 * @param mountlist The mountlist containing information about the user's partitions.
2084 * @param raidlist The raidlist that goes with @p mountlist.
2085 * @return 0 for success, or the number of errors encountered.
2086 * @warning <b><i>THIS WILL ERASE ALL EXISTING DATA!</i></b>
2087 */
2088int nuke_mode(struct mountlist_itself *mountlist, struct raidlist_itself *raidlist)
2089{
2090    int retval = 0;
2091    int res = 0;
2092    char *tmp = NULL;
2093    char *tmp1 = NULL;
2094    char *flaws_str = NULL;
2095
2096    assert(bkpinfo != NULL);
2097    assert(mountlist != NULL);
2098    assert(raidlist != NULL);
2099
2100    log_msg(2, "nuke_mode --- starting");
2101
2102    get_cfg_file_from_archive_or_bust();
2103    load_mountlist(mountlist, g_mountlist_fname);   // in case read_cfg_file_into_bkpinfo updated the mountlist
2104#ifdef __FreeBSD__
2105    if (strstr(call_program_and_get_last_line_of_output("cat /tmp/cmdline"), "noresize"))
2106#else
2107    if (strstr(call_program_and_get_last_line_of_output("cat /proc/cmdline"), "noresize"))
2108#endif
2109    {
2110        log_msg(2, "Not resizing mountlist.");
2111    } else {
2112        resize_mountlist_proportionately_to_suit_new_drives(mountlist);
2113    }
2114    flaws_str = evaluate_mountlist(mountlist);
2115    if (flaws_str != NULL) {
2116        mr_asprintf(tmp, "Mountlist analyzed. Result: \"%s\" Switch to Interactive Mode?", flaws_str);
2117        mr_free(flaws_str);
2118        if (ask_me_yes_or_no(tmp)) {
2119            retval = interactive_mode(mountlist, raidlist);
2120            mr_free(tmp);
2121            goto after_the_nuke;
2122        } else {
2123            fatal_error("Nuke Mode aborted. ");
2124        }
2125    }
2126
2127    save_mountlist_to_disk(mountlist, g_mountlist_fname);
2128    mvaddstr_and_log_it(1, 30, "Restoring Automatically");
2129    if (bkpinfo->differential) {
2130        log_to_screen("Because this is a differential backup, disk");
2131        log_to_screen("partitioning and formatting will not take place.");
2132        res = 0;
2133    } else {
2134        if (partition_table_contains_Compaq_diagnostic_partition
2135            (mountlist)) {
2136            offer_to_abort_because_Compaq_ProLiants_suck();
2137        } else {
2138            twenty_seconds_til_yikes();
2139            g_fprep = fopen("/tmp/prep.sh", "w");
2140            mr_asprintf(tmp1, "%s", call_program_and_get_last_line_of_output("cat "CMDLINE));
2141            if (strstr(tmp1, "nopart")) {
2142                log_msg(2, "Not partitioning drives due to 'nopart' option.");
2143                res = 0;
2144            } else {
2145                res = partition_everything(mountlist);
2146                if (res) {
2147                    log_to_screen("Warning. Errors occurred during partitioning.");
2148                    res = 0;
2149                }
2150            }
2151            mr_free(tmp1);
2152
2153            retval += res;
2154            if (!res) {
2155                log_to_screen("Preparing to format your disk(s)");
2156                sleep(1);
2157                sync();
2158                log_to_screen("Please wait. This may take a few minutes.");
2159                res += format_everything(mountlist, FALSE, raidlist);
2160            }
2161            paranoid_fclose(g_fprep);
2162        }
2163    }
2164    retval += res;
2165    if (res) {
2166        mvaddstr_and_log_it(g_currentY++, 0, "Failed to partition and/or format your hard drives.");
2167
2168        if (ask_me_yes_or_no("Try in interactive mode instead?")) {
2169            retval = interactive_mode(mountlist, raidlist);
2170            goto after_the_nuke;
2171        } else
2172            if (!ask_me_yes_or_no("Would you like to try to proceed anyway?")) {
2173            return (retval);
2174        }
2175    }
2176    retval = mount_all_devices(mountlist, TRUE);
2177    if (retval) {
2178        unmount_all_devices(mountlist);
2179        log_to_screen("Unable to mount all partitions. Sorry, I cannot proceed.");
2180        return (retval);
2181    }
2182    log_it("Restoring everything");
2183    retval += restore_everything(NULL);
2184    clean_multipathconf();
2185    if (!run_boot_loader(FALSE)) {
2186        log_msg(1, "Great! Boot loader was installed. No need for msg at end.");
2187    }
2188    clean_blkid();
2189    protect_against_braindead_sysadmins();
2190    retval += unmount_all_devices(mountlist);
2191    mvaddstr_and_log_it(g_currentY, 0, "Using tune2fs/tune4fs to identify your ext2,3,4 partitions");
2192
2193    mr_asprintf(tmp1, "label-partitions-as-necessary %s < /tmp/fstab >> %s 2>> %s", "/tmp/mountlist.txt", MONDO_LOGFILE, MONDO_LOGFILE);
2194    res = run_program_and_log_output(tmp1, TRUE);
2195    mr_free(tmp1);
2196    if (res) {
2197        log_to_screen("label-partitions-as-necessary returned an error");
2198        mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
2199    } else {
2200        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2201    }
2202    retval += res;
2203
2204  after_the_nuke:
2205    mr_asprintf(tmp1, "%s", call_program_and_get_last_line_of_output("cat "CMDLINE));
2206    if (retval) {
2207        log_to_screen("Errors occurred during the nuke phase.");
2208    } else if (strstr(tmp1, "RESTORE")) {
2209        log_to_screen("PC was restored successfully. Thank you for using Mondo Rescue.");
2210        log_to_screen("Please visit our website at http://www.mondorescue.org for more information.");
2211    } else {
2212        mr_asprintf(tmp,"%s","Mondo has restored your system.\n\nPlease wait for the command prompt. Then remove the backup media and reboot.\n\nPlease visit our website at http://www.mondorescue.org for more information.");
2213        popup_and_OK(tmp);
2214        mr_free(tmp);
2215        log_to_screen("Mondo has restored your system. Please wait for the command prompt.");
2216        log_to_screen("Then remove the backup media and reboot.");
2217        log_to_screen("Please visit our website at http://www.mondorescue.org for more information.");
2218    }
2219    mr_free(tmp1);
2220
2221    g_I_have_just_nuked = TRUE;
2222    return (retval);
2223}
2224
2225/**************************************************************************
2226 *END_NUKE_MODE                                                           *
2227 **************************************************************************/
2228
2229
2230
2231/**
2232 * Allow the user to modify the mountlist before we partition & format their drives.
2233 * @param bkpinfo The backup information structure. @c disaster_recovery is the only field used.
2234 * @param mountlist The mountlist to let the user modify.
2235 * @param raidlist The raidlist that goes with @p mountlist.
2236 * @return 0 for success, nonzero for failure.
2237 * @ingroup restoreGuiGroup
2238 */
2239int let_user_edit_the_mountlist(struct mountlist_itself *mountlist,
2240                                struct raidlist_itself *raidlist)
2241{
2242    int retval = 0, res = 0;
2243
2244    log_msg(2, "let_user_edit_the_mountlist() --- starting");
2245
2246    assert(bkpinfo != NULL);
2247    assert(mountlist != NULL);
2248    assert(raidlist != NULL);
2249    if (!bkpinfo->disaster_recovery) {
2250        strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
2251        log_msg(2, "I guess you're testing edit_mountlist()");
2252    }
2253    if (!does_file_exist(g_mountlist_fname)) {
2254        log_to_screen(g_mountlist_fname);
2255        log_to_screen("does not exist");
2256        return (1);
2257    }
2258
2259    retval = load_mountlist(mountlist, g_mountlist_fname);
2260    load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2261    if (retval) {
2262        log_to_screen
2263            ("Warning - load_raidtab_into_raidlist returned an error");
2264    }
2265    res = edit_mountlist(g_mountlist_fname, mountlist, raidlist);
2266    if (res) {
2267        return (1);
2268    }
2269
2270    save_mountlist_to_disk(mountlist, g_mountlist_fname);
2271    save_raidlist_to_raidtab(raidlist, RAIDTAB_FNAME);
2272
2273    log_to_screen("I have finished editing the mountlist for you.");
2274
2275    return (retval);
2276}
2277
2278
2279
2280
2281
2282/**
2283 * Call interactive_mode(), nuke_mode(), or compare_mode() depending on the user's choice.
2284 * @param bkpinfo The backup information structure. Most fields are used.
2285 * @param mountlist The mountlist containing information about the user's partitions.
2286 * @param raidlist The raidlist to go with @p mountlist.
2287 * @return The return code from the mode function called.
2288 * @ingroup restoreGroup
2289 */
2290int
2291catchall_mode(struct mountlist_itself *mountlist,
2292              struct raidlist_itself *raidlist)
2293{
2294    char c;
2295    char *tmp = NULL;
2296    int retval = 0;
2297
2298    log_it("inside catchall");
2299    assert(bkpinfo != NULL);
2300    assert(mountlist != NULL);
2301    assert(raidlist != NULL);
2302    log_it("pre wrm");
2303    c = which_restore_mode();
2304    log_it("post wrm");
2305    if (c == 'I' || c == 'C') {
2306        interactively_obtain_media_parameters_from_user(FALSE);
2307    } else if (c == 'N') {
2308        // Auto mode nothing special to do
2309    } else {
2310        popup_and_OK("No restoring or comparing will take place today.");
2311        if (is_this_device_mounted("/mnt/cdrom")) {
2312            run_program_and_log_output("umount -d /mnt/cdrom", FALSE);
2313        }
2314        if (g_ISO_restore_mode) {
2315            mr_asprintf(tmp, "umount -d %s", bkpinfo->isodir);
2316            run_program_and_log_output(tmp, FALSE);
2317            mr_free(tmp);
2318        }
2319        paranoid_MR_finish(0);
2320    }
2321
2322    log_it("post int");
2323
2324    if (bkpinfo->backup_media_type == iso) {
2325        if (iso_fiddly_bits((c == 'N') ? TRUE : FALSE)) {
2326            log_msg(2,
2327                    "catchall_mode --- iso_fiddly_bits returned w/ error");
2328            return (1);
2329        } else {
2330            log_msg(2, "catchall_mode --- iso_fiddly_bits ok");
2331        }
2332    }
2333
2334    if (c == 'I') {
2335        log_msg(2, "IM selected");
2336        retval += interactive_mode(mountlist, raidlist);
2337    } else if (c == 'N') {
2338        log_msg(2, "NM selected");
2339        retval += nuke_mode(mountlist, raidlist);
2340    } else if (c == 'C') {
2341        log_msg(2, "CM selected");
2342        retval += compare_mode(mountlist, raidlist);
2343    }
2344    return (retval);
2345}
2346
2347/**************************************************************************
2348 *END_CATCHALL_MODE                                                      *
2349 **************************************************************************/
2350
2351/**************************************************************************
2352 *END_  EXTRACT_CONFIG_FILE_FROM_RAMDISK                                  *
2353 **************************************************************************/
2354
2355
2356/**
2357 * Locate an executable in the directory structure rooted at @p restg.
2358 * @param out_path Where to put the executable.
2359 * @param fname The basename of the executable.
2360 * @param restg The directory structure to look in.
2361 * @note If it could not be found in @p restg then @p fname is put in @p out_path.
2362 * @ingroup restoreUtilityGroup
2363 */
2364void
2365find_pathname_of_executable_preferably_in_RESTORING(char *out_path,
2366                                                    char *fname,
2367                                                    char *restg)
2368{
2369    assert(out_path != NULL);
2370    assert_string_is_neither_NULL_nor_zerolength(fname);
2371
2372    sprintf(out_path, "%s/sbin/%s", restg, fname);
2373    if (does_file_exist(out_path)) {
2374        sprintf(out_path, "%s/usr/sbin/%s", restg, fname);
2375        if (does_file_exist(out_path)) {
2376            sprintf(out_path, "%s/bin/%s", restg, fname);
2377            if (does_file_exist(out_path)) {
2378                sprintf(out_path, "%s/usr/bin/%s", restg, fname);
2379                if (does_file_exist(out_path)) {
2380                    strcpy(out_path, fname);
2381                }
2382            }
2383        }
2384    }
2385}
2386
2387/**************************************************************************
2388 *END_FIND_PATHNAME_OF_EXECUTABLE_PREFERABLY_IN_RESTORING                 *
2389 **************************************************************************/
2390
2391
2392/**
2393 * Run an arbitrary restore mode (prompt the user), but from ISO images
2394 * instead of real media.
2395 * @param mountlist The mountlist containing information about the user's partitions.
2396 * @param raidlist The raidlist that goes with @p mountlist.
2397 * @param nuke_me_please If TRUE, we plan to run Nuke Mode.
2398 * @return 0 for success, or the number of errors encountered.
2399 */
2400int
2401iso_mode(struct mountlist_itself *mountlist,
2402         struct raidlist_itself *raidlist, bool nuke_me_please)
2403{
2404    char c;
2405    int retval = 0;
2406
2407    assert(mountlist != NULL);
2408    assert(raidlist != NULL);
2409    if (iso_fiddly_bits(nuke_me_please)) {
2410        log_msg(1, "iso_mode --- returning w/ error");
2411        return (1);
2412    } else {
2413        c = which_restore_mode();
2414        if (c == 'I' || c == 'N' || c == 'C') {
2415            interactively_obtain_media_parameters_from_user(FALSE);
2416        }
2417        if (c == 'I') {
2418            retval += interactive_mode(mountlist, raidlist);
2419        } else if (c == 'N') {
2420            retval += nuke_mode(mountlist, raidlist);
2421        } else if (c == 'C') {
2422            retval += compare_mode(mountlist, raidlist);
2423        } else {
2424            log_to_screen("OK, I shan't restore/compare any files.");
2425        }
2426    }
2427    if (is_this_device_mounted(MNT_CDROM)) {
2428        paranoid_system("umount -d " MNT_CDROM);
2429    }
2430//  if (! already_mounted)
2431//    {
2432    if (system("umount -d /tmp/isodir 2> /dev/null")) {
2433        log_to_screen("WARNING - unable to unmount device where the ISO files are stored.");
2434    }
2435//    }
2436    return (retval);
2437}
2438
2439/**************************************************************************
2440 *END_ISO_MODE                                                            *
2441 **************************************************************************/
2442
2443
2444/*            MONDO - saving your systems since Feb 18th, 2000            */
2445
2446
2447
2448/**
2449 * Restore the user's data (or a subset of it) to the live filesystem.
2450 * This should not be called if we're booted from CD!
2451 * @param bkpinfo The backup information structure. Most fields are used.
2452 * @return 0 for success, or the number of errors encountered.
2453 */
2454int restore_to_live_filesystem()
2455{
2456    int retval = 0;
2457
2458    char *old_restpath = NULL;
2459
2460    struct mountlist_itself *mountlist = NULL;
2461    struct raidlist_itself *raidlist = NULL;
2462    struct s_node *filelist = NULL;
2463
2464    log_msg(1, "restore_to_live_filesystem() - starting");
2465    assert(bkpinfo != NULL);
2466    mountlist = malloc(sizeof(struct mountlist_itself));
2467    raidlist = malloc(sizeof(struct raidlist_itself));
2468    if (!mountlist || !raidlist) {
2469        fatal_error("Cannot malloc() mountlist and/or raidlist");
2470    }
2471
2472    strcpy(bkpinfo->restore_path, "/");
2473    if (!g_restoring_live_from_cd && !g_restoring_live_from_netfs) {
2474        popup_and_OK("Please insert tape/CD/USB Key, then hit 'OK' to continue.");
2475        sleep(1);
2476    }
2477    if (!g_restoring_live_from_netfs) {
2478        interactively_obtain_media_parameters_from_user(FALSE);
2479    }
2480    log_msg(2, "bkpinfo->media_device = %s", bkpinfo->media_device);
2481    if (!bkpinfo->media_device[0]) {
2482        log_msg(2, "Warning - failed to find media dev");
2483    }
2484
2485
2486    log_msg(2, "bkpinfo->isodir = %s", bkpinfo->isodir);
2487
2488    open_evalcall_form("Thinking...");
2489
2490    get_cfg_file_from_archive_or_bust();
2491    read_cfg_file_into_bkpinfo(g_mondo_cfg_file);
2492    load_mountlist(mountlist, g_mountlist_fname);   // in case read_cfg_file_into_bkpinfo
2493
2494    close_evalcall_form();
2495    retval = load_mountlist(mountlist, g_mountlist_fname);
2496    load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2497
2498    if (!g_restoring_live_from_netfs && (filelist = process_filelist_and_biggielist())) {
2499        save_filelist(filelist, "/tmp/selected-files.txt");
2500        mr_asprintf(old_restpath, "%s", bkpinfo->restore_path);
2501        if (popup_and_get_string("Restore path", "Restore files to where? ", bkpinfo->restore_path, MAX_STR_LEN / 4)) {
2502            log_it("Restoring everything");
2503            retval += restore_everything(filelist);
2504            free_filelist(filelist);
2505            strcpy(bkpinfo->restore_path, old_restpath);
2506        } else {
2507            free_filelist(filelist);
2508        }
2509        strcpy(bkpinfo->restore_path, old_restpath);
2510        mr_free(old_restpath);
2511    } else {
2512        if (filelist != NULL) {
2513            retval += restore_everything(NULL);
2514        }
2515    }
2516    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2517        log_msg(2, "Tape : I don't need to unmount or eject the CD-ROM.");
2518    } else {
2519        run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
2520        if (!bkpinfo->please_dont_eject) {
2521            eject_device(bkpinfo->media_device);
2522        }
2523    }
2524    run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
2525    if (!bkpinfo->please_dont_eject) {
2526        eject_device(bkpinfo->media_device);
2527    }
2528    free(mountlist);
2529    free(raidlist);
2530    return (retval);
2531}
2532
2533/**************************************************************************
2534 *END_RESTORE_TO_LIVE_FILESYSTEM                                          *
2535 **************************************************************************/
2536
2537/* @} - end of restoreGroup */
2538
2539
2540/**
2541 * Log a "don't panic" message to the logfile.
2542 */
2543void welcome_to_mondorestore()
2544{
2545    log_msg(0, "-------------- Mondo Restore v%s -------------", PACKAGE_VERSION);
2546    log_msg(0, "DON'T PANIC! Mondorestore logs almost everything, so please ");
2547    log_msg(0, "don't break out in a cold sweat just because you see a few  ");
2548    log_msg(0, "error messages in the log. Read them; analyze them; see if  ");
2549    log_msg(0, "they are significant; above all, verify your backups! Please");
2550    log_msg(0, "attach a compressed copy of this log to any e-mail you send ");
2551    log_msg(0, "to the Mondo mailing list when you are seeking technical    ");
2552    log_msg(0, "support. Without it, we can't help you.            - DevTeam");
2553    log_msg(0, "------------------------------------------------------------");
2554    log_msg(0, "BTW, despite (or perhaps because of) the wealth of messages,");
2555    log_msg(0, "some users are inclined to stop reading this log.  If Mondo ");
2556    log_msg(0, "stopped for some reason, chances are it's detailed here.    ");
2557    log_msg(0, "More than likely there's a message at the very end of this  ");
2558    log_msg(0, "log that will tell you what is wrong.  Please read it!      ");
2559    log_msg(0, "------------------------------------------------------------");
2560}
2561
2562
2563
2564/**
2565 * Restore the user's data.
2566 * What did you think it did, anyway? :-)
2567 */
2568int main(int argc, char *argv[])
2569{
2570    FILE *fin;
2571    FILE *fout;
2572    int retval = 0;
2573    int res;
2574    char *tmp = NULL;
2575
2576    struct mountlist_itself *mountlist;
2577    struct raidlist_itself *raidlist;
2578    struct s_node *filelist;
2579    char *tmp1 = NULL;
2580    char *tmp2 = NULL;
2581    bool run_postnuke = FALSE;
2582
2583    if (getuid() != 0) {
2584        fprintf(stderr, "Please run as root.\r\n");
2585        exit(127);
2586    }
2587    if (!
2588        (bkpinfo = malloc(sizeof(struct s_bkpinfo)))) {
2589        fatal_error("Cannot malloc bkpinfo");
2590    }
2591    reset_bkpinfo();
2592
2593    g_loglevel = DEFAULT_MR_LOGLEVEL;
2594
2595/* Configure global variables */
2596#ifdef __FreeBSD__
2597    if (strstr
2598        (call_program_and_get_last_line_of_output("cat /tmp/cmdline"),
2599         "textonly"))
2600#else
2601    if (strstr
2602        (call_program_and_get_last_line_of_output("cat /proc/cmdline"),
2603         "textonly"))
2604#endif
2605    {
2606        g_text_mode = TRUE;
2607        log_msg(1, "TEXTONLY MODE");
2608    } else {
2609        g_text_mode = FALSE;
2610    }                           // newt :-)
2611    if (!(mountlist = malloc(sizeof(struct mountlist_itself)))) {
2612        fatal_error("Cannot malloc mountlist");
2613    }
2614    if (!(raidlist = malloc(sizeof(struct raidlist_itself)))) {
2615        fatal_error("Cannot malloc raidlist");
2616    }
2617
2618    /* Init GUI */
2619    setup_newt_stuff();         /* call newtInit and setup screen log */
2620
2621    malloc_libmondo_global_strings();
2622
2623    strcpy(g_mondo_home,
2624           call_program_and_get_last_line_of_output("which mondorestore"));
2625    g_current_media_number = 1; // precaution
2626
2627    run_program_and_log_output("mkdir -p " MNT_CDROM, FALSE);
2628
2629    setup_MR_global_filenames();    // malloc() and set globals, using bkpinfo->tmpdir etc.
2630    bkpinfo->backup_media_type = none;  // in case boot disk was made for one backup type but user wants to restore from another backup type
2631    bkpinfo->restore_data = TRUE;   // Well, yeah :-)
2632    if (am_I_in_disaster_recovery_mode()) {
2633        run_program_and_log_output("mount / -o remount,rw", 2);
2634    }                           // for b0rken distros
2635    g_main_pid = getpid();
2636    srandom((int) (time(NULL)));
2637    set_signals(TRUE);
2638    g_kernel_version = get_kernel_version();
2639
2640    log_msg(1, "FYI - g_mountlist_fname = %s", g_mountlist_fname);
2641    if (strlen(g_mountlist_fname) < 3) {
2642        fatal_error
2643            ("Serious error in malloc()'ing. Could be a bug in your glibc.");
2644    }
2645    mkdir(MNT_CDROM, 0x770);
2646    make_hole_for_dir(MONDO_CACHE);
2647
2648    /* Backup original mountlist.txt */
2649    mr_asprintf(tmp, "%s.orig", g_mountlist_fname);
2650    if (!does_file_exist(g_mountlist_fname)) {
2651        log_msg(2, "%ld: Warning - g_mountlist_fname (%s) does not exist yet", __LINE__, g_mountlist_fname);
2652    } else if (!does_file_exist(tmp)) {
2653        mr_free(tmp);
2654        mr_asprintf(tmp, "cp -f %s %s.orig", g_mountlist_fname, g_mountlist_fname);
2655        run_program_and_log_output(tmp, FALSE);
2656    }
2657    mr_free(tmp);
2658
2659    /* Init directories */
2660    make_hole_for_dir("/var/log");
2661    make_hole_for_dir("/tmp/tmpfs");    /* just in case... */
2662    run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
2663
2664    run_program_and_log_output("rm -Rf /tmp/tmpfs/mondo.tmp.*", FALSE);
2665
2666    welcome_to_mondorestore();
2667    if (bkpinfo->disaster_recovery) {
2668        log_msg(1, "I am in disaster recovery mode");
2669    } else {
2670        log_msg(1, "I am in normal, live mode");
2671    }
2672
2673    log_it("what time is it");
2674
2675    /* Process command-line parameters */
2676    if (argc == 2 && strcmp(argv[1], "--edit-mountlist") == 0) {
2677#ifdef __FreeBSD__
2678        paranoid_system("mv -f /tmp/raidconf.txt /etc/raidtab");
2679        if (!does_file_exist("/etc/raidtab"))
2680            paranoid_system("vinum printconfig > /etc/raidtab");
2681#endif
2682        load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2683        if (!does_file_exist(g_mountlist_fname)) {
2684            strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
2685        }
2686        res = let_user_edit_the_mountlist(mountlist, raidlist);
2687#ifdef __FreeBSD__
2688        paranoid_system("mv -f /etc/raidtab /tmp/raidconf.txt");
2689#endif
2690        paranoid_MR_finish(res);
2691    }
2692
2693    if (argc == 3 && strcmp(argv[1], "--echo-to-screen") == 0) {
2694        fout = fopen("/tmp/out.txt", "w");
2695        fput_string_one_char_at_a_time(stderr, argv[2]);
2696        finish(0);
2697    }
2698
2699    if (argc == 5 && strcmp(argv[1], "--common") == 0) {
2700        g_loglevel = 6;
2701        filelist = load_filelist(argv[2]);
2702        if (!filelist) {
2703            fatal_error("Failed to load filelist");
2704        }
2705        toggle_node_selection(filelist, FALSE);
2706        toggle_all_root_dirs_on(filelist);
2707        // TODO: /usr/lib ???
2708        toggle_path_selection(filelist, "/usr/share", TRUE);
2709        save_filelist(filelist, "/tmp/out.txt");
2710        mr_asprintf(tmp1, "%s", argv[3]);
2711        mr_asprintf(tmp2, "%s", argv[4]);
2712
2713        res = save_filelist_entries_in_common(tmp1, filelist, tmp2, FALSE);
2714        mr_free(tmp1);
2715        mr_free(tmp2);
2716
2717        free_filelist(filelist);
2718        printf("res = %d", res);
2719        finish(0);
2720    }
2721
2722    if (argc == 3 && strcmp(argv[1], "--popuplist") == 0) {
2723        popup_changelist_from_file(argv[2]);
2724        paranoid_MR_finish(0);
2725    }
2726
2727    if (argc == 5 && strcmp(argv[1], "--copy") == 0) {
2728        log_msg(1, "SCORE");
2729        g_loglevel = 10;
2730        if (strstr(argv[2], "save")) {
2731            log_msg(1, "Saving from %s to %s", argv[3], argv[4]);
2732            fin = fopen(argv[3], "r");
2733            fout = fopen(argv[4], "w");
2734            copy_from_src_to_dest(fin, fout, 'w');
2735            fclose(fin);
2736            fin = fopen(argv[3], "r");
2737            copy_from_src_to_dest(fin, fout, 'w');
2738            fclose(fout);
2739            fclose(fin);
2740        } else if (strstr(argv[2], "restore")) {
2741            fout = fopen(argv[3], "w");
2742            fin = fopen(argv[4], "r");
2743            copy_from_src_to_dest(fout, fin, 'r');
2744            fclose(fin);
2745            fin = fopen(argv[4], "r");
2746            copy_from_src_to_dest(fout, fin, 'r');
2747            fclose(fout);
2748            fclose(fin);
2749        } else {
2750            fatal_error("Unknown additional param");
2751        }
2752        finish(0);
2753    }
2754
2755    if (argc == 3 && strcmp(argv[1], "--mdstat") == 0) {
2756        wait_until_software_raids_are_prepped(argv[2], 100);
2757        finish(0);
2758    }
2759
2760    if (argc == 3 && strcmp(argv[1], "--mdconv") == 0) {
2761        finish(create_raidtab_from_mdstat(MDSTAT_FILE,argv[2]));
2762    }
2763
2764    if (argc == 2 && strcmp(argv[1], "--live-grub") == 0) {
2765        retval = run_grub(FALSE, "/dev/hda");
2766        if (retval) {
2767            log_to_screen("Failed to write Master Boot Record");
2768        }
2769        paranoid_MR_finish(0);
2770    }
2771    if (argc == 3 && strcmp(argv[1], "--paa") == 0) {
2772        g_current_media_number = atoi(argv[2]);
2773        pause_and_ask_for_cdr(5, NULL);
2774        paranoid_MR_finish(0);
2775    }
2776    if ((argc == 2 && strcmp(argv[1], "--partition-only") == 0) && (bkpinfo->disaster_recovery)) {
2777        log_msg(0, "Partitioning only.");
2778        load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2779        strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
2780        load_mountlist(mountlist, g_mountlist_fname);
2781        res = partition_everything(mountlist);
2782        finish(res);
2783    }
2784
2785    if ((argc == 2 && strcmp(argv[1], "--format-only") == 0) && (bkpinfo->disaster_recovery)) {
2786        log_msg(0, "Formatting only.");
2787        load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2788        strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
2789        load_mountlist(mountlist, g_mountlist_fname);
2790        res = format_everything(mountlist, FALSE, raidlist);
2791        finish(res);
2792    }
2793
2794    if ((argc == 2 && strcmp(argv[1], "--stop-lvm-and-raid") == 0) && (bkpinfo->disaster_recovery)) {
2795        log_msg(0, "Stopping LVM and RAID");
2796        load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2797        strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
2798        load_mountlist(mountlist, g_mountlist_fname);
2799        res = do_my_funky_lvm_stuff(TRUE, FALSE);
2800        res += stop_all_raid_devices(mountlist);
2801        finish(res);
2802    }
2803
2804    if ((argc > 1 && strcmp(argv[argc - 1], "--live-from-cd") == 0) && (!bkpinfo->disaster_recovery)) {
2805        g_restoring_live_from_cd = TRUE;
2806    }
2807
2808    // Handle params here first
2809    handle_incoming_parameters(argc,argv);
2810
2811    if (!bkpinfo->disaster_recovery) {  // live!
2812        log_msg(1, "I am in normal, live mode.");
2813        log_msg(2, "FYI, MOUNTLIST_FNAME = %s", g_mountlist_fname);
2814        mount_boot_if_necessary();  /* for Gentoo users */
2815        log_msg(2, "Still here.");
2816        if (bkpinfo->backup_media_type == netfs) {
2817            g_restoring_live_from_netfs = TRUE;
2818        }
2819        log_msg(2, "Calling restore_to_live_filesystem()");
2820        retval = restore_to_live_filesystem();
2821
2822        log_msg(2, "Still here. Yay.");
2823        if ((strlen(bkpinfo->tmpdir) > 0) && (strstr(bkpinfo->tmpdir,"mondo.tmp.") != NULL)) {
2824            mr_asprintf(tmp, "rm -Rf %s/*", bkpinfo->tmpdir);
2825            run_program_and_log_output(tmp, FALSE);
2826            mr_free(tmp);
2827        }
2828        unmount_boot_if_necessary();    /* for Gentoo users */
2829        paranoid_MR_finish(retval);
2830    } else {
2831        /* Disaster recovery mode (must be) */
2832        log_msg(1, "I must be in disaster recovery mode.");
2833        log_msg(2, "FYI, MOUNTLIST_FNAME = %s ", g_mountlist_fname);
2834
2835        log_it("About to call load_mountlist and load_raidtab");
2836        strcpy(bkpinfo->restore_path, MNT_RESTORING);
2837        read_cfg_file_into_bkpinfo(g_mondo_cfg_file);
2838        retval = load_mountlist(mountlist, g_mountlist_fname);
2839        retval += load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2840        log_it("Returned from calling load_mountlist and load_raidtab successfully");
2841
2842        // TODO:To be reviewed
2843        if ((bkpinfo->restore_mode == compare) || (bkpinfo->restore_mode == nuke)) {
2844            if ((bkpinfo->backup_media_type == netfs) && bkpinfo->netfs_mount && !is_this_device_mounted(bkpinfo->netfs_mount)) {
2845                log_msg(1, "Mounting remote %s dir", bkpinfo->netfs_proto);
2846                sprintf(bkpinfo->isodir, "/tmp/isodir");
2847                run_program_and_log_output("mkdir -p /tmp/isodir", 5);
2848                if (strstr(bkpinfo->netfs_proto, "sshfs")) {
2849                    if (bkpinfo->netfs_user) {
2850                        mr_asprintf(tmp, "sshfs -o ro %s@%s /tmp/isodir", bkpinfo->netfs_user,bkpinfo->netfs_mount);
2851                    } else {
2852                        mr_asprintf(tmp, "sshfs -o ro %s /tmp/isodir", bkpinfo->netfs_mount);
2853                    }
2854                } else  {
2855                    if (strstr(bkpinfo->netfs_proto, "smbfs")) {
2856                        if (bkpinfo->netfs_user) {
2857                            mr_asprintf(tmp, "mount -t cifs %s /tmp/isodir -o user=%s,nolock,ro ", bkpinfo->netfs_mount,bkpinfo->netfs_user);
2858                        } else {
2859                            mr_asprintf(tmp, "mount -t cifs %s /tmp/isodir -o nolock,ro ", bkpinfo->netfs_mount);
2860                        }
2861                    } else {
2862                        if (bkpinfo->netfs_user) {
2863                            mr_asprintf(tmp, "mount %s@%s -o nolock,ro /tmp/isodir", bkpinfo->netfs_user,bkpinfo->netfs_mount);
2864                        } else {
2865                            mr_asprintf(tmp, "mount %s -o nolock,ro /tmp/isodir", bkpinfo->netfs_mount);
2866                        }
2867                    }
2868                }
2869                run_program_and_log_output(tmp, 1);
2870                mr_free(tmp);
2871            }
2872        }
2873
2874        if (retval) {
2875            log_to_screen("Warning - load_raidtab_into_raidlist returned an error");
2876        }
2877
2878        log_msg(1, "Send in the clowns.");
2879
2880
2881        if (bkpinfo->restore_mode == nuke) {
2882            log_it("nuking");
2883            retval += nuke_mode(mountlist, raidlist);
2884        } else if (bkpinfo->restore_mode == interactive) {
2885            log_it("catchall");
2886            retval += catchall_mode(mountlist, raidlist);
2887        } else if (bkpinfo->restore_mode == compare) {
2888            log_it("compare");
2889            retval += compare_mode(mountlist, raidlist);
2890        } else if (bkpinfo->restore_mode == isoonly) {
2891            log_it("iso");
2892            retval = iso_mode(mountlist, raidlist, FALSE);
2893        } else if (bkpinfo->restore_mode == mbr) {
2894            log_it("mbr");
2895            retval = mount_all_devices(mountlist, TRUE);
2896            if (!retval) {
2897                retval += run_boot_loader(FALSE);
2898                retval += unmount_all_devices(mountlist);
2899            }
2900            if (retval) {
2901                log_to_screen("Failed to write Master Boot Record");
2902            }
2903        } else if (bkpinfo->restore_mode == isonuke) {
2904            log_it("isonuke");
2905            retval = iso_mode(mountlist, raidlist, TRUE);
2906        } else {
2907            log_it("catchall (no mode specified in command-line call");
2908            retval += catchall_mode(mountlist, raidlist);
2909        }
2910    }
2911
2912    /* clean up at the end */
2913    if (retval) {
2914        if (does_file_exist(MONDO_CACHE"/changed.files")) {
2915            log_to_screen
2916                ("See "MONDO_CACHE"/changed.files for list of files that have changed.");
2917        }
2918        mvaddstr_and_log_it(g_currentY++,
2919                            0,
2920                            "Run complete. Errors were reported. Please review the logfile.");
2921    } else {
2922        if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2923            mvaddstr_and_log_it(g_currentY++,
2924                                0,
2925                                "Run complete. Please remove media and reboot.");
2926        } else {
2927            sync();
2928            if (is_this_device_mounted(MNT_CDROM)) {
2929                run_program_and_log_output("umount -d " MNT_CDROM, FALSE);
2930            }
2931
2932            if (!bkpinfo->please_dont_eject) {
2933                (void)eject_device("/dev/cdrom");
2934            }
2935            mvaddstr_and_log_it(g_currentY++,
2936                                0,
2937                                "Run complete. Please remove media and reboot.");
2938        }
2939    }
2940
2941// g_I_have_just_nuked is set true by nuke_mode() just before it returns
2942    if (!system("which post-nuke > /dev/null 2> /dev/null")) {
2943      log_msg(1, "post-nuke found; find out whether we should run it...");
2944      if (g_I_have_just_nuked || does_file_exist("/POST-NUKE-ANYWAY")) {
2945        run_postnuke = TRUE;
2946        log_msg(1, "Yes, will run post-nuke because in nuke mode or file /POST-NUKE-ANYWAY exists.");
2947      } else if (ask_me_yes_or_no("post-nuke script found. Do you want to run it?")) {
2948        run_postnuke = TRUE;
2949        log_msg(1, "Yes, will run post-nuke because user interactively asked for it.");
2950      } else {
2951        run_postnuke = FALSE;
2952        log_msg(1, "No, will not run post-nuke.");
2953      }
2954    } else {
2955      log_msg(1, "No post-nuke found.");
2956    }
2957    if (run_postnuke) {
2958      log_to_screen("Running post-nuke...");
2959      if (mount_all_devices(mountlist, TRUE)) {
2960        log_to_screen
2961          ("Unable to re-mount partitions for post-nuke stuff");
2962      } else {
2963        log_msg(1, "Re-mounted partitions for post-nuke stuff");
2964        mr_asprintf(tmp, "post-nuke %s %d", bkpinfo->restore_path, retval);
2965        log_msg(2, "Calling '%s'", tmp);
2966        if ((res = run_program_and_log_output(tmp, 0))) {
2967          log_OS_error(tmp);
2968        }
2969        mr_free(tmp);
2970        log_msg(1, "post-nuke returned w/ res=%d", res);
2971      }
2972      unmount_all_devices(mountlist);
2973      log_to_screen("I've finished post-nuking.");
2974    }
2975
2976/* 
2977  log_to_screen("If you are REALLY in a hurry, hit Ctrl-Alt-Del now.");
2978  log_to_screen("Otherwise, please wait until the RAID disks are done.");
2979  wait_until_software_raids_are_prepped("/proc/mdstat", 100);
2980  log_to_screen("Thank you.");
2981*/
2982    unlink("/tmp/mondo-run-prog.tmp");
2983    set_signals(FALSE);
2984    log_to_screen("Restore log (%s) copied to /var/log on your hard disk", MONDO_LOGFILE);
2985    log_to_screen("Mondo-restore is exiting (retval=%d)                                      ", retval);
2986
2987    mr_asprintf(tmp, "umount -d %s", bkpinfo->isodir);
2988    run_program_and_log_output(tmp, 5);
2989    mr_free(tmp);
2990
2991    paranoid_free(mountlist);
2992    paranoid_free(raidlist);
2993    if (am_I_in_disaster_recovery_mode()) {
2994        run_program_and_log_output("mount / -o remount,rw", 2);
2995    }                           // for b0rken distros
2996    if (strstr(bkpinfo->tmpdir,"mondo.tmp.") != NULL) {
2997        mr_asprintf(tmp, "rm -Rf %s", bkpinfo->tmpdir);
2998        paranoid_system(tmp);
2999        mr_free(tmp);
3000    }
3001    paranoid_MR_finish(retval); // frees global stuff plus bkpinfo
3002    free_libmondo_global_strings(); // it's fine to have this here :) really :)
3003
3004    unlink("/tmp/filelist.full");
3005    unlink("/tmp/filelist.full.gz");
3006
3007    exit(retval);
3008}
3009
3010/**************************************************************************
3011 *END_MAIN                                                                *
3012 **************************************************************************/
3013
3014
3015
3016
3017
3018/**************************************************************************
3019 *END_MONDO-RESTORE.C                                                     *
3020 **************************************************************************/
Note: See TracBrowser for help on using the repository browser.