source: trunk/mondo/mondo/mondorestore/mondo-restore.c @ 252

Last change on this file since 252 was 252, checked in by bcornec, 13 years ago

merge -r249:251 $SVN_M/branches/2.05

  • Property svn:keywords set to Id
File size: 113.2 KB
Line 
1/***************************************************************************
2       mondo-restore.c  -  restores mondoarchive data
3                             -------------------
4    begin                : Fri May 19 2000
5    copyright            : (C) 2000 by Hugo Rabson
6    email                : Hugo Rabson <hugorabson@msn.com>
7    cvsid                : $Id: mondo-restore.c 252 2005-12-21 01:45:55Z bcornec $
8***************************************************************************/
9
10/***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation; either version 2 of the License, or     *
15 *   (at your option) any later version.                                   *
16 *                                                                         *
17 ***************************************************************************/
18
19/***************************************************************************
20 *                         Change Log                                      *
21 ***************************************************************************
22.
23
24
2507/26
26- workaround - if not /dev then don't call partimagehack
27
2807/21
29- if switch to Interactive Mode during nuke then don't nuke twice :) just once
30
3107/18
32- better support of users who boot from LVM CD and nuke-restore non-LVM backups
33
3407/10
35- when using 'star', exclude all 'not removed.' messages from log (misleading, they are)
36- try w/ ACLs, then w/o ACLs if star fails w/ ACLs
37- added ACL, xattr support for afio
38
3906/26
40- remove make_relevant_partition_bootable(); roll into mondo-prep.c
41- changed various newtDrawRootText() calls to use g_noof_rows instead of
42  integers
43
4406/19
45- don't try to access biggiestruct before it is populated w/ data relating
46  to biggiefile; instead, use control char to find out if biggiefile is
47  to be restored w/ partimagehack or not
48- added AUX_VER
49
5006/15
51- read_cfg_file_into_bkpinfo() --- use user-supplied 'isodir' instead of
52  archive's 'isodir' if different
53  Conor Daly <conor.daly@met.ie>
54
5506/17
56- restore with partimagehack if NTFS device; dd, if non-NTFS device
57
5806/14
59- unmount all; remount, run grub-mr; unmount again
60
6104/09
62- fixed subset restoration bug introduced when I added 'star' support
63
6404/04
65- cleaned up restore_a_tarball_from_stream()
66
6704/03
68- added star support
69- pause for 3s between partitioning and formatting (if in Nuke Mode)
70
7103/28
72- check that g_mountlist_fname was correcfly set; abort if it wasn't
73
7403/25
75- removed dev_null code
76
7703/22/04
78- added mode_of_file()
79- added code to check for unauthorised modification of /dev/null
80  by afio (for instance)
81
8212/27/03
83- check /tmp/cmdline instead of /proc/cmdline if on FreeBSD
84
8511/15
86- added g_fprep
87- "Switch to interactive mode?" msg is now more informative
88
8911/05
90- after 'Are you sure?' when user specifies / as restore path, set
91  restore_path[] to "" so that files are restored to [blank]/file/name :)
92
9310/29
94- moved "No restoring or comparing will take place today" block
95  up to before iso_fiddly_bits (if iso) is called - fix bug
96  if you're in ISO Mode and you say "exit to shell"
97
9810/22
99- swapped calls to chmod() and chown() after restoration
100  of big files (Jens)
101
10210/21
103- changed "/mnt/cdrom" to MNT_CDROM
104
10510/19
106- restore biggiefiles selectively from CD properly
107- use find_my_editor() to find pico/vi/whatever
108- better use of call_program_and_get_last_line_of_output() to
109  scan /proc/cmdline
110
11110/18
112- don't report 'missing compressor' if no compressor used at all
113
11410/14
115- log afio's error messages to /var/log/mondo-archive.log
116  when restoring :)
117- call vi if pico is not available
118
11910/09
120- better logging if fatal error (cannot openout bigfile)
121- better GUI feedback when restoring big files
122- restore_everything() now uses s_node* instead of char*
123- ditto restore_all_*_from_*()
124
12510/02
126- succinct msg instead of pop-ups, if used -H
127
12809/27
129- tweaked restore-time gui
130
13109/26
132- proper reporting of DVD/CDR/etc. type in displayed dialogs
133
13409/23
135- malloc/free global strings in new subroutines - malloc_libmondo_global_strings()
136  and free_libmondo_global_strings() - which are in libmondo-tools.c
137
13809/21
139- trying to fix "mondorestore <no params>" segfault
140
14109/18
142- better remounting of /
143- cleaned up run_grub()
144- sensible command-line handling in Live Mode
145
14609/17
147- cleaned up GRUB installer script a bit
148
14909/15
150- remount / as r/w if in disaster recovery mode;
151  helps for b0rken distros
152
15309/13
154- major NTFS hackage
155
15609/12
157- changed some in-sub var decl'ns to malloc()'s
158
15909/05
160- don't let me run unless I'm root
161- finished NTFS/partimagehack support (CD only); working on tape now
162
16309/01
164- fixed cosmetic bug in biggiefile restore window
165
16606/01 - 08/31
167- added code to main() to make sure NFS
168  (backup) share is mounted in Nuke and Compare Modes
169- added code to run_grub() to mount /boot before running grub-install
170- fixed some stray assert()'s in restore_a_biggiefile_from_stream()
171- fixed bugs in extract_config_file_from_ramdisk()
172  and get_cfg_file_from_archive() which
173  stopped fape+floppy users from being able to
174  boot from floppy and leave floppy in drive :)
175- added hooks to partimage for doing imagedevs
176- fixed calls to popup_and_get_string()
177
17805/01 - 05/31
179- fixed biggiefile atime/utime dates restoration bug, I think
180- added misc clean-up (Steve Hindle)
181- fixed support for subdir-within-NFS-mount
182- if nuke mode fails & user reverts to interactive mode _and succeeds_,
183  don't claim nuke mode aborted :)
184- unmount_all_devices() uses mountlist->el[lino].mountpt
185  instead of mountlist->el[lino].device where possible
186- added Joshua Oreman's FreeBSD patches
187- copied missing paragraph from 1.6x's read_cfg_file_into_bkpinfo()
188  to 1.7x's; affected tape streamer users (badly!)
189- cleaned up some paranoid assert()'s
190- use which("post-nuke") instead of find_home_of_exe("post-nuke")
191- fixed "Don't eject while restoring" mode
192- get_cfg_file_from_archive() --- also recovers mountlist.txt now :)
193- don't eject if 'donteject' is in kernel's command line
194- added 'don't panic' msg to start of log
195
19604/01 - 04/30
197- added text mode (cat /proc/cmdline; if textonly then text mode is on)
198- delete /var/lock/subsys/ * when nuking
199- don't resize mountlist if "noresize" present in /proc/cmdline
200- changed from chmod -R 1777 tmp to chmod 1777 tmp
201- replace newtFinished() and newtInit() with
202  newtSuspend() and newtResume()
203- get_cfg_file_from_archive() returns 1 instead of aborting now
204- read_cfg_file_into_bkpinfo) --- if autorun CD but its config
205  file indicates a non-CD backup media then ask, just in case
206- sped up restore_a_tarball_from_CD() a bit
207- line 4469 --- if post-nuke not found then don't run it :)
208- replaced "/mnt/RESTORING" with MNT_RESTORING (#define'd)
209- moved compare_*() into mondorestore/mondo-rstr-compare.c
210- moved some RAID subroutines into common/libmondo-raid.c
211- fixed some iso live compare problems
212- replaced FILELIST_FULL with g_filelist_full and FILELIST_FULL_STUB;
213  g_filelist_full being the full path of the filelist.full text file and
214  FILELIST_FULL_STUB being "tmp/filelist.full" (relative path);
215- ditto BIGGIELIST_TXT, MONDO_CFG_FILE
216- added lots of assert()'s and log_OS_error()'s
217- in Nuke Mode, check mountlist's sanity before doing anything else;
218  if it fails sanity test, offer to revert to Interactive Mode (or abort)
219- copy log to /mnt/RESTORING/root at end
220- read_cfg_file_into_bkpinfo() --- read iso-dev and isodir if bkptype==iso
221- line 1701 --- delete ramdisk file after extracting config info
222- moved call to make_relevant_partitions_bootable() from
223  within run_boot_loader() to within interactive_mode() and
224  nuke_mode(), after unmounting disks
225- if editing fstab or *.conf, try to use pico if available
226- better calling of make-me-bootable
227- don't sort mountlist anywhere anymore except _locally_ in
228  mount_all_devices() and unmount_all_devices()
229- edit fstab, grub.conf _after_ stabgrub-me if it fails
230- run_boot_loader() --- backup all crucial files to /etc/ *.pristine first
231- added iso_fiddly_bits()
232- fixed ISO mode support
233- mount_cdrom() only searches for device if NOT in disaster recovery mode
234- changed lost of system()'s into run_program_and_log_output()'s
235- don't eject if bkpinfo->please_dont_eject_when_restoring
236- cleaned up post-nuke handling code
237- always eject CD at end, unless bkpinfo->please_dont_...
238- misc clean-up (Tom Mortell)
239- afio uses -c (1024L*1024L)/TAPE_BLOCK_SIZE now
240  instead of -c 1024
241
24201/01 - 03/31/2003
243- commented out sort_... line (BB)
244- clean-up (Stan Benoit)
245- added code for LVM and SW Raid (Brian Borgeson)
246- line 814 - added -p to 'mkdir -p tmp'
247- mount_cdrom() - calls find_cdrom_device() if
248  bkpinfo->media_device is blank (to fill it)
249
25011/01 - 12/31/2002
251- mount_cdrom() better at handling multiple CD drives
252- minor clean-up in restore_a_tarball_from_CD()
253- if --live-from-cd then assume restoring live from CD
254- tweaked it to run better w/ ArkLinux
255- create /mnt/RESTORING/mnt/.boot.d for Gentoo users
256  after restoring
257- cleaned up iso_mode(); no longer asks for NFS info 3 times
258- mount_cdrom() was trying to mount /mnt/isodir/%s/%d.iso;
259  is now just %s/%d.iso
260- mount/unmount /boot if necessary (Gentoo)
261- added RAW MBR support; added run_raw_mbr() for the purpose
262- unmount & remount supermounts at start/end of live restore
263- copy /tmp/mountlist.txt to /tmp/mountlist.txt.orig at start
264- cleaned up string-handling in get_cfg_info_from_archives()
265- fixed run_grub() to call new stabgrub-me script
266- popup list of changed files after Compare Mode
267- permit mondorestore --edit-mountlist even if live mode
268- create a repaired copy of grub-install which is RAID-friendly;
269  use it when initializing boot sector with run_grub()
270- use grub-MR instead of grub-install
271- fixed read_cfg_file_into_bkpinfo() to ignore cfg file's backup_media_type
272  if user has already specified a backup media type interactively
273
27410/01 - 10/31
275- run_grub() will let you specify the boot device as well as edit the system
276  files, if grub-install fails
277- fixed bug in fwrite() call in restore_biggiefile_from_CD()
278- fixed bug affecting restoration of bigfiles from CD's w/0 compression
279- run_grub() will run 'grub-install {boot device}' instead of
280  'grub-install (hd0)'
281
28209/01 - 09/30
283- use /tmp/tmpfs/mondo.tmp instead of /tmp/mondo.tmp
284- initialize MOUNTLIST_FNAME at start of main()
285- differential-related cleanup
286- better handling of CD-ROM drives which aren't /dev/cdrom :)
287- run_program_and_log_output() now takes boolean operator to specify
288  whether it will log its activities in the event of _success_
289- always load config file from archive before operating on it
290- moved some subroutines around; now closer to alphabetical order
291- changed mount.mindi to mount.bootisk
292- mount disks readonly if in Compare Mode
293- set /dev/null's permissions to 777, just in case it somehow gets mangled
294  ...which apparently happen with some devfs-based Linux distributions
295- remove /var/run/ *.pid after restoring
296- move spurious lockfiles from /home/ * to /home/ * /.disabled
297- if Interactive Mode then ask user which media, etc. (i.e. catchall mode
298  is now same as Interactive Mode)
299
30008/01 - 08/30
301- use data structure to store the fname, checksum, mods & perms of each bigfile
302  ... biggiestruct :)
303- if a filelist is tiny (2 bytes or less) then ignore it
304- insist_on_this_cd_number() --- now takes 2 params, not 1
305- re-enabled 'g_current_media_number = 1' in restore_everything()
306- added same to compare_mode()
307- replaced lots of global char[]'s with malloc()'s
308- if differential backup then don't permit formatting or fdisking,
309  whether Interactive or Nuke mode
310- added call to register_pid() at start of main()
311- if Nuke Mode & it succeeds then ask user if they have contributed yet
312- changed tape-size to media-size (config file)
313- changed using_* to backup_media_type
314- changed *_from_tape to *_from_stream
315
31607/01 - 07/31
317- added find_and_mount_actual_cdrom()
318- temp dir is always random
319- skip tarballs if they don't contain files we're looking for
320  (used to read the whole thing & _then_ skip)
321- use media_size[1] instead of media_size[0]
322- fixed serious bug in line 1546 - should have been !=, not ==; stopped
323  mondorestore from correctly restoring big files
324- bigfile piping enhancements (Philippe de Muyter)
325- unmount CD-ROM after restoring from live filesystem
326- TAPE_BLOCK_SIZE treated as %ld, not %d
327
32806/01 - 06/30
329- added signal-trapping
330- disabled 'nr-failed-disks' flag
331- fixed problem w/selective restore
332- don't change /tmp's permissions unless it doesn't exist & must be created
333- fixed bug in --mbr
334- is_file_in_list() enhanced to exclude /mnt/RESTORING or whatever
335- added support for uncompressed archives
336- --monitas-live now accepts path-to-restore_to_, not just path to restore
337- added some debugging/tracking code to the NFS section
338- various monitas-related enhancements
339- added --isonuke and --mbr switches
340- better logging in run_grub()
341- improved --monitas-live
342- mkdir -p /mnt/RESTORING/var/run/console just in case user excludes it
343- afio now uses 16MB buffer instead of 8MB
344- always use bkpinfo->media_size[0], now that -s has been expanded
345- popup and ask where to restore data, if restoring selectively
346
34705/01 - 05/31
348- add '--monitas' flag
349- don't run chmod -R 1777 /mnt/RESTORING/tmp before unmounting unless
350  restoring at the time...
351
35204/01 - 04/30
353- delete old /tmp/filelist.full,biggielist.txt if found when restoring to
354  live filesystem
355- replace MONDO_VERSION #define with VERSION from ../config.h
356- write fname of bigfile to screen when having trouble reading/comparing it
357- if restoring to live filesystem then wipe /tmp/tmpfs/ * afterwards
358- removed spurious finish(0) from main()
359
36003/01 - 03/31/2002
361- if /tmp/m*ndo-restore.cfg not found then assume live restore; restore
362  to / instead of /mnt/RESTORING
363- clean up is_file_in_list() to deal with the /mnt/RESTORING/ prefix
364- exclude leading '/' from filelist.restore-these
365- if /tmp/fstab.new exists then use _it_ instead of /tmp/fstab to label
366  ext2 or ext3 partitions
367- improved logging
368
369[...]
370
37107/10/2001 --- first incarnation
372*/
373
374
375/**
376 * @file
377 * The main file for mondorestore.
378 */
379
380/**************************************************************************
381 * #include statements                                                    *
382 **************************************************************************/
383#include "../common/my-stuff.h"
384#include "../common/mondostructures.h"
385#include "../common/libmondo.h"
386#include "mr-externs.h"
387#include "mondo-restore.h"
388#include "mondo-rstr-compare-EXT.h"
389#include "mondo-rstr-tools-EXT.h"
390#ifndef S_SPLINT_S
391#include <pthread.h>
392#endif
393
394extern void success_message(void);
395extern void twenty_seconds_til_yikes(void);
396
397
398/* For use in other programs (ex. XMondo) */
399#ifdef MONDORESTORE_MODULE
400#define main __mondorestore_main
401#define g_ISO_restore_mode __mondorestore_g_ISO_restore_mode
402#endif
403
404//static char cvsid[] = "$Id: mondo-restore.c 252 2005-12-21 01:45:55Z bcornec $";
405
406/**************************************************************************
407 * Globals                                                                *
408 **************************************************************************/
409extern char *g_tmpfs_mountpt;   // declared in libmondo-tools.c
410extern struct s_bkpinfo *g_bkpinfo_DONTUSETHIS; // used by finish() to free
411                                                // up global bkpinfo struct
412extern bool g_text_mode;
413extern FILE *g_fprep;
414extern double g_kernel_version;
415extern int g_partition_table_locked_up;
416extern int g_noof_rows;
417
418extern int partition_everything(struct mountlist_itself *mountlist);
419
420
421/**
422 * @name Restore-Time Globals
423 * @ingroup globalGroup
424 * @{
425 */
426/**
427 * If TRUE, then SIGPIPE was just caught.
428 * Set by the signal handler; cleared after it's handled.
429 */
430bool g_sigpipe_caught = FALSE;
431
432/**
433 * If TRUE, then we're restoring from ISOs or an NFS server.
434 * If FALSE, then we're restoring from some kind of real media (tape, CD, etc.)
435 */
436bool g_ISO_restore_mode = FALSE;    /* are we in Iso Mode? */
437
438/**
439 * If TRUE, then we have had a successful "nuke" restore.
440 */
441bool g_I_have_just_nuked = FALSE;
442
443/**
444 * The device to mount to get at the ISO images. Ignored unless @p g_ISO_restore_mode.
445 */
446char *g_isodir_device;
447
448/**
449 * The format of @p g_isodir_device. Ignored unless @p g_ISO_restore_mode.
450 */
451char *g_isodir_format;
452
453/**
454 * The location of 'biggielist.txt', containing the biggiefiles on the current archive set.
455 */
456char *g_biggielist_txt;
457
458/**
459 * The location of 'filelist.full', containing all files (<em>including biggiefiles</em>) on
460 * the current archive set.
461 */
462char *g_filelist_full;
463
464/**
465 * The location of a file containing a list of the devices that were archived
466 * as images, not as individual files.
467 */
468char *g_filelist_imagedevs;
469
470/**
471 * The location of a file containing a list of imagedevs to actually restore.
472 * @see g_filelist_imagedevs
473 */
474char *g_imagedevs_restthese;
475
476/**
477 * The location of 'mondo-restore.cfg', containing the metadata
478 * information for this backup.
479 */
480char *g_mondo_cfg_file;
481
482/**
483 * The location of 'mountlist.txt', containing the information on the
484 * user's partitions and hard drives.
485 */
486char *g_mountlist_fname;
487
488/**
489 * Mondo's home directory during backup. Unused in mondo-restore; included
490 * to avoid link errors.
491 */
492char *g_mondo_home;
493
494/* @} - end of "Restore-Time Globals" in globalGroup */
495
496
497
498extern int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived,
499                                 char direction);
500
501
502
503/**************************************************************************
504 * COMPAQ PROLIANT Stuff:  needs some special help                        *
505**************************************************************************/
506
507/**
508 * The message to display if we detect that the user is using a Compaq Proliant.
509 */
510#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 CD/floppy in Interactive Mode."
511
512
513
514
515/**
516 * Allow the user to modify the mountlist before we partition & format their drives.
517 * @param bkpinfo The backup information structure. @c disaster_recovery is the only field used.
518 * @param mountlist The mountlist to let the user modify.
519 * @param raidlist The raidlist that goes with @p mountlist.
520 * @return 0 for success, nonzero for failure.
521 * @ingroup restoreGuiGroup
522 */
523int let_user_edit_the_mountlist(struct s_bkpinfo *bkpinfo,
524                                struct mountlist_itself *mountlist,
525                                struct raidlist_itself *raidlist)
526{
527    int retval = 0, res = 0;
528
529    log_msg(2, "let_user_edit_the_mountlist() --- starting");
530
531    assert(bkpinfo != NULL);
532    assert(mountlist != NULL);
533    assert(raidlist != NULL);
534    if (!bkpinfo->disaster_recovery) {
535        strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
536        log_msg(2, "I guess you're testing edit_mountlist()");
537    }
538    if (!does_file_exist(g_mountlist_fname)) {
539        log_to_screen(g_mountlist_fname);
540        log_to_screen("does not exist");
541        return (1);
542    }
543
544    retval = load_mountlist(mountlist, g_mountlist_fname);
545    load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
546    if (retval) {
547        log_to_screen
548            ("Warning - load_raidtab_into_raidlist returned an error");
549    }
550    res = edit_mountlist(g_mountlist_fname, mountlist, raidlist);
551    if (res) {
552        return (1);
553    }
554
555    save_mountlist_to_disk(mountlist, g_mountlist_fname);
556    save_raidlist_to_raidtab(raidlist, RAIDTAB_FNAME);
557
558    log_to_screen("I have finished editing the mountlist for you.");
559
560    return (retval);
561}
562
563
564
565
566
567/**
568 * Determine whether @p mountlist contains a Compaq diagnostic partition.
569 * @param mountlist The mountlist to examine.
570 * @return TRUE if there's a Compaq diagnostic partition; FALSE if not.
571 * @ingroup restoreUtilityGroup
572 */
573bool
574partition_table_contains_Compaq_diagnostic_partition(struct
575                                                     mountlist_itself *
576                                                     mountlist)
577{
578    int i;
579
580    assert(mountlist != NULL);
581
582    for (i = 0; i < mountlist->entries; i++) {
583        if (strstr(mountlist->el[i].format, "ompaq")) {
584            log_msg(2, "mountlist[%d] (%s) is %s (Compaq alert!)",
585                    i, mountlist->el[i].device, mountlist->el[i].format);
586
587            return (TRUE);
588        }
589    }
590    return (FALSE);
591}
592
593/**************************************************************************
594 *END_PARTITION_TABLE_CONTAINS_COMPAQ_DIAGNOSTIC_PARTITION                *
595 **************************************************************************/
596
597
598/**
599 * Allow the user to abort the backup if we find that there is a Compaq diagnostic partition.
600 * @note This function does not actually check for the presence of a Compaq partition.
601 * @ingroup restoreUtilityGroup
602 */
603void offer_to_abort_because_Compaq_Proliants_suck(void)
604{
605    popup_and_OK(COMPAQ_PROLIANTS_SUCK);
606    if (ask_me_yes_or_no
607        ("Would you like to reboot and use your Compaq CD to prep your hard drive?"))
608    {
609        fatal_error
610            ("Aborting. Please reboot and prep your hard drive with your Compaq CD.");
611    }
612}
613
614/**************************************************************************
615 *END_OFFER_TO_ABORT_BECAUSE_COMPAQ_PROLIANTS_SUCK                        *
616 **************************************************************************/
617
618
619
620/**
621 * Call interactive_mode(), nuke_mode(), or compare_mode() depending on the user's choice.
622 * @param bkpinfo The backup information structure. Most fields are used.
623 * @param mountlist The mountlist containing information about the user's partitions.
624 * @param raidlist The raidlist to go with @p mountlist.
625 * @return The return code from the mode function called.
626 * @ingroup restoreGroup
627 */
628int
629catchall_mode(struct s_bkpinfo *bkpinfo,
630              struct mountlist_itself *mountlist,
631              struct raidlist_itself *raidlist)
632{
633    char c, *tmp;
634    int retval = 0;
635
636    iamhere("inside catchall");
637    assert(bkpinfo != NULL);
638    assert(mountlist != NULL);
639    assert(raidlist != NULL);
640    malloc_string(tmp);
641    iamhere("pre wrm");
642    c = which_restore_mode();
643    iamhere("post wrm");
644    if (c == 'I' || c == 'N' || c == 'C') {
645        interactively_obtain_media_parameters_from_user(bkpinfo, FALSE);
646    } else {
647        popup_and_OK("No restoring or comparing will take place today.");
648        if (is_this_device_mounted("/mnt/cdrom")) {
649            run_program_and_log_output("umount /mnt/cdrom", FALSE);
650        }
651        if (g_ISO_restore_mode) {
652            sprintf(tmp, "umount %s", bkpinfo->isodir);
653            run_program_and_log_output(tmp, FALSE);
654        }
655        paranoid_MR_finish(0);
656    }
657
658    iamhere("post int");
659
660    if (bkpinfo->backup_media_type == iso) {
661        if (iso_fiddly_bits(bkpinfo, (c == 'N') ? TRUE : FALSE)) {
662            log_msg(2,
663                    "catchall_mode --- iso_fiddly_bits returned w/ error");
664            return (1);
665        } else {
666            log_msg(2, "catchall_mode --- iso_fiddly_bits ok");
667        }
668    }
669
670    if (c == 'I') {
671        log_msg(2, "IM selected");
672        retval += interactive_mode(bkpinfo, mountlist, raidlist);
673    } else if (c == 'N') {
674        log_msg(2, "NM selected");
675        retval += nuke_mode(bkpinfo, mountlist, raidlist);
676    } else if (c == 'C') {
677        log_msg(2, "CM selected");
678        retval += compare_mode(bkpinfo, mountlist, raidlist);
679    }
680    paranoid_free(tmp);
681    return (retval);
682}
683
684/**************************************************************************
685 *END_CATCHALL_MODE                                                      *
686 **************************************************************************/
687
688/**************************************************************************
689 *END_  EXTRACT_CONFIG_FILE_FROM_RAMDISK                                  *
690 **************************************************************************/
691
692
693/**
694 * Locate an executable in the directory structure rooted at @p restg.
695 * @param out_path Where to put the executable.
696 * @param fname The basename of the executable.
697 * @param restg The directory structure to look in.
698 * @note If it could not be found in @p restg then @p fname is put in @p out_path.
699 * @ingroup restoreUtilityGroup
700 */
701void
702find_pathname_of_executable_preferably_in_RESTORING(char *out_path,
703                                                    char *fname,
704                                                    char *restg)
705{
706    assert(out_path != NULL);
707    assert_string_is_neither_NULL_nor_zerolength(fname);
708
709    sprintf(out_path, "%s/sbin/%s", restg, fname);
710    if (does_file_exist(out_path)) {
711        sprintf(out_path, "%s/usr/sbin/%s", restg, fname);
712        if (does_file_exist(out_path)) {
713            sprintf(out_path, "%s/bin/%s", restg, fname);
714            if (does_file_exist(out_path)) {
715                sprintf(out_path, "%s/usr/bin/%s", restg, fname);
716                if (does_file_exist(out_path)) {
717                    strcpy(out_path, fname);
718                }
719            }
720        }
721    }
722}
723
724/**************************************************************************
725 *END_FIND_PATHNAME_OF_EXECUTABLE_PREFERABLY_IN_RESTORING                 *
726 **************************************************************************/
727
728
729
730
731/**
732 * @addtogroup restoreGroup
733 * @{
734 */
735/**
736 * Restore the user's data, in a disaster recovery situation, prompting the
737 * user about whether or not to do every step.
738 * The user can edit the mountlist, choose files to restore, etc.
739 * @param bkpinfo The backup information structure. Most fields are used.
740 * @param mountlist The mountlist containing information about the user's partitions.
741 * @param raidlist The raidlist to go with @p mountlist.
742 * @return 0 for success, or the number of errors encountered.
743 */
744int
745interactive_mode(struct s_bkpinfo *bkpinfo,
746                 struct mountlist_itself *mountlist,
747                 struct raidlist_itself *raidlist)
748{
749    int retval = 0;
750    int res;
751    int ptn_errs = 0;
752    int fmt_errs = 0;
753
754    bool done;
755    bool restore_all;
756
757  /** needs malloc **********/
758    char *tmp;
759    char *fstab_fname;
760    char *old_restpath;
761
762    struct s_node *filelist;
763
764    /* try to partition and format */
765
766    log_msg(2, "interactive_mode --- starting (great, assertions OK)");
767
768    malloc_string(tmp);
769    malloc_string(fstab_fname);
770    malloc_string(old_restpath);
771    assert(bkpinfo != NULL);
772    assert(mountlist != NULL);
773    assert(raidlist != NULL);
774
775    log_msg(2, "interactive_mode --- assertions OK");
776
777    if (g_text_mode) {
778        if (!ask_me_yes_or_no
779            ("Interactive Mode + textonly = experimental! Proceed anyway?"))
780        {
781            fatal_error("Wise move.");
782        }
783    }
784
785    iamhere("About to load config file");
786    get_cfg_file_from_archive_or_bust(bkpinfo);
787    read_cfg_file_into_bkpinfo(g_mondo_cfg_file, bkpinfo);
788    iamhere("Done loading config file; resizing ML");
789#ifdef __FreeBSD__
790    if (strstr
791        (call_program_and_get_last_line_of_output("cat /tmp/cmdline"),
792         "noresize"))
793#else
794    if (strstr
795        (call_program_and_get_last_line_of_output("cat /proc/cmdline"),
796         "noresize"))
797#endif
798    {
799        log_msg(1, "Not resizing mountlist.");
800    } else {
801        resize_mountlist_proportionately_to_suit_new_drives(mountlist);
802    }
803    for (done = FALSE; !done;) {
804        iamhere("About to edit mountlist");
805        if (g_text_mode) {
806            save_mountlist_to_disk(mountlist, g_mountlist_fname);
807            sprintf(tmp, "%s %s", find_my_editor(), g_mountlist_fname);
808            res = system(tmp);
809            load_mountlist(mountlist, g_mountlist_fname);
810        } else {
811            res = edit_mountlist(g_mountlist_fname, mountlist, raidlist);
812        }
813        iamhere("Finished editing mountlist");
814        if (res) {
815            paranoid_MR_finish(1);
816        }
817        log_msg(2, "Proceeding...");
818        save_mountlist_to_disk(mountlist, g_mountlist_fname);
819        save_raidlist_to_raidtab(raidlist, RAIDTAB_FNAME);
820        mvaddstr_and_log_it(1, 30, "Restoring Interactively");
821        if (bkpinfo->differential) {
822            log_to_screen("Because this is a differential backup, disk");
823            log_to_screen
824                (" partitioning and formatting will not take place.");
825            done = TRUE;
826        } else {
827            if (ask_me_yes_or_no
828                ("Do you want to erase and partition your hard drives?")) {
829                if (partition_table_contains_Compaq_diagnostic_partition
830                    (mountlist)) {
831                    offer_to_abort_because_Compaq_Proliants_suck();
832                    done = TRUE;
833                } else {
834                    twenty_seconds_til_yikes();
835                    g_fprep = fopen("/tmp/prep.sh", "w");
836                    ptn_errs = partition_everything(mountlist);
837                    if (ptn_errs) {
838                        log_to_screen
839                            ("Warning. Errors occurred during disk partitioning.");
840                    }
841
842                    fmt_errs = format_everything(mountlist, FALSE);
843                    if (!fmt_errs) {
844                        log_to_screen
845                            ("Errors during disk partitioning were handled OK.");
846                        log_to_screen
847                            ("Partitions were formatted OK despite those errors.");
848                        ptn_errs = 0;
849                    }
850                    if (!ptn_errs && !fmt_errs) {
851                        done = TRUE;
852                    }
853                }
854                paranoid_fclose(g_fprep);
855            } else {
856                mvaddstr_and_log_it(g_currentY++, 0,
857                                    "User opted not to partition the devices");
858                if (ask_me_yes_or_no
859                    ("Do you want to format your hard drives?")) {
860                    fmt_errs = format_everything(mountlist, TRUE);
861                    if (!fmt_errs) {
862                        done = TRUE;
863                    }
864                } else {
865                    ptn_errs = fmt_errs = 0;
866                    done = TRUE;
867                }
868            }
869            if (fmt_errs) {
870                mvaddstr_and_log_it(g_currentY++,
871                                    0,
872                                    "Errors occurred. Please repartition and format drives manually.");
873                done = FALSE;
874            }
875            if (ptn_errs & !fmt_errs) {
876                mvaddstr_and_log_it(g_currentY++,
877                                    0,
878                                    "Errors occurred during partitioning. Formatting, however, went OK.");
879                done = TRUE;
880            }
881            if (!done) {
882                if (!ask_me_yes_or_no("Re-edit the mountlist?")) {
883                    retval++;
884                    goto end_of_func;
885                }
886            }
887        }
888    }
889
890    /* mount */
891    if (mount_all_devices(mountlist, TRUE)) {
892        unmount_all_devices(mountlist);
893        retval++;
894        goto end_of_func;
895    }
896    /* restore */
897    if ((restore_all =
898         ask_me_yes_or_no("Do you want me to restore all of your data?")))
899    {
900        log_msg(1, "Restoring all data");
901        retval += restore_everything(bkpinfo, NULL);
902    } else
903        if ((restore_all =
904             ask_me_yes_or_no
905             ("Do you want me to restore _some_ of your data?"))) {
906        strcpy(old_restpath, bkpinfo->restore_path);
907        for (done = FALSE; !done;) {
908            unlink("/tmp/filelist.full");
909            filelist = process_filelist_and_biggielist(bkpinfo);
910            /* Now you have /tmp/tmpfs/filelist.restore-these and /tmp/tmpfs/biggielist.restore-these;
911               the former is a list of regular files; the latter, biggiefiles and imagedevs.
912             */
913            if (filelist) {
914              gotos_suck:
915                strcpy(tmp, old_restpath);
916// (NB: %s is where your filesystem is mounted now, by default)", MNT_RESTORING);
917                if (popup_and_get_string
918                    ("Restore path", "Restore files to where?", tmp,
919                     MAX_STR_LEN / 4)) {
920                    if (!strcmp(tmp, "/")) {
921                        if (!ask_me_yes_or_no("Are you sure?")) {
922                            goto gotos_suck;
923                        }
924                        tmp[0] = '\0';  // so we restore to [blank]/file/name :)
925                    }
926                    strcpy(bkpinfo->restore_path, tmp);
927                    log_msg(1, "Restoring subset");
928                    retval += restore_everything(bkpinfo, filelist);
929                    free_filelist(filelist);
930                } else {
931                    strcpy(bkpinfo->restore_path, old_restpath);
932                    free_filelist(filelist);
933                }
934                if (!ask_me_yes_or_no
935                    ("Restore another subset of your backup?")) {
936                    done = TRUE;
937                }
938            } else {
939                done = TRUE;
940            }
941        }
942        strcpy(old_restpath, bkpinfo->restore_path);
943    } else {
944        mvaddstr_and_log_it(g_currentY++,
945                            0,
946                            "User opted not to restore any data.                                  ");
947    }
948    if (retval) {
949        mvaddstr_and_log_it(g_currentY++,
950                            0,
951                            "Errors occurred during the restore phase.            ");
952    }
953
954    if (ask_me_yes_or_no("Initialize the boot loader?")) {
955        run_boot_loader(TRUE);
956    } else {
957        mvaddstr_and_log_it(g_currentY++,
958                            0,
959                            "User opted not to initialize the boot loader.");
960    }
961
962//  run_program_and_log_output("cp -af /etc/lvm " MNT_RESTORING "/etc/", 1);
963    protect_against_braindead_sysadmins();
964    //  modify_rclocal_one_time( MNT_RESTORING "/etc" );
965    retval += unmount_all_devices(mountlist);
966    /*  if (restore_some || restore_all || */
967    if (ask_me_yes_or_no
968        ("Label your ext2 and ext3 partitions if necessary?")) {
969        mvaddstr_and_log_it(g_currentY, 0,
970                            "Using e2label to label your ext2,3 partitions");
971        if (does_file_exist("/tmp/fstab.new")) {
972            strcpy(fstab_fname, "/tmp/fstab.new");
973        } else {
974            strcpy(fstab_fname, "/tmp/fstab");
975        }
976        sprintf(tmp,
977                "label-partitions-as-necessary %s < %s >> %s 2>> %s",
978                g_mountlist_fname, fstab_fname, MONDO_LOGFILE,
979                MONDO_LOGFILE);
980        res = system(tmp);
981        if (res) {
982            log_to_screen
983                ("label-partitions-as-necessary returned an error");
984            mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
985        } else {
986            mvaddstr_and_log_it(g_currentY++, 74, "Done.");
987        }
988        retval += res;
989    }
990
991    iamhere("About to leave interactive_mode()");
992    if (retval) {
993        mvaddstr_and_log_it(g_currentY++,
994                            0,
995                            "Warning - errors occurred during the restore phase.");
996    }
997  end_of_func:
998    paranoid_free(tmp);
999    paranoid_free(fstab_fname);
1000    paranoid_free(old_restpath);
1001    iamhere("Leaving interactive_mode()");
1002    return (retval);
1003}
1004
1005/**************************************************************************
1006 *END_INTERACTIVE_MODE                                                    *
1007 **************************************************************************/
1008
1009
1010
1011/**
1012 * Run an arbitrary restore mode (prompt the user), but from ISO images
1013 * instead of real media.
1014 * @param bkpinfo The backup information structure. Most fields are used.
1015 * @param mountlist The mountlist containing information about the user's partitions.
1016 * @param raidlist The raidlist that goes with @p mountlist.
1017 * @param nuke_me_please If TRUE, we plan to run Nuke Mode.
1018 * @return 0 for success, or the number of errors encountered.
1019 */
1020int
1021iso_mode(struct s_bkpinfo *bkpinfo,
1022         struct mountlist_itself *mountlist,
1023         struct raidlist_itself *raidlist, bool nuke_me_please)
1024{
1025    char c;
1026    int retval = 0;
1027
1028    assert(bkpinfo != NULL);
1029    assert(mountlist != NULL);
1030    assert(raidlist != NULL);
1031    if (iso_fiddly_bits(bkpinfo, nuke_me_please)) {
1032        log_msg(1, "iso_mode --- returning w/ error");
1033        return (1);
1034    } else {
1035        c = which_restore_mode();
1036        if (c == 'I' || c == 'N' || c == 'C') {
1037            interactively_obtain_media_parameters_from_user(bkpinfo,
1038                                                            FALSE);
1039        }
1040        if (c == 'I') {
1041            retval += interactive_mode(bkpinfo, mountlist, raidlist);
1042        } else if (c == 'N') {
1043            retval += nuke_mode(bkpinfo, mountlist, raidlist);
1044        } else if (c == 'C') {
1045            retval += compare_mode(bkpinfo, mountlist, raidlist);
1046        } else {
1047            log_to_screen("OK, I shan't restore/compare any files.");
1048        }
1049    }
1050    if (is_this_device_mounted(MNT_CDROM)) {
1051        paranoid_system("umount " MNT_CDROM);
1052    }
1053//  if (! already_mounted)
1054//    {
1055    if (system("umount /tmp/isodir 2> /dev/null")) {
1056        log_to_screen
1057            ("WARNING - unable to unmount device where the ISO files are stored.");
1058    }
1059//    }
1060    return (retval);
1061}
1062
1063/**************************************************************************
1064 *END_ISO_MODE                                                            *
1065 **************************************************************************/
1066
1067
1068/*            MONDO - saving your a$$ since Feb 18th, 2000            */
1069
1070
1071
1072
1073/**
1074 * Restore the user's data automatically (no prompts), after a twenty-second
1075 * warning period.
1076 * @param bkpinfo The backup information structure. Most fields are used.
1077 * @param mountlist The mountlist containing information about the user's partitions.
1078 * @param raidlist The raidlist that goes with @p mountlist.
1079 * @return 0 for success, or the number of errors encountered.
1080 * @warning <b><i>THIS WILL ERASE ALL EXISTING DATA!</i></b>
1081 */
1082int
1083nuke_mode(struct s_bkpinfo *bkpinfo,
1084          struct mountlist_itself *mountlist,
1085          struct raidlist_itself *raidlist)
1086{
1087    int retval = 0;
1088    int res = 0;
1089    bool boot_loader_installed = FALSE;
1090  /** malloc **/
1091    char tmp[MAX_STR_LEN], tmpA[MAX_STR_LEN], tmpB[MAX_STR_LEN],
1092        tmpC[MAX_STR_LEN];
1093
1094    assert(bkpinfo != NULL);
1095    assert(mountlist != NULL);
1096    assert(raidlist != NULL);
1097
1098    log_msg(2, "nuke_mode --- starting");
1099
1100    get_cfg_file_from_archive_or_bust(bkpinfo);
1101    load_mountlist(mountlist, g_mountlist_fname);   // in case read_cfg_file_into_bkpinfo updated the mountlist
1102#ifdef __FreeBSD__
1103    if (strstr
1104        (call_program_and_get_last_line_of_output("cat /tmp/cmdline"),
1105         "noresize"))
1106#else
1107    if (strstr
1108        (call_program_and_get_last_line_of_output("cat /proc/cmdline"),
1109         "noresize"))
1110#endif
1111    {
1112        log_msg(2, "Not resizing mountlist.");
1113    } else {
1114        resize_mountlist_proportionately_to_suit_new_drives(mountlist);
1115    }
1116    if (!evaluate_mountlist(mountlist, tmpA, tmpB, tmpC)) {
1117        sprintf(tmp,
1118                "Mountlist analyzed. Result: \"%s %s %s\" Switch to Interactive Mode?",
1119                tmpA, tmpB, tmpC);
1120        if (ask_me_yes_or_no(tmp)) {
1121            retval = interactive_mode(bkpinfo, mountlist, raidlist);
1122            finish(retval);
1123        } else {
1124            fatal_error("Nuke Mode aborted. ");
1125        }
1126    }
1127    save_mountlist_to_disk(mountlist, g_mountlist_fname);
1128    mvaddstr_and_log_it(1, 30, "Restoring Automatically");
1129    if (bkpinfo->differential) {
1130        log_to_screen("Because this is a differential backup, disk");
1131        log_to_screen("partitioning and formatting will not take place.");
1132        res = 0;
1133    } else {
1134        if (partition_table_contains_Compaq_diagnostic_partition
1135            (mountlist)) {
1136            offer_to_abort_because_Compaq_Proliants_suck();
1137        } else {
1138            twenty_seconds_til_yikes();
1139            g_fprep = fopen("/tmp/prep.sh", "w");
1140#ifdef __FreeBSD__
1141            if (strstr
1142                (call_program_and_get_last_line_of_output
1143                 ("cat /tmp/cmdline"), "nopart"))
1144#else
1145            if (strstr
1146                (call_program_and_get_last_line_of_output
1147                 ("cat /proc/cmdline"), "nopart"))
1148#endif
1149            {
1150                log_msg(2,
1151                        "Not partitioning drives due to 'nopart' option.");
1152                res = 0;
1153            } else {
1154                res = partition_everything(mountlist);
1155                if (res) {
1156                    log_to_screen
1157                        ("Warning. Errors occurred during partitioning.");
1158                    res = 0;
1159                }
1160            }
1161            retval += res;
1162            if (!res) {
1163                log_to_screen("Preparing to format your disk(s)");
1164                sleep(1);
1165                system("sync");
1166                log_to_screen("Please wait. This may take a few minutes.");
1167                res += format_everything(mountlist, FALSE);
1168            }
1169            paranoid_fclose(g_fprep);
1170        }
1171    }
1172    retval += res;
1173    if (res) {
1174        mvaddstr_and_log_it(g_currentY++,
1175                            0,
1176                            "Failed to partition and/or format your hard drives.");
1177
1178        if (ask_me_yes_or_no("Try in interactive mode instead?")) {
1179            retval = interactive_mode(bkpinfo, mountlist, raidlist);
1180            goto after_the_nuke;
1181        } else
1182            if (!ask_me_yes_or_no
1183                ("Would you like to try to proceed anyway?")) {
1184            return (retval);
1185        }
1186    }
1187    retval = mount_all_devices(mountlist, TRUE);
1188    if (retval) {
1189        unmount_all_devices(mountlist);
1190        log_to_screen
1191            ("Unable to mount all partitions. Sorry, I cannot proceed.");
1192        return (retval);
1193    }
1194    iamhere("Restoring everything");
1195    retval += restore_everything(bkpinfo, NULL);
1196    if (!run_boot_loader(FALSE)) {
1197        log_msg(1,
1198                "Great! Boot loader was installed. No need for msg at end.");
1199        boot_loader_installed = TRUE;
1200    }
1201    protect_against_braindead_sysadmins();
1202//  run_program_and_log_output("cp -af /etc/lvm " MNT_RESTORING "/etc/", 1);
1203    //  modify_rclocal_one_time( MNT_RESTORING "/etc" );
1204    retval += unmount_all_devices(mountlist);
1205    mvaddstr_and_log_it(g_currentY,
1206                        0,
1207                        "Using e2label to label your ext2,3 partitions");
1208
1209    sprintf(tmp, "label-partitions-as-necessary %s < /tmp/fstab",
1210            g_mountlist_fname);
1211    res = run_program_and_log_output(tmp, TRUE);
1212    if (res) {
1213        log_to_screen("label-partitions-as-necessary returned an error");
1214        mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1215    } else {
1216        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1217    }
1218    retval += res;
1219
1220  after_the_nuke:
1221    if (retval) {
1222        log_to_screen("Errors occurred during the nuke phase.");
1223    } else if (strstr(call_program_and_get_last_line_of_output("cat /proc/cmdline"), "RESTORE"))    // Bruno's thing
1224    {
1225        log_to_screen
1226            ("PC was restored successfully. Thank you for using Mondo Rescue.");
1227        log_to_screen
1228            ("Please visit http://www.mondorescue.org and thank the dev team.");
1229    } else {
1230#ifdef FREELOADER
1231        success_message();
1232#else
1233        log_to_screen("PC was restored successfully!");
1234#endif
1235    }
1236    g_I_have_just_nuked = TRUE;
1237/*
1238  if (!boot_loader_installed && !does_file_exist(DO_MBR_PLEASE))
1239    {
1240      log_to_screen("PLEASE RUN 'mondorestore --mbr' NOW TO INITIALIZE YOUR BOOT SECTOR");
1241      write_one_liner_data_file(DO_MBR_PLEASE, "mondorestore --mbr");
1242    }
1243*/
1244    return (retval);
1245}
1246
1247/**************************************************************************
1248 *END_NUKE_MODE                                                           *
1249 **************************************************************************/
1250
1251
1252
1253/**
1254 * Restore the user's data (or a subset of it) to the live filesystem.
1255 * This should not be called if we're booted from CD!
1256 * @param bkpinfo The backup information structure. Most fields are used.
1257 * @return 0 for success, or the number of errors encountered.
1258 */
1259int restore_to_live_filesystem(struct s_bkpinfo *bkpinfo)
1260{
1261    int retval = 0;
1262
1263  /** malloc **/
1264    char *old_restpath;
1265
1266    struct mountlist_itself *mountlist;
1267//  static
1268    struct raidlist_itself *raidlist;
1269    struct s_node *filelist;
1270
1271    log_msg(1, "restore_to_live_filesystem() - starting");
1272    assert(bkpinfo != NULL);
1273    malloc_string(old_restpath);
1274    mountlist = malloc(sizeof(struct mountlist_itself));
1275    raidlist = malloc(sizeof(struct raidlist_itself));
1276    if (!mountlist || !raidlist) {
1277        fatal_error("Cannot malloc() mountlist and/or raidlist");
1278    }
1279
1280    strcpy(bkpinfo->restore_path, "/");
1281    if (!g_restoring_live_from_cd) {
1282        popup_and_OK
1283            ("Please insert tape/CD/boot floppy, then hit 'OK' to continue.");
1284        sleep(1);
1285    }
1286    interactively_obtain_media_parameters_from_user(bkpinfo, FALSE);
1287    if (bkpinfo->media_device == NULL) {
1288        log_msg(2, "Warning - failed to find media dev");
1289    } else {
1290        log_msg(2, "bkpinfo->media_device = %s", bkpinfo->media_device);
1291    }
1292
1293
1294    log_msg(2, "bkpinfo->isodir = %s", bkpinfo->isodir);
1295
1296    open_evalcall_form("Thinking...");
1297
1298    get_cfg_file_from_archive_or_bust(bkpinfo);
1299    read_cfg_file_into_bkpinfo(g_mondo_cfg_file, bkpinfo);
1300    load_mountlist(mountlist, g_mountlist_fname);   // in case read_cfg_file_into_bkpinfo
1301
1302    close_evalcall_form();
1303    retval = load_mountlist(mountlist, g_mountlist_fname);
1304    load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
1305    filelist = process_filelist_and_biggielist(bkpinfo);
1306    if (filelist) {
1307        save_filelist(filelist, "/tmp/selected-files.txt");
1308        strcpy(old_restpath, bkpinfo->restore_path);
1309        if (popup_and_get_string("Restore path",
1310                                 "Restore files to where? )",
1311                                 bkpinfo->restore_path, MAX_STR_LEN / 4)) {
1312            iamhere("Restoring everything");
1313            retval += restore_everything(bkpinfo, filelist);
1314            free_filelist(filelist);
1315            strcpy(bkpinfo->restore_path, old_restpath);
1316        } else {
1317            free_filelist(filelist);
1318        }
1319        strcpy(bkpinfo->restore_path, old_restpath);
1320    }
1321    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1322        log_msg(2,
1323                "I probably don't need to unmount or eject the CD-ROM but I'm doing it anyway.");
1324    }
1325    run_program_and_log_output("umount " MNT_CDROM, FALSE);
1326    if ((!bkpinfo->please_dont_eject) && (bkpinfo->media_device != NULL)) {
1327        eject_device(bkpinfo->media_device);
1328    }
1329    paranoid_free(old_restpath);
1330    free(mountlist);
1331    free(raidlist);
1332    return (retval);
1333}
1334
1335/**************************************************************************
1336 *END_RESTORE_TO_LIVE_FILESYSTEM                                          *
1337 **************************************************************************/
1338
1339/* @} - end of restoreGroup */
1340
1341
1342#include <utime.h>
1343/**
1344 * @addtogroup LLrestoreGroup
1345 * @{
1346 */
1347/**
1348 * Restore biggiefile @p bigfileno from the currently mounted CD.
1349 * @param bkpinfo The backup information structure. Fields used:
1350 * - @c bkpinfo->backup_media_type
1351 * - @c bkpinfo->restore_path
1352 * @param bigfileno The biggiefile number (starting from 0) to restore.
1353 * @param filelist The node structure containing the list of files to restore.
1354 * If the biggiefile is not in this list, it will be skipped (return value will
1355 * still indicate success).
1356 * @return 0 for success (or skip), nonzero for failure.
1357 */
1358int
1359restore_a_biggiefile_from_CD(struct s_bkpinfo *bkpinfo,
1360                             long bigfileno,
1361                             struct s_node *filelist,
1362                             char *pathname_of_last_file_restored)
1363{
1364    FILE *fin;
1365    FILE *fout;
1366    FILE *fbzip2;
1367
1368  /** malloc ***/
1369    char *checksum, *outfile_fname, *tmp, *bzip2_command,
1370        *partimagehack_command, *suffix, *sz_devfile;
1371    char *bigblk;
1372    char *p;
1373    int retval = 0;
1374    int finished = FALSE;
1375    long sliceno;
1376    long siz;
1377    char partimagehack_fifo[MAX_STR_LEN];
1378    char *file_to_openout = NULL;
1379    struct s_filename_and_lstat_info biggiestruct;
1380    struct utimbuf the_utime_buf, *ubuf;
1381    bool use_partimage_hack = FALSE;
1382    pid_t pid;
1383    int res = 0;
1384    int old_loglevel;
1385    char sz_msg[MAX_STR_LEN];
1386    struct s_node *node;
1387
1388    old_loglevel = g_loglevel;
1389    ubuf = &the_utime_buf;
1390    assert(bkpinfo != NULL);
1391
1392    malloc_string(checksum);
1393    malloc_string(outfile_fname);
1394    malloc_string(tmp);
1395    malloc_string(bzip2_command);
1396    malloc_string(partimagehack_command);
1397    malloc_string(suffix);
1398    malloc_string(sz_devfile);
1399
1400    pathname_of_last_file_restored[0] = '\0';
1401    if (!(bigblk = malloc(TAPE_BLOCK_SIZE))) {
1402        fatal_error("Cannot malloc bigblk");
1403    }
1404
1405    if (!(fin = fopen(slice_fname(bigfileno, 0, ARCHIVES_PATH, ""), "r"))) {
1406        log_to_screen("Cannot even open bigfile's info file");
1407        return (1);
1408    }
1409
1410    memset((void *) &biggiestruct, 0, sizeof(biggiestruct));
1411    if (fread((void *) &biggiestruct, 1, sizeof(biggiestruct), fin) <
1412        sizeof(biggiestruct)) {
1413        log_msg(2, "Warning - unable to get biggiestruct of bigfile #%d",
1414                bigfileno + 1);
1415    }
1416    paranoid_fclose(fin);
1417
1418    strcpy(checksum, biggiestruct.checksum);
1419
1420    if (!checksum[0]) {
1421        sprintf(tmp, "Warning - bigfile %ld does not have a checksum",
1422                bigfileno + 1);
1423        log_msg(3, tmp);
1424        p = checksum;
1425    }
1426
1427    if (!strncmp(biggiestruct.filename, "/dev/", 5))    // Whether NTFS or not :)
1428    {
1429        strcpy(outfile_fname, biggiestruct.filename);
1430    } else {
1431        sprintf(outfile_fname, "%s/%s", bkpinfo->restore_path,
1432                biggiestruct.filename);
1433    }
1434
1435    /* skip file if we have a selective restore subset & it doesn't match */
1436    if (filelist != NULL) {
1437        node = find_string_at_node(filelist, biggiestruct.filename);
1438        if (!node) {
1439            log_msg(0, "Skipping %s (name isn't in filelist)",
1440                    biggiestruct.filename);
1441            pathname_of_last_file_restored[0] = '\0';
1442            return (0);
1443        } else if (!(node->selected)) {
1444            log_msg(1, "Skipping %s (name isn't in biggielist subset)",
1445                    biggiestruct.filename);
1446            pathname_of_last_file_restored[0] = '\0';
1447            return (0);
1448        }
1449    }
1450    /* otherwise, continue */
1451
1452    log_msg(1, "DEFINITELY restoring %s", biggiestruct.filename);
1453    if (biggiestruct.use_partimagehack) {
1454        if (strncmp(biggiestruct.filename, "/dev/", 5)) {
1455            log_msg(1,
1456                    "I was in error when I set biggiestruct.use_partimagehack to TRUE.");
1457            log_msg(1, "%s isn't even in /dev", biggiestruct.filename);
1458            biggiestruct.use_partimagehack = FALSE;
1459        }
1460    }
1461
1462    if (biggiestruct.use_partimagehack) // if it's an NTFS device
1463//  if (!strncmp ( biggiestruct.filename, "/dev/", 5))
1464    {
1465        g_loglevel = 4;
1466        use_partimage_hack = TRUE;
1467        log_msg(2,
1468                "Calling partimagehack in background because %s is an NTFS /dev entry",
1469                outfile_fname);
1470        sprintf(sz_devfile, "/tmp/%d.%d.000", (int) (random() % 32768),
1471                (int) (random() % 32768));
1472        mkfifo(sz_devfile, 0x770);
1473        strcpy(partimagehack_fifo, sz_devfile);
1474        file_to_openout = partimagehack_fifo;
1475        switch (pid = fork()) {
1476        case -1:
1477            fatal_error("Fork failure");
1478        case 0:
1479            log_msg(3,
1480                    "CHILD - fip - calling feed_outfrom_partimage(%s, %s)",
1481                    biggiestruct.filename, partimagehack_fifo);
1482            res =
1483                feed_outfrom_partimage(biggiestruct.filename,
1484                                       partimagehack_fifo);
1485//          log_msg(3, "CHILD - fip - exiting");
1486            exit(res);
1487            break;
1488        default:
1489            log_msg(3,
1490                    "feed_into_partimage() called in background --- pid=%ld",
1491                    (long int) (pid));
1492        }
1493    } else {
1494        use_partimage_hack = FALSE;
1495        partimagehack_fifo[0] = '\0';
1496        file_to_openout = outfile_fname;
1497        if (!does_file_exist(outfile_fname))    // yes, it looks weird with the '!' but it's correct that way
1498        {
1499            make_hole_for_file(outfile_fname);
1500        }
1501    }
1502
1503    sprintf(tmp, "Reassembling big file %ld (%s)", bigfileno + 1,
1504            outfile_fname);
1505    log_msg(2, tmp);
1506
1507    /*
1508       last slice is zero-length and uncompressed; when we find it, we stop.
1509       We DON'T wait until there are no more slices; if we did that,
1510       We might stop at end of CD, not at last slice (which is 0-len and uncompd)
1511     */
1512
1513    strncpy(pathname_of_last_file_restored, biggiestruct.filename,
1514            MAX_STR_LEN - 1);
1515    pathname_of_last_file_restored[MAX_STR_LEN - 1] = '\0';
1516
1517    log_msg(3, "file_to_openout = %s", file_to_openout);
1518    if (!(fout = fopen(file_to_openout, "w"))) {
1519        log_to_screen("Cannot openout outfile_fname - hard disk full?");
1520        return (1);
1521    }
1522    log_msg(3, "Opened out to %s", outfile_fname);  // CD/DVD --> mondorestore --> partimagehack --> hard disk itself
1523
1524    for (sliceno = 1, finished = FALSE; !finished;) {
1525        if (!does_file_exist
1526            (slice_fname(bigfileno, sliceno, ARCHIVES_PATH, ""))
1527            &&
1528            !does_file_exist(slice_fname
1529                             (bigfileno, sliceno, ARCHIVES_PATH, "lzo"))
1530            &&
1531            !does_file_exist(slice_fname
1532                             (bigfileno, sliceno, ARCHIVES_PATH, "bz2"))) {
1533            log_msg(3,
1534                    "Cannot find a data slice or terminator slice on CD %d",
1535                    g_current_media_number);
1536            g_current_media_number++;
1537            sprintf(tmp,
1538                    "Asking for %s #%d so that I may read slice #%ld\n",
1539                    media_descriptor_string(bkpinfo->backup_media_type),
1540                    g_current_media_number, sliceno);
1541            log_msg(2, tmp);
1542            sprintf(tmp, "Restoring from %s #%d",
1543                    media_descriptor_string(bkpinfo->backup_media_type),
1544                    g_current_media_number);
1545            log_to_screen(tmp);
1546            insist_on_this_cd_number(bkpinfo, g_current_media_number);
1547            log_to_screen("Continuing to restore.");
1548        } else {
1549            strcpy(tmp,
1550                   slice_fname(bigfileno, sliceno, ARCHIVES_PATH, ""));
1551            if (does_file_exist(tmp) && length_of_file(tmp) == 0) {
1552                log_msg(2,
1553                        "End of bigfile # %ld (slice %ld is the terminator)",
1554                        bigfileno + 1, sliceno);
1555                finished = TRUE;
1556                continue;
1557            } else {
1558                if (does_file_exist
1559                    (slice_fname
1560                     (bigfileno, sliceno, ARCHIVES_PATH, "lzo"))) {
1561                    strcpy(bzip2_command, "lzop");
1562                    strcpy(suffix, "lzo");
1563                } else
1564                    if (does_file_exist
1565                        (slice_fname
1566                         (bigfileno, sliceno, ARCHIVES_PATH, "bz2"))) {
1567                    strcpy(bzip2_command, "bzip2");
1568                    strcpy(suffix, "bz2");
1569                } else
1570                    if (does_file_exist
1571                        (slice_fname
1572                         (bigfileno, sliceno, ARCHIVES_PATH, ""))) {
1573                    strcpy(bzip2_command, "");
1574                    strcpy(suffix, "");
1575                } else {
1576                    log_to_screen("OK, that's pretty fsck0red...");
1577                    return (1);
1578                }
1579            }
1580            if (bzip2_command[0] != '\0') {
1581                sprintf(bzip2_command + strlen(bzip2_command),
1582                        " -dc %s 2>> %s",
1583                        slice_fname(bigfileno, sliceno, ARCHIVES_PATH,
1584                                    suffix), MONDO_LOGFILE);
1585            } else {
1586                sprintf(bzip2_command, "cat %s 2>> %s",
1587                        slice_fname(bigfileno, sliceno, ARCHIVES_PATH,
1588                                    suffix), MONDO_LOGFILE);
1589            }
1590            sprintf(tmp, "Working on %s #%d, file #%ld, slice #%ld    ",
1591                    media_descriptor_string(bkpinfo->backup_media_type),
1592                    g_current_media_number, bigfileno + 1, sliceno);
1593            log_msg(2, tmp);
1594
1595            if (!g_text_mode) {
1596                newtDrawRootText(0, g_noof_rows - 2, tmp);
1597                newtRefresh();
1598                strip_spaces(tmp);
1599                update_progress_form(tmp);
1600            }
1601            if (!(fbzip2 = popen(bzip2_command, "r"))) {
1602                fatal_error("Can't run popen command");
1603            }
1604            while (!feof(fbzip2)) {
1605                siz = fread(bigblk, 1, TAPE_BLOCK_SIZE, fbzip2);
1606                if (siz > 0) {
1607                    sprintf(sz_msg, "Read %ld from fbzip2", siz);
1608                    siz = fwrite(bigblk, 1, siz, fout);
1609                    sprintf(sz_msg + strlen(sz_msg),
1610                            "; written %ld to fout", siz);
1611//        log_msg(2. sz_msg);
1612                }
1613            }
1614            paranoid_pclose(fbzip2);
1615
1616
1617            sliceno++;
1618            g_current_progress++;
1619        }
1620    }
1621/*
1622  memset(bigblk, TAPE_BLOCK_SIZE, 1); // This all looks very fishy...
1623  fwrite( bigblk, 1, TAPE_BLOCK_SIZE, fout);
1624  fwrite( bigblk, 1, TAPE_BLOCK_SIZE, fout);
1625  fwrite( bigblk, 1, TAPE_BLOCK_SIZE, fout);
1626  fwrite( bigblk, 1, TAPE_BLOCK_SIZE, fout);
1627*/
1628    paranoid_fclose(fout);
1629    g_loglevel = old_loglevel;
1630
1631    if (use_partimage_hack) {
1632        log_msg(3, "Waiting for partimage to finish");
1633        sprintf(tmp,
1634                " ps ax | grep \" partimagehack \" | grep -v grep > /dev/null 2> /dev/null");
1635        while (system(tmp) == 0) {
1636            sleep(1);
1637        }
1638        log_it("OK, partimage has really finished");
1639    }
1640
1641    if (strcmp(outfile_fname, "/dev/null")) {
1642        chown(outfile_fname, biggiestruct.properties.st_uid,
1643              biggiestruct.properties.st_gid);
1644        chmod(outfile_fname, biggiestruct.properties.st_mode);
1645        ubuf->actime = biggiestruct.properties.st_atime;
1646        ubuf->modtime = biggiestruct.properties.st_mtime;
1647        utime(outfile_fname, ubuf);
1648    }
1649    paranoid_free(bigblk);
1650    paranoid_free(checksum);
1651    paranoid_free(outfile_fname);
1652    paranoid_free(tmp);
1653    paranoid_free(bzip2_command);
1654    paranoid_free(partimagehack_command);
1655    paranoid_free(suffix);
1656    paranoid_free(sz_devfile);
1657
1658    return (retval);
1659}
1660
1661/**************************************************************************
1662 *END_ RESTORE_A_BIGGIEFILE_FROM_CD                                       *
1663 **************************************************************************/
1664
1665
1666
1667/**
1668 * Restore a biggiefile from the currently opened stream.
1669 * @param bkpinfo The backup information structure. Fields used:
1670 * - @c bkpinfo->restore_path
1671 * - @c bkpinfo->zip_exe
1672 * @param orig_bf_fname The original filename of the biggiefile.
1673 * @param biggiefile_number The number of the biggiefile (starting from 0).
1674 * @param orig_checksum Unused.
1675 * @param biggiefile_size Unused.
1676 * @param filelist The node structure containing the list of files to be restored.
1677 * If @p orig_bf_fname is not in the list, it will be ignored.
1678 * @return 0 for success (or skip), nonzero for failure.
1679 * @bug orig_checksum and biggiefile_size are unused (except to check that they are non-NULL).
1680 */
1681int restore_a_biggiefile_from_stream(struct s_bkpinfo *bkpinfo, char *orig_bf_fname, long biggiefile_number, char *orig_checksum,   //UNUSED
1682                                     long long biggiefile_size, //UNUSED
1683                                     struct s_node *filelist,
1684                                     int use_partimagehack,
1685                                     char *pathname_of_last_file_restored)
1686{
1687    FILE *pout;
1688    FILE *fin;
1689
1690  /** mallocs ********/
1691    char *tmp;
1692    char *command;
1693    char *outfile_fname;
1694    char *partimagehack_command;
1695    char *sz_devfile;
1696    char *partimagehack_fifo;
1697    char *file_to_openout = NULL;
1698
1699    struct s_node *node;
1700
1701    int old_loglevel;
1702    long current_slice_number = 0;
1703    int retval = 0;
1704    int res = 0;
1705    int ctrl_chr = '\0';
1706    long long slice_siz;
1707    bool dummy_restore = FALSE;
1708    bool use_partimage_hack = FALSE;
1709    pid_t pid;
1710    struct s_filename_and_lstat_info biggiestruct;
1711    struct utimbuf the_utime_buf, *ubuf;
1712    ubuf = &the_utime_buf;
1713
1714    malloc_string(tmp);
1715    malloc_string(partimagehack_fifo);
1716    malloc_string(outfile_fname);
1717    malloc_string(command);
1718    malloc_string(sz_devfile);
1719    malloc_string(partimagehack_command);
1720    old_loglevel = g_loglevel;
1721    assert(bkpinfo != NULL);
1722    assert(orig_bf_fname != NULL);
1723    assert(orig_checksum != NULL);
1724
1725    pathname_of_last_file_restored[0] = '\0';
1726    if (use_partimagehack == BLK_START_A_PIHBIGGIE) {
1727        use_partimagehack = 1;
1728        log_msg(1, "%s --- pih=YES", orig_bf_fname);
1729    } else if (use_partimagehack == BLK_START_A_NORMBIGGIE) {
1730        use_partimagehack = 0;
1731        log_msg(1, "%s --- pih=NO", orig_bf_fname);
1732    } else {
1733        use_partimagehack = 0;
1734        log_msg(1, "%s --- pih=NO (weird marker though)", orig_bf_fname);
1735    }
1736
1737    strncpy(pathname_of_last_file_restored, orig_bf_fname,
1738            MAX_STR_LEN - 1);
1739    pathname_of_last_file_restored[MAX_STR_LEN - 1] = '\0';
1740
1741    /* open out to biggiefile to be restored (or /dev/null if biggiefile is not to be restored) */
1742
1743    if (filelist != NULL) {
1744        node = find_string_at_node(filelist, orig_bf_fname);
1745        if (!node) {
1746            dummy_restore = TRUE;
1747            log_msg(1,
1748                    "Skipping big file %ld (%s) - not in biggielist subset",
1749                    biggiefile_number + 1, orig_bf_fname);
1750            pathname_of_last_file_restored[0] = '\0';
1751        } else if (!(node->selected)) {
1752            dummy_restore = TRUE;
1753            log_msg(1, "Skipping %s (name isn't in biggielist subset)",
1754                    orig_bf_fname);
1755            pathname_of_last_file_restored[0] = '\0';
1756        }
1757    }
1758
1759    if (use_partimagehack) {
1760        if (strncmp(orig_bf_fname, "/dev/", 5)) {
1761            log_msg(1,
1762                    "I was in error when I set use_partimagehack to TRUE.");
1763            log_msg(1, "%s isn't even in /dev", orig_bf_fname);
1764            use_partimagehack = FALSE;
1765        }
1766    }
1767
1768    if (use_partimagehack) {
1769        g_loglevel = 4;
1770        strcpy(outfile_fname, orig_bf_fname);
1771        use_partimage_hack = TRUE;
1772        log_msg(2,
1773                "Calling partimagehack in background because %s is a /dev entry",
1774                outfile_fname);
1775        sprintf(sz_devfile, "/tmp/%d.%d.000", (int) (random() % 32768),
1776                (int) (random() % 32768));
1777        mkfifo(sz_devfile, 0x770);
1778        strcpy(partimagehack_fifo, sz_devfile);
1779        file_to_openout = partimagehack_fifo;
1780        switch (pid = fork()) {
1781        case -1:
1782            fatal_error("Fork failure");
1783        case 0:
1784            log_msg(3,
1785                    "CHILD - fip - calling feed_outfrom_partimage(%s, %s)",
1786                    outfile_fname, partimagehack_fifo);
1787            res =
1788                feed_outfrom_partimage(outfile_fname, partimagehack_fifo);
1789//          log_msg(3, "CHILD - fip - exiting");
1790            exit(res);
1791            break;
1792        default:
1793            log_msg(3,
1794                    "feed_into_partimage() called in background --- pid=%ld",
1795                    (long int) (pid));
1796        }
1797    } else {
1798        if (!strncmp(orig_bf_fname, "/dev/", 5))    // non-NTFS partition
1799        {
1800            strcpy(outfile_fname, orig_bf_fname);
1801        } else                  // biggiefile
1802        {
1803            sprintf(outfile_fname, "%s/%s", bkpinfo->restore_path,
1804                    orig_bf_fname);
1805        }
1806        use_partimage_hack = FALSE;
1807        partimagehack_fifo[0] = '\0';
1808        file_to_openout = outfile_fname;
1809        if (!does_file_exist(outfile_fname))    // yes, it looks weird with the '!' but it's correct that way
1810        {
1811            make_hole_for_file(outfile_fname);
1812        }
1813        sprintf(tmp, "Reassembling big file %ld (%s)",
1814                biggiefile_number + 1, orig_bf_fname);
1815        log_msg(2, tmp);
1816    }
1817
1818    if (dummy_restore) {
1819        sprintf(outfile_fname, "/dev/null");
1820    }
1821
1822    if (!bkpinfo->zip_exe[0]) {
1823        sprintf(command, "cat > \"%s\"", file_to_openout);
1824    } else {
1825        sprintf(command, "%s -dc > \"%s\" 2>> %s", bkpinfo->zip_exe,
1826                file_to_openout, MONDO_LOGFILE);
1827    }
1828    sprintf(tmp, "Pipe command = '%s'", command);
1829    log_msg(3, tmp);
1830
1831    /* restore biggiefile, one slice at a time */
1832    if (!(pout = popen(command, "w"))) {
1833        fatal_error("Cannot pipe out");
1834    }
1835    for (res = read_header_block_from_stream(&slice_siz, tmp, &ctrl_chr);
1836         ctrl_chr != BLK_STOP_A_BIGGIE;
1837         res = read_header_block_from_stream(&slice_siz, tmp, &ctrl_chr)) {
1838        if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
1839            wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
1840        }
1841        sprintf(tmp, "Working on file #%ld, slice #%ld    ",
1842                biggiefile_number + 1, current_slice_number);
1843        log_msg(2, tmp);
1844        if (!g_text_mode) {
1845            newtDrawRootText(0, g_noof_rows - 2, tmp);
1846            newtRefresh();
1847        }
1848        strip_spaces(tmp);
1849        update_progress_form(tmp);
1850        if (current_slice_number == 0) {
1851            res =
1852                read_file_from_stream_to_file(bkpinfo,
1853                                              "/tmp/biggie-blah.txt",
1854                                              slice_siz);
1855            if (!(fin = fopen("/tmp/biggie-blah.txt", "r"))) {
1856                log_OS_error("blah blah");
1857            } else {
1858                if (fread
1859                    ((void *) &biggiestruct, 1, sizeof(biggiestruct),
1860                     fin) < sizeof(biggiestruct)) {
1861                    log_msg(2,
1862                            "Warning - unable to get biggiestruct of bigfile #%d",
1863                            biggiefile_number + 1);
1864                }
1865                paranoid_fclose(fin);
1866            }
1867        } else {
1868            res =
1869                read_file_from_stream_to_stream(bkpinfo, pout, slice_siz);
1870        }
1871        retval += res;
1872        res = read_header_block_from_stream(&slice_siz, tmp, &ctrl_chr);
1873        if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
1874            wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
1875        }
1876        current_slice_number++;
1877        g_current_progress++;
1878    }
1879    paranoid_pclose(pout);
1880
1881    log_msg(1, "pathname_of_last_file_restored is now %s",
1882            pathname_of_last_file_restored);
1883
1884    if (use_partimage_hack) {
1885        log_msg(3, "Waiting for partimage to finish");
1886        sprintf(tmp,
1887                " ps ax | grep \" partimagehack \" | grep -v grep > /dev/null 2> /dev/null");
1888        while (system(tmp) == 0) {
1889            sleep(1);
1890        }
1891        log_msg(3, "OK, partimage has really finished");
1892    }
1893
1894    log_msg(3, "biggiestruct.filename = %s", biggiestruct.filename);
1895    log_msg(3, "biggiestruct.checksum = %s", biggiestruct.checksum);
1896    if (strcmp(outfile_fname, "/dev/null")) {
1897        chmod(outfile_fname, biggiestruct.properties.st_mode);
1898        chown(outfile_fname, biggiestruct.properties.st_uid,
1899              biggiestruct.properties.st_gid);
1900        ubuf->actime = biggiestruct.properties.st_atime;
1901        ubuf->modtime = biggiestruct.properties.st_mtime;
1902        utime(outfile_fname, ubuf);
1903    }
1904
1905    paranoid_free(tmp);
1906    paranoid_free(outfile_fname);
1907    paranoid_free(command);
1908    paranoid_free(partimagehack_command);
1909    paranoid_free(sz_devfile);
1910    paranoid_free(partimagehack_fifo);
1911    g_loglevel = old_loglevel;
1912    return (retval);
1913}
1914
1915/**************************************************************************
1916 *END_RESTORE_A_BIGGIEFILE_FROM_STREAM                                    *
1917 **************************************************************************/
1918
1919
1920
1921/**
1922 * Restore @p tarball_fname from CD.
1923 * @param tarball_fname The filename of the tarball to restore (in /mnt/cdrom).
1924 * This will be used unmodified.
1925 * @param current_tarball_number The number (starting from 0) of the fileset
1926 * we're restoring now.
1927 * @param filelist The node structure containing the list of files to be
1928 * restored. If no file in the afioball is in this list, afio will still be
1929 * called, but nothing will be written.
1930 * @return 0 for success, nonzero for failure.
1931 */
1932int
1933restore_a_tarball_from_CD(char *tarball_fname,
1934                          long current_tarball_number,
1935                          struct s_node *filelist)
1936{
1937    int retval = 0;
1938    int res;
1939    char *p;
1940
1941  /** malloc **/
1942    char *command;
1943    char *tmp;
1944    char *filelist_name;
1945    char *filelist_subset_fname;
1946    char *executable;
1947    char *temp_log;
1948    char screen_message[100];
1949    long matches = 0;
1950    bool use_star;
1951    char *xattr_fname;
1952    char *acl_fname;
1953//  char files_to_restore_this_time_fname[MAX_STR_LEN];
1954
1955    assert_string_is_neither_NULL_nor_zerolength(tarball_fname);
1956    malloc_string(command);
1957    malloc_string(tmp);
1958    malloc_string(filelist_name);
1959    malloc_string(filelist_subset_fname);
1960    malloc_string(executable);
1961    malloc_string(temp_log);
1962    malloc_string(xattr_fname);
1963    malloc_string(acl_fname);
1964
1965    log_msg(5, "Entering");
1966    filelist_subset_fname[0] = '\0';
1967    use_star = (strstr(tarball_fname, ".star")) ? TRUE : FALSE;
1968//  sprintf(files_to_restore_this_time_fname, "/tmp/ftrttf.%d.%d", (int)getpid(), (int)random());
1969    sprintf(command, "mkdir -p %s/tmp", MNT_RESTORING);
1970    run_program_and_log_output(command, 9);
1971    sprintf(temp_log, "/tmp/%d.%d", (int) (random() % 32768),
1972            (int) (random() % 32768));
1973
1974    sprintf(filelist_name, MNT_CDROM "/archives/filelist.%ld",
1975            current_tarball_number);
1976    if (length_of_file(filelist_name) <= 2) {
1977        log_msg(2, "There are _zero_ files in filelist '%s'",
1978                filelist_name);
1979        log_msg(2,
1980                "This is a bit silly (ask Hugo to fix mondo-makefilelist, please)");
1981        log_msg(2,
1982                "but it's non-critical. It's cosmetic. Don't worry about it.");
1983        retval = 0;
1984        goto leave_sub;
1985    }
1986    if (count_lines_in_file(filelist_name) <= 0
1987        || length_of_file(tarball_fname) <= 0) {
1988        log_msg(3, "length_of_file(%s) = %ld", tarball_fname,
1989                length_of_file(tarball_fname));
1990        sprintf(tmp, "Unable to restore fileset #%ld (CD I/O error)",
1991                current_tarball_number);
1992        log_to_screen(tmp);
1993        retval = 1;
1994        goto leave_sub;
1995    }
1996
1997    if (filelist) {
1998        sprintf(filelist_subset_fname, "/tmp/filelist-subset-%ld.tmp",
1999                current_tarball_number);
2000        if ((matches =
2001             save_filelist_entries_in_common(filelist_name, filelist,
2002                                             filelist_subset_fname,
2003                                             use_star))
2004            <= 0) {
2005            sprintf(tmp, "Skipping fileset %ld", current_tarball_number);
2006            log_msg(1, tmp);
2007        } else {
2008            log_msg(3, "Saved fileset %ld's subset to %s",
2009                    current_tarball_number, filelist_subset_fname);
2010        }
2011        sprintf(screen_message, "Tarball #%ld --- %ld matches",
2012                current_tarball_number, matches);
2013        log_to_screen(screen_message);
2014    } else {
2015        filelist_subset_fname[0] = '\0';
2016    }
2017
2018    if (filelist == NULL || matches > 0) {
2019        sprintf(xattr_fname, XATTR_LIST_FNAME_RAW_SZ,
2020                MNT_CDROM "/archives", current_tarball_number);
2021        sprintf(acl_fname, ACL_LIST_FNAME_RAW_SZ, MNT_CDROM "/archives",
2022                current_tarball_number);
2023        if (strstr(tarball_fname, ".bz2")) {
2024            strcpy(executable, "bzip2");
2025        } else if (strstr(tarball_fname, ".lzo")) {
2026            strcpy(executable, "lzop");
2027        } else {
2028            executable[0] = '\0';
2029        }
2030        if (executable[0]) {
2031            sprintf(tmp, "which %s > /dev/null 2> /dev/null", executable);
2032            if (run_program_and_log_output(tmp, FALSE)) {
2033                log_to_screen
2034                    ("(compare_a_tarball) Compression program not found - oh no!");
2035                paranoid_MR_finish(1);
2036            }
2037            strcpy(tmp, executable);
2038            sprintf(executable, "-P %s -Z", tmp);
2039        }
2040#ifdef __FreeBSD__
2041#define BUFSIZE 512
2042#else
2043#define BUFSIZE (1024L*1024L)/TAPE_BLOCK_SIZE
2044#endif
2045
2046//      if (strstr(tarball_fname, ".star."))
2047        if (use_star) {
2048            sprintf(command,
2049                    "star -x -force-remove -U " STAR_ACL_SZ
2050                    " errctl= file=%s", tarball_fname);
2051            if (strstr(tarball_fname, ".bz2")) {
2052                strcat(command, " -bz");
2053            }
2054        } else {
2055            if (filelist_subset_fname[0] != '\0') {
2056                sprintf(command,
2057                        "afio -i -M 8m -b %ld -c %ld %s -w %s %s",
2058                        TAPE_BLOCK_SIZE,
2059                        BUFSIZE, executable, filelist_subset_fname,
2060//             files_to_restore_this_time_fname,
2061                        tarball_fname);
2062            } else {
2063                sprintf(command,
2064                        "afio -i -b %ld -c %ld -M 8m %s %s",
2065                        TAPE_BLOCK_SIZE,
2066                        BUFSIZE, executable, tarball_fname);
2067            }
2068        }
2069#undef BUFSIZE
2070        sprintf(command + strlen(command), " 2>> %s >> %s", temp_log,
2071                temp_log);
2072        log_msg(1, "command = '%s'", command);
2073        unlink(temp_log);
2074        res = system(command);
2075        if (res) {
2076            p = strstr(command, "-acl ");
2077            if (p) {
2078                p[0] = p[1] = p[2] = p[3] = ' ';
2079                log_msg(1, "new command = '%s'", command);
2080                res = system(command);
2081            }
2082        }
2083        if (res && length_of_file(temp_log) < 5) {
2084            res = 0;
2085        }
2086
2087        log_msg(1, "Setting fattr list %s", xattr_fname);
2088        if (length_of_file(xattr_fname) > 0) {
2089            res = set_fattr_list(filelist_subset_fname, xattr_fname);
2090            if (res) {
2091                log_to_screen
2092                    ("Errors occurred while setting extended attributes");
2093            } else {
2094                log_msg(1, "I set xattr OK");
2095            }
2096            retval += res;
2097        }
2098        if (length_of_file(acl_fname) > 0) {
2099            log_msg(1, "Setting acl list %s", acl_fname);
2100            res = set_acl_list(filelist_subset_fname, acl_fname);
2101            if (res) {
2102                log_to_screen
2103                    ("Errors occurred while setting access control lists");
2104            } else {
2105                log_msg(1, "I set ACL OK");
2106            }
2107            retval += res;
2108        }
2109        if (retval) {
2110            sprintf(command, "cat %s >> %s", temp_log, MONDO_LOGFILE);
2111            system(command);
2112            log_msg(2, "Errors occurred while processing fileset #%d",
2113                    current_tarball_number);
2114        } else {
2115            log_msg(2, "Fileset #%d processed OK", current_tarball_number);
2116        }
2117    }
2118    if (does_file_exist("/PAUSE")) {
2119        popup_and_OK
2120            ("Press ENTER to go on. Delete /PAUSE to stop these pauses.");
2121    }
2122    unlink(filelist_subset_fname);
2123    unlink(xattr_fname);
2124    unlink(acl_fname);
2125    unlink(temp_log);
2126
2127  leave_sub:
2128    paranoid_free(command);
2129    paranoid_free(tmp);
2130    paranoid_free(filelist_name);
2131    paranoid_free(filelist_subset_fname);
2132    paranoid_free(executable);
2133    paranoid_free(temp_log);
2134    paranoid_free(xattr_fname);
2135    paranoid_free(acl_fname);
2136    log_msg(5, "Leaving");
2137    return (retval);
2138}
2139
2140/**************************************************************************
2141 *END_RESTORE_A_TARBALL_FROM_CD                                           *
2142 **************************************************************************/
2143
2144
2145/**
2146 * Restore a tarball from the currently opened stream.
2147 * @param bkpinfo The backup information structure. Fields used:
2148 * - @c bkpinfo->backup_media_type
2149 * - @c bkpinfo->media_device
2150 * - @c bkpinfo->zip_exe
2151 * @param tarball_fname The filename of the afioball to restore.
2152 * @param current_tarball_number The number (starting from 0) of the fileset
2153 * we're restoring now.
2154 * @param filelist The node structure containing the list of files to be
2155 * restored. If no file in the afioball is in this list, afio will still be
2156 * called, but nothing will be written.
2157 * @param size The size (in @b bytes) of the afioball.
2158 * @return 0 for success, nonzero for failure.
2159 */
2160int
2161restore_a_tarball_from_stream(struct s_bkpinfo *bkpinfo,
2162                              char *tarball_fname,
2163                              long current_tarball_number,
2164                              struct s_node *filelist,
2165                              long long size, char *xattr_fname,
2166                              char *acl_fname)
2167{
2168    int retval = 0;
2169    int res = 0;
2170
2171  /** malloc add ***/
2172    char *tmp;
2173    char *command;
2174    char *afio_fname;
2175    char *filelist_fname;
2176    char *filelist_subset_fname;
2177    char *executable;
2178    long matches = 0;
2179    bool restore_this_fileset = FALSE;
2180    bool use_star;
2181
2182    assert(bkpinfo != NULL);
2183    assert_string_is_neither_NULL_nor_zerolength(tarball_fname);
2184    malloc_string(filelist_subset_fname);
2185    malloc_string(filelist_fname);
2186    malloc_string(afio_fname);
2187    malloc_string(executable);
2188    malloc_string(command);
2189    malloc_string(tmp);
2190    filelist_subset_fname[0] = '\0';
2191    /* to do it with a file... */
2192    use_star = (strstr(tarball_fname, ".star")) ? TRUE : FALSE;
2193    sprintf(tmp,
2194            "Restoring from fileset #%ld (%ld KB) on %s #%d",
2195            current_tarball_number, (long) size >> 10,
2196            media_descriptor_string(bkpinfo->backup_media_type),
2197            g_current_media_number);
2198    log_msg(2, tmp);
2199    run_program_and_log_output("mkdir -p " MNT_RESTORING "/tmp", FALSE);
2200
2201  /****************************************************************************
2202   * Use RAMDISK's /tmp; saves time; oh wait, it's too small                  *
2203   * Well, pipe from tape to afio, then; oh wait, can't do that either: bug   *
2204   * in afio or someting; oh darn.. OK, use tmpfs :-)                         *
2205   ****************************************************************************/
2206    filelist_fname[0] = filelist_subset_fname[0] = '\0';
2207    sprintf(afio_fname, "/tmp/tmpfs/archive.tmp.%ld",
2208            current_tarball_number);
2209    sprintf(filelist_fname, "%s/filelist.%ld", bkpinfo->tmpdir,
2210            current_tarball_number);
2211    sprintf(filelist_subset_fname, "%s/filelist-subset-%ld.tmp",
2212            bkpinfo->tmpdir, current_tarball_number);
2213//  sprintf(filelist_fname, "/tmp/tmpfs/temp-filelist.%ld", current_tarball_number);
2214    res = read_file_from_stream_to_file(bkpinfo, afio_fname, size);
2215    if (strstr(tarball_fname, ".star")) {
2216        bkpinfo->use_star = TRUE;
2217    }
2218    if (res) {
2219        log_msg(1, "Warning - error reading afioball from tape");
2220    }
2221    if (bkpinfo->compression_level == 0) {
2222        executable[0] = '\0';
2223    } else {
2224        if (bkpinfo->use_star) {
2225            strcpy(executable, " -bz");
2226        } else {
2227            sprintf(executable, "-P %s -Z", bkpinfo->zip_exe);
2228        }
2229    }
2230
2231    if (!filelist)              // if unconditional restore then restore entire fileset
2232    {
2233        restore_this_fileset = TRUE;
2234    } else                      // If restoring selectively then get TOC from tarball
2235    {
2236        if (strstr(tarball_fname, ".star.")) {
2237            use_star = TRUE;
2238            sprintf(command, "star -t file=%s %s", afio_fname, executable);
2239        } else {
2240            use_star = FALSE;
2241            sprintf(command, "afio -t -M 8m -b %ld %s %s", TAPE_BLOCK_SIZE,
2242                    executable, afio_fname);
2243        }
2244        sprintf(command + strlen(command), " > %s 2>> %s", filelist_fname,
2245                MONDO_LOGFILE);
2246        log_msg(1, "command = %s", command);
2247        if (system(command)) {
2248            log_msg(4, "Warning - error occurred while retrieving TOC");
2249        }
2250        if ((matches =
2251             save_filelist_entries_in_common(filelist_fname, filelist,
2252                                             filelist_subset_fname,
2253                                             use_star))
2254            <= 0 || length_of_file(filelist_subset_fname) < 2) {
2255            if (length_of_file(filelist_subset_fname) < 2) {
2256                log_msg(1, "No matches found in fileset %ld",
2257                        current_tarball_number);
2258            }
2259            sprintf(tmp, "Skipping fileset %ld", current_tarball_number);
2260            log_msg(2, tmp);
2261            restore_this_fileset = FALSE;
2262        } else {
2263            log_msg(5, "%ld matches. Saved fileset %ld's subset to %s",
2264                    matches, current_tarball_number,
2265                    filelist_subset_fname);
2266            restore_this_fileset = TRUE;
2267        }
2268    }
2269
2270// Concoct the call to star/afio to restore files
2271    if (strstr(tarball_fname, ".star."))    // star
2272    {
2273        sprintf(command, "star -x file=%s %s", afio_fname, executable);
2274        if (filelist) {
2275            sprintf(command + strlen(command), " list=%s",
2276                    filelist_subset_fname);
2277        }
2278    } else                      // afio
2279    {
2280        sprintf(command, "afio -i -M 8m -b %ld %s", TAPE_BLOCK_SIZE,
2281                executable);
2282        if (filelist) {
2283            sprintf(command + strlen(command), " -w %s",
2284                    filelist_subset_fname);
2285        }
2286        sprintf(command + strlen(command), " %s", afio_fname);
2287    }
2288    sprintf(command + strlen(command), " 2>> %s", MONDO_LOGFILE);
2289
2290// Call if IF there are files to restore (selectively/unconditionally)
2291    if (restore_this_fileset) {
2292        log_msg(1, "Calling command='%s'", command);
2293        paranoid_system(command);
2294
2295        iamhere("Restoring xattr, acl stuff");
2296        res = set_fattr_list(filelist_subset_fname, xattr_fname);
2297        if (res) {
2298            log_msg(1, "Errors occurred while setting xattr");
2299        } else {
2300            log_msg(1, "I set xattr OK");
2301        }
2302        retval += res;
2303
2304        res = set_acl_list(filelist_subset_fname, acl_fname);
2305        if (res) {
2306            log_msg(1, "Errors occurred while setting ACL");
2307        } else {
2308            log_msg(1, "I set ACL OK");
2309        }
2310        retval += res;
2311
2312    } else {
2313        log_msg(1, "NOT CALLING '%s'", command);
2314    }
2315
2316    if (does_file_exist("/PAUSE") && current_tarball_number >= 50) {
2317        log_to_screen("Paused after set %ld", current_tarball_number);
2318        popup_and_OK("Pausing. Press ENTER to continue.");
2319    }
2320
2321    unlink(filelist_subset_fname);
2322    unlink(filelist_fname);
2323    unlink(afio_fname);
2324
2325    paranoid_free(filelist_subset_fname);
2326    paranoid_free(filelist_fname);
2327    paranoid_free(afio_fname);
2328    paranoid_free(command);
2329    paranoid_free(tmp);
2330    return (retval);
2331}
2332
2333/**************************************************************************
2334 *END_RESTORE_A_TARBALL_FROM_STREAM                                       *
2335 **************************************************************************/
2336
2337
2338
2339
2340/**
2341 * Restore all biggiefiles from all media in this CD backup.
2342 * The CD with the last afioball should be currently mounted.
2343 * @param bkpinfo The backup information structure. @c backup_media_type is the
2344 * only field used in this function.
2345 * @param filelist The node structure containing the list of files to be
2346 * restored. If a prospective biggiefile is not in this list, it will be ignored.
2347 * @return 0 for success, nonzero for failure.
2348 */
2349int
2350restore_all_biggiefiles_from_CD(struct s_bkpinfo *bkpinfo,
2351                                struct s_node *filelist)
2352{
2353    int retval = 0;
2354    int res;
2355    long noof_biggiefiles, bigfileno = 0, total_slices;
2356  /** malloc **/
2357    char *tmp;
2358    bool just_changed_cds = FALSE, finished;
2359    char *xattr_fname;
2360    char *acl_fname;
2361    char *biggies_whose_EXATs_we_should_set;    // EXtended ATtributes
2362    char *pathname_of_last_biggie_restored;
2363    FILE *fbw = NULL;
2364
2365    malloc_string(xattr_fname);
2366    malloc_string(acl_fname);
2367    malloc_string(tmp);
2368    malloc_string(biggies_whose_EXATs_we_should_set);
2369    malloc_string(pathname_of_last_biggie_restored);
2370    assert(bkpinfo != NULL);
2371
2372    sprintf(biggies_whose_EXATs_we_should_set,
2373            "%s/biggies-whose-EXATs-we-should-set", bkpinfo->tmpdir);
2374    if (!(fbw = fopen(biggies_whose_EXATs_we_should_set, "w"))) {
2375        log_msg(1, "Warning - cannot openout %s",
2376                biggies_whose_EXATs_we_should_set);
2377    }
2378
2379    read_cfg_var(g_mondo_cfg_file, "total-slices", tmp);
2380    total_slices = atol(tmp);
2381    sprintf(tmp, "Reassembling large files      ");
2382    mvaddstr_and_log_it(g_currentY, 0, tmp);
2383    if (length_of_file(BIGGIELIST) < 6) {
2384        log_msg(1, "OK, no biggielist; not restoring biggiefiles");
2385        return (0);
2386    }
2387    noof_biggiefiles = count_lines_in_file(BIGGIELIST);
2388    if (noof_biggiefiles <= 0) {
2389        log_msg(2,
2390                "OK, no biggiefiles in biggielist; not restoring biggiefiles");
2391        return (0);
2392    }
2393    sprintf(tmp, "OK, there are %ld biggiefiles in the archives",
2394            noof_biggiefiles);
2395    log_msg(2, tmp);
2396
2397    open_progress_form("Reassembling large files",
2398                       "I am now reassembling all the large files.",
2399                       "Please wait. This may take some time.",
2400                       "", total_slices);
2401    for (bigfileno = 0, finished = FALSE; !finished;) {
2402        log_msg(2, "Thinking about restoring bigfile %ld", bigfileno + 1);
2403        if (!does_file_exist(slice_fname(bigfileno, 0, ARCHIVES_PATH, ""))) {
2404            log_msg(3,
2405                    "...but its first slice isn't on this CD. Perhaps this was a selective restore?");
2406            log_msg(3, "Cannot find bigfile #%ld 's first slice on %s #%d",
2407                    bigfileno + 1,
2408                    media_descriptor_string(bkpinfo->backup_media_type),
2409                    g_current_media_number);
2410            log_msg(3, "Slicename would have been %s",
2411                    slice_fname(bigfileno + 1, 0, ARCHIVES_PATH, ""));
2412            // I'm not positive 'just_changed_cds' is even necessary...
2413            if (just_changed_cds) {
2414                just_changed_cds = FALSE;
2415                log_msg(3,
2416                        "I'll continue to scan this CD for bigfiles to be restored.");
2417            } else if (does_file_exist(MNT_CDROM "/archives/NOT-THE-LAST")) {
2418                insist_on_this_cd_number(bkpinfo,
2419                                         ++g_current_media_number);
2420                sprintf(tmp, "Restoring from %s #%d",
2421                        media_descriptor_string(bkpinfo->
2422                                                backup_media_type),
2423                        g_current_media_number);
2424                log_to_screen(tmp);
2425                just_changed_cds = TRUE;
2426            } else {
2427                log_msg(2, "There was no bigfile #%ld. That's OK.",
2428                        bigfileno + 1);
2429                log_msg(2, "I'm going to stop restoring bigfiles now.");
2430                finished = TRUE;
2431            }
2432        } else {
2433            just_changed_cds = FALSE;
2434            sprintf(tmp, "Restoring big file %ld", bigfileno + 1);
2435            update_progress_form(tmp);
2436            res =
2437                restore_a_biggiefile_from_CD(bkpinfo, bigfileno, filelist,
2438                                             pathname_of_last_biggie_restored);
2439            iamhere(pathname_of_last_biggie_restored);
2440            if (fbw && pathname_of_last_biggie_restored[0]) {
2441                fprintf(fbw, "%s\n", pathname_of_last_biggie_restored);
2442            }
2443            retval += res;
2444            bigfileno++;
2445
2446        }
2447    }
2448
2449    if (fbw) {
2450        fclose(fbw);
2451        sprintf(acl_fname, ACL_BIGGLST_FNAME_RAW_SZ, ARCHIVES_PATH);
2452        sprintf(xattr_fname, XATTR_BIGGLST_FNAME_RAW_SZ, ARCHIVES_PATH);
2453        if (length_of_file(acl_fname) > 0 && find_home_of_exe("setfacl")) {
2454            set_acl_list(biggies_whose_EXATs_we_should_set, acl_fname);
2455        }
2456        if (length_of_file(xattr_fname) > 0
2457            && find_home_of_exe("setfattr")) {
2458            set_fattr_list(biggies_whose_EXATs_we_should_set, xattr_fname);
2459        }
2460    }
2461    if (does_file_exist("/PAUSE")) {
2462        popup_and_OK
2463            ("Press ENTER to go on. Delete /PAUSE to stop these pauses.");
2464    }
2465    close_progress_form();
2466    if (retval) {
2467        mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2468    } else {
2469        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2470    }
2471    paranoid_free(xattr_fname);
2472    paranoid_free(acl_fname);
2473    paranoid_free(tmp);
2474    paranoid_free(biggies_whose_EXATs_we_should_set);
2475    paranoid_free(pathname_of_last_biggie_restored);
2476    return (retval);
2477}
2478
2479/**************************************************************************
2480 *END_RESTORE_ALL_BIGGIFILES_FROM_CD                                      *
2481 **************************************************************************/
2482
2483
2484
2485/**
2486 * Restore all afioballs from all CDs in the backup.
2487 * The first CD should be inserted (if not, it will be asked for).
2488 * @param bkpinfo The backup information structure. @c backup_media_type is the
2489 * only field used in @e this function.
2490 * @param filelist The node structure containing the list of files to be
2491 * restored. If no file in some particular afioball is in this list, afio will
2492 * still be called for that fileset, but nothing will be written.
2493 * @return 0 for success, or the number of filesets that failed.
2494 */
2495int
2496restore_all_tarballs_from_CD(struct s_bkpinfo *bkpinfo,
2497                             struct s_node *filelist)
2498{
2499    int retval = 0;
2500    int res;
2501    int attempts;
2502    long current_tarball_number = 0;
2503    long max_val;
2504  /**malloc ***/
2505    char *tmp;
2506    char *tarball_fname;
2507    char *progress_str;
2508    char *comment;
2509
2510    malloc_string(tmp);
2511    malloc_string(tarball_fname);
2512    malloc_string(progress_str);
2513    malloc_string(comment);
2514
2515    assert(bkpinfo != NULL);
2516
2517    mvaddstr_and_log_it(g_currentY, 0, "Restoring from archives");
2518    log_msg(2,
2519            "Insisting on 1st CD, so that I can have a look at LAST-FILELIST-NUMBER");
2520    if (g_current_media_number != 1) {
2521        log_msg(3, "OK, that's jacked up.");
2522        g_current_media_number = 1;
2523    }
2524    insist_on_this_cd_number(bkpinfo, g_current_media_number);
2525    read_cfg_var(g_mondo_cfg_file, "last-filelist-number", tmp);
2526    max_val = atol(tmp) + 1;
2527    sprintf(progress_str, "Restoring from %s #%d",
2528            media_descriptor_string(bkpinfo->backup_media_type),
2529            g_current_media_number);
2530    log_to_screen(progress_str);
2531    open_progress_form("Restoring from archives",
2532                       "Restoring data from the archives.",
2533                       "Please wait. This may take some time.",
2534                       progress_str, max_val);
2535    for (;;) {
2536        insist_on_this_cd_number(bkpinfo, g_current_media_number);
2537        update_progress_form(progress_str);
2538        sprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.bz2",
2539                current_tarball_number);
2540        if (!does_file_exist(tarball_fname)) {
2541            sprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.lzo",
2542                    current_tarball_number);
2543        }
2544        if (!does_file_exist(tarball_fname)) {
2545            sprintf(tarball_fname, MNT_CDROM "/archives/%ld.afio.",
2546                    current_tarball_number);
2547        }
2548        if (!does_file_exist(tarball_fname)) {
2549            sprintf(tarball_fname, MNT_CDROM "/archives/%ld.star.bz2",
2550                    current_tarball_number);
2551        }
2552        if (!does_file_exist(tarball_fname)) {
2553            sprintf(tarball_fname, MNT_CDROM "/archives/%ld.star.",
2554                    current_tarball_number);
2555        }
2556        if (!does_file_exist(tarball_fname)) {
2557            if (current_tarball_number == 0) {
2558                log_to_screen
2559                    ("No tarballs. Strange. Maybe you only backed up freakin' big files?");
2560                return (0);
2561            }
2562            if (!does_file_exist(MNT_CDROM "/archives/NOT-THE-LAST")
2563                || system("find " MNT_CDROM
2564                          "/archives/slice* > /dev/null 2> /dev/null") ==
2565                0) {
2566                break;
2567            }
2568            g_current_media_number++;
2569            sprintf(progress_str, "Restoring from %s #%d",
2570                    media_descriptor_string(bkpinfo->backup_media_type),
2571                    g_current_media_number);
2572            log_to_screen(progress_str);
2573        } else {
2574            sprintf(progress_str, "Restoring from fileset #%ld on %s #%d",
2575                    current_tarball_number,
2576                    media_descriptor_string(bkpinfo->backup_media_type),
2577                    g_current_media_number);
2578//    log_msg(3, "progress_str = %s", progress_str);
2579            for (res = 999, attempts = 0; attempts < 3 && res != 0;
2580                 attempts++) {
2581                res =
2582                    restore_a_tarball_from_CD(tarball_fname,
2583                                              current_tarball_number,
2584                                              filelist);
2585            }
2586            sprintf(tmp, "%s #%d, fileset #%ld - restore ",
2587                    media_descriptor_string(bkpinfo->backup_media_type),
2588                    g_current_media_number, current_tarball_number);
2589            if (res) {
2590                strcat(tmp, "reported errors");
2591            } else if (attempts > 1) {
2592                strcat(tmp, "succeeded");
2593            } else {
2594                strcat(tmp, "succeeded");
2595            }
2596            if (attempts > 1) {
2597                sprintf(tmp + strlen(tmp), " (%d attempts) - review logs",
2598                        attempts);
2599            }
2600            strcpy(comment, tmp);
2601            if (attempts > 1) {
2602                log_to_screen(comment);
2603            }
2604
2605            retval += res;
2606            current_tarball_number++;
2607            g_current_progress++;
2608        }
2609    }
2610    close_progress_form();
2611    if (retval) {
2612        mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2613    } else {
2614        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2615    }
2616    paranoid_free(tmp);
2617    paranoid_free(tarball_fname);
2618    paranoid_free(progress_str);
2619    paranoid_free(comment);
2620
2621    return (retval);
2622}
2623
2624/**************************************************************************
2625 *END_RESTORE_ALL_TARBALLS_FROM_CD                                        *
2626 **************************************************************************/
2627
2628
2629
2630/**
2631 * Restore all biggiefiles from the currently opened stream.
2632 * @param bkpinfo The backup information structure. Passed to other functions.
2633 * @param filelist The node structure containing the list of files to be
2634 * restored. If a prospective biggiefile is not in the list, it will be ignored.
2635 * @return 0 for success, or the number of biggiefiles that failed.
2636 */
2637int
2638restore_all_biggiefiles_from_stream(struct s_bkpinfo *bkpinfo,
2639                                    struct s_node *filelist)
2640{
2641    long noof_biggiefiles;
2642    long current_bigfile_number = 0;
2643    long total_slices;
2644
2645    int retval = 0;
2646    int res = 0;
2647    int ctrl_chr;
2648
2649  /** malloc add ****/
2650    char *tmp;
2651    char *biggie_fname;
2652    char *biggie_cksum;
2653    char *xattr_fname;
2654    char *acl_fname;
2655    char *p;
2656    char *pathname_of_last_biggie_restored;
2657    char *biggies_whose_EXATs_we_should_set;    // EXtended ATtributes
2658    long long biggie_size;
2659    FILE *fbw = NULL;
2660
2661    malloc_string(tmp);
2662    malloc_string(biggie_fname);
2663    malloc_string(biggie_cksum);
2664    malloc_string(xattr_fname);
2665    malloc_string(acl_fname);
2666    malloc_string(biggies_whose_EXATs_we_should_set);
2667    malloc_string(pathname_of_last_biggie_restored);
2668    assert(bkpinfo != NULL);
2669
2670    read_cfg_var(g_mondo_cfg_file, "total-slices", tmp);
2671
2672    total_slices = atol(tmp);
2673    sprintf(tmp, "Reassembling large files      ");
2674    sprintf(xattr_fname, XATTR_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
2675    sprintf(acl_fname, ACL_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
2676    mvaddstr_and_log_it(g_currentY, 0, tmp);
2677    sprintf(biggies_whose_EXATs_we_should_set,
2678            "%s/biggies-whose-EXATs-we-should-set", bkpinfo->tmpdir);
2679    if (!(fbw = fopen(biggies_whose_EXATs_we_should_set, "w"))) {
2680        log_msg(1, "Warning - cannot openout %s",
2681                biggies_whose_EXATs_we_should_set);
2682    }
2683// get xattr and acl files if they're there
2684    res =
2685        read_header_block_from_stream(&biggie_size, biggie_fname,
2686                                      &ctrl_chr);
2687    if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
2688        res =
2689            read_EXAT_files_from_tape(bkpinfo, &biggie_size, biggie_fname,
2690                                      &ctrl_chr, xattr_fname, acl_fname);
2691    }
2692
2693    noof_biggiefiles = atol(biggie_fname);
2694    sprintf(tmp, "OK, there are %ld biggiefiles in the archives",
2695            noof_biggiefiles);
2696    log_msg(2, tmp);
2697    open_progress_form("Reassembling large files",
2698                       "I am now reassembling all the large files.",
2699                       "Please wait. This may take some time.",
2700                       "", total_slices);
2701
2702    for (res =
2703         read_header_block_from_stream(&biggie_size, biggie_fname,
2704                                       &ctrl_chr);
2705         ctrl_chr != BLK_STOP_BIGGIEFILES;
2706         res =
2707         read_header_block_from_stream(&biggie_size, biggie_fname,
2708                                       &ctrl_chr)) {
2709        if (ctrl_chr != BLK_START_A_NORMBIGGIE
2710            && ctrl_chr != BLK_START_A_PIHBIGGIE) {
2711            wrong_marker(BLK_START_A_NORMBIGGIE, ctrl_chr);
2712        }
2713        p = strrchr(biggie_fname, '/');
2714        if (!p) {
2715            p = biggie_fname;
2716        } else {
2717            p++;
2718        }
2719        sprintf(tmp, "Restoring big file %ld (%lld K)",
2720                current_bigfile_number + 1, biggie_size / 1024);
2721        update_progress_form(tmp);
2722        res = restore_a_biggiefile_from_stream(bkpinfo, biggie_fname,
2723                                               current_bigfile_number,
2724                                               biggie_cksum,
2725                                               biggie_size,
2726                                               filelist, ctrl_chr,
2727                                               pathname_of_last_biggie_restored);
2728        log_msg(1, "I believe I have restored %s",
2729                pathname_of_last_biggie_restored);
2730        if (fbw && pathname_of_last_biggie_restored[0]) {
2731            fprintf(fbw, "%s\n", pathname_of_last_biggie_restored);
2732        }
2733        retval += res;
2734        current_bigfile_number++;
2735
2736    }
2737    if (current_bigfile_number != noof_biggiefiles
2738        && noof_biggiefiles != 0) {
2739        sprintf(tmp, "Warning - bigfileno=%ld but noof_biggiefiles=%ld\n",
2740                current_bigfile_number, noof_biggiefiles);
2741    } else {
2742        sprintf(tmp,
2743                "%ld biggiefiles in biggielist.txt; %ld biggiefiles processed today.",
2744                noof_biggiefiles, current_bigfile_number);
2745    }
2746    log_msg(1, tmp);
2747
2748    if (fbw) {
2749        fclose(fbw);
2750        if (length_of_file(biggies_whose_EXATs_we_should_set) > 2) {
2751            iamhere("Setting biggie-EXATs");
2752            if (length_of_file(acl_fname) > 0) {
2753                log_msg(1, "set_acl_list(%s,%s)",
2754                        biggies_whose_EXATs_we_should_set, acl_fname);
2755                set_acl_list(biggies_whose_EXATs_we_should_set, acl_fname);
2756            }
2757            if (length_of_file(xattr_fname) > 0) {
2758                log_msg(1, "set_fattr_List(%s,%s)",
2759                        biggies_whose_EXATs_we_should_set, xattr_fname);
2760                set_fattr_list(biggies_whose_EXATs_we_should_set,
2761                               xattr_fname);
2762            }
2763        } else {
2764            iamhere
2765                ("No biggiefiles selected. So, no biggie-EXATs to set.");
2766        }
2767    }
2768    if (does_file_exist("/PAUSE")) {
2769        popup_and_OK
2770            ("Press ENTER to go on. Delete /PAUSE to stop these pauses.");
2771    }
2772
2773    close_progress_form();
2774    if (retval) {
2775        mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2776    } else {
2777        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2778    }
2779    paranoid_free(biggies_whose_EXATs_we_should_set);
2780    paranoid_free(pathname_of_last_biggie_restored);
2781    paranoid_free(biggie_fname);
2782    paranoid_free(biggie_cksum);
2783    paranoid_free(xattr_fname);
2784    paranoid_free(acl_fname);
2785    paranoid_free(tmp);
2786    return (retval);
2787}
2788
2789/**************************************************************************
2790 *END_RESTORE_ALL_BIGGIEFILES_FROM_STREAM                                 *
2791 **************************************************************************/
2792
2793
2794
2795
2796
2797
2798/**
2799 * Restore all afioballs from the currently opened tape stream.
2800 * @param bkpinfo The backup information structure. Fields used:
2801 * - @c bkpinfo->backup_media_type
2802 * - @c bkpinfo->restore_path
2803 * @param filelist The node structure containing the list of files to be
2804 * restored. If no file in an afioball is in this list, afio will still be
2805 * called for that fileset, but nothing will be written.
2806 * @return 0 for success, or the number of filesets that failed.
2807 */
2808int
2809restore_all_tarballs_from_stream(struct s_bkpinfo *bkpinfo,
2810                                 struct s_node *filelist)
2811{
2812    int retval = 0;
2813    int res;
2814    long current_afioball_number = 0;
2815    int ctrl_chr;
2816    long max_val /*, total_noof_files */ ;
2817
2818  /** malloc **/
2819    char *tmp;
2820    char *progress_str;
2821    char *tmp_fname;
2822    char *xattr_fname;
2823    char *acl_fname;
2824
2825    long long tmp_size;
2826
2827    malloc_string(tmp);
2828    malloc_string(progress_str);
2829    malloc_string(tmp_fname);
2830    assert(bkpinfo != NULL);
2831    malloc_string(xattr_fname);
2832    malloc_string(acl_fname);
2833    mvaddstr_and_log_it(g_currentY, 0, "Restoring from archives");
2834    read_cfg_var(g_mondo_cfg_file, "last-filelist-number", tmp);
2835    max_val = atol(tmp) + 1;
2836
2837    chdir(bkpinfo->restore_path);   /* I don't know why this is needed _here_ but it seems to be. -HR, 02/04/2002 */
2838
2839    run_program_and_log_output("pwd", 5);
2840
2841    sprintf(progress_str, "Restoring from media #%d",
2842            g_current_media_number);
2843    log_to_screen(progress_str);
2844    open_progress_form("Restoring from archives",
2845                       "Restoring data from the archives.",
2846                       "Please wait. This may take some time.",
2847                       progress_str, max_val);
2848
2849    log_msg(3, "hey");
2850
2851    res = read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
2852    if (res) {
2853        log_msg(2, "Warning - error reading afioball from tape");
2854    }
2855    retval += res;
2856    if (ctrl_chr != BLK_START_AFIOBALLS) {
2857        wrong_marker(BLK_START_AFIOBALLS, ctrl_chr);
2858    }
2859    log_msg(2, "ho");
2860    res = read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
2861    while (ctrl_chr != BLK_STOP_AFIOBALLS) {
2862        update_progress_form(progress_str);
2863        sprintf(xattr_fname, "%s/xattr-subset-%ld.tmp", bkpinfo->tmpdir,
2864                current_afioball_number);
2865        sprintf(acl_fname, "%s/acl-subset-%ld.tmp", bkpinfo->tmpdir,
2866                current_afioball_number);
2867        unlink(xattr_fname);
2868        unlink(acl_fname);
2869        if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
2870            iamhere("Reading EXAT files from tape");
2871            res =
2872                read_EXAT_files_from_tape(bkpinfo, &tmp_size, tmp_fname,
2873                                          &ctrl_chr, xattr_fname,
2874                                          acl_fname);
2875        }
2876        if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
2877            wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
2878        }
2879        sprintf(tmp,
2880                "Restoring from fileset #%ld (name=%s, size=%ld K)",
2881                current_afioball_number, tmp_fname, (long) tmp_size >> 10);
2882        res =
2883            restore_a_tarball_from_stream(bkpinfo, tmp_fname,
2884                                          current_afioball_number,
2885                                          filelist, tmp_size, xattr_fname,
2886                                          acl_fname);
2887        retval += res;
2888        if (res) {
2889            sprintf(tmp, "Fileset %ld - errors occurred",
2890                    current_afioball_number);
2891            log_to_screen(tmp);
2892        }
2893        res =
2894            read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
2895        if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
2896            wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
2897        }
2898
2899        current_afioball_number++;
2900        g_current_progress++;
2901        sprintf(progress_str, "Restoring from fileset #%ld on %s #%d",
2902                current_afioball_number,
2903                media_descriptor_string(bkpinfo->backup_media_type),
2904                g_current_media_number);
2905        res =
2906            read_header_block_from_stream(&tmp_size, tmp_fname, &ctrl_chr);
2907        unlink(xattr_fname);
2908        unlink(acl_fname);
2909    }                           // next
2910    log_msg(1, "All done with afioballs");
2911    close_progress_form();
2912    if (retval) {
2913        mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2914    } else {
2915        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2916    }
2917    paranoid_free(tmp);
2918    paranoid_free(progress_str);
2919    paranoid_free(tmp_fname);
2920    paranoid_free(xattr_fname);
2921    paranoid_free(acl_fname);
2922    return (retval);
2923}
2924
2925/**************************************************************************
2926 *END_ RESTORE_ALL_TARBALLS_FROM_STREAM                                   *
2927 **************************************************************************/
2928
2929/* @} - end of LLrestoreGroup */
2930
2931
2932/**
2933 * Restore all files in @p filelist.
2934 * @param bkpinfo The backup information structure. Most fields are used.
2935 * @param filelist The node structure containing the list of files to be
2936 * restored.
2937 * @return 0 for success, or the number of afioballs and biggiefiles that failed.
2938 * @ingroup restoreGroup
2939 */
2940int restore_everything(struct s_bkpinfo *bkpinfo, struct s_node *filelist)
2941{
2942    int resA;
2943    int resB;
2944
2945  /** mallco ***/
2946    char *cwd;
2947    char *newpath;
2948    char *tmp;
2949    assert(bkpinfo != NULL);
2950
2951    malloc_string(cwd);
2952    malloc_string(newpath);
2953    malloc_string(tmp);
2954    log_msg(2, "restore_everything() --- starting");
2955    g_current_media_number = 1;
2956    getcwd(cwd, MAX_STR_LEN - 1);
2957    sprintf(tmp, "mkdir -p %s", bkpinfo->restore_path);
2958    run_program_and_log_output(tmp, FALSE);
2959    log_msg(1, "Changing dir to %s", bkpinfo->restore_path);
2960    chdir(bkpinfo->restore_path);
2961    getcwd(newpath, MAX_STR_LEN - 1);
2962    log_msg(1, "path is now %s", newpath);
2963    log_msg(1, "restoring everything");
2964    if (!find_home_of_exe("petris") && !g_text_mode) {
2965        newtDrawRootText(0, g_noof_rows - 2,
2966                         "Press ALT-<left cursor> twice to play Petris :-) ");
2967        newtRefresh();
2968    }
2969    mvaddstr_and_log_it(g_currentY, 0, "Preparing to read your archives");
2970    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2971        mount_cdrom(bkpinfo);
2972        mvaddstr_and_log_it(g_currentY++, 0,
2973                            "Restoring OS and data from streaming media");
2974        if (bkpinfo->backup_media_type == cdstream) {
2975            openin_cdstream(bkpinfo);
2976        } else {
2977            assert_string_is_neither_NULL_nor_zerolength(bkpinfo->
2978                                                         media_device);
2979            openin_tape(bkpinfo);
2980        }
2981        resA = restore_all_tarballs_from_stream(bkpinfo, filelist);
2982        resB = restore_all_biggiefiles_from_stream(bkpinfo, filelist);
2983        if (bkpinfo->backup_media_type == cdstream) {
2984            closein_cdstream(bkpinfo);
2985        } else {
2986            closein_tape(bkpinfo);
2987        }
2988    } else {
2989        mvaddstr_and_log_it(g_currentY++, 0,
2990                            "Restoring OS and data from CD       ");
2991        mount_cdrom(bkpinfo);
2992        resA = restore_all_tarballs_from_CD(bkpinfo, filelist);
2993        resB = restore_all_biggiefiles_from_CD(bkpinfo, filelist);
2994    }
2995    chdir(cwd);
2996    if (resA + resB) {
2997        log_to_screen("Errors occurred while data was being restored.");
2998    }
2999    if (length_of_file("/etc/raidtab") > 0) {
3000        log_msg(2, "Copying local raidtab to restored filesystem");
3001        run_program_and_log_output("cp -f /etc/raidtab " MNT_RESTORING
3002                                   "/etc/raidtab", FALSE);
3003    }
3004    kill_petris();
3005    log_msg(2, "restore_everything() --- leaving");
3006    paranoid_free(cwd);
3007    paranoid_free(newpath);
3008    paranoid_free(tmp);
3009    return (resA + resB);
3010}
3011
3012/**************************************************************************
3013 *END_RESTORE_EVERYTHING                                                  *
3014 **************************************************************************/
3015
3016
3017
3018/**
3019 * @brief Haha. You wish! (This function is not implemented :-)
3020 */
3021int
3022restore_live_from_monitas_server(struct s_bkpinfo *bkpinfo,
3023                                 char *monitas_device,
3024                                 char *restore_this_directory,
3025                                 char *restore_here)
3026     /* NB: bkpinfo hasn't been populated yet, except for ->tmp which is "/tmp" */
3027{
3028    FILE *fout;
3029    int retval = 0;
3030    int i;
3031    int j;
3032    struct mountlist_itself the_mountlist;
3033    static struct raidlist_itself the_raidlist;
3034  /** malloc **/
3035    char tmp[MAX_STR_LEN + 1];
3036    char command[MAX_STR_LEN + 1];
3037    char datablock[256 * 1024];
3038    char datadisks_fname[MAX_STR_LEN + 1];
3039    long k;
3040    long length;
3041    long long llt;
3042    struct s_node *filelist = NULL;
3043    assert(bkpinfo != NULL);
3044    assert_string_is_neither_NULL_nor_zerolength(monitas_device);
3045    assert(restore_this_directory != NULL);
3046    assert(restore_here != NULL);
3047
3048    sprintf(tmp, "restore_here = '%s'", restore_here);
3049
3050    log_msg(2, tmp);
3051
3052    log_msg(2, "restore_live_from_monitas_server() - starting");
3053    unlink("/tmp/mountlist.txt");
3054    unlink("/tmp/filelist.full");
3055    unlink("/tmp/biggielist.txt");
3056    if (restore_here[0] == '\0') {
3057        strcpy(bkpinfo->restore_path, MNT_RESTORING);
3058    } else {
3059        strcpy(bkpinfo->restore_path, restore_here);
3060    }
3061    log_msg(3, "FYI FYI FYI FYI FYI FYI FYI FYI FYI FYI FYI");
3062    sprintf(tmp, "FYI - data will be restored to %s",
3063            bkpinfo->restore_path);
3064    log_msg(3, tmp);
3065    log_msg(3, "FYI FYI FYI FYI FYI FYI FYI FYI FYI FYI FYI");
3066    sprintf(datadisks_fname, "/tmp/mondorestore.datadisks.%d",
3067            (int) (random() % 32768));
3068    chdir(bkpinfo->tmpdir);
3069
3070    sprintf(command, "cat %s", monitas_device);
3071    g_tape_stream = popen(command, "r");    // for compatibility with openin_tape()
3072    if (!(fout = fopen(datadisks_fname, "w"))) {
3073        log_OS_error(datadisks_fname);
3074        return (1);
3075    }
3076    for (i = 0; i < 32; i++) {
3077        for (j = 0; j < 4; j++) {
3078            for (length = k = 0; length < 256 * 1024; length += k) {
3079                k = fread(datablock + length, 1, 256 * 1024 - length,
3080                          g_tape_stream);
3081            }
3082            fwrite(datablock, 1, length, fout);
3083            g_tape_posK += length;
3084        }
3085    }
3086    paranoid_fclose(fout);
3087    sprintf(command,
3088            "tar -zxvf %s tmp/mondo-restore.cfg tmp/mountlist.txt tmp/filelist.full tmp/biggielist.txt",
3089            datadisks_fname);
3090    run_program_and_log_output(command, 4);
3091    read_header_block_from_stream(&llt, tmp, &i);
3092    read_header_block_from_stream(&llt, tmp, &i);
3093
3094    unlink(datadisks_fname);
3095    read_cfg_file_into_bkpinfo(g_mondo_cfg_file, bkpinfo);
3096    retval = load_mountlist(&the_mountlist, g_mountlist_fname);
3097
3098
3099    load_raidtab_into_raidlist(&the_raidlist, RAIDTAB_FNAME);
3100    iamhere("FIXME");
3101    fatal_error("This will fail");
3102    sprintf(command,
3103            "cat %s | grep -x \"%s.*\" > %s",
3104            g_filelist_full, restore_this_directory, g_filelist_full);
3105    if (system(command)) {
3106        retval++;
3107        log_to_screen
3108            ("Error(s) occurred while processing filelist and wildcard");
3109    }
3110    iamhere("FIXME");
3111    fatal_error("This will fail");
3112    sprintf(command,
3113            "cat %s | grep -x \"%s.*\" > %s",
3114            g_biggielist_txt, restore_this_directory, g_biggielist_txt);
3115    if (system(command)) {
3116        log_msg(1,
3117                "Error(s) occurred while processing biggielist and wildcard");
3118    }
3119    sprintf(command, "touch %s", g_biggielist_txt);
3120    run_program_and_log_output(command, FALSE);
3121//  filelist = load_filelist(g_filelist_restthese);  // FIXME --- this probably doesn't work because it doesn't include the biggiefiles
3122    retval += restore_everything(bkpinfo, filelist);
3123    free_filelist(filelist);
3124    log_msg(2, "--------End of restore_live_from_monitas_server--------");
3125    return (retval);
3126}
3127
3128/**************************************************************************
3129 *END_RESTORE_LIVE_FROM_MONITAS_SERVER                                    *
3130 **************************************************************************/
3131
3132
3133
3134
3135extern void wait_until_software_raids_are_prepped(char *, int);
3136
3137
3138char which_restore_mode(void);
3139
3140
3141/**
3142 * Log a "don't panic" message to the logfile.
3143 */
3144void welcome_to_mondorestore()
3145{
3146    log_msg(0, "-------------- Mondo Restore v%s -------------", PACKAGE_VERSION);
3147    log_msg(0,
3148            "DON'T PANIC! Mondorestore logs almost everything, so please ");
3149    log_msg(0,
3150            "don't break out in a cold sweat just because you see a few  ");
3151    log_msg(0,
3152            "error messages in the log. Read them; analyze them; see if  ");
3153    log_msg(0,
3154            "they are significant; above all, verify your backups! Please");
3155    log_msg(0,
3156            "attach a compressed copy of this log to any e-mail you send ");
3157    log_msg(0,
3158            "to the Mondo mailing list when you are seeking technical    ");
3159    log_msg(0,
3160            "support. Without it, we can't help you.               - Hugo");
3161    log_msg(0,
3162            "------------------------------------------------------------");
3163    log_msg(0,
3164            "BTW, despite (or perhaps because of) the wealth of messages,");
3165    log_msg(0,
3166            "some users are inclined to stop reading this log.  If Mondo ");
3167    log_msg(0,
3168            "stopped for some reason, chances are it's detailed here.    ");
3169    log_msg(0,
3170            "More than likely there's a message at the very end of this  ");
3171    log_msg(0,
3172            "log that will tell you what is wrong.  Please read it!      ");
3173    log_msg(0,
3174            "------------------------------------------------------------");
3175}
3176
3177
3178
3179/**
3180 * Restore the user's data.
3181 * What did you think it did, anyway? :-)
3182 */
3183int main(int argc, char *argv[])
3184{
3185    FILE *fin;
3186    FILE *fout;
3187    int retval = 0;
3188    int res;
3189//  int c;
3190    char *tmp;
3191
3192    struct mountlist_itself *mountlist;
3193    struct raidlist_itself *raidlist;
3194    struct s_bkpinfo *bkpinfo;
3195    struct s_node *filelist;
3196    char *a, *b;
3197
3198  /**************************************************************************
3199   * hugo-                                                                  *
3200   * busy stuff here - it needs some comments -stan                           *
3201   *                                                                        *
3202   **************************************************************************/
3203    if (getuid() != 0) {
3204        fprintf(stderr, "Please run as root.\r\n");
3205        exit(127);
3206    }
3207
3208    g_loglevel = DEFAULT_MR_LOGLEVEL;
3209    malloc_string(tmp);
3210
3211/* Configure global variables */
3212#ifdef __FreeBSD__
3213    if (strstr
3214        (call_program_and_get_last_line_of_output("cat /tmp/cmdline"),
3215         "textonly"))
3216#else
3217    if (strstr
3218        (call_program_and_get_last_line_of_output("cat /proc/cmdline"),
3219         "textonly"))
3220#endif
3221    {
3222        g_text_mode = TRUE;
3223        log_msg(1, "TEXTONLY MODE");
3224    } else {
3225        g_text_mode = FALSE;
3226    }                           // newt :-)
3227    if (!
3228        (bkpinfo = g_bkpinfo_DONTUSETHIS =
3229         malloc(sizeof(struct s_bkpinfo)))) {
3230        fatal_error("Cannot malloc bkpinfo");
3231    }
3232    if (!(mountlist = malloc(sizeof(struct mountlist_itself)))) {
3233        fatal_error("Cannot malloc mountlist");
3234    }
3235    if (!(raidlist = malloc(sizeof(struct raidlist_itself)))) {
3236        fatal_error("Cannot malloc raidlist");
3237    }
3238
3239    malloc_libmondo_global_strings();
3240
3241    asprintf(&g_mondo_home,
3242             call_program_and_get_last_line_of_output
3243             ("which mondorestore"));
3244    sprintf(g_tmpfs_mountpt, "/tmp/tmpfs");
3245    make_hole_for_dir(g_tmpfs_mountpt);
3246    g_current_media_number = 1; // precaution
3247
3248    run_program_and_log_output("mkdir -p " MNT_CDROM, FALSE);
3249    run_program_and_log_output("mkdir -p /mnt/floppy", FALSE);
3250
3251    malloc_string(tmp);
3252    malloc_string(a);
3253    malloc_string(b);
3254    setup_MR_global_filenames(bkpinfo); // malloc() and set globals, using bkpinfo->tmpdir etc.
3255    reset_bkpinfo(bkpinfo);
3256    bkpinfo->backup_media_type = none;  // in case boot disk was made for one backup type but user wants to restore from another backup type
3257    bkpinfo->restore_data = TRUE;   // Well, yeah :-)
3258    if (am_I_in_disaster_recovery_mode()) {
3259        run_program_and_log_output("mount / -o remount,rw", 2);
3260    }                           // for b0rken distros
3261    g_main_pid = getpid();
3262    srandom((int) (time(NULL)));
3263    register_pid(getpid(), "mondo");
3264    set_signals(TRUE);
3265    g_kernel_version = get_kernel_version();
3266
3267    log_msg(1, "FYI - g_mountlist_fname = %s", g_mountlist_fname);
3268    if (strlen(g_mountlist_fname) < 3) {
3269        fatal_error
3270            ("Serious error in malloc()'ing. Could be a bug in your glibc.");
3271    }
3272    mkdir(MNT_CDROM, 0x770);
3273
3274/* Backup original mountlist.txt */
3275    sprintf(tmp, "%s.orig", g_mountlist_fname);
3276    if (!does_file_exist(g_mountlist_fname)) {
3277        log_msg(2,
3278                "%ld: Warning - g_mountlist_fname (%s) does not exist yet",
3279                __LINE__, g_mountlist_fname);
3280    } else if (!does_file_exist(tmp)) {
3281        sprintf(tmp, "cp -f %s %s.orig", g_mountlist_fname,
3282                g_mountlist_fname);
3283        run_program_and_log_output(tmp, FALSE);
3284    }
3285
3286/* Init directories */
3287    make_hole_for_dir(bkpinfo->tmpdir);
3288    sprintf(tmp, "mkdir -p %s", bkpinfo->tmpdir);
3289    run_program_and_log_output(tmp, FALSE);
3290    make_hole_for_dir("/var/log");
3291    make_hole_for_dir("/tmp/tmpfs");    /* just in case... */
3292    run_program_and_log_output("umount " MNT_CDROM, FALSE);
3293    run_program_and_log_output
3294        ("ln -sf "MONDO_LOGFILE" /tmp/mondo-restore.log",
3295         FALSE);
3296
3297    run_program_and_log_output("rm -Rf /tmp/tmpfs/mondo.tmp.*", FALSE);
3298
3299/* Init GUI */
3300    malloc_libmondo_global_strings();
3301    setup_newt_stuff();         /* call newtInit and setup screen log */
3302    welcome_to_mondorestore();
3303    if (bkpinfo->disaster_recovery) {
3304        log_msg(1, "I am in disaster recovery mode");
3305    } else {
3306        log_msg(1, "I am in normal, live mode");
3307    }
3308
3309    iamhere("what time is it");
3310
3311/* Process command-line parameters */
3312    if (argc == 2 && strcmp(argv[1], "--edit-mountlist") == 0) {
3313#ifdef __FreeBSD__
3314        system("mv -f /tmp/raidconf.txt /etc/raidtab");
3315        if (!does_file_exist("/etc/raidtab"))
3316            system("vinum printconfig > /etc/raidtab");
3317#endif
3318        load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
3319        if (!does_file_exist(g_mountlist_fname)) {
3320            strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
3321        }
3322        res = let_user_edit_the_mountlist(bkpinfo, mountlist, raidlist);
3323#ifdef __FreeBSD__
3324        system("mv -f /etc/raidtab /tmp/raidconf.txt");
3325#endif
3326        paranoid_MR_finish(res);
3327    }
3328
3329    g_loglevel = DEFAULT_MR_LOGLEVEL;
3330    if (argc == 3 && strcmp(argv[1], "--echo-to-screen") == 0) {
3331        fout = fopen("/tmp/out.txt", "w");
3332        fput_string_one_char_at_a_time(stderr, argv[2]);
3333        finish(0);
3334    }
3335
3336    if (argc == 3 && strcmp(argv[1], "--gendf") == 0) {
3337        make_grub_install_scriptlet(argv[2]);
3338        finish(0);
3339    }
3340
3341    if (argc >= 2 && strcmp(argv[1], "--pih") == 0) {
3342        if (system("mount | grep cdrom 2> /dev/null > /dev/null")) {
3343            system("mount " MNT_CDROM);
3344        }
3345        bkpinfo->compression_level = 1;
3346        g_current_media_number = 2;
3347        strcpy(bkpinfo->restore_path, "/tmp/TESTING");
3348        bkpinfo->backup_media_type = dvd;
3349        open_progress_form("Reassembling /dev/hda1",
3350                           "Shark is a bit of a silly person.",
3351                           "Please wait. This may take some time.",
3352                           "", 1999);
3353        system("rm -Rf /tmp/*pih*");
3354
3355        restore_a_biggiefile_from_CD(bkpinfo, 42, NULL, tmp);
3356    }
3357
3358    if (argc == 5 && strcmp(argv[1], "--common") == 0) {
3359        g_loglevel = 6;
3360        filelist = load_filelist(argv[2]);
3361        if (!filelist) {
3362            fatal_error("Failed to load filelist");
3363        }
3364        toggle_node_selection(filelist, FALSE);
3365        toggle_all_root_dirs_on(filelist);
3366        // BERLIOS: /usr/lib ???
3367        toggle_path_selection(filelist, "/usr/share", TRUE);
3368//      show_filelist(filelist);
3369        save_filelist(filelist, "/tmp/out.txt");
3370//      finish(0);
3371//      toggle_path_selection (filelist, "/root/stuff", TRUE);
3372        strcpy(a, argv[3]);
3373        strcpy(b, argv[4]);
3374
3375        res = save_filelist_entries_in_common(a, filelist, b, FALSE);
3376        free_filelist(filelist);
3377        printf("res = %d", res);
3378        finish(0);
3379    }
3380
3381    if (argc == 3 && strcmp(argv[1], "--popuplist") == 0) {
3382        popup_changelist_from_file(argv[2]);
3383        paranoid_MR_finish(0);
3384    }
3385
3386    if (argc == 5 && strcmp(argv[1], "--copy") == 0) {
3387        log_msg(1, "SCORE");
3388        g_loglevel = 10;
3389        if (strstr(argv[2], "save")) {
3390            log_msg(1, "Saving from %s to %s", argv[3], argv[4]);
3391            fin = fopen(argv[3], "r");
3392            fout = fopen(argv[4], "w");
3393            copy_from_src_to_dest(fin, fout, 'w');
3394            fclose(fin);
3395            fin = fopen(argv[3], "r");
3396            copy_from_src_to_dest(fin, fout, 'w');
3397            fclose(fout);
3398            fclose(fin);
3399        } else if (strstr(argv[2], "restore")) {
3400            fout = fopen(argv[3], "w");
3401            fin = fopen(argv[4], "r");
3402            copy_from_src_to_dest(fout, fin, 'r');
3403            fclose(fin);
3404            fin = fopen(argv[4], "r");
3405            copy_from_src_to_dest(fout, fin, 'r');
3406            fclose(fout);
3407            fclose(fin);
3408        } else {
3409            fatal_error("Unknown additional param");
3410        }
3411        finish(0);
3412    }
3413
3414    if (argc == 3 && strcmp(argv[1], "--mdstat") == 0) {
3415        wait_until_software_raids_are_prepped(argv[2], 100);
3416        finish(0);
3417    }
3418
3419    if (argc == 4 && strcmp(argv[1], "--mdconv") == 0) {
3420        finish(create_raidtab_from_mdstat(argv[2], argv[3]));
3421    }
3422
3423
3424    if (argc == 2 && strcmp(argv[1], "--live-grub") == 0) {
3425        retval = run_grub(FALSE, "/dev/hda");
3426        if (retval) {
3427            log_to_screen("Failed to write Master Boot Record");
3428        }
3429        paranoid_MR_finish(0);
3430    }
3431    if (argc == 3 && strcmp(argv[1], "--paa") == 0) {
3432        g_current_media_number = atoi(argv[2]);
3433        pause_and_ask_for_cdr(5, NULL);
3434        paranoid_MR_finish(0);
3435    } else if (!bkpinfo->disaster_recovery) {   // live!
3436        if (argc != 1) {
3437            popup_and_OK
3438                ("Live mode doesn't support command-line parameters yet.");
3439            paranoid_MR_finish(1);
3440//    return(1);
3441        }
3442        log_msg(1, "I am in normal, live mode.");
3443        log_msg(2, "FYI, MOUNTLIST_FNAME = %s", g_mountlist_fname);
3444        mount_boot_if_necessary();  /* for Gentoo users */
3445        log_msg(2, "Still here.");
3446        if (argc > 1 && strcmp(argv[argc - 1], "--live-from-cd") == 0) {
3447            g_restoring_live_from_cd = TRUE;
3448        }
3449        if (argc == 5 && strcmp(argv[1], "--monitas-live") == 0) {
3450            retval =
3451                restore_live_from_monitas_server(bkpinfo,
3452                                                 argv[2],
3453                                                 argv[3], argv[4]);
3454        } else {
3455            log_msg(2, "Calling restore_to_live_filesystem()");
3456            retval = restore_to_live_filesystem(bkpinfo);
3457        }
3458        log_msg(2, "Still here. Yay.");
3459        if (strlen(bkpinfo->tmpdir) > 0) {
3460            sprintf(tmp, "rm -Rf %s/*", bkpinfo->tmpdir);
3461            run_program_and_log_output(tmp, FALSE);
3462        }
3463        unmount_boot_if_necessary();    /* for Gentoo users */
3464        paranoid_MR_finish(retval);
3465    } else {
3466/* Disaster recovery mode (must be) */
3467        log_msg(1, "I must be in disaster recovery mode.");
3468        log_msg(2, "FYI, MOUNTLIST_FNAME = %s ", g_mountlist_fname);
3469        if (argc == 3 && strcmp(argv[1], "--monitas-memorex") == 0) {
3470            log_to_screen("Uh, that hasn't been implemented yet.");
3471            paranoid_MR_finish(1);
3472        }
3473
3474        iamhere("About to call load_mountlist and load_raidtab");
3475        strcpy(bkpinfo->restore_path, MNT_RESTORING);
3476        read_cfg_file_into_bkpinfo(g_mondo_cfg_file, bkpinfo);
3477        retval = load_mountlist(mountlist, g_mountlist_fname);
3478        retval += load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
3479        iamhere
3480            ("Returned from calling load_mountlist and load_raidtab successfully");
3481
3482        if (argc > 1
3483            && (strcmp(argv[1], "--compare") == 0
3484                || strcmp(argv[1], "--nuke") == 0)) {
3485            if (bkpinfo->backup_media_type == nfs
3486                && !is_this_device_mounted(bkpinfo->nfs_mount)) {
3487                log_msg(1, "Mounting nfs dir");
3488                sprintf(bkpinfo->isodir, "/tmp/isodir");
3489                run_program_and_log_output("mkdir -p /tmp/isodir", 5);
3490                sprintf(tmp, "mount %s -t nfs -o nolock /tmp/isodir",
3491                        bkpinfo->nfs_mount);
3492                run_program_and_log_output(tmp, 1);
3493            }
3494        }
3495
3496
3497        if (retval) {
3498            log_to_screen
3499                ("Warning - load_raidtab_into_raidlist returned an error");
3500        }
3501
3502
3503        log_msg(1, "Send in the clowns.");
3504
3505        if (argc == 2 && strcmp(argv[1], "--partition-only") == 0) {
3506            log_msg(0, "Partitioning only.");
3507            load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
3508            strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
3509            load_mountlist(mountlist, g_mountlist_fname);
3510            res = partition_everything(mountlist);
3511            finish(res);
3512        }
3513
3514        if (argc == 2 && strcmp(argv[1], "--format-only") == 0) {
3515            log_msg(0, "Formatting only.");
3516            load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
3517            strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
3518            load_mountlist(mountlist, g_mountlist_fname);
3519            res = format_everything(mountlist, FALSE);
3520            finish(res);
3521        }
3522
3523        if (argc == 2 && strcmp(argv[1], "--stop-lvm-and-raid") == 0) {
3524            log_msg(0, "Stopping LVM and RAID");
3525            load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
3526            strcpy(g_mountlist_fname, "/tmp/mountlist.txt");
3527            load_mountlist(mountlist, g_mountlist_fname);
3528            res = do_my_funky_lvm_stuff(TRUE, FALSE);
3529            res += stop_all_raid_devices(mountlist);
3530            finish(res);
3531        }
3532
3533        if (argc == 2 && strcmp(argv[1], "--nuke") == 0) {
3534            iamhere("nuking");
3535            retval += nuke_mode(bkpinfo, mountlist, raidlist);
3536        } else if (argc == 2 && strcmp(argv[1], "--interactive") == 0) {
3537            iamhere("catchall");
3538            retval += catchall_mode(bkpinfo, mountlist, raidlist);
3539        } else if (argc == 2 && strcmp(argv[1], "--compare") == 0) {
3540            iamhere("compare");
3541            retval += compare_mode(bkpinfo, mountlist, raidlist);
3542        } else if (argc == 2 && strcmp(argv[1], "--iso") == 0) {
3543            iamhere("iso");
3544            retval = iso_mode(bkpinfo, mountlist, raidlist, FALSE);
3545        } else if (argc == 2 && strcmp(argv[1], "--mbr") == 0) {
3546            iamhere("mbr");
3547            retval = mount_all_devices(mountlist, TRUE);
3548            if (!retval) {
3549                retval += run_boot_loader(FALSE);
3550                retval += unmount_all_devices(mountlist);
3551            }
3552            if (retval) {
3553                log_to_screen("Failed to write Master Boot Record");
3554            }
3555        } else if (argc == 2 && strcmp(argv[1], "--isonuke") == 0) {
3556            iamhere("isonuke");
3557            retval = iso_mode(bkpinfo, mountlist, raidlist, TRUE);
3558        } else if (argc != 1) {
3559            log_to_screen("Invalid paremeters");
3560            paranoid_MR_finish(1);
3561        } else {
3562            iamhere("catchall (no mode specified in command-line call");
3563            retval += catchall_mode(bkpinfo, mountlist, raidlist);
3564        }
3565    }
3566
3567    /* clean up at the end */
3568    if (retval) {
3569        if (does_file_exist("/tmp/changed.files")) {
3570            log_to_screen
3571                ("See /tmp/changed.files for list of files that have changed.");
3572        }
3573        mvaddstr_and_log_it(g_currentY++,
3574                            0,
3575                            "Run complete. Errors were reported. Please review the logfile.");
3576    } else {
3577        if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
3578            mvaddstr_and_log_it(g_currentY++,
3579                                0,
3580                                "Run complete. Please remove floppy/CD/media and reboot.");
3581        } else {
3582            run_program_and_log_output("sync", FALSE);
3583            if (is_this_device_mounted(MNT_CDROM)) {
3584                res =
3585                    run_program_and_log_output("umount " MNT_CDROM, FALSE);
3586            } else {
3587                res = 0;
3588            }
3589
3590            if (!bkpinfo->please_dont_eject) {
3591                res = eject_device("/dev/cdrom");
3592/*
3593              if (res)
3594        {
3595          log_to_screen( "WARNING - failed to eject CD-ROM disk" );
3596        }
3597*/
3598            }
3599            mvaddstr_and_log_it(g_currentY++,
3600                                0,
3601                                "Run complete. Please remove media and reboot.");
3602        }
3603    }
3604
3605// g_I_have_just_nuked is set true by nuke_mode() just before it returns
3606    if (g_I_have_just_nuked || does_file_exist("/POST-NUKE-ANYWAY")) {
3607        if (!system("which post-nuke > /dev/null 2> /dev/null")) {
3608            log_msg(1, "post-nuke found; running...");
3609            if (mount_all_devices(mountlist, TRUE)) {
3610                log_to_screen
3611                    ("Unable to re-mount partitions for post-nuke stuff");
3612            } else {
3613                log_msg(1, "Re-mounted partitions for post-nuke stuff");
3614                sprintf(tmp, "post-nuke %s %d", bkpinfo->restore_path,
3615                        retval);
3616                if (!g_text_mode) {
3617                    newtSuspend();
3618                }
3619                log_msg(2, "Calling '%s'", tmp);
3620                if ((res = system(tmp))) {
3621                    log_OS_error(tmp);
3622                }
3623                if (!g_text_mode) {
3624                    newtResume();
3625                }
3626//              newtCls();
3627                log_msg(1, "post-nuke returned w/ res=%d", res);
3628            }
3629            unmount_all_devices(mountlist);
3630            log_msg(1, "I've finished post-nuking.");
3631        }
3632    }
3633/* 
3634  log_to_screen("If you are REALLY in a hurry, hit Ctrl-Alt-Del now.");
3635  log_to_screen("Otherwise, please wait until the RAID disks are done.");
3636  wait_until_software_raids_are_prepped("/proc/mdstat", 100);
3637  log_to_screen("Thank you.");
3638*/
3639    unlink("/tmp/mondo-run-prog.tmp");
3640    set_signals(FALSE);
3641    sprintf(tmp, "rm -Rf %s", bkpinfo->tmpdir);
3642    run_program_and_log_output(tmp, FALSE);
3643    log_to_screen
3644        ("Restore log copied to /tmp/mondo-restore.log on your hard disk");
3645    sprintf(tmp,
3646            "Mondo-restore is exiting (retval=%d)                                      ",
3647            retval);
3648    log_to_screen(tmp);
3649    sprintf(tmp, "umount %s", bkpinfo->isodir);
3650    run_program_and_log_output(tmp, 5);
3651    paranoid_free(mountlist);
3652    paranoid_free(raidlist);
3653    if (am_I_in_disaster_recovery_mode()) {
3654        run_program_and_log_output("mount / -o remount,rw", 2);
3655    }                           // for b0rken distros
3656    paranoid_MR_finish(retval); // frees global stuff plus bkpinfo
3657    free_libmondo_global_strings(); // it's fine to have this here :) really :)
3658    paranoid_free(a);
3659    paranoid_free(b);
3660    paranoid_free(tmp);
3661
3662    unlink("/tmp/filelist.full");
3663    unlink("/tmp/filelist.full.gz");
3664
3665    exit(retval);
3666}
3667
3668/**************************************************************************
3669 *END_MAIN                                                                *
3670 **************************************************************************/
3671
3672
3673
3674
3675
3676/**************************************************************************
3677 *END_MONDO-RESTORE.C                                                     *
3678 **************************************************************************/
Note: See TracBrowser for help on using the repository browser.