source: branches/2.2.10/mondo/src/mondorestore/mondorestore.c @ 2816

Last change on this file since 2816 was 2816, checked in by Bruno Cornec, 9 years ago

svn merge (in svk) -r 2714:2773 2.2.9 in 2.2.10

r4207@localhost (orig r2716): bruno | 2011-02-03 01:29:51 +0100

  • Increase boot size again to support better fedora 13/14


r4208@localhost (orig r2717): bruno | 2011-02-12 15:43:36 +0100

  • Excluding vmhgfs FS for VMWare guests mounting areas of the host.


r4209@localhost (orig r2718): bruno | 2011-02-18 00:30:35 +0100
-Extend EXTRA-SIZE for Debian 6.0 (at least)


r4210@localhost (orig r2719): bruno | 2011-02-18 00:45:40 +0100

  • Fix #462 (removes temp dir if analyze-my-lvm launched in stand alonae mode)


r4213@localhost (orig r2722): bruno | 2011-02-25 18:44:47 +0100

  • Fixes for RHEL6 support at least around dm extensions, bnx2x and cp210x dependencies


r4214@localhost (orig r2723): bruno | 2011-02-25 19:00:17 +0100

  • Adds a restore quiet option


r4215@localhost (orig r2724): bruno | 2011-02-25 19:43:50 +0100

  • Adds support for dm-log used on RHEL6 as well.


r4216@localhost (orig r2725): bruno | 2011-02-25 21:26:54 +0100

  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3


r4217@localhost (orig r2726): bruno | 2011-02-26 11:38:29 +0100

  • Fix error introduced in previous patch in the if/then/else case without member in the then


r4218@localhost (orig r2727): bruno | 2011-02-26 12:05:33 +0100

  • With mindi-busybox 1.18.3 the syntax of openvt changed. This patch keeps a compatible init script for both previous and current version.


r4219@localhost (orig r2728): bruno | 2011-02-26 15:27:40 +0100

  • Latest lvdisplay print MiB instead of MB. Impact on analyze-my-lvm fixed.


r4220@localhost (orig r2729): bruno | 2011-02-26 16:39:32 +0100

  • Adds feature rpc to mindi-busybox in case of need



r4221@localhost (orig r2730): bruno | 2011-02-26 16:42:43 +0100

  • Adds the rebot command to the list of mandatory ones at restore time


r4222@localhost (orig r2731): bruno | 2011-02-26 17:37:13 +0100

  • Adds functions and /bin/loadkeys (new location) to minimal set of commands to have
  • provides a makemessage option to mindi and this function now works with pbdistrocheck
  • Do not create an error when a phys device has 0 size and is a /dev/dm-* device


r4223@localhost (orig r2732): bruno | 2011-02-26 19:07:43 +0100

  • Syntax fix


r4224@localhost (orig r2733): bruno | 2011-02-26 19:51:16 +0100

  • Fix test on dm which was made in wrong sense
  • Adds a driver to allow raid456 to work


r4225@localhost (orig r2734): bruno | 2011-02-26 20:55:00 +0100

  • Fix 2 redirections issues


r4317@localhost (orig r2737): bruno | 2011-03-13 13:12:57 +0100

  • Add support for libahci driver, mandatory for debian 6.


r4318@localhost (orig r2738): bruno | 2011-03-13 14:46:37 +0100

  • Update from Lester Wade (lester.wade_at_hp.com) incorporating latest ML feedbacks


r4319@localhost (orig r2739): bruno | 2011-03-13 17:49:15 +0100

  • Updated Lab for TES 2011


r4323@localhost (orig r2743): bruno | 2011-03-20 01:37:40 +0100

  • Fix a seg fault bug when using the GUI in case exclude_paths was NULL (mc_at_carreira.com.pt)


r4324@localhost (orig r2744): bruno | 2011-03-20 19:38:14 +0100

  • Adds Arch Linux Keyboard support (mc_at_carreira.com.pt)


r4325@localhost (orig r2745): bruno | 2011-03-20 19:48:01 +0100

  • Avoids coredump again when exclude_path is NULL with mr_make_devlist_from_pathlist (feedback from mc_at_carreira.com.pt)


r4326@localhost (orig r2746): bruno | 2011-03-20 19:39:47 +0100

  • Remove blkid from busybox to solve #435


r4327@localhost (orig r2747): bruno | 2011-03-20 19:43:22 +0100

  • Adds reboot to fix #435


r4328@localhost (orig r2748): bruno | 2011-03-22 00:58:07 +0100

  • Fix Arch Linux kbd printed in log file now instead of screen.


r4329@localhost (orig r2749): bruno | 2011-03-22 00:59:58 +0100

  • Again increase size for boot env to support better fedora 14 and other latest distros


r4356@localhost (orig r2750): bruno | 2011-03-25 19:30:01 +0100

  • Improve Arch Linux support for mindi tools (mc_at_carreira.com.pt)


r4357@localhost (orig r2751): bruno | 2011-03-25 19:48:33 +0100

  • Accept to work with either wodim or cdreord and genisoimage or mkisofs


r4358@localhost (orig r2752): bruno | 2011-03-25 20:20:49 +0100

  • Documents option -v of mondoarchive


r4359@localhost (orig r2753): bruno | 2011-03-29 01:34:30 +0200

  • Use modprobe instead of insmod to load loop, cd, ... modules (in order to force them at rstore time). Also drop very old aes support for mdk 9.2


r4360@localhost (orig r2754): bruno | 2011-03-29 01:38:49 +0200

  • Arch Linux support for boot message of mindi (mc_at_carreira.com.pt) using /etc/arch-release and pacman.log


r4383@localhost (orig r2755): bruno | 2011-04-07 09:49:38 +0200

  • Adds a UdevadmTrigger? function at restore time to deal with the various syntax formats changes ddepending on version (mc_at_carreira.com.pt)


r4384@localhost (orig r2756): bruno | 2011-04-07 10:05:24 +0200

  • Increase again sizes for Debian 6 support (victor.gattegno_at_hp.com)


r4385@localhost (orig r2757): bruno | 2011-04-07 10:13:00 +0200
Conform to the report of size really !


r4386@localhost (orig r2758): bruno | 2011-04-07 11:31:12 +0200

  • Improved message for Arch Linux for rebuilding the initrd (mc_at_carreira.com.pt)


r4390@localhost (orig r2759): bruno | 2011-04-12 01:15:00 +0200

  • Typo in Web site


r4392@localhost (orig r2761): bruno | 2011-04-14 00:23:38 +0200

  • Adds download references for latest versions.


r4394@localhost (orig r2763): bruno | 2011-04-16 00:35:08 +0200

  • Adds support for MT back mandatory for OBDR.


r4395@localhost (orig r2764): bruno | 2011-04-20 12:18:46 +0200
Function resize_drive_proportionately_to_suit_new_drives

  • Integrate some mr_asprintf
  • Use systematically long long data types in all computation to match the struct mountlist_reference size member and have correct precision
  • Apply JB general_at_itpsg.com patch to make the partition resizing work properly according to the drive size (keeping all calculation in KB and converting only for display)


r4396@localhost (orig r2765): bruno | 2011-04-21 11:20:58 +0200

  • Remove very old code creating confusing symlinks pico to nano in init (patch from MC). Fix #477


r4397@localhost (orig r2766): bruno | 2011-04-21 11:57:44 +0200

  • text's are properly centered in open_and_list_dir (Patch from Chucky). Fix #404.


r4398@localhost (orig r2767): bruno | 2011-04-21 12:49:40 +0200

  • Attempt to fix #432 with a better message at a start.


r4399@localhost (orig r2768): bruno | 2011-04-21 13:00:37 +0200

Log content of mondo-restore.cfg


r4400@localhost (orig r2769): bruno | 2011-04-21 13:48:52 +0200

  • Introduce a new bkpinfo member (subdir) to support storing the subdir under which ISO images are stored on an external HDD. Should fix mostly #433.


r4401@localhost (orig r2770): bruno | 2011-04-21 14:14:52 +0200

  • Fix compilation issues due to level of visibility of functions


r4402@localhost (orig r2771): bruno | 2011-04-22 01:49:03 +0200

  • Adds GUI support for compresion type. Fix half of #468.


r4403@localhost (orig r2772): bruno | 2011-04-22 02:00:07 +0200

  • Adds support of ext attr through the GUI. Fix second part of #468


r4404@localhost (orig r2773): bruno | 2011-04-22 02:22:24 +0200

  • Propose more variables in the mindi.conf example
  • More explicit messages when size unsufficient in mindi for kernel + initrd as per #471
  • Better loging of labeling in mondo when in nuke mode


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