source: MondoRescue/branches/2.2.5/mondo/src/common/libmondo-archive.c@ 1687

Last change on this file since 1687 was 1687, checked in by Bruno Cornec, 17 years ago

First attempt to integrate USB support in mondoarchive (may not compile)

  • Property svn:keywords set to Id
File size: 116.3 KB
Line 
1/* libmondo-archive.c
2 $Id: libmondo-archive.c 1687 2007-10-20 12:47:12Z bruno $
3
4subroutines to handle the archiving of files
5
6
7
807/14
9- moved all ACL, xattr stuff here from libmondo-filelist.c
10
1107/12
12- when trying to find boot loader, look for /boot's /dev, not just /'s dev
13
1407/10
15- better detection of changed files
16- added ACL, xattr support for afio
17
1806/17
19- backup with partimagehack if NTFS device; dd, if non-NTFS device
20
2106/14
22- use mondorescue.iso instead of mindi.iso
23
2404/28
25- cleaned up if/then architecture-specific stuff re: CD-R(W)s
26
2704/03
28- added star support
29
3003/12
31- modified offer_to_write_floppies() to support dual-disk boot/root thing
32
3301/20/2004
34- reformat dvd twice if necessary
35
3612/01/2003
37- added pause_for_N_seconds(5, "Letting DVD drive settle");
38
3911/23
40- added #define DVDRWFORMAT
41
4211/20
43- use --no-emul-boot -b isolinux.bin instead of -b mindi-boot.2880.img
44
4510/23
46- wipe DVD at start, whether or not disk is DVD-RW; this is
47 just a test, to see why dvd+rw-format followed by growiosfs
48 locks up the drive
49- use the singlethreaded make_afioballs_and_images_OLD()
50 instead of the multithreaded make_afioballs_and_images()
51 if backing up to tape
52
5310/21
54- if backing up to dvd, look for relevant tools;
55 abort if missing
56
5710/15
58- UI tweaks
59
6010/10
61- eject/inject DVD after wiping it
62
6309/27
64- pause_and_ask_for_cdr() will now blank DVD if necessary
65
6609/26
67- proper reporting of media type in displayed strings
68 (i.e. DVD if DVD, CD if CD, etc.)
69
7009/25
71- add DVD support
72
7309/23
74- malloc/free global strings in new subroutines - malloc_libmondo_global_strings()
75 and free_libmondo_global_strings() - which are in libmondo-tools.c
76
7709/15
78- changed a bunch of char[MAX_STR_LEN]'s to char*; malloc; free;
79
8009/14
81- cosmetic bug re: call to 'which dvdrecord'
82
8309/09
84- copy `locate isolinux.bin | tail -n1` to CD before calling mkisfso
85 if file is missing (in which case, bug in Mindi.pl!)
86- reduced noof_threads from 3 to 2
87- fixed cosmetic bug in make_slices_and_images()
88
8909/02
90- fixed cosmetic bug in verify_data()
91
9205/01 - 08/31
93- added partimagehack hooks
94- make twice as many archives at once as before
95- fixed syntax error re: system(xxx,FALSE)
96- unmount CD-ROM before burning (necessary for RH8/9)
97- only ask for new media if sensible
98- fixed mondoarchive -Vi multi-CD verify bug (Tom Mortell)
99- use single-threaded make_afioballs_and_images() if FreeBSD
100- fixed bug on line 476 (Joshua Oreman)
101- re-enabled the pause, for people w/ weird CD-ROM drives
102- added Joshua Oreman's FreeBSD patches
103- don't listen to please_dont_eject_when_restoring
104 ...after all, we're not restoring :)
105
10604/01 - 04/30
107- cleaned up archive_this_fileset()
108- decreased ARCH_THREADS from 3 to 2
109- pause_and_ask_for_cd() --- calls retract_CD_tray_and_defeat_autorun()
110- call assert() and log_OS_error() in various places
111- cleaned up code a bit
112- increased ARCH_THREADS from 2 to 3
113- misc clean-up (Tom Mortell)
114
11503/15/2003
116- fixed problem w/ multi-ISO verify cycle (Tom Mortell)
117
11811/01 - 12/31/2002
119- removed references to make_afioballs_and_images_OLD()
120- added some error-checking to make_afioballs_in_background
121- make_iso_and_go_on() now copies Mondo's autorun file to CD
122- set scratchdir dir to 1744 when burning CD
123- cleaned up code a bit
124- in call_mindi_...(), gracefully handle the user's input
125 if they specify the boot loader but not the boot device
126- offer to abort if GRUB is boot loader and /dev/md* is
127 the boot device (bug in grub-install)
128- if boot loader is R then write RAW as bootloader.name
129- multithreaded make_afioballs_and_images()
130- fixed slice_up_file_etc() for 0-compression users
131- line 1198: added call to is_this_file_compressed() to stop
132 slice_up_file_etc() from compressing files which are
133 already compressed
134- other hackery related to the above enhancement
135- afio no longer forcibly compresses all files (i.e. I dropped
136 the -U following suggestions from users); let's see if
137 it works :)
138
13910/01 - 10/31
140- mondoarchive (with no parameters) wasn't working
141 if user said yes when asked if BurnProof drive; FIXED
142- if manual CD tray and writing to ISO's then prompt
143 for each & every new CD
144- moved a lot of subroutines here
145 from mondo-archive.c and mondo-floppies.c
146
14709/01 - 09/30
148- if CD not burned OK then don't try to verify
149- change 64k to TAPE_BLOCK_SIZE
150- run_program_and_log_output() now takes boolean operator to specify
151 whether it will log its activities in the event of _success_
152- orig_vfy_flag_val added to write_iso_and_go_on, to restore bkpinfo->verify_data's
153 value if altered by verify_cd_image()
154
15508/01 - 08/31
156- use data structure to store the fname, checksum, mods & perms of each bigfile
157 ... biggiestruct :)
158- bigger tmp[]'s in a few places
159- cleaned up the (user-friendly) counting of biggiefiles a bit
160- if media_size[N]<=0 then catch it & abort, unless it's tape,
161 in which case, allow it
162- cleaned up a lot of log_it() calls
163- fixed NULL filename-related bug in can_we_fit_these_files_on_media()
164- deleted can_we_fit.....() subroutine because it was causing problems
165 --- moved its code into the one subroutine which called it
166- created [08/01/2002]
167*/
168
169/**
170 * @file
171 * Functions to handle backing up data.
172 * This is the main file (at least the longest one) in libmondo.
173 */
174
175#include "my-stuff.h"
176#include "mondostructures.h"
177#include "libmondo-string-EXT.h"
178#include "libmondo-stream-EXT.h"
179#include "libmondo-devices-EXT.h"
180#include "libmondo-tools-EXT.h"
181#include "libmondo-gui-EXT.h"
182#include "libmondo-fork-EXT.h"
183#include "libmondo-files-EXT.h"
184#include "libmondo-filelist-EXT.h"
185#include "libmondo-tools-EXT.h"
186#include "libmondo-verify-EXT.h"
187#include "libmondo-archive.h"
188#include "lib-common-externs.h"
189#include <sys/sem.h>
190#include <sys/types.h>
191#include <sys/ipc.h>
192#include <stdarg.h>
193#define DVDRWFORMAT 1
194
195
196
197/** @def DEFAULT_1722MB_DISK The default 1.722M floppy disk to write images to. */
198/** @def BACKUP_1722MB_DISK The 1.722M floppy disk to try if the default fails. */
199
200#ifdef __FreeBSD__
201#define DEFAULT_1722MB_DISK "/dev/fd0.1722"
202#define BACKUP_1722MB_DISK "/dev/fd0.1722"
203#else
204#define DEFAULT_1722MB_DISK "/dev/fd0u1722"
205#define BACKUP_1722MB_DISK "/dev/fd0H1722"
206#ifndef _SEMUN_H
207#define _SEMUN_H
208
209 /**
210 * The semaphore union, provided only in case the user's system doesn't.
211 */
212union semun {
213 int val;
214 struct semid_ds *buf;
215 unsigned short int *array;
216 struct seminfo *__buf;
217};
218#endif
219#endif /* __FreeBSD__ */
220
221/*@unused@*/
222//static char cvsid[] = "$Id: libmondo-archive.c 1687 2007-10-20 12:47:12Z bruno $";
223
224/* *************************** external global vars ******************/
225extern int g_current_media_number;
226extern int g_currentY;
227extern bool g_text_mode;
228extern bool g_exiting;
229extern long g_current_progress;
230extern FILE *g_tape_stream;
231extern long long g_tape_posK;
232extern char *g_mondo_home;
233extern char *g_tmpfs_mountpt;
234extern bool g_cd_recovery;
235extern char *g_serial_string;
236
237extern char *g_getfacl;
238extern char *g_getfattr;
239extern char *MONDO_LOGFILE;
240
241/* Reference to global bkpinfo */
242extern struct s_bkpinfo *bkpinfo;
243
244
245
246/**
247 * @addtogroup globalGroup
248 * @{
249 */
250/**
251 * The current backup media type in use.
252 */
253t_bkptype g_backup_media_type = none;
254
255/**
256 * Incremented by each archival thread when it starts up. After that,
257 * this is the number of threads running.
258 */
259int g_current_thread_no = 0;
260
261/* @} - end of globalGroup */
262
263extern int g_noof_rows;
264
265/* Semaphore-related code */
266
267static int set_semvalue(void);
268static void del_semvalue(void);
269static int semaphore_p(void);
270static int semaphore_v(void);
271
272static int g_sem_id;
273static int g_sem_key;
274
275
276
277
278/**
279 * Initialize the semaphore.
280 * @see del_semvalue
281 * @see semaphore_p
282 * @see semaphore_v
283 * @return 1 for success, 0 for failure.
284 */
285static int set_semvalue(void) // initializes semaphore
286{
287 union semun sem_union;
288 sem_union.val = 1;
289 if (semctl(g_sem_id, 0, SETVAL, sem_union) == -1) {
290 return (0);
291 }
292 return (1);
293}
294
295/**
296 * Frees (deletes) the semaphore. Failure is indicated by a log
297 * message.
298 * @see set_semvalue
299 */
300static void del_semvalue(void) // deletes semaphore
301{
302 union semun sem_union;
303
304 if (semctl(g_sem_id, 0, IPC_RMID, sem_union) == -1) {
305 log_msg(3, "Failed to delete semaphore");
306 }
307}
308
309/**
310 * Acquire (increment) the semaphore (change status to P).
311 * @return 1 for success, 0 for failure.
312 * @see semaphore_v
313 */
314static int semaphore_p(void) // changes status to 'P' (waiting)
315{
316 struct sembuf sem_b;
317
318 sem_b.sem_num = 0;
319 sem_b.sem_op = -1; // P()
320 sem_b.sem_flg = SEM_UNDO;
321 if (semop(g_sem_id, &sem_b, 1) == -1) {
322 log_msg(3, "semaphore_p failed");
323 return (0);
324 }
325 return (1);
326}
327
328/**
329 * Free (decrement) the semaphore (change status to V).
330 * @return 1 for success, 0 for failure.
331 */
332static int semaphore_v(void) // changes status to 'V' (free)
333{
334 struct sembuf sem_b;
335
336 sem_b.sem_num = 0;
337 sem_b.sem_op = 1; // V()
338 sem_b.sem_flg = SEM_UNDO;
339 if (semop(g_sem_id, &sem_b, 1) == -1) {
340 log_msg(3, "semaphore_v failed");
341 return (0);
342 }
343 return (1);
344}
345
346
347//------------------------------------------------------
348
349
350/**
351 * Size in megabytes of the buffer afforded to the executable "buffer".
352 * This figure is used when we calculate how much data we have probably 'lost'
353 * when writing off the end of tape N, so that we can then figure out how much
354 * data we must recreate & write to the start of tape N+1.
355 */
356extern int g_tape_buffer_size_MB;
357
358
359
360
361int
362archive_this_fileset_with_star(char *filelist, char *fname, int setno)
363{
364 int retval = 0;
365 unsigned int res = 0;
366 int tries = 0;
367 char *command;
368 char *zipparams;
369 char *tmp;
370 char *p;
371
372 malloc_string(command);
373 malloc_string(zipparams);
374 malloc_string(tmp);
375
376 if (!does_file_exist(filelist)) {
377 sprintf(tmp, "(archive_this_fileset) - filelist %s does not exist",
378 filelist);
379 log_to_screen(tmp);
380 return (1);
381 }
382
383 sprintf(tmp, "echo hi > %s 2> /dev/null", fname);
384 if (system(tmp)) {
385 fatal_error("Unable to write tarball to scratchdir");
386 }
387
388 sprintf(command, "star H=star list=%s -c " STAR_ACL_SZ " file=%s",
389 filelist, fname);
390 if (bkpinfo->use_lzo) {
391 fatal_error("Can't use lzop");
392 }
393 if (bkpinfo->compression_level > 0) {
394 strcat(command, " -bz");
395 }
396 sprintf(command + strlen(command), " 2>> %s", MONDO_LOGFILE);
397 log_msg(4, "command = '%s'", command);
398
399 for (res = 99, tries = 0; tries < 3 && res != 0; tries++) {
400 log_msg(5, "command='%s'", command);
401 res = system(command);
402 strcpy(tmp, last_line_of_file(MONDO_LOGFILE));
403 log_msg(1, "res=%d; tmp='%s'", res, tmp);
404 if (bkpinfo->use_star && (res == 254 || res == 65024)
405 && strstr(tmp, "star: Processed all possible files")
406 && tries > 0) {
407 log_msg(1, "Star returned nonfatal error");
408 res = 0;
409 }
410 if (res) {
411 log_OS_error(command);
412 p = strstr(command, "-acl ");
413 if (p) {
414 p[0] = p[1] = p[2] = p[3] = ' ';
415 log_msg(1, "new command = '%s'", command);
416 } else {
417 log_msg(3,
418 "Attempt #%d failed. Pausing 3 seconds and retrying...",
419 tries + 1);
420 sleep(3);
421 }
422 }
423 }
424 retval += res;
425 if (retval) {
426 log_msg(3, "Failed to write set %d", setno);
427 } else if (tries > 1) {
428 log_msg(3, "Succeeded in writing set %d, on try #%d", setno,
429 tries);
430 }
431
432 paranoid_free(command);
433 paranoid_free(zipparams);
434 paranoid_free(tmp);
435 return (retval);
436}
437
438
439/**
440 * Call @c afio to archive the filelist @c filelist to the file @c fname.
441 *
442 * @param bkpinfo The backup information structure. Fields used:
443 * - @c compression_level
444 * - @c scratchdir (only verifies existence)
445 * - @c tmpdir (only verifies existence)
446 * - @c zip_exe
447 * - @c zip_suffix
448 * @param filelist The path to a file containing a list of files to be archived
449 * in this fileset.
450 * @param fname The output file to archive to.
451 * @param setno This fileset number.
452 * @return The number of errors encountered (0 for success).
453 * @ingroup LLarchiveGroup
454 */
455int
456archive_this_fileset(char *filelist, char *fname, int setno)
457{
458
459 /*@ int *************************************************************** */
460 int retval = 0;
461 int res = 0;
462 int i = 0;
463 int tries = 0;
464 static int free_ramdisk_space = 9999;
465
466 /*@ buffers ************************************************************ */
467 char *command;
468 char *zipparams;
469 char *tmp;
470
471 assert(bkpinfo != NULL);
472 assert_string_is_neither_NULL_nor_zerolength(filelist);
473 assert_string_is_neither_NULL_nor_zerolength(fname);
474
475 if (bkpinfo->compression_level > 0 && bkpinfo->use_star) {
476 return (archive_this_fileset_with_star(filelist, fname, setno));
477 }
478
479 malloc_string(command);
480 malloc_string(zipparams);
481 malloc_string(tmp);
482
483 if (!does_file_exist(filelist)) {
484 sprintf(tmp, "(archive_this_fileset) - filelist %s does not exist",
485 filelist);
486 log_to_screen(tmp);
487 return (1);
488 }
489 sprintf(tmp, "echo hi > %s 2> /dev/null", fname);
490 if (system(tmp)) {
491 fatal_error("Unable to write tarball to scratchdir");
492 }
493
494
495 if (bkpinfo->compression_level > 0) {
496 sprintf(tmp, "%s/do-not-compress-these", g_mondo_home);
497 // -b %ld, TAPE_BLOCK_SIZE
498 sprintf(zipparams, "-Z -P %s -G %d -T 3k", bkpinfo->zip_exe,
499 bkpinfo->compression_level);
500 if (does_file_exist(tmp)) {
501 strcat(zipparams, " -E ");
502 strcat(zipparams, tmp);
503 } else {
504 log_msg(3, "%s not found. Cannot exclude zipfiles, etc.", tmp);
505 }
506 } else {
507 zipparams[0] = '\0';
508 }
509
510// make_hole_for_file(fname);
511
512 if (!does_file_exist(bkpinfo->tmpdir)) {
513 log_OS_error("tmpdir not found");
514 fatal_error("tmpdir not found");
515 }
516 if (!does_file_exist(bkpinfo->scratchdir)) {
517 log_OS_error("scratchdir not found");
518 fatal_error("scratchdir not found");
519 }
520 sprintf(command, "rm -f %s %s. %s.gz %s.%s", fname, fname, fname,
521 fname, bkpinfo->zip_suffix);
522 paranoid_system(command);
523
524 sprintf(command, "afio -o -b %ld -M 16m %s %s < %s 2>> %s",
525 TAPE_BLOCK_SIZE, zipparams, fname, filelist, MONDO_LOGFILE);
526
527 sprintf(tmp, "echo hi > %s 2> /dev/null", fname);
528 if (system(tmp)) {
529 fatal_error("Unable to write tarball to scratchdir");
530 }
531
532 for (res = 99, tries = 0; tries < 3 && res != 0; tries++) {
533 log_msg(5, "command='%s'", command);
534 res = system(command);
535 if (res) {
536 log_OS_error(command);
537 log_msg(3,
538 "Attempt #%d failed. Pausing 3 seconds and retrying...",
539 tries + 1);
540 sleep(3);
541 }
542 }
543 retval += res;
544 if (retval) {
545 log_msg(3, "Failed to write set %d", setno);
546 } else if (tries > 1) {
547 log_msg(3, "Succeeded in writing set %d, on try #%d", setno,
548 tries);
549 }
550
551 if (g_tmpfs_mountpt[0] != '\0') {
552 i = atoi(call_program_and_get_last_line_of_output
553 ("df -m -P | grep dev/shm | grep -v none | tr -s ' ' '\t' | cut -f4"));
554 if (i > 0) {
555 if (free_ramdisk_space > i) {
556 free_ramdisk_space = i;
557 log_msg(2, "min(free_ramdisk_space) is now %d",
558 free_ramdisk_space);
559 if (free_ramdisk_space < 10) {
560 fatal_error
561 ("Please increase PPCFG_RAMDISK_SIZE in my-stuff.h to increase size of ramdisk ");
562 }
563 }
564 }
565 }
566 paranoid_free(command);
567 paranoid_free(zipparams);
568 paranoid_free(tmp);
569 return (retval);
570}
571
572
573
574
575
576
577/**
578 * Wrapper function for all the backup commands.
579 * Calls these other functions: @c prepare_filelist(),
580 * @c call_filelist_chopper(), @c copy_mondo_and_mindi_stuff_to_scratchdir(),
581 * @c call_mindi_to_supply_boot_disks(), @c do_that_initial_phase(),
582 * @c make_those_afios_phase(), @c make_those_slices_phase(), and
583 * @c do_that_final_phase(). If anything fails before @c do_that_initial_phase(),
584 * @c fatal_error is called with a suitable message.
585 * @param bkpinfo The backup information structure. Uses most fields.
586 * @return The number of non-fatal errors encountered (0 for success).
587 * @ingroup archiveGroup
588 */
589int backup_data()
590{
591 int retval = 0, res = 0;
592 char *tmp;
593
594 assert(bkpinfo != NULL);
595 set_g_cdrom_and_g_dvd_to_bkpinfo_value();
596 malloc_string(tmp);
597 if (bkpinfo->backup_media_type == dvd) {
598#ifdef DVDRWFORMAT
599 if (!find_home_of_exe("dvd+rw-format")) {
600 fatal_error
601 ("Cannot find dvd+rw-format. Please install it or fix your PATH.");
602 }
603#endif
604 if (!find_home_of_exe("growisofs")) {
605 fatal_error
606 ("Cannot find growisofs. Please install it or fix your PATH.");
607 }
608 }
609
610 if ((res = prepare_filelist())) { /* generate scratchdir/filelist.full */
611 fatal_error("Failed to generate filelist catalog");
612 }
613 if (call_filelist_chopper()) {
614 fatal_error("Failed to run filelist chopper");
615 }
616
617/*
618 sprintf(tmp, "wc -l %s/archives/filelist.full > %s/archives/filelist.count",bkpinfo->scratchdir, bkpinfo->scratchdir);
619 if (run_program_and_log_output(tmp, 2))
620 { fatal_error("Failed to count filelist.full"); }
621*/
622 sprintf(tmp, "gzip -9 %s/archives/filelist.full", bkpinfo->scratchdir);
623 if (run_program_and_log_output(tmp, 2)) {
624 fatal_error("Failed to gzip filelist.full");
625 }
626 sprintf(tmp, "cp -f %s/archives/*list*.gz %s", bkpinfo->scratchdir,
627 bkpinfo->tmpdir);
628 if (run_program_and_log_output(tmp, 2)) {
629 fatal_error("Failed to copy to tmpdir");
630 }
631
632 copy_mondo_and_mindi_stuff_to_scratchdir(); // payload, too, if it exists
633#if __FreeBSD__ == 5
634 strcpy(bkpinfo->kernel_path, "/boot/kernel/kernel");
635#elif __FreeBSD__ == 4
636 strcpy(bkpinfo->kernel_path, "/kernel");
637#elif linux
638 if (figure_out_kernel_path_interactively_if_necessary
639 (bkpinfo->kernel_path)) {
640 fatal_error
641 ("Kernel not found. Please specify manually with the '-k' switch.");
642 }
643#else
644#error "I don't know about this system!"
645#endif
646 if ((res = call_mindi_to_supply_boot_disks())) {
647 fatal_error("Failed to generate boot+data disks");
648 }
649 retval += do_that_initial_phase(); // prepare
650 sprintf(tmp, "rm -f %s/images/*.iso", bkpinfo->scratchdir);
651 run_program_and_log_output(tmp, 1);
652 retval += make_those_afios_phase(); // backup regular files
653 retval += make_those_slices_phase(); // backup BIG files
654 retval += do_that_final_phase(); // clean up
655 log_msg(1, "Creation of archives... complete.");
656 if (bkpinfo->verify_data) {
657 sleep(2);
658 }
659 paranoid_free(tmp);
660 return (retval);
661}
662
663
664
665
666/**
667 * Call Mindi to generate boot and data disks.
668 * @note This binds correctly to the new Perl version of mindi.
669 * @param bkpinfo The backup information structure. Fields used:
670 * - @c backup_media_type
671 * - @c boot_loader
672 * - @c boot_device
673 * - @c compression_level
674 * - @c differential
675 * - @c exclude_paths
676 * - @c image_devs
677 * - @c kernel_path
678 * - @c make_cd_use_lilo
679 * - @c media_device
680 * - @c media_size
681 * - @c nonbootable_backup
682 * - @c scratchdir
683 * - @c tmpdir
684 * - @c use_lzo
685 *
686 * @return The number of errors encountered (0 for success)
687 * @bug The code to automagically determine the boot drive
688 * is messy and system-dependent. In particular, it breaks
689 * for Linux RAID and LVM users.
690 * @ingroup MLarchiveGroup
691 */
692int call_mindi_to_supply_boot_disks()
693{
694 /*@ buffer ************************************************************ */
695 char *tmp;
696 char *tmp1 = NULL;
697 char *tmp2 = NULL;
698 char *scratchdir;
699 char *command;
700 char *use_lzo_sz;
701 char *use_gzip_sz;
702 char *use_comp_sz;
703 char *use_star_sz;
704 char *bootldr_str;
705 char *tape_device;
706 char *last_filelist_number;
707 char *broken_bios_sz;
708 char *cd_recovery_sz;
709 char *tape_size_sz;
710 char *devs_to_exclude;
711 char *use_lilo_sz;
712 char *value;
713 char *bootdev;
714
715
716
717 /*@ char ************************************************************** */
718 char ch = '\0';
719
720 /*@ long ********************************************************** */
721 long lines_in_filelist = 0;
722
723 /*@ int ************************************************************* */
724 int res = 0;
725 long estimated_total_noof_slices = 0;
726
727 assert(bkpinfo != NULL);
728 command = malloc(1200);
729 tmp = malloc(9*MAX_STR_LEN);
730 malloc_string(scratchdir);
731 malloc_string(use_lzo_sz);
732 malloc_string(use_gzip_sz);
733 malloc_string(use_star_sz);
734 malloc_string(use_comp_sz);
735 malloc_string(bootldr_str);
736 malloc_string(tape_device);
737 malloc_string(last_filelist_number);
738 malloc_string(broken_bios_sz);
739 malloc_string(cd_recovery_sz);
740 malloc_string(tape_size_sz);
741 malloc_string(devs_to_exclude);
742 malloc_string(use_lilo_sz); /* BCO: shared between LILO/ELILO */
743 malloc_string(value);
744 malloc_string(bootdev);
745
746 strcpy(scratchdir, bkpinfo->scratchdir);
747 sprintf(tmp,
748 "echo '%s' | tr -s ' ' '\n' | grep -E '^/dev/.*$' | tr -s '\n' ' ' | awk '{print $0\"\\n\";}'",
749 bkpinfo->exclude_paths);
750 strcpy(devs_to_exclude, call_program_and_get_last_line_of_output(tmp));
751 sprintf(tmp, "devs_to_exclude = '%s'", devs_to_exclude);
752 log_msg(2, tmp);
753 mvaddstr_and_log_it(g_currentY, 0,
754 "Calling MINDI to create boot+data disks");
755 sprintf(tmp, "%s/filelist.full", bkpinfo->tmpdir);
756 if (!does_file_exist(tmp)) {
757 sprintf(tmp, "%s/tmpfs/filelist.full", bkpinfo->tmpdir);
758 if (!does_file_exist(tmp)) {
759 fatal_error
760 ("Cannot find filelist.full, so I cannot count its lines");
761 }
762 }
763 lines_in_filelist = count_lines_in_file(tmp);
764 sprintf(tmp, "%s/LAST-FILELIST-NUMBER", bkpinfo->tmpdir);
765 strcpy(last_filelist_number, last_line_of_file(tmp));
766 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
767 sprintf(tape_size_sz, "%ld", bkpinfo->media_size[1]);
768 strcpy(tape_device, bkpinfo->media_device);
769 } else {
770 tape_size_sz[0] = '\0';
771 tape_device[0] = '\0';
772 }
773 if (bkpinfo->use_lzo) {
774 strcpy(use_lzo_sz, "yes");
775 } else {
776 strcpy(use_lzo_sz, "no");
777 }
778 if (bkpinfo->use_gzip) {
779 strcpy(use_gzip_sz, "yes");
780 } else {
781 strcpy(use_gzip_sz, "no");
782 }
783 if (bkpinfo->use_star) {
784 strcpy(use_star_sz, "yes");
785 } else {
786 strcpy(use_star_sz, "no");
787 }
788
789 if (bkpinfo->compression_level > 0) {
790 strcpy(use_comp_sz, "yes");
791 } else {
792 strcpy(use_comp_sz, "no");
793 }
794
795 strcpy(broken_bios_sz, "yes"); /* assume so */
796 if (g_cd_recovery) {
797 strcpy(cd_recovery_sz, "yes");
798 } else {
799 strcpy(cd_recovery_sz, "no");
800 }
801 if (bkpinfo->make_cd_use_lilo) {
802 strcpy(use_lilo_sz, "yes");
803 } else {
804 strcpy(use_lilo_sz, "no");
805 }
806
807 if (!bkpinfo->nonbootable_backup
808 && (bkpinfo->boot_loader == '\0'
809 || bkpinfo->boot_device[0] == '\0')) {
810
811#ifdef __FreeBSD__
812 strcpy(bootdev, call_program_and_get_last_line_of_output
813 ("mount | grep ' /boot ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"));
814 if (!bootdev[0]) {
815 strcpy(bootdev, call_program_and_get_last_line_of_output
816 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"));
817 }
818#else
819 strcpy(bootdev, call_program_and_get_last_line_of_output
820 ("mount | grep ' /boot ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
821 if (strstr(bootdev, "/dev/cciss/")) {
822 strcpy(bootdev, call_program_and_get_last_line_of_output
823 ("mount | grep ' /boot ' | head -1 | cut -d' ' -f1 | cut -dp -f1"));
824 }
825 if (!bootdev[0]) {
826 strcpy(bootdev, call_program_and_get_last_line_of_output
827 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
828 if (strstr(bootdev, "/dev/cciss/")) {
829 strcpy(bootdev, call_program_and_get_last_line_of_output
830 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | cut -dp -f1"));
831 }
832 }
833#endif
834 if (bootdev[0])
835 ch = which_boot_loader(bootdev);
836 else
837 ch = 'U';
838 if (bkpinfo->boot_loader != '\0') {
839 sprintf(tmp, "User specified boot loader. It is '%c'.",
840 bkpinfo->boot_loader);
841 log_msg(2, tmp);
842 } else {
843 bkpinfo->boot_loader = ch;
844 }
845 if (bkpinfo->boot_device[0] != '\0') {
846 sprintf(tmp, "User specified boot device. It is '%s'.",
847 bkpinfo->boot_device);
848 log_msg(2, tmp);
849 } else {
850 strcpy(bkpinfo->boot_device, bootdev);
851 }
852 }
853
854 if (
855#ifdef __FreeBSD__
856 bkpinfo->boot_loader != 'B' && bkpinfo->boot_loader != 'D' &&
857#endif
858#ifdef __IA64__
859 bkpinfo->boot_loader != 'E' &&
860#endif
861 bkpinfo->boot_loader != 'L' && bkpinfo->boot_loader != 'G'
862 && bkpinfo->boot_loader != 'R'
863 && !bkpinfo->nonbootable_backup) {
864 fatal_error
865 ("Please specify your boot loader and device, e.g. -l GRUB -f /dev/hda. Type 'man mondoarchive' to read the manual.");
866 }
867 if (bkpinfo->boot_loader == 'L') {
868 strcpy(bootldr_str, "LILO");
869 if (!does_file_exist("/etc/lilo.conf")) {
870 fatal_error
871 ("The de facto standard location for your boot loader's config file is /etc/lilo.conf but I cannot find it there. What is wrong with your Linux distribution?");
872 }
873 } else if (bkpinfo->boot_loader == 'G') {
874 strcpy(bootldr_str, "GRUB");
875 if (!does_file_exist("/etc/grub.conf")
876 && does_file_exist("/boot/grub/grub.conf")) {
877 run_program_and_log_output
878 ("ln -sf /boot/grub/grub.conf /etc/grub.conf", 5);
879 }
880 /* Detect Debian's grub config file */
881 else if (!does_file_exist("/etc/grub.conf")
882 && does_file_exist("/boot/grub/menu.lst")) {
883 run_program_and_log_output
884 ("ln -s /boot/grub/menu.lst /etc/grub.conf", 5);
885 }
886 if (!does_file_exist("/etc/grub.conf")) {
887 fatal_error
888 ("The de facto standard location for your boot loader's config file is /etc/grub.conf but I cannot find it there. What is wrong with your Linux distribution? Try 'ln -s /boot/grub/menu.lst /etc/grub.conf'...");
889 }
890 } else if (bkpinfo->boot_loader == 'E') {
891 strcpy(bootldr_str, "ELILO");
892 /* BCO: fix it for Debian, Mandrake, ... */
893 if (!does_file_exist("/etc/elilo.conf")
894 && does_file_exist("/boot/efi/efi/redhat/elilo.conf")) {
895 run_program_and_log_output
896 ("ln -sf /boot/efi/efi/redhat/elilo.conf /etc/elilo.conf",
897 5);
898 }
899 if (!does_file_exist("/etc/elilo.conf")
900 && does_file_exist("/boot/efi/efi/SuSE/elilo.conf")) {
901 run_program_and_log_output
902 ("ln -sf /boot/efi/efi/SuSE/elilo.conf /etc/elilo.conf",
903 5);
904 }
905 if (!does_file_exist("/etc/elilo.conf")
906 && does_file_exist("/boot/efi/efi/debian/elilo.conf")) {
907 run_program_and_log_output
908 ("ln -sf /boot/efi/efi/debian/elilo.conf /etc/elilo.conf",
909 5);
910 }
911 if (!does_file_exist("/etc/elilo.conf")
912 && does_file_exist("/boot/efi/debian/elilo.conf")) {
913 run_program_and_log_output
914 ("ln -sf /boot/efi/debian/elilo.conf /etc/elilo.conf",
915 5);
916 }
917 if (!does_file_exist("/etc/elilo.conf")) {
918 fatal_error
919 ("The de facto mondo standard location for your boot loader's config file is /etc/elilo.conf but I cannot find it there. What is wrong with your Linux distribution? Try finding it under /boot/efi and do 'ln -s /boot/efi/..../elilo.conf /etc/elilo.conf'");
920 }
921 } else if (bkpinfo->boot_loader == 'R') {
922 strcpy(bootldr_str, "RAW");
923 }
924#ifdef __FreeBSD__
925 else if (bkpinfo->boot_loader == 'D') {
926 strcpy(bootldr_str, "DD");
927 }
928
929 else if (bkpinfo->boot_loader == 'B') {
930 strcpy(bootldr_str, "BOOT0");
931 }
932#endif
933 else {
934 strcpy(bootldr_str, "unknown");
935 }
936 sprintf(tmp, "Your boot loader is %s and it boots from %s",
937 bootldr_str, bkpinfo->boot_device);
938 log_to_screen(tmp);
939 sprintf(tmp, "%s/BOOTLOADER.DEVICE", bkpinfo->tmpdir);
940 if (write_one_liner_data_file(tmp, bkpinfo->boot_device)) {
941 log_msg(1, "%ld: Unable to write one-liner boot device", __LINE__);
942 }
943 switch (bkpinfo->backup_media_type) {
944 case cdr:
945 strcpy(value, "cdr");
946 break;
947 case cdrw:
948 strcpy(value, "cdrw");
949 break;
950 case cdstream:
951 strcpy(value, "cdstream");
952 break;
953 case tape:
954 strcpy(value, "tape");
955 break;
956 case udev:
957 strcpy(value, "udev");
958 break;
959 case iso:
960 strcpy(value, "iso");
961 break;
962 case nfs:
963 strcpy(value, "nfs");
964 break;
965 case dvd:
966 strcpy(value, "dvd");
967 break;
968 case usb:
969 strcpy(value, "usb");
970 break;
971 default:
972 fatal_error("Unknown backup_media_type");
973 }
974 if (bkpinfo->backup_media_type == usb) {
975 asprintf(&tmp2, "--usb %s", bkpinfo->media_device);
976 } else {
977 asprintf(&tmp2,"");
978 }
979-
980
981 sprintf(tmp, "%s/BACKUP-MEDIA-TYPE", bkpinfo->tmpdir);
982 if (write_one_liner_data_file(tmp, value)) {
983 res++;
984 log_msg(1, "%ld: Unable to write one-liner backup-media-type",
985 __LINE__);
986 }
987 log_to_screen(bkpinfo->tmpdir);
988 sprintf(tmp, "%s/BOOTLOADER.NAME", bkpinfo->tmpdir);
989 if (write_one_liner_data_file(tmp, bootldr_str)) {
990 res++;
991 log_msg(1, "%ld: Unable to write one-liner bootloader.name",
992 __LINE__);
993 }
994 sprintf(tmp, "%s/DIFFERENTIAL", bkpinfo->tmpdir);
995 if (bkpinfo->differential) {
996 res += write_one_liner_data_file(tmp, "1");
997 } else {
998 res += write_one_liner_data_file(tmp, "0");
999 }
1000
1001 if (g_getfattr) {
1002 asprintf(&tmp1, "%s/XATTR", bkpinfo->tmpdir);
1003 if (write_one_liner_data_file(tmp1, "TRUE")) {
1004 log_msg(1, "%ld: Unable to write one-liner XATTR",
1005 __LINE__);
1006 }
1007 paranoid_free(tmp1);
1008 }
1009 if (g_getfacl) {
1010 asprintf(&tmp1, "%s/ACL", bkpinfo->tmpdir);
1011 if (write_one_liner_data_file(tmp1, "TRUE")) {
1012 log_msg(1, "%ld: Unable to write one-liner ACL",
1013 __LINE__);
1014 }
1015 paranoid_free(tmp1);
1016 }
1017
1018 estimated_total_noof_slices =
1019 size_of_all_biggiefiles_K(bkpinfo) / bkpinfo->optimal_set_size + 1;
1020/* add nfs stuff here? */
1021 sprintf(command, "mkdir -p %s/images", bkpinfo->scratchdir);
1022 if (system(command)) {
1023 res++;
1024 log_OS_error("Unable to make images directory");
1025 }
1026 sprintf(command, "mkdir -p %s%s", bkpinfo->scratchdir, MNT_FLOPPY);
1027 if (system(command)) {
1028 res++;
1029 log_OS_error("Unable to make mnt floppy directory");
1030 }
1031 sprintf(tmp, "BTW, I'm telling Mindi your kernel is '%s'",
1032 bkpinfo->kernel_path);
1033
1034 log_msg(1, "lines_in_filelist = %ld", lines_in_filelist);
1035
1036 sprintf(command,
1037/* "mindi --custom 2=%s 3=%s/images 4=\"%s\" 5=\"%s\" \
10386=\"%s\" 7=%ld 8=\"%s\" 9=\"%s\" 10=\"%s\" \
103911=\"%s\" 12=%s 13=%ld 14=\"%s\" 15=\"%s\" 16=\"%s\" 17=\"%s\" 18=%ld 19=%d",*/
1040 "mindi %s --custom %s %s/images '%s' '%s' \
1041'%s' %ld '%s' '%s' '%s' \
1042'%s' %s %ld '%s' '%s' '%s' '%s' %ld %d '%s'", tmp2, bkpinfo->tmpdir, // parameter #2
1043 bkpinfo->scratchdir, // parameter #3
1044 bkpinfo->kernel_path, // parameter #4
1045 tape_device, // parameter #5
1046 tape_size_sz, // parameter #6
1047 lines_in_filelist, // parameter #7 (INT)
1048 use_lzo_sz, // parameter #8
1049 cd_recovery_sz, // parameter #9
1050 bkpinfo->image_devs, // parameter #10
1051 broken_bios_sz, // parameter #11
1052 last_filelist_number, // parameter #12 (STRING)
1053 estimated_total_noof_slices, // parameter #13 (INT)
1054 devs_to_exclude, // parameter #14
1055 use_comp_sz, // parameter #15
1056 use_lilo_sz, // parameter #16
1057 use_star_sz, // parameter #17
1058 bkpinfo->internal_tape_block_size, // parameter #18 (LONG)
1059 bkpinfo->differential, // parameter #19 (INT)
1060 use_gzip_sz); // parameter #20 (STRING)
1061
1062// Watch it! This next line adds a parameter...
1063 if (bkpinfo->nonbootable_backup) {
1064 strcat(command, " NONBOOTABLE");
1065 }
1066 log_msg(2, command);
1067
1068// popup_and_OK("Pausing");
1069
1070 res =
1071 run_program_and_log_to_screen(command,
1072 "Generating boot+data disks");
1073 if (bkpinfo->nonbootable_backup) {
1074 res = 0;
1075 } // hack
1076 if (!res) {
1077 log_to_screen("Boot+data disks were created OK");
1078 sprintf(command, "cp -f %s/images/mindi.iso %s/mondorescue.iso",
1079 bkpinfo->scratchdir, MINDI_CACHE);
1080 log_msg(2, command);
1081 run_program_and_log_output(command, FALSE);
1082 if (bkpinfo->nonbootable_backup) {
1083 sprintf(command, "cp -f %s/all.tar.gz %s/images",
1084 bkpinfo->tmpdir, bkpinfo->scratchdir);
1085 if (system(command)) {
1086 fatal_error("Unable to create temporary duff tarball");
1087 }
1088 }
1089 sprintf(command, "cp -f %s/mindi-*oot*.img %s/images",
1090 bkpinfo->tmpdir, bkpinfo->scratchdir);
1091 sprintf(tmp, "cp -f %s/images/all.tar.gz %s", bkpinfo->scratchdir,
1092 bkpinfo->tmpdir);
1093 if (system(tmp)) {
1094 fatal_error("Cannot find all.tar.gz in tmpdir");
1095 }
1096 if (res) {
1097 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
1098 } else {
1099 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1100 }
1101 } else {
1102 log_to_screen("Mindi failed to create your boot+data disks.");
1103 strcpy(command, "grep 'Fatal error' /var/log/mindi.log");
1104 strcpy(tmp, call_program_and_get_last_line_of_output(command));
1105 if (strlen(tmp) > 1) {
1106 popup_and_OK(tmp);
1107 }
1108 }
1109 paranoid_free(tmp);
1110 paranoid_free(use_lzo_sz);
1111 paranoid_free(use_gzip_sz);
1112 paranoid_free(scratchdir);
1113 paranoid_free(use_comp_sz);
1114 paranoid_free(bootldr_str);
1115 paranoid_free(tape_device);
1116 paranoid_free(last_filelist_number);
1117 paranoid_free(broken_bios_sz);
1118 paranoid_free(cd_recovery_sz);
1119 paranoid_free(tape_size_sz);
1120 paranoid_free(devs_to_exclude);
1121 paranoid_free(use_lilo_sz);
1122 paranoid_free(value);
1123 paranoid_free(bootdev);
1124 paranoid_free(command);
1125 paranoid_free(use_star_sz);
1126 return (res);
1127}
1128
1129
1130
1131/**
1132 * Maximum number of filesets allowed in this function.
1133 */
1134#define MAX_NOOF_SETS_HERE 32767
1135
1136/**
1137 * Offset of the bkpinfo pointer (in bytes) from the
1138 * buffer passed to create_afio_files_in_background.
1139 */
1140#define BKPINFO_LOC_OFFSET (16+MAX_NOOF_SETS_HERE/8+16)
1141
1142/**
1143 * Main function for each @c afio thread.
1144 * @param inbuf A transfer block containing:
1145 * - @c p_last_set_archived: [offset 0] pointer to an @c int
1146 * containing the last set archived.
1147 * - @c p_archival_threads_running: [offset 4] pointer to an @c int
1148 * containing the number of archival threads currently running.
1149 * - @c p_next_set_to_archive: [offset 8] pointer to an @c int containing
1150 * the next set that should be archived.
1151 * - @c p_list_of_fileset_flags: [offset 12] @c char pointer pointing to a
1152 * bit array, where each bit corresponds to a filelist (1=needs
1153 * to be archived, 0=archived).
1154 * - @c bkpinfo: [offset BKPINFO_LOC_OFFSET] pointer to backup information
1155 * structure. Fields used:
1156 * - @c tmpdir
1157 * - @c zip_suffix
1158 *
1159 * Any of the above may be modified by the caller at any time.
1160 *
1161 * @bug Assumes @c int pointers are 4 bytes.
1162 * @see archive_this_fileset
1163 * @see make_afioballs_and_images
1164 * @return NULL, always.
1165 * @ingroup LLarchiveGroup
1166 */
1167void *create_afio_files_in_background(void *inbuf)
1168{
1169 long int archiving_set_no;
1170 char *archiving_filelist_fname;
1171 char *archiving_afioball_fname;
1172 char *curr_xattr_list_fname = NULL;
1173 char *curr_acl_list_fname;
1174
1175 struct s_bkpinfo *bkpinfo_bis;
1176 char *tmp;
1177 int res = 0, retval = 0;
1178 int *p_archival_threads_running;
1179 int *p_last_set_archived;
1180 int *p_next_set_to_archive;
1181 char *p_list_of_fileset_flags;
1182 int this_thread_no = g_current_thread_no++;
1183
1184 malloc_string(curr_xattr_list_fname);
1185 malloc_string(curr_acl_list_fname);
1186 malloc_string(archiving_filelist_fname);
1187 malloc_string(archiving_afioball_fname);
1188 malloc_string(tmp);
1189 p_last_set_archived = (int *) inbuf;
1190 p_archival_threads_running = (int *) (inbuf + 4);
1191 p_next_set_to_archive = (int *) (inbuf + 8);
1192 p_list_of_fileset_flags = (char *) (inbuf + 12);
1193 bkpinfo_bis = (struct s_bkpinfo *) (inbuf + BKPINFO_LOC_OFFSET);
1194
1195 sprintf(archiving_filelist_fname, FILELIST_FNAME_RAW_SZ,
1196 bkpinfo->tmpdir, 0L);
1197 for (archiving_set_no = 0; does_file_exist(archiving_filelist_fname);
1198 sprintf(archiving_filelist_fname, FILELIST_FNAME_RAW_SZ,
1199 bkpinfo->tmpdir, archiving_set_no)) {
1200 if (g_exiting) {
1201 fatal_error("Execution run aborted (pthread)");
1202 }
1203 if (archiving_set_no >= MAX_NOOF_SETS_HERE) {
1204 fatal_error
1205 ("Maximum number of filesets exceeded. Adjust MAX_NOOF_SETS_HERE, please.");
1206 }
1207 if (!semaphore_p()) {
1208 log_msg(3, "P sem failed (pid=%d)", (int) getpid());
1209 fatal_error("Cannot get semaphore P");
1210 }
1211 if (archiving_set_no < *p_next_set_to_archive) {
1212 archiving_set_no = *p_next_set_to_archive;
1213 }
1214 *p_next_set_to_archive = *p_next_set_to_archive + 1;
1215 if (!semaphore_v()) {
1216 fatal_error("Cannot get semaphore V");
1217 }
1218
1219 /* backup this set of files */
1220 sprintf(archiving_afioball_fname, AFIOBALL_FNAME_RAW_SZ,
1221 bkpinfo->tmpdir, archiving_set_no, bkpinfo->zip_suffix);
1222 sprintf(archiving_filelist_fname, FILELIST_FNAME_RAW_SZ,
1223 bkpinfo->tmpdir, archiving_set_no);
1224 if (!does_file_exist(archiving_filelist_fname)) {
1225 log_msg(3,
1226 "%s[%d:%d] - well, I would archive %d, except that it doesn't exist. I'll stop now.",
1227 FORTY_SPACES, getpid(), this_thread_no,
1228 archiving_set_no);
1229 break;
1230 }
1231
1232 sprintf(tmp, AFIOBALL_FNAME_RAW_SZ, bkpinfo->tmpdir,
1233 archiving_set_no - ARCH_BUFFER_NUM, bkpinfo->zip_suffix);
1234 if (does_file_exist(tmp)) {
1235 log_msg(4, "%s[%d:%d] - waiting for storer", FORTY_SPACES,
1236 getpid(), this_thread_no);
1237 while (does_file_exist(tmp)) {
1238 sleep(1);
1239 }
1240 log_msg(4, "[%d] - continuing", getpid());
1241 }
1242
1243 log_msg(4, "%s[%d:%d] - EXATing %d...", FORTY_SPACES, getpid(),
1244 this_thread_no, archiving_set_no);
1245 if (g_getfattr) {
1246 sprintf(curr_xattr_list_fname, XATTR_LIST_FNAME_RAW_SZ,
1247 bkpinfo->tmpdir, archiving_set_no);
1248 get_fattr_list(archiving_filelist_fname, curr_xattr_list_fname);
1249 }
1250 if (g_getfacl) {
1251 sprintf(curr_acl_list_fname, ACL_LIST_FNAME_RAW_SZ,
1252 bkpinfo->tmpdir, archiving_set_no);
1253 get_acl_list(archiving_filelist_fname, curr_acl_list_fname);
1254 }
1255
1256 log_msg(4, "%s[%d:%d] - archiving %d...", FORTY_SPACES, getpid(),
1257 this_thread_no, archiving_set_no);
1258 res =
1259 archive_this_fileset(archiving_filelist_fname,
1260 archiving_afioball_fname,
1261 archiving_set_no);
1262 retval += res;
1263
1264 if (res) {
1265 sprintf(tmp,
1266 "Errors occurred while archiving set %ld. Please review logs.",
1267 archiving_set_no);
1268 log_to_screen(tmp);
1269 }
1270 if (!semaphore_p()) {
1271 fatal_error("Cannot get semaphore P");
1272 }
1273
1274 set_bit_N_of_array(p_list_of_fileset_flags, archiving_set_no, 5);
1275
1276 if (*p_last_set_archived < archiving_set_no) {
1277 *p_last_set_archived = archiving_set_no;
1278 } // finished archiving this one
1279
1280 if (!semaphore_v()) {
1281 fatal_error("Cannot get semaphore V");
1282 }
1283 log_msg(4, "%s[%d:%d] - archived %d OK", FORTY_SPACES, getpid(),
1284 this_thread_no, archiving_set_no);
1285 archiving_set_no++;
1286 }
1287 if (!semaphore_p()) {
1288 fatal_error("Cannot get semaphore P");
1289 }
1290 (*p_archival_threads_running)--;
1291 if (!semaphore_v()) {
1292 fatal_error("Cannot get semaphore V");
1293 }
1294 log_msg(3, "%s[%d:%d] - exiting", FORTY_SPACES, getpid(),
1295 this_thread_no);
1296 paranoid_free(archiving_filelist_fname);
1297 paranoid_free(archiving_afioball_fname);
1298 paranoid_free(curr_xattr_list_fname);
1299 paranoid_free(curr_acl_list_fname);
1300 paranoid_free(tmp);
1301 pthread_exit(NULL);
1302}
1303
1304
1305
1306
1307
1308/**
1309 * Finalize the backup.
1310 * For streaming backups, this writes the closing block
1311 * to the stream. For CD-based backups, this creates
1312 * the final ISO image.
1313 * @param bkpinfo The backup information structure, used only
1314 * for the @c backup_media_type.
1315 * @ingroup MLarchiveGroup
1316 */
1317int do_that_final_phase()
1318{
1319
1320 /*@ int ************************************** */
1321 int res = 0;
1322 int retval = 0;
1323
1324 /*@ buffers ********************************** */
1325
1326 assert(bkpinfo != NULL);
1327 mvaddstr_and_log_it(g_currentY, 0,
1328 "Writing any remaining data to media ");
1329
1330 log_msg(1, "Closing tape/CD ... ");
1331 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type))
1332 /* write tape/cdstream */
1333 {
1334 closeout_tape();
1335 } else
1336 /* write final ISO/USB */
1337 {
1338 res = write_final_iso_if_necessary();
1339 retval += res;
1340 if (res) {
1341 log_msg(1, "write_final_iso_if_necessary returned an error");
1342 }
1343 }
1344 log_msg(2, "Fork is exiting ... ");
1345
1346 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1347
1348 /* final stuff */
1349 if (retval) {
1350 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
1351 } else {
1352 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1353 }
1354
1355 return (retval);
1356}
1357
1358
1359/**
1360 * Initialize the backup.
1361 * Does the following:
1362 * - Sets up the serial number.
1363 * - For streaming backups, opens the tape stream and writes the data disks
1364 * and backup headers.
1365 * - For CD-based backups, wipes the ISOs in the target directory.
1366 *
1367 * @param bkpinfo The backup information structure. Fields used:
1368 * - @c backup_media_type
1369 * - @c cdrw_speed
1370 * - @c prefix
1371 * - @c isodir
1372 * - @c media_device
1373 * - @c scratchdir
1374 * - @c tmpdir
1375 * @return The number of errors encountered (0 for success).
1376 * @ingroup MLarchiveGroup
1377 */
1378int do_that_initial_phase()
1379{
1380 /*@ int *************************************** */
1381 int retval = 0;
1382
1383 /*@ buffers *********************************** */
1384 char *command, *tmpfile, *data_disks_file;
1385
1386 assert(bkpinfo != NULL);
1387 malloc_string(command);
1388 malloc_string(tmpfile);
1389 malloc_string(data_disks_file);
1390 sprintf(data_disks_file, "%s/all.tar.gz", bkpinfo->tmpdir);
1391
1392 snprintf(g_serial_string, MAX_STR_LEN - 1,
1393 call_program_and_get_last_line_of_output("dd \
1394if=/dev/urandom bs=16 count=1 2> /dev/null | \
1395hexdump | tr -s ' ' '0' | head -n1"));
1396 strip_spaces(g_serial_string);
1397 strcat(g_serial_string, "...word.");
1398 log_msg(2, "g_serial_string = '%s'", g_serial_string);
1399 assert(strlen(g_serial_string) < MAX_STR_LEN);
1400
1401 sprintf(tmpfile, "%s/archives/SERIAL-STRING", bkpinfo->scratchdir);
1402 if (write_one_liner_data_file(tmpfile, g_serial_string)) {
1403 log_msg(1, "%ld: Failed to write serial string", __LINE__);
1404 }
1405
1406 mvaddstr_and_log_it(g_currentY, 0, "Preparing to archive your data");
1407 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1408 if (bkpinfo->backup_media_type == cdstream) {
1409 openout_cdstream(bkpinfo->media_device, bkpinfo->cdrw_speed);
1410 } else {
1411 openout_tape(bkpinfo->media_device, bkpinfo->internal_tape_block_size); /* sets g_tape_stream */
1412 }
1413 if (!g_tape_stream) {
1414 fatal_error("Cannot open backup (streaming) device");
1415 }
1416 log_msg(1, "Backup (stream) opened OK");
1417 write_data_disks_to_stream(data_disks_file);
1418 } else {
1419 log_msg(1, "Backing up to CD's");
1420 }
1421
1422 sprintf(command, "rm -f %s/%s/%s-[1-9]*.iso", bkpinfo->isodir,
1423 bkpinfo->nfs_remote_dir, bkpinfo->prefix);
1424 paranoid_system(command);
1425 wipe_archives(bkpinfo->scratchdir);
1426 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1427 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1428 write_header_block_to_stream((off_t)0, "start-of-tape",
1429 BLK_START_OF_TAPE);
1430 write_header_block_to_stream((off_t)0, "start-of-backup",
1431 BLK_START_OF_BACKUP);
1432 }
1433 paranoid_free(command);
1434 paranoid_free(tmpfile);
1435 paranoid_free(data_disks_file);
1436 return (retval);
1437}
1438
1439
1440/**
1441 * Calls floppy-formatting @c cmd and tracks its progress if possible.
1442 *
1443 * @param cmd The command to run (e.g. @c fdformat @c /dev/fd0).
1444 * @param title The human-friendly description of the floppy you are writing.
1445 * This will be used as the title in the progress bar window. Example:
1446 * "Formatting disk /dev/fd0".
1447 * @see format_disk
1448 * @return The exit code of fdformat/superformat.
1449 */
1450int format_disk_SUB(char *cmd, char *title)
1451{
1452
1453 /*@ int *************************************************************** */
1454 int res = 0;
1455 int percentage = 0;
1456 int maxtracks = 0;
1457 int trackno = 0;
1458 int last_trkno = 0;
1459
1460 /*@ buffers *********************************************************** */
1461 char *command;
1462 char *tempfile;
1463
1464 /*@ pointers ********************************************************** */
1465 FILE *pin;
1466
1467 assert_string_is_neither_NULL_nor_zerolength(cmd);
1468 assert_string_is_neither_NULL_nor_zerolength(title);
1469
1470 malloc_string(command);
1471 malloc_string(tempfile);
1472#ifdef __FreeBSD__
1473/* Ugh. FreeBSD fdformat prints out this pretty progress indicator that's
1474 impossible to parse. It looks like
1475 VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVF-------------------
1476 where V means verified, E means error, F means formatted, and - means
1477 not done yet.
1478*/
1479 return (run_program_and_log_to_screen(cmd, title));
1480#endif
1481
1482/* if Debian then do bog-standard superformat; don't be pretty */
1483 if (strstr(cmd, "superformat")) {
1484 return (run_program_and_log_to_screen(cmd, title));
1485 }
1486/* if not Debian then go ahead & use fdformat */
1487 sprintf(tempfile, "%s/mondo.floppy.log", bkpinfo->tmpdir);
1488 sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
1489 tempfile);
1490 log_msg(3, command);
1491 open_evalcall_form(title);
1492 if (!(pin = popen(command, "r"))) {
1493 log_OS_error("fmt err");
1494 return (1);
1495 }
1496 if (strstr(command, "1722")) {
1497 maxtracks = 82;
1498 } else {
1499 maxtracks = 80;
1500 }
1501 for (sleep(1); does_file_exist(tempfile); sleep(1)) {
1502 trackno = get_trackno_from_logfile(tempfile);
1503 if (trackno < 0 || trackno > 80) {
1504 log_msg(1, "Weird track#");
1505 continue;
1506 }
1507 percentage = trackno * 100 / maxtracks;
1508 if (trackno <= 5 && last_trkno > 40) {
1509 close_evalcall_form();
1510 strcpy(title, "Verifying format");
1511 open_evalcall_form(title);
1512 }
1513 last_trkno = trackno;
1514 update_evalcall_form(percentage);
1515 }
1516 close_evalcall_form();
1517 if (pclose(pin)) {
1518 res++;
1519 log_OS_error("Unable to pclose");
1520 }
1521 unlink(tempfile);
1522 paranoid_free(command);
1523 paranoid_free(tempfile);
1524 return (res);
1525}
1526
1527/**
1528 * Wrapper around @c format_disk_SUB().
1529 * This function calls @c format_disk_SUB() with a @c device of its @c device
1530 * parameter and a @c title of Formatting disk @c device. If the format
1531 * fails, the user will be given the option of retrying.
1532 *
1533 * @param device The floppy drive to write to.
1534 * @see format_disk_SUB
1535 * @return The exit code of fdformat/superformat.
1536 * @ingroup deviceGroup
1537 */
1538int format_disk(char *device)
1539{
1540
1541 /*@ int ************************************************************** */
1542 int res = 0;
1543
1544 /*@ buffer *********************************************************** */
1545 char *command;
1546 char *title;
1547
1548
1549 assert_string_is_neither_NULL_nor_zerolength(device);
1550 malloc_string(title);
1551 command = malloc(1000);
1552 if (!system("which superformat > /dev/null 2> /dev/null")) {
1553 sprintf(command, "superformat %s", device);
1554 } else {
1555#ifdef __FreeBSD__
1556 sprintf(command, "fdformat -y %s", device);
1557#else
1558 sprintf(command, "fdformat %s", device);
1559#endif
1560 }
1561 sprintf(title, "Formatting disk %s", device);
1562 while ((res = format_disk_SUB(command, title))) {
1563 if (!ask_me_yes_or_no("Failed to format disk. Retry?")) {
1564 return (res);
1565 }
1566 }
1567 paranoid_free(title);
1568 paranoid_free(command);
1569 return (res);
1570}
1571
1572/**
1573 * Get the <tt>N</tt>th bit of @c array.
1574 * @param array The bit-array (as a @c char pointer).
1575 * @param N The number of the bit you want.
1576 * @return TRUE (bit is set) or FALSE (bit is not set).
1577 * @see set_bit_N_of_array
1578 * @ingroup utilityGroup
1579 */
1580bool get_bit_N_of_array(char *array, int N)
1581{
1582 int element_number;
1583 int bit_number;
1584 int mask;
1585
1586 element_number = N / 8;
1587 bit_number = N % 8;
1588 mask = 1 << bit_number;
1589 if (array[element_number] & mask) {
1590 return (TRUE);
1591 } else {
1592 return (FALSE);
1593 }
1594}
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604/**
1605 * @addtogroup LLarchiveGroup
1606 * @{
1607 */
1608/**
1609 * Start up threads to archive your files.
1610 *
1611 * This function starts @c ARCH_THREADS threads,
1612 * each starting execution in @c create_afio_files_in_background().
1613 * Each thread will archive individual filesets, based on the
1614 * pointers passed to it and continually updated, until all files
1615 * have been backed up. This function coordinates the threads
1616 * and copies their output to the @c scratchdir.
1617 *
1618 * @param bkpinfo The backup information structure. Fields used:
1619 * - @c backup_media_type
1620 * - @c scratchdir
1621 * - @c tmpdir
1622 * - @c zip_suffix
1623 *
1624 * @return The number of errors encountered (0 for success)
1625 */
1626int make_afioballs_and_images()
1627{
1628
1629 /*@ int ************************************************** */
1630 int retval = 0;
1631 long int storing_set_no = 0;
1632 int res = 0;
1633 bool done_storing = FALSE;
1634 char *result_str;
1635 char *transfer_block;
1636 void *vp;
1637 void **pvp;
1638
1639 /*@ buffers ********************************************** */
1640 char *storing_filelist_fname;
1641 char *storing_afioball_fname;
1642 char *tmp;
1643 char *media_usage_comment;
1644 pthread_t archival_thread[ARCH_THREADS];
1645 char *p_list_of_fileset_flags;
1646 int *p_archival_threads_running;
1647 int *p_last_set_archived;
1648 int *p_next_set_to_archive;
1649 int noof_threads;
1650 int i;
1651 char *curr_xattr_list_fname = NULL;
1652 char *curr_acl_list_fname;
1653 int misc_counter_that_is_not_important = 0;
1654
1655 log_msg(8, "here");
1656 assert(bkpinfo != NULL);
1657 tmp = malloc(MAX_STR_LEN * 2);
1658 malloc_string(result_str);
1659 malloc_string(curr_xattr_list_fname);
1660 malloc_string(curr_acl_list_fname);
1661 malloc_string(storing_filelist_fname);
1662 malloc_string(media_usage_comment);
1663 malloc_string(storing_afioball_fname);
1664 transfer_block =
1665 malloc(sizeof(struct s_bkpinfo) + BKPINFO_LOC_OFFSET + 64);
1666 memset((void *) transfer_block, 0,
1667 sizeof(struct s_bkpinfo) + BKPINFO_LOC_OFFSET + 64);
1668 p_last_set_archived = (int *) transfer_block;
1669 p_archival_threads_running = (int *) (transfer_block + 4);
1670 p_next_set_to_archive = (int *) (transfer_block + 8);
1671 p_list_of_fileset_flags = (char *) (transfer_block + 12);
1672 memcpy((void *) (transfer_block + BKPINFO_LOC_OFFSET),
1673 (void *) bkpinfo, sizeof(struct s_bkpinfo));
1674 pvp = &vp;
1675 vp = (void *) result_str;
1676 *p_archival_threads_running = 0;
1677 *p_last_set_archived = -1;
1678 *p_next_set_to_archive = 0;
1679 sprintf(tmp, "%s/archives/filelist.full", bkpinfo->scratchdir);
1680 log_to_screen("Archiving regular files");
1681 log_msg(5, "Go, Shorty. It's your birthday.");
1682 open_progress_form("Backing up filesystem",
1683 "I am backing up your live filesystem now.",
1684 "Please wait. This may take a couple of hours.",
1685 "Working...",
1686 get_last_filelist_number() + 1);
1687
1688 log_msg(5, "We're gonna party like it's your birthday.");
1689
1690 srand((unsigned int) getpid());
1691 g_sem_key = 1234 + random() % 30000;
1692 if ((g_sem_id =
1693 semget((key_t) g_sem_key, 1,
1694 IPC_CREAT | S_IREAD | S_IWRITE)) == -1) {
1695 fatal_error("MABAI - unable to semget");
1696 }
1697 if (!set_semvalue()) {
1698 fatal_error("Unable to init semaphore");
1699 } // initialize semaphore
1700 for (noof_threads = 0; noof_threads < ARCH_THREADS; noof_threads++) {
1701 log_msg(8, "Creating thread #%d", noof_threads);
1702 (*p_archival_threads_running)++;
1703 if ((res =
1704 pthread_create(&archival_thread[noof_threads], NULL,
1705 create_afio_files_in_background,
1706 (void *) transfer_block))) {
1707 fatal_error("Unable to create an archival thread");
1708 }
1709 }
1710
1711 log_msg(8, "About to enter while() loop");
1712 while (!done_storing) {
1713 if (g_exiting) {
1714 fatal_error("Execution run aborted (main loop)");
1715 }
1716 if (*p_archival_threads_running == 0
1717 && *p_last_set_archived == storing_set_no - 1) {
1718 log_msg(2,
1719 "No archival threads are running. The last stored set was %d and I'm looking for %d. Take off your make-up; the party's over... :-)",
1720 *p_last_set_archived, storing_set_no);
1721 done_storing = TRUE;
1722 } else
1723 if (!get_bit_N_of_array
1724 (p_list_of_fileset_flags, storing_set_no)) {
1725 misc_counter_that_is_not_important =
1726 (misc_counter_that_is_not_important + 1) % 5;
1727 if (!misc_counter_that_is_not_important) {
1728 update_progress_form(media_usage_comment);
1729 }
1730 sleep(1);
1731 } else
1732 // store set N
1733 {
1734 sprintf(storing_filelist_fname, FILELIST_FNAME_RAW_SZ,
1735 bkpinfo->tmpdir, storing_set_no);
1736 sprintf(storing_afioball_fname, AFIOBALL_FNAME_RAW_SZ,
1737 bkpinfo->tmpdir, storing_set_no, bkpinfo->zip_suffix);
1738 if (g_getfattr) {
1739 sprintf(curr_xattr_list_fname, XATTR_LIST_FNAME_RAW_SZ,
1740 bkpinfo->tmpdir, storing_set_no);
1741 }
1742 if (g_getfacl) {
1743 sprintf(curr_acl_list_fname, ACL_LIST_FNAME_RAW_SZ,
1744 bkpinfo->tmpdir, storing_set_no);
1745 }
1746
1747 log_msg(2, "Storing set %d", storing_set_no);
1748 while (!does_file_exist(storing_filelist_fname)
1749 || !does_file_exist(storing_afioball_fname)) {
1750 log_msg(2,
1751 "Warning - either %s or %s doesn't exist yet. I'll pause 5 secs.",
1752 storing_filelist_fname, storing_afioball_fname);
1753 sleep(5);
1754 }
1755 strcpy(media_usage_comment, percent_media_full_comment());
1756 /* copy to CD (scratchdir) ... and an actual CD-R if necessary */
1757 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1758 register_in_tape_catalog(fileset, storing_set_no, -1,
1759 storing_afioball_fname);
1760 maintain_collection_of_recent_archives(bkpinfo->tmpdir,
1761 storing_afioball_fname);
1762 iamhere("Writing EXAT files");
1763 res +=
1764 write_EXAT_files_to_tape(curr_xattr_list_fname,
1765 curr_acl_list_fname);
1766// archives themselves
1767 res +=
1768 move_files_to_stream(storing_afioball_fname,
1769 NULL);
1770 } else {
1771 if (g_getfacl) {
1772 if (g_getfattr) {
1773 res = move_files_to_cd(storing_filelist_fname,
1774 curr_xattr_list_fname,
1775 curr_acl_list_fname,
1776 storing_afioball_fname, NULL);
1777 } else {
1778 res = move_files_to_cd(storing_filelist_fname,
1779 curr_acl_list_fname,
1780 storing_afioball_fname, NULL);
1781 }
1782 } else {
1783 if (g_getfattr) {
1784 res = move_files_to_cd(storing_filelist_fname,
1785 curr_xattr_list_fname,
1786 storing_afioball_fname, NULL);
1787 } else {
1788 res = move_files_to_cd(storing_filelist_fname,
1789 storing_afioball_fname, NULL);
1790 }
1791 }
1792 }
1793 retval += res;
1794 g_current_progress++;
1795 update_progress_form(media_usage_comment);
1796 if (res) {
1797 sprintf(tmp,
1798 "Failed to add archive %ld's files to CD dir\n",
1799 storing_set_no);
1800 log_to_screen(tmp);
1801 fatal_error
1802 ("Is your hard disk full? If not, please send the author the logfile.");
1803 }
1804 storing_set_no++;
1805 // sleep(2);
1806 }
1807 }
1808 close_progress_form();
1809
1810 sprintf(tmp, "Your regular files have been archived ");
1811 log_msg(2, "Joining background threads to foreground thread");
1812 for (i = 0; i < noof_threads; i++) {
1813 pthread_join(archival_thread[i], pvp);
1814 log_msg(3, "Thread %d of %d: closed OK", i + 1, noof_threads);
1815 }
1816 del_semvalue();
1817 log_msg(2, "Done.");
1818 if (retval) {
1819 strcat(tmp, "(with errors).");
1820 } else {
1821 strcat(tmp, "successfully.");
1822 }
1823 log_to_screen(tmp);
1824 paranoid_free(transfer_block);
1825 paranoid_free(result_str);
1826 paranoid_free(storing_filelist_fname);
1827 paranoid_free(media_usage_comment);
1828 paranoid_free(storing_afioball_fname);
1829 paranoid_free(curr_xattr_list_fname);
1830 paranoid_free(curr_acl_list_fname);
1831 return (retval);
1832}
1833
1834
1835void pause_for_N_seconds(int how_long, char *msg)
1836{
1837 int i;
1838 open_evalcall_form(msg);
1839 for (i = 0; i < how_long; i++) {
1840 update_evalcall_form((int) ((100.0 / (float) (how_long) * i)));
1841 sleep(1);
1842 }
1843 close_evalcall_form();
1844}
1845
1846
1847
1848
1849/**
1850 * Create an ISO image in @c destfile, from files in @c bkpinfo->scratchdir.
1851 *
1852 * @param bkpinfo The backup information structure. Fields used:
1853 * - @c backup_media_type
1854 * - @c call_after_iso
1855 * - @c call_before_iso
1856 * - @c call_burn_iso
1857 * - @c call_make_iso
1858 * - @c make_cd_use_lilo
1859 * - @c manual_cd_tray
1860 * - @c nonbootable_backup
1861 * - @c scratchdir
1862 *
1863 * @param destfile Where to put the generated ISO image.
1864 * @return The number of errors encountered (0 for success)
1865 */
1866int make_iso_fs(char *destfile)
1867{
1868 /*@ int ********************************************** */
1869 int retval = 0;
1870 int res;
1871
1872 /*@ buffers ****************************************** */
1873 char *tmp;
1874 char *old_pwd;
1875 char *result_sz;
1876 char *message_to_screen;
1877 char *sz_blank_disk;
1878 char *fnam;
1879 char *tmp2;
1880 char *tmp3;
1881 bool cd_is_mountable;
1882
1883 malloc_string(old_pwd);
1884 malloc_string(result_sz);
1885 malloc_string(message_to_screen);
1886 malloc_string(sz_blank_disk);
1887 malloc_string(fnam);
1888 tmp = malloc(1200);
1889 tmp2 = malloc(1200);
1890 tmp3 = malloc(1200);
1891 assert(bkpinfo != NULL);
1892 assert_string_is_neither_NULL_nor_zerolength(destfile);
1893
1894 sprintf(tmp, "%s/isolinux.bin", bkpinfo->scratchdir);
1895 sprintf(tmp2, "%s/isolinux.bin", bkpinfo->tmpdir);
1896 if (does_file_exist(tmp)) {
1897 sprintf(tmp3, "cp -f %s %s", tmp, tmp2);
1898 paranoid_system(tmp3);
1899 }
1900 if (!does_file_exist(tmp) && does_file_exist(tmp2)) {
1901 sprintf(tmp3, "cp -f %s %s", tmp2, tmp);
1902 paranoid_system(tmp3);
1903 }
1904 /*
1905 if (!does_file_exist(tmp))
1906 {
1907 log_msg (2, "Silly bug in Mindi.pl; workaround in progress...");
1908 strcpy(fnam, call_program_and_get_last_line_of_output("locate isolinux.bin | tail -n1"));
1909 if (strlen(fnam)>0 && does_file_exist(fnam))
1910 {
1911 sprintf(tmp, "cp -f %s %s", fnam, bkpinfo->scratchdir);
1912 res = run_program_and_log_output(tmp, FALSE);
1913 }
1914 else
1915 {
1916 res = 1;
1917 }
1918 if (res)
1919 {
1920 log_msg (2, "Could not work around silly bug in Mindi.pl - sorry! Isolinux.bin missing");
1921 }
1922 }
1923 */
1924 free(tmp2);
1925 free(tmp3);
1926 tmp2 = NULL;
1927 tmp3 = NULL;
1928 if (bkpinfo->backup_media_type == iso && bkpinfo->manual_cd_tray) {
1929 popup_and_OK("Please insert new media and press Enter.");
1930 }
1931
1932 log_msg(2, "make_iso_fs --- scratchdir=%s --- destfile=%s",
1933 bkpinfo->scratchdir, destfile);
1934 (void) getcwd(old_pwd, MAX_STR_LEN - 1);
1935 sprintf(tmp, "chmod 744 %s", bkpinfo->scratchdir);
1936 run_program_and_log_output(tmp, FALSE);
1937 chdir(bkpinfo->scratchdir);
1938
1939 if (bkpinfo->call_before_iso[0] != '\0') {
1940 sprintf(message_to_screen, "Running pre-ISO call for CD#%d",
1941 g_current_media_number);
1942 res =
1943 eval_call_to_make_ISO(bkpinfo->call_before_iso,
1944 destfile, g_current_media_number,
1945 MONDO_LOGFILE, message_to_screen);
1946 if (res) {
1947 strcat(message_to_screen, "...failed");
1948 } else {
1949 strcat(message_to_screen, "...OK");
1950 }
1951 log_to_screen(message_to_screen);
1952 retval += res;
1953 }
1954
1955 if (bkpinfo->call_make_iso[0] != '\0') {
1956 log_msg(2, "bkpinfo->call_make_iso = %s", bkpinfo->call_make_iso);
1957 sprintf(tmp, "%s/archives/NOT-THE-LAST", bkpinfo->scratchdir);
1958 sprintf(message_to_screen, "Making an ISO (%s #%d)",
1959 media_descriptor_string(bkpinfo->backup_media_type),
1960 g_current_media_number);
1961
1962 pause_and_ask_for_cdr(2, &cd_is_mountable); /* if g_current_media_number >= 2 then pause & ask */
1963 if (retval) {
1964 log_to_screen
1965 ("Serious error(s) occurred already. I shan't try to write to media.");
1966 } else {
1967 res =
1968 eval_call_to_make_ISO(bkpinfo->call_make_iso,
1969 bkpinfo->scratchdir,
1970 g_current_media_number,
1971 MONDO_LOGFILE, message_to_screen);
1972 if (res) {
1973 log_to_screen("%s...failed to write", message_to_screen);
1974 } else {
1975 log_to_screen("%s...OK", message_to_screen);
1976 sprintf(tmp, "tail -n10 %s | grep -F ':-('", MONDO_LOGFILE);
1977 if (!run_program_and_log_output(tmp, 1)) {
1978 log_to_screen
1979 ("Despite nonfatal errors, growisofs confirms the write was successful.");
1980 }
1981 }
1982 retval += res;
1983#ifdef DVDRWFORMAT
1984 sprintf(tmp,
1985 "tail -n8 %s | grep 'blank=full.*dvd-compat.*DAO'",
1986 MONDO_LOGFILE);
1987 if (g_backup_media_type == dvd
1988 && (res || !run_program_and_log_output(tmp, 1))) {
1989 log_to_screen
1990 ("Failed to write to disk. I shall blank it and then try again.");
1991 sleep(5);
1992 system("sync");
1993 pause_for_N_seconds(5, "Letting DVD drive settle");
1994
1995// dvd+rw-format --- OPTION 2
1996 if (!bkpinfo->please_dont_eject) {
1997 log_to_screen("Ejecting media to clear drive status.");
1998 eject_device(bkpinfo->media_device);
1999 inject_device(bkpinfo->media_device);
2000 }
2001 pause_for_N_seconds(5, "Letting DVD drive settle");
2002 sprintf(sz_blank_disk, "dvd+rw-format -force %s",
2003 bkpinfo->media_device);
2004 log_msg(3, "sz_blank_disk = '%s'", sz_blank_disk);
2005 res =
2006 run_external_binary_with_percentage_indicator_NEW
2007 ("Blanking DVD disk", sz_blank_disk);
2008 if (res) {
2009 log_to_screen
2010 ("Warning - format failed. (Was it a DVD-R?) Sleeping for 5 seconds to take a breath...");
2011 pause_for_N_seconds(5,
2012 "Letting DVD drive settle... and trying again.");
2013 res =
2014 run_external_binary_with_percentage_indicator_NEW
2015 ("Blanking DVD disk", sz_blank_disk);
2016 if (res) {
2017 log_to_screen("Format failed a second time.");
2018 }
2019 } else {
2020 log_to_screen
2021 ("Format succeeded. Sleeping for 5 seconds to take a breath...");
2022 }
2023 pause_for_N_seconds(5, "Letting DVD drive settle");
2024 if (!bkpinfo->please_dont_eject) {
2025 log_to_screen("Ejecting media to clear drive status.");
2026 eject_device(bkpinfo->media_device);
2027 inject_device(bkpinfo->media_device);
2028 }
2029 pause_for_N_seconds(5, "Letting DVD drive settle");
2030 res =
2031 eval_call_to_make_ISO(bkpinfo->call_make_iso,
2032 bkpinfo->scratchdir,
2033 g_current_media_number,
2034 MONDO_LOGFILE,
2035 message_to_screen);
2036 retval += res;
2037 if (!bkpinfo->please_dont_eject) {
2038 log_to_screen("Ejecting media.");
2039 eject_device(bkpinfo->media_device);
2040 }
2041 if (res) {
2042 log_to_screen("Dagnabbit. It still failed.");
2043 } else {
2044 log_to_screen
2045 ("OK, this time I successfully backed up to DVD.");
2046 }
2047 }
2048#endif
2049 if (g_backup_media_type == dvd && !bkpinfo->please_dont_eject) {
2050 eject_device(bkpinfo->media_device);
2051 }
2052 }
2053 } else {
2054 sprintf(message_to_screen, "Running mkisofs to make %s #%d",
2055 media_descriptor_string(bkpinfo->backup_media_type),
2056 g_current_media_number);
2057 log_msg(1, message_to_screen);
2058 sprintf(result_sz, "Call to mkisofs to make ISO (%s #%d) ",
2059 media_descriptor_string(bkpinfo->backup_media_type),
2060 g_current_media_number);
2061 if (bkpinfo->nonbootable_backup) {
2062 log_msg(1, "Making nonbootable backup");
2063// FIXME --- change mkisofs string to MONDO_MKISOFS_NONBOOTABLE and add ' .' at end
2064 res =
2065 eval_call_to_make_ISO("mkisofs -o '_ISO_' -r -p MondoRescue -publisher www.mondorescue.org -A MondoRescue_GPL -V _CD#_ .",
2066 destfile, g_current_media_number,
2067 MONDO_LOGFILE, message_to_screen);
2068 } else {
2069 log_msg(1, "Making bootable backup");
2070
2071#ifdef __FreeBSD__
2072 bkpinfo->make_cd_use_lilo = TRUE;
2073#endif
2074
2075
2076 log_msg(1, "make_cd_use_lilo is actually %d",
2077 bkpinfo->make_cd_use_lilo);
2078 if (bkpinfo->make_cd_use_lilo) {
2079 log_msg(1, "make_cd_use_lilo = TRUE");
2080// FIXME --- change mkisofs string to MONDO_MKISOFS_REGULAR_SYSLINUX/LILO depending on bkpinfo->make_cd_usE_lilo
2081// and add ' .' at end
2082#ifdef __IA64__
2083 log_msg(1, "IA64 --> elilo");
2084 res = eval_call_to_make_ISO("mkisofs -no-emul-boot -b images/mindi-bootroot."
2085 IA64_BOOT_SIZE
2086 ".img -c boot.cat -o '_ISO_' -J -r -p MondoRescue -publisher www.mondorescue.org -A Mondo_Rescue_GPL -V _CD#_ .",
2087 destfile,
2088 g_current_media_number,
2089 MONDO_LOGFILE,
2090 message_to_screen);
2091#else
2092// FIXME --- change mkisofs string to MONDO_MKISOFS_REGULAR_SYSLINUX/LILO depending on bkpinfo->make_cd_usE_lilo
2093// and add ' .' at end
2094 log_msg(1, "Non-ia64 --> lilo");
2095 res =
2096 eval_call_to_make_ISO("mkisofs -b images/mindi-bootroot.2880.img -c boot.cat -o '_ISO_' -J -r -p MondoRescue -publisher www.mondorescue.org -A Mondo_Rescue_GPL -V _CD#_ .",
2097 destfile, g_current_media_number,
2098 MONDO_LOGFILE,
2099 message_to_screen);
2100#endif
2101 } else {
2102 log_msg(1, "make_cd_use_lilo = FALSE");
2103 log_msg(1, "Isolinux");
2104 res =
2105 eval_call_to_make_ISO("mkisofs -no-emul-boot -b isolinux.bin -boot-load-size 4 -boot-info-table -c boot.cat -o '_ISO_' -J -r -p MondoRescue -publisher www.mondorescue.org -A Mondo_Rescue_GPL -V _CD#_ .",
2106 destfile, g_current_media_number,
2107 MONDO_LOGFILE,
2108 message_to_screen);
2109 }
2110 }
2111 if (res) {
2112 strcat(result_sz, "...failed");
2113 } else {
2114 strcat(result_sz, "...OK");
2115 }
2116 log_to_screen(result_sz);
2117 retval += res;
2118 }
2119
2120 if (bkpinfo->backup_media_type == cdr
2121 || bkpinfo->backup_media_type == cdrw) {
2122 if (is_this_device_mounted(bkpinfo->media_device)) {
2123 log_msg(2,
2124 "Warning - %s mounted. I'm unmounting it before I burn to it.",
2125 bkpinfo->media_device);
2126 sprintf(tmp, "umount %s", bkpinfo->media_device);
2127 run_program_and_log_output(tmp, FALSE);
2128 }
2129 }
2130
2131 if (bkpinfo->call_burn_iso[0] != '\0') {
2132 log_msg(2, "bkpinfo->call_burn_iso = %s", bkpinfo->call_burn_iso);
2133 sprintf(message_to_screen, "Burning %s #%d",
2134 media_descriptor_string(bkpinfo->backup_media_type),
2135 g_current_media_number);
2136 pause_and_ask_for_cdr(2, &cd_is_mountable);
2137 res =
2138 eval_call_to_make_ISO(bkpinfo->call_burn_iso,
2139 destfile, g_current_media_number,
2140 MONDO_LOGFILE, message_to_screen);
2141 if (res) {
2142 strcat(message_to_screen, "...failed");
2143 } else {
2144 strcat(message_to_screen, "...OK");
2145 }
2146 log_to_screen(message_to_screen);
2147 retval += res;
2148 }
2149
2150 if (bkpinfo->call_after_iso[0] != '\0') {
2151 sprintf(message_to_screen, "Running post-ISO call (%s #%d)",
2152 media_descriptor_string(bkpinfo->backup_media_type),
2153 g_current_media_number);
2154 res =
2155 eval_call_to_make_ISO(bkpinfo->call_after_iso,
2156 destfile, g_current_media_number,
2157 MONDO_LOGFILE, message_to_screen);
2158 if (res) {
2159 strcat(message_to_screen, "...failed");
2160 } else {
2161 strcat(message_to_screen, "...OK");
2162 }
2163 log_to_screen(message_to_screen);
2164 retval += res;
2165 }
2166
2167 chdir(old_pwd);
2168 if (retval) {
2169 log_msg(1, "WARNING - make_iso_fs returned an error");
2170 }
2171 paranoid_free(old_pwd);
2172 paranoid_free(result_sz);
2173 paranoid_free(message_to_screen);
2174 paranoid_free(sz_blank_disk);
2175 paranoid_free(fnam);
2176 paranoid_free(tmp);
2177 return (retval);
2178}
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188bool is_dev_an_NTFS_dev(char *bigfile_fname)
2189{
2190 char *tmp;
2191 char *command;
2192 malloc_string(tmp);
2193 malloc_string(command);
2194 sprintf(command,
2195 "dd if=%s bs=512 count=1 2> /dev/null | strings | head -n1",
2196 bigfile_fname);
2197 log_msg(1, "command = '%s'", command);
2198 strcpy(tmp, call_program_and_get_last_line_of_output(command));
2199 log_msg(1, "--> tmp = '%s'", tmp);
2200 if (strstr(tmp, "NTFS")) {
2201 iamhere("TRUE");
2202 return (TRUE);
2203 } else {
2204 iamhere("FALSE");
2205 return (FALSE);
2206 }
2207}
2208
2209
2210/**
2211 * Back up big files by chopping them up.
2212 * This function backs up all "big" files (where "big" depends
2213 * on your backup media) in "chunks" (whose size again depends
2214 * on your media).
2215 *
2216 * @param bkpinfo The backup information structure. Fields used:
2217 * - @c backup_media_type
2218 * - @c optimal_set_size
2219 * @param biggielist_fname The path to a file containing a list of
2220 * all "big" files.
2221 * @return The number of errors encountered (0 for success)
2222 * @see slice_up_file_etc
2223 */
2224int
2225make_slices_and_images(char *biggielist_fname)
2226{
2227
2228 /*@ pointers ******************************************* */
2229 FILE *fin;
2230 char *p;
2231
2232 /*@ buffers ******************************************** */
2233 char *tmp;
2234 char *bigfile_fname;
2235 char *sz_devfile;
2236 char *ntfsprog_fifo = NULL;
2237 /*@ long *********************************************** */
2238 long biggie_file_number = 0;
2239 long noof_biggie_files = 0;
2240 long estimated_total_noof_slices = 0;
2241
2242 /*@ int ************************************************ */
2243 int retval = 0;
2244 int res = 0;
2245 pid_t pid;
2246 FILE *ftmp = NULL;
2247 bool delete_when_done;
2248 bool use_ntfsprog;
2249 off_t biggie_fsize;
2250
2251 assert(bkpinfo != NULL);
2252 assert_string_is_neither_NULL_nor_zerolength(biggielist_fname);
2253
2254 malloc_string(tmp);
2255 malloc_string(bigfile_fname);
2256 malloc_string(sz_devfile);
2257 estimated_total_noof_slices =
2258 size_of_all_biggiefiles_K() / bkpinfo->optimal_set_size + 1;
2259
2260 log_msg(1, "size of all biggiefiles = %ld",
2261 size_of_all_biggiefiles_K());
2262 log_msg(1, "estimated_total_noof_slices = %ld KB / %ld KB = %ld",
2263 size_of_all_biggiefiles_K(), bkpinfo->optimal_set_size,
2264 estimated_total_noof_slices);
2265
2266 if (length_of_file(biggielist_fname) < 6) {
2267 log_msg(1, "No biggiefiles; fair enough...");
2268 return (0);
2269 }
2270 sprintf(tmp, "I am now backing up all large files.");
2271 log_to_screen(tmp);
2272 noof_biggie_files = count_lines_in_file(biggielist_fname);
2273 open_progress_form("Backing up big files", tmp,
2274 "Please wait. This may take some time.", "",
2275 estimated_total_noof_slices);
2276 if (!(fin = fopen(biggielist_fname, "r"))) {
2277 log_OS_error("Unable to openin biggielist");
2278 return (1);
2279 }
2280 for (fgets(bigfile_fname, MAX_STR_LEN, fin); !feof(fin);
2281 fgets(bigfile_fname, MAX_STR_LEN, fin), biggie_file_number++) {
2282 use_ntfsprog = FALSE;
2283 if (bigfile_fname[strlen(bigfile_fname) - 1] < 32) {
2284 bigfile_fname[strlen(bigfile_fname) - 1] = '\0';
2285 }
2286 biggie_fsize = length_of_file(bigfile_fname);
2287 delete_when_done = FALSE;
2288
2289 if (!does_file_exist(bigfile_fname)) {
2290 ftmp = fopen(bigfile_fname, "w");
2291 if (ftmp == NULL) {
2292 log_msg(3, "Unable to write to %s", bigfile_fname);
2293 // So skip it as it doesn't exist
2294 continue;
2295 } else {
2296 paranoid_fclose(ftmp);
2297 }
2298 delete_when_done = TRUE;
2299 } else {
2300 // Call ntfsclone (formerly partimagehack) if it's a /dev entry
2301 // (i.e. a partition to be imaged)
2302 log_msg(2, "bigfile_fname = %s", bigfile_fname);
2303 use_ntfsprog = FALSE;
2304 if (!strncmp(bigfile_fname, "/dev/", 5)
2305 && is_dev_an_NTFS_dev(bigfile_fname)) {
2306 use_ntfsprog = TRUE;
2307 log_msg(2,
2308 "Calling ntfsclone in background because %s is an NTFS partition",
2309 bigfile_fname);
2310 sprintf(sz_devfile, "%s/%d.%d.000",
2311 bkpinfo->tmpdir,
2312 (int) (random() % 32768),
2313 (int) (random() % 32768));
2314 mkfifo(sz_devfile, 0x770);
2315 ntfsprog_fifo = sz_devfile;
2316 switch (pid = fork()) {
2317 case -1:
2318 fatal_error("Fork failure");
2319 case 0:
2320 log_msg(2,
2321 "CHILD - fip - calling feed_into_ntfsprog(%s, %s)",
2322 bigfile_fname, sz_devfile);
2323 res = feed_into_ntfsprog(bigfile_fname, sz_devfile);
2324 exit(res);
2325 break;
2326 default:
2327 log_msg(2,
2328 "feed_into_ntfsprog() called in background --- pid=%ld",
2329 (long int) (pid));
2330 }
2331 }
2332// Otherwise, use good old 'dd' and 'bzip2'
2333 else {
2334 sz_devfile[0] = '\0';
2335 ntfsprog_fifo = NULL;
2336 }
2337
2338// Whether partition or biggiefile, just do your thang :-)
2339 sprintf(tmp, "Bigfile #%ld is '%s' (%ld KB)",
2340 biggie_file_number + 1, bigfile_fname,
2341 (long) biggie_fsize >> 10);
2342 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2343 write_header_block_to_stream(biggie_fsize, bigfile_fname,
2344 use_ntfsprog ?
2345 BLK_START_A_PIHBIGGIE :
2346 BLK_START_A_NORMBIGGIE);
2347 }
2348 res =
2349 slice_up_file_etc(bigfile_fname,
2350 ntfsprog_fifo, biggie_file_number,
2351 noof_biggie_files, use_ntfsprog);
2352 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2353 write_header_block_to_stream((off_t)0,
2354 calc_checksum_of_file
2355 (bigfile_fname),
2356 BLK_STOP_A_BIGGIE);
2357 }
2358 retval += res;
2359 p = strrchr(bigfile_fname, '/');
2360 if (p) {
2361 p++;
2362 } else {
2363 p = bigfile_fname;
2364 }
2365 sprintf(tmp, "Archiving %s ... ", bigfile_fname);
2366 if (res) {
2367 strcat(tmp, "Failed!");
2368 } else {
2369 strcat(tmp, "OK");
2370 }
2371 if (delete_when_done) {
2372 unlink(bigfile_fname);
2373 delete_when_done = FALSE;
2374 }
2375 }
2376#ifndef _XWIN
2377 if (!g_text_mode) {
2378 newtDrawRootText(0, g_noof_rows - 2, tmp);
2379 newtRefresh();
2380 }
2381#endif
2382 }
2383 log_msg(1, "Finished backing up bigfiles");
2384 log_msg(1, "estimated slices = %ld; actual slices = %ld",
2385 estimated_total_noof_slices, g_current_progress);
2386 close_progress_form();
2387 paranoid_fclose(fin);
2388 paranoid_free(tmp);
2389 paranoid_free(bigfile_fname);
2390 paranoid_free(sz_devfile);
2391 return (retval);
2392}
2393
2394
2395
2396
2397/**
2398 * Single-threaded version of @c make_afioballs_and_images().
2399 * @see make_afioballs_and_images
2400 */
2401int make_afioballs_and_images_OLD()
2402{
2403
2404 /*@ int ************************************************** */
2405 int retval = 0;
2406 long int curr_set_no = 0;
2407 int res = 0;
2408
2409 /*@ buffers ********************************************** */
2410 char *curr_filelist_fname;
2411 char *curr_afioball_fname;
2412 char *curr_xattr_list_fname;
2413 char *curr_acl_list_fname;
2414 char *tmp;
2415 char *media_usage_comment;
2416
2417 malloc_string(curr_afioball_fname);
2418 malloc_string(media_usage_comment);
2419 malloc_string(curr_filelist_fname);
2420 malloc_string(curr_xattr_list_fname);
2421 malloc_string(curr_acl_list_fname);
2422
2423 tmp = malloc(MAX_STR_LEN * 2);
2424
2425 sprintf(tmp, "%s/archives/filelist.full", bkpinfo->scratchdir);
2426
2427 log_to_screen("Archiving regular files");
2428
2429 open_progress_form("Backing up filesystem",
2430 "I am backing up your live filesystem now.",
2431 "Please wait. This may take a couple of hours.",
2432 "Working...",
2433 get_last_filelist_number() + 1);
2434
2435 sprintf(curr_filelist_fname, FILELIST_FNAME_RAW_SZ, bkpinfo->tmpdir,
2436 0L);
2437
2438 for (curr_set_no = 0; does_file_exist(curr_filelist_fname);
2439 sprintf(curr_filelist_fname, FILELIST_FNAME_RAW_SZ,
2440 bkpinfo->tmpdir, ++curr_set_no)) {
2441 /* backup this set of files */
2442 sprintf(curr_filelist_fname, FILELIST_FNAME_RAW_SZ,
2443 bkpinfo->tmpdir, curr_set_no);
2444 sprintf(curr_afioball_fname, AFIOBALL_FNAME_RAW_SZ,
2445 bkpinfo->tmpdir, curr_set_no, bkpinfo->zip_suffix);
2446
2447 log_msg(1, "EXAT'g set %ld", curr_set_no);
2448 if (g_getfattr) {
2449 sprintf(curr_xattr_list_fname, XATTR_LIST_FNAME_RAW_SZ,
2450 bkpinfo->tmpdir, curr_set_no);
2451 get_fattr_list(curr_filelist_fname, curr_xattr_list_fname);
2452 }
2453 if (g_getfacl) {
2454 sprintf(curr_acl_list_fname, ACL_LIST_FNAME_RAW_SZ,
2455 bkpinfo->tmpdir, curr_set_no);
2456 get_acl_list(curr_filelist_fname, curr_acl_list_fname);
2457 }
2458
2459 log_msg(1, "Archiving set %ld", curr_set_no);
2460 res =
2461 archive_this_fileset(curr_filelist_fname,
2462 curr_afioball_fname, curr_set_no);
2463 retval += res;
2464 if (res) {
2465 sprintf(tmp,
2466 "Errors occurred while archiving set %ld. Perhaps your live filesystem changed?",
2467 curr_set_no);
2468 log_to_screen(tmp);
2469 }
2470
2471 strcpy(media_usage_comment, percent_media_full_comment());
2472
2473 /* copy to CD (scratchdir) ... and an actual CD-R if necessary */
2474 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2475 register_in_tape_catalog(fileset, curr_set_no, -1,
2476 curr_afioball_fname);
2477 maintain_collection_of_recent_archives(bkpinfo->tmpdir,
2478 curr_afioball_fname);
2479 iamhere("Writing EXAT files");
2480 res +=
2481 write_EXAT_files_to_tape(curr_xattr_list_fname,
2482 curr_acl_list_fname);
2483// archives themselves
2484 res = move_files_to_stream(curr_afioball_fname, NULL);
2485 } else {
2486 if (g_getfacl) {
2487 if (g_getfattr) {
2488 res = move_files_to_cd(curr_filelist_fname,
2489 curr_xattr_list_fname,
2490 curr_acl_list_fname,
2491 curr_afioball_fname, NULL);
2492 } else {
2493 res = move_files_to_cd(curr_filelist_fname,
2494 curr_acl_list_fname,
2495 curr_afioball_fname, NULL);
2496 }
2497 } else {
2498 if (g_getfattr) {
2499 res = move_files_to_cd(curr_filelist_fname,
2500 curr_xattr_list_fname,
2501 curr_afioball_fname, NULL);
2502 } else {
2503 res = move_files_to_cd(curr_filelist_fname,
2504 curr_afioball_fname, NULL);
2505 }
2506 }
2507 }
2508 retval += res;
2509 g_current_progress++;
2510 update_progress_form(media_usage_comment);
2511
2512 if (res) {
2513 sprintf(tmp, "Failed to add archive %ld's files to CD dir\n",
2514 curr_set_no);
2515 log_to_screen(tmp);
2516 fatal_error
2517 ("Is your hard disk is full? If not, please send the author the logfile.");
2518 }
2519 }
2520 close_progress_form();
2521 sprintf(tmp, "Your regular files have been archived ");
2522 if (retval) {
2523 strcat(tmp, "(with errors).");
2524 } else {
2525 strcat(tmp, "successfully.");
2526 }
2527 log_to_screen(tmp);
2528 paranoid_free(tmp);
2529 paranoid_free(curr_filelist_fname);
2530 paranoid_free(curr_afioball_fname);
2531 paranoid_free(media_usage_comment);
2532 paranoid_free(curr_xattr_list_fname);
2533 paranoid_free(curr_acl_list_fname);
2534 return (retval);
2535}
2536
2537/* @} - end of LLarchiveGroup */
2538
2539
2540/**
2541 * Wrapper around @c make_afioballs_and_images().
2542 * @param bkpinfo the backup information structure. Only the
2543 * @c backup_media_type field is used within this function.
2544 * @return return code of make_afioballs_and_images
2545 * @see make_afioballs_and_images
2546 * @ingroup MLarchiveGroup
2547 */
2548int make_those_afios_phase()
2549{
2550 /*@ int ******************************************* */
2551 int res = 0;
2552 int retval = 0;
2553
2554 assert(bkpinfo != NULL);
2555
2556 mvaddstr_and_log_it(g_currentY, 0,
2557 "Archiving regular files to media ");
2558
2559 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2560 write_header_block_to_stream((off_t)0, "start-of-afioballs",
2561 BLK_START_AFIOBALLS);
2562#if __FreeBSD__ == 5
2563 log_msg(1,
2564 "Using single-threaded make_afioballs_and_images() to suit b0rken FreeBSD 5.0");
2565 res = make_afioballs_and_images_OLD();
2566#else
2567 res = make_afioballs_and_images_OLD();
2568#endif
2569 write_header_block_to_stream((off_t)0, "stop-afioballs",
2570 BLK_STOP_AFIOBALLS);
2571 } else {
2572 res = make_afioballs_and_images();
2573 }
2574
2575 retval += res;
2576 if (res) {
2577 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2578 log_msg(1, "make_afioballs_and_images returned an error");
2579 } else {
2580 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2581 }
2582 return (retval);
2583}
2584
2585/**
2586 * Wrapper around @c make_slices_and_images().
2587 * @param bkpinfo The backup information structure. Fields used:
2588 * - @c backup_media_type
2589 * - @c scratchdir
2590 * - @c tmpdir
2591 * @return The number of errors encountered (0 for success)
2592 * @ingroup MLarchiveGroup
2593 */
2594int make_those_slices_phase()
2595{
2596
2597 /*@ int ***************************************************** */
2598 int res = 0;
2599 int retval = 0;
2600
2601 /*@ buffers ************************************************** */
2602 char *biggielist;
2603 char *command;
2604 char *blah;
2605 char *xattr_fname;
2606 char *acl_fname;
2607
2608 assert(bkpinfo != NULL);
2609 /* slice big files */
2610 malloc_string(biggielist);
2611 malloc_string(xattr_fname);
2612 malloc_string(acl_fname);
2613 mvaddstr_and_log_it(g_currentY, 0,
2614 "Archiving large files to media ");
2615 sprintf(biggielist, "%s/archives/biggielist.txt", bkpinfo->scratchdir);
2616 if (g_getfattr) {
2617 sprintf(xattr_fname, XATTR_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
2618 }
2619 if (g_getfacl) {
2620 sprintf(acl_fname, ACL_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
2621 }
2622
2623 asprintf(&command, "cp %s/biggielist.txt %s", bkpinfo->tmpdir,
2624 biggielist);
2625 paranoid_system(command);
2626 paranoid_free(command);
2627
2628 asprintf(&blah, "biggielist = %s", biggielist);
2629 log_msg(2, blah);
2630 paranoid_free(blah);
2631
2632 if (!does_file_exist(biggielist)) {
2633 log_msg(1, "BTW, the biggielist does not exist");
2634 }
2635
2636 if (g_getfattr) {
2637 get_fattr_list(biggielist, xattr_fname);
2638 asprintf(&command, "cp %s %s/archives/", xattr_fname,
2639 bkpinfo->scratchdir);
2640 paranoid_system(command);
2641 paranoid_free(command);
2642 }
2643 if (g_getfacl) {
2644 get_acl_list(biggielist, acl_fname);
2645 asprintf(&command, "cp %s %s/archives/", acl_fname,
2646 bkpinfo->scratchdir);
2647 paranoid_system(command);
2648 paranoid_free(command);
2649 }
2650
2651 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2652 res += write_EXAT_files_to_tape(xattr_fname, acl_fname);
2653 asprintf(&blah, "%ld", count_lines_in_file(biggielist));
2654 write_header_block_to_stream((off_t)0, blah, BLK_START_BIGGIEFILES);
2655 paranoid_free(blah);
2656 }
2657 res = make_slices_and_images(biggielist);
2658 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2659 write_header_block_to_stream((off_t)0, "end-of-biggiefiles",
2660 BLK_STOP_BIGGIEFILES);
2661 }
2662 retval += res;
2663 if (res) {
2664 log_msg(1, "make_slices_and_images returned an error");
2665 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2666 } else {
2667 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2668 }
2669 paranoid_free(blah);
2670 paranoid_free(biggielist);
2671 paranoid_free(command);
2672 paranoid_free(xattr_fname);
2673 paranoid_free(acl_fname);
2674 return (retval);
2675}
2676
2677
2678/**
2679 * @addtogroup LLarchiveGroup
2680 * @{
2681 */
2682/**
2683 * Function pointer to an appropriate @c move_files_to_cd routine.
2684 * You can set this to your own function (for example, one to
2685 * transfer files over the network) or leave it as is.
2686 */
2687int (*move_files_to_cd) (char *, ...) =
2688 _move_files_to_cd;
2689
2690/**
2691 * Move some files to the ISO scratch directory.
2692 * This function moves files specified as parameters, into the directory
2693 * @c bkpinfo->scratchdir, where the files that will be stored on the next
2694 * CD are waiting.
2695 *
2696 * @param bkpinfo The backup information structure. Fields used:
2697 * - @c media_size
2698 * - @c scratchdir
2699 * @param files_to_add The files to add to the scratchdir.
2700 * @warning The list of @c files_to_add must be terminated with @c NULL.
2701 * @note If and when the space occupied by the scratchdir would exceed
2702 * the capacity of the current CD,
2703 * <tt>write_iso_and_go_on(bkpinfo, FALSE)</tt> is called and the
2704 * scratchdir is emptied.
2705 *
2706 * @return The number of errors encountered (0 for success)
2707 */
2708int _move_files_to_cd(char *files_to_add, ...)
2709{
2710
2711 /*@ int ************************************************************ */
2712 int retval = 0;
2713 int res = 0;
2714
2715 /*@ buffers ******************************************************** */
2716 char *tmp, *curr_file, *cf;
2717
2718 /*@ long ************************************************************ */
2719 va_list ap;
2720 long long would_occupy;
2721
2722 assert(bkpinfo != NULL);
2723 malloc_string(curr_file);
2724 tmp = malloc(1200);
2725 would_occupy = space_occupied_by_cd(bkpinfo->scratchdir);
2726 va_start(ap, files_to_add); // initialize the variable arguments
2727 for (cf = files_to_add; cf != NULL; cf = va_arg(ap, char *)) {
2728 if (!cf) {
2729 continue;
2730 }
2731 strcpy(curr_file, cf);
2732 if (!does_file_exist(curr_file)) {
2733 log_msg(1,
2734 "Warning - you're trying to add a non-existent file - '%s' to the CD",
2735 curr_file);
2736 } else {
2737 log_msg(8, "Trying to add file %s to CD", curr_file);
2738 would_occupy += length_of_file(curr_file) / 1024;
2739 }
2740 }
2741 va_end(ap);
2742
2743 if (bkpinfo->media_size[g_current_media_number] <= 0) {
2744 fatal_error("move_files_to_cd() - unknown media size");
2745 }
2746 if (would_occupy / 1024 > bkpinfo->media_size[g_current_media_number]) {
2747 res = write_iso_and_go_on(FALSE); /* FALSE because this is not the last CD we'll write */
2748 retval += res;
2749 if (res) {
2750 log_msg(1, "WARNING - write_iso_and_go_on returned an error");
2751 }
2752 }
2753
2754 va_start(ap, files_to_add); // initialize the variable arguments
2755 for (cf = files_to_add; cf != NULL; cf = va_arg(ap, char *)) {
2756 if (!cf) {
2757 continue;
2758 }
2759 strcpy(curr_file, cf);
2760
2761 sprintf(tmp, "mv -f %s %s/archives/", curr_file,
2762 bkpinfo->scratchdir);
2763 res = run_program_and_log_output(tmp, 5);
2764 retval += res;
2765 if (res) {
2766 log_msg(1, "(move_files_to_cd) '%s' failed", tmp);
2767 } else {
2768 log_msg(8, "Moved %s to CD OK", tmp);
2769 }
2770 // unlink (curr_file);
2771 }
2772 va_end(ap);
2773
2774 if (retval) {
2775 log_msg(1,
2776 "Warning - errors occurred while I was adding files to CD dir");
2777 }
2778 paranoid_free(tmp);
2779 paranoid_free(curr_file);
2780 return (retval);
2781}
2782
2783/* @} - end of LLarchiveGroup */
2784
2785
2786
2787
2788
2789
2790
2791
2792/**
2793 * Offer to write boot and data disk images to 3.5" floppy disks.
2794 * @param bkpinfo The backup information structure. Only the
2795 * @c backup_media_type field is used in this function.
2796 * @param imagesdir The directory containing the floppy images (usually
2797 * /root/images/mindi).
2798 *
2799 * @return The number of errors encountered (0 for success)
2800 * @see write_image_to_floppy
2801 * @see format_disk
2802 * @ingroup MLarchiveGroup
2803 */
2804int offer_to_write_floppies(char *imagesdir)
2805{
2806 /*@ buffer ************************************************************ */
2807 char *tmp;
2808 char *comment;
2809 char *bootdisk_dev;
2810 char *datadisk_dev;
2811 char *bootdisk_file;
2812 char *rootdisk_file;
2813
2814 /*@ int *************************************************************** */
2815 int i = 0;
2816 int res = 0;
2817
2818 /*@ bool ************************************************************** */
2819 bool format_first;
2820 bool root_disk_exists = FALSE;
2821
2822 malloc_string(tmp);
2823 malloc_string(comment);
2824 malloc_string(bootdisk_dev);
2825 malloc_string(datadisk_dev);
2826 malloc_string(rootdisk_file);
2827 malloc_string(bootdisk_file);
2828 assert(bkpinfo != NULL);
2829 assert_string_is_neither_NULL_nor_zerolength(imagesdir);
2830
2831
2832 if (!ask_me_yes_or_no
2833 ("Write boot and data disk images to 3.5\" floppy disks?")) {
2834 return (0);
2835 }
2836 if (does_device_exist(DEFAULT_1722MB_DISK)) {
2837#ifdef __FreeBSD__
2838 // tell the system that this is a 1.72m floppy
2839 system("/usr/sbin/fdcontrol -F 1722 /dev/fd0.1722");
2840#endif
2841 strcpy(bootdisk_dev, DEFAULT_1722MB_DISK);
2842 } else if (does_device_exist(BACKUP_1722MB_DISK)) {
2843 sprintf(bootdisk_dev, "/dev/fd0H1722");
2844 } else {
2845 log_msg(1, "Warning - can't find a 1.72MB floppy device *sigh*");
2846 strcpy(bootdisk_dev, DEFAULT_1722MB_DISK);
2847// return (1);
2848 }
2849 strcpy(datadisk_dev, "/dev/fd0");
2850 if (!does_device_exist(datadisk_dev)) {
2851 log_msg(1, "Warning - can't find a 1.44MB floppy device *sigh*");
2852 strcpy(datadisk_dev, "/dev/fd0");
2853// return (1);
2854 }
2855 format_first =
2856 ask_me_yes_or_no
2857 ("Do you want me to format the disks before I write to them?");
2858
2859/* boot disk */
2860 if (ask_me_OK_or_cancel("About to write boot disk")) {
2861 log_to_screen("Writing boot floppy");
2862#ifdef __FreeBSD__
2863 sprintf(tmp, "%s/mindi-kern.1722.img", imagesdir);
2864 if (format_first) {
2865 format_disk(bootdisk_dev);
2866 }
2867 res += write_image_to_floppy(bootdisk_dev, tmp);
2868 if (ask_me_OK_or_cancel("About to write 1.44MB mfsroot disk")) {
2869 log_to_screen("Writing mfsroot floppy");
2870 if (format_first) {
2871 format_disk(datadisk_dev);
2872 }
2873 sprintf(tmp, "%s/mindi-mfsroot.1440.img", imagesdir);
2874 write_image_to_floppy(datadisk_dev, tmp);
2875 }
2876#else
2877 sprintf(bootdisk_file, "%s/mindi-bootroot.1722.img", imagesdir);
2878 if (does_file_exist(bootdisk_file)) {
2879 if (format_first) {
2880 format_disk(bootdisk_dev);
2881 }
2882 res += write_image_to_floppy(bootdisk_dev, bootdisk_file);
2883 } else {
2884 sprintf(bootdisk_file, "%s/mindi-boot.1440.img", imagesdir);
2885 sprintf(rootdisk_file, "%s/mindi-root.1440.img", imagesdir);
2886 root_disk_exists = TRUE;
2887 if (!does_file_exist(rootdisk_file)
2888 || !does_file_exist(bootdisk_file)) {
2889 popup_and_OK
2890 ("Cannot write boot/root floppies. Files not found.");
2891 log_to_screen
2892 ("Failed to find boot/root floppy images. Oh dear.");
2893 return (1);
2894 }
2895 if (format_first) {
2896 format_disk(datadisk_dev);
2897 }
2898 /*
2899 sprintf(tmp, "cat %s > %s", bootdisk_file, datadisk_dev);
2900 res += run_external_binary_with_percentage_indicator_NEW("Writing boot floppy", tmp);
2901 */
2902 res += write_image_to_floppy(datadisk_dev, bootdisk_file);
2903 if (ask_me_OK_or_cancel("About to write root disk")) {
2904 log_to_screen("Writing root floppy");
2905 if (format_first) {
2906 format_disk(datadisk_dev);
2907 }
2908 sprintf(tmp, "cat %s > %s", rootdisk_file, datadisk_dev);
2909 log_msg(1, "tmp = '%s'", tmp);
2910 res +=
2911 run_external_binary_with_percentage_indicator_NEW
2912 ("Writing root floppy", tmp);
2913// res += write_image_to_floppy (datadisk_dev, rootdisk_file);
2914 }
2915 }
2916#endif
2917 }
2918 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2919 log_to_screen
2920 ("FYI, the data disks are stored on tape/CD for your convenience.");
2921 return (0);
2922 }
2923 for (i = 1; i < 99; i++) {
2924 sprintf(tmp, "%s/mindi-data-%d.img", imagesdir, i);
2925 log_msg(3, tmp);
2926 if (!does_file_exist(tmp)) {
2927 log_msg(3, "...not found");
2928 break;
2929 }
2930 sprintf(comment, "About to write data disk #%d", i);
2931 if (ask_me_OK_or_cancel(comment)) {
2932 sprintf(comment, "Writing data disk #%3d", i);
2933 log_to_screen(comment);
2934 if (format_first) {
2935 res += format_disk(datadisk_dev);
2936 }
2937 res += write_image_to_floppy(datadisk_dev, tmp);
2938 }
2939 }
2940 paranoid_free(tmp);
2941 paranoid_free(comment);
2942 paranoid_free(bootdisk_dev);
2943 paranoid_free(datadisk_dev);
2944 return (res);
2945}
2946
2947
2948
2949
2950
2951
2952
2953
2954/**
2955 * Wrapper around @c offer_to_write_floppies().
2956 * @param bkpinfo The backup information structure. Used only
2957 * in the call to @c offer_to_write_floppies().
2958 * @return 0 if the boot floppies were found (not necessarily written OK),
2959 * 1 otherwise.
2960 * @see offer_to_write_floppies
2961 * @ingroup MLarchiveGroup
2962 */
2963
2964int offer_to_write_boot_floppies_to_physical_disks()
2965{
2966 int res = 0;
2967
2968 assert(bkpinfo != NULL);
2969
2970 mvaddstr_and_log_it(g_currentY, 0,
2971 "Writing boot+data floppy images to disk");
2972
2973 if (!bkpinfo->nonbootable_backup) {
2974#ifdef __FreeBSD__
2975 if (!does_file_exist(MINDI_CACHE"/mindi-kern.1722.img"))
2976#else
2977 if (!does_file_exist(MINDI_CACHE"/mindi-bootroot.1722.img") && !does_file_exist(MINDI_CACHE"/mindi-boot.1440.img"))
2978#endif
2979 {
2980 mvaddstr_and_log_it(g_currentY++, 74, "No Imgs");
2981 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
2982 popup_and_OK
2983 ("No regular Boot+data floppies were created due of space constraints. However, you can burn "MINDI_CACHE"/mondorescue.iso to a CD and boot from that.");
2984 res++;
2985 }
2986 } else {
2987 offer_to_write_floppies(MINDI_CACHE);
2988 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2989 }
2990 } else {
2991 popup_and_OK
2992 ("Since you opted for a nonbootable backup, no boot floppies were created.");
2993 }
2994
2995 return (res);
2996}
2997
2998
2999
3000
3001/**
3002 * @addtogroup LLarchiveGroup
3003 * @{
3004 */
3005/**
3006 * Function pointer to an appropriate @c move_files_to_stream routine.
3007 * You can set this to your own function (for example, one to
3008 * transfer files over the network) or leave it as is.
3009 */
3010int (*move_files_to_stream) (char *, ...) =
3011 _move_files_to_stream;
3012
3013/**
3014 * Copy some files to tape.
3015 * This function copies the files specified as parameters into the tape stream.
3016 *
3017 * @param bkpinfo The backup information structure. Used only in the call to
3018 * @c write_file_to_stream_from_file().
3019 *
3020 * @param files_to_add The files to copy to the tape stream.
3021 * @warning The list of @c files_to_add must be terminated with @c NULL.
3022 * @note Files may be split across multiple tapes if necessary.
3023 *
3024 * @return The number of errors encountered (0 for success)
3025 */
3026int
3027_move_files_to_stream(char *files_to_add, ...)
3028{
3029
3030 /*@ int ************************************************************ */
3031 int retval = 0;
3032 int res = 0;
3033 /*@ buffers ******************************************************** */
3034
3035 /*@ char *********************************************************** */
3036 char start_chr;
3037 char stop_chr;
3038 char *curr_file, *cf;
3039 /*@ long long ****************************************************** */
3040 off_t length_of_incoming_file = (off_t)0;
3041 t_archtype type;
3042 va_list ap;
3043
3044 assert(bkpinfo != NULL);
3045 malloc_string(curr_file);
3046 va_start(ap, files_to_add);
3047 for (cf = files_to_add; cf != NULL; cf = va_arg(ap, char *)) {
3048 if (!cf) {
3049 continue;
3050 }
3051 strcpy(curr_file, cf);
3052 if (!does_file_exist(curr_file)) {
3053 log_msg(1,
3054 "Warning - you're trying to add a non-existent file - '%s' to the tape",
3055 curr_file);
3056 }
3057/* create header chars */
3058 start_chr = BLK_START_AN_AFIO_OR_SLICE;
3059 stop_chr = BLK_STOP_AN_AFIO_OR_SLICE;
3060/* ask for new tape if necessary */
3061 length_of_incoming_file = length_of_file(curr_file);
3062 write_header_block_to_stream(length_of_incoming_file, curr_file,
3063 start_chr);
3064 if (strstr(curr_file, ".afio.") || strstr(curr_file, ".star.")) {
3065 type = fileset;
3066 } else if (strstr(curr_file, "slice")) {
3067 type = biggieslice;
3068 } else {
3069 type = other;
3070 }
3071 res = write_file_to_stream_from_file(curr_file);
3072 retval += res;
3073 unlink(curr_file);
3074/* write closing header */
3075 write_header_block_to_stream((off_t)0, "finished-writing-file", stop_chr);
3076 }
3077 va_end(ap);
3078
3079 if (retval) {
3080 log_msg(1,
3081 "Warning - errors occurred while I was adding file to tape");
3082 }
3083 paranoid_free(curr_file);
3084 return (retval);
3085}
3086
3087/* @} - end of LLarchiveGroup */
3088
3089
3090
3091/**
3092 * @addtogroup utilityGroup
3093 * @{
3094 */
3095/**
3096 * Make sure the user has a valid CD-R(W) in the CD drive.
3097 * @param cdrw_dev Set to the CD-R(W) device checked.
3098 * @param keep_looping If TRUE, keep pestering user until they insist
3099 * or insert a correct CD; if FALSE, only check once.
3100 * @return 0 (there was an OK CD in the drive) or 1 (there wasn't).
3101 */
3102int interrogate_disk_currently_in_cdrw_drive(char *cdrw_dev,
3103 bool keep_looping)
3104{
3105 char *tmp;
3106 int res = 0;
3107 char *bkp;
3108 char *cdrecord;
3109
3110 malloc_string(tmp);
3111 malloc_string(bkp);
3112 malloc_string(cdrecord);
3113 strcpy(bkp, cdrw_dev);
3114 if (find_cdrw_device(cdrw_dev)) {
3115 strcpy(cdrw_dev, bkp);
3116 } else {
3117 if (!system("which cdrecord > /dev/null 2> /dev/null")) {
3118 sprintf(cdrecord, "cdrecord dev=%s -atip", cdrw_dev);
3119 } else if (!system("which dvdrecord > /dev/null 2> /dev/null")) {
3120 sprintf(cdrecord, "cdrecord dev=%s -atip", cdrw_dev);
3121 } else {
3122 cdrecord[0] = '\0';
3123 log_msg(2, "Oh well. I guess I'll just pray then.");
3124 }
3125 if (cdrecord[0]) {
3126 if (!keep_looping) {
3127 retract_CD_tray_and_defeat_autorun();
3128 res = run_program_and_log_output(cdrecord, 5);
3129 } else {
3130 while ((res = run_program_and_log_output(cdrecord, 5))) {
3131 retract_CD_tray_and_defeat_autorun();
3132 if (ask_me_yes_or_no
3133 ("Unable to examine CD. Are you sure this is a valid CD-R(W) CD?"))
3134 {
3135 log_msg(1, "Well, he insisted...");
3136 break;
3137 }
3138 }
3139 }
3140 }
3141 }
3142// retract_CD_tray_and_defeat_autorun();
3143 paranoid_free(tmp);
3144 paranoid_free(cdrecord);
3145 paranoid_free(bkp);
3146 return (res);
3147}
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157/**
3158 * Asks the user to put a CD-R(W) in the drive.
3159 * @param ask_for_one_if_more_than_this (unused)
3160 * @param pmountable If non-NULL, pointed-to value is set to TRUE if the CD is mountable, FALSE otherwise.
3161 */
3162void
3163pause_and_ask_for_cdr(int ask_for_one_if_more_than_this, bool * pmountable)
3164{
3165
3166 /*@ buffers ********************************************* */
3167 char *tmp;
3168 char *szmsg;
3169 char *cdrom_dev;
3170 char *cdrw_dev;
3171 char *our_serial_str;
3172 bool ok_go_ahead_burn_it;
3173 int cd_number = -1;
3174 int attempt_to_mount_returned_this = 999;
3175 char *mtpt;
3176 char *szcdno;
3177 char *szserfname;
3178 char *szunmount;
3179
3180 malloc_string(tmp);
3181 malloc_string(szmsg);
3182 malloc_string(cdrom_dev);
3183 malloc_string(cdrw_dev);
3184 malloc_string(mtpt);
3185 malloc_string(szcdno);
3186 malloc_string(szserfname);
3187 malloc_string(our_serial_str);
3188 malloc_string(szunmount);
3189
3190 sprintf(szmsg, "I am about to burn %s #%d",
3191 media_descriptor_string(g_backup_media_type),
3192 g_current_media_number);
3193 log_to_screen(szmsg);
3194 if (g_current_media_number < ask_for_one_if_more_than_this) {
3195 return;
3196 }
3197 log_to_screen("Scanning CD-ROM drive...");
3198 sprintf(mtpt, "%s/cd.mtpt", bkpinfo->tmpdir);
3199 make_hole_for_dir(mtpt);
3200
3201 gotos_make_me_puke:
3202 ok_go_ahead_burn_it = TRUE;
3203 if (!find_cdrom_device(cdrom_dev, FALSE)) {
3204/* When enabled, it made CD eject-and-retract when wrong CD inserted.. Weird
3205 log_msg(2, "paafcd: Retracting CD-ROM drive if possible" );
3206 retract_CD_tray_and_defeat_autorun();
3207*/
3208 sprintf(tmp, "umount %s", cdrom_dev);
3209 run_program_and_log_output(tmp, 1);
3210 sprintf(szcdno, "%s/archives/THIS-CD-NUMBER", mtpt);
3211 sprintf(szserfname, "%s/archives/SERIAL-STRING", mtpt);
3212 sprintf(szunmount, "umount %s", mtpt);
3213 cd_number = -1;
3214 our_serial_str[0] = '\0';
3215 sprintf(tmp, "mount %s %s", cdrom_dev, mtpt);
3216 if ((attempt_to_mount_returned_this =
3217 run_program_and_log_output(tmp, 1))) {
3218 log_msg(4, "Failed to mount %s at %s", cdrom_dev, mtpt);
3219 log_to_screen("If there's a CD/DVD in the drive, it's blank.");
3220 /*
3221 if (interrogate_disk_currently_in_cdrw_drive(cdrw_dev, FALSE))
3222 {
3223 ok_go_ahead_burn_it = FALSE;
3224 log_to_screen("There isn't a writable CD/DVD in the drive.");
3225 }
3226 else
3227 {
3228 log_to_screen("Confirmed. There is a blank CD/DVD in the drive.");
3229 }
3230 */
3231 } else if (!does_file_exist(szcdno)
3232 || !does_file_exist(szserfname)) {
3233 log_to_screen
3234 ("%s has data on it but it's probably not a Mondo CD.",
3235 media_descriptor_string(g_backup_media_type));
3236 } else {
3237 log_to_screen("%s found in drive. It's a Mondo disk.",
3238 media_descriptor_string(g_backup_media_type));
3239 cd_number = atoi(last_line_of_file(szcdno));
3240 sprintf(tmp, "cat %s 2> /dev/null", szserfname);
3241 strcpy(our_serial_str,
3242 call_program_and_get_last_line_of_output(tmp));
3243 // FIXME - should be able to use last_line_of_file(), surely?
3244 }
3245 run_program_and_log_output(szunmount, 1);
3246 log_msg(2, "paafcd: cd_number = %d", cd_number);
3247 log_msg(2, "our serial str = %s; g_serial_string = %s",
3248 our_serial_str, g_serial_string);
3249 if (cd_number > 0 && !strcmp(our_serial_str, g_serial_string)) {
3250 log_msg(2, "This %s is part of this backup set!",
3251 media_descriptor_string(g_backup_media_type));
3252 ok_go_ahead_burn_it = FALSE;
3253 if (cd_number == g_current_media_number - 1) {
3254 log_to_screen
3255 ("I think you've left the previous %s in the drive.",
3256 media_descriptor_string(g_backup_media_type));
3257 } else {
3258 log_to_screen
3259 ("Please remove this %s. It is part of the backup set you're making now.",
3260 media_descriptor_string(g_backup_media_type));
3261 }
3262 } else {
3263 log_to_screen("...but not part of _our_ backup set.");
3264 }
3265 } else {
3266 log_msg(2,
3267 "paafcd: Can't find CD-ROM drive. Perhaps it has a blank %s in it?",
3268 media_descriptor_string(g_backup_media_type));
3269 if (interrogate_disk_currently_in_cdrw_drive(cdrw_dev, FALSE)) {
3270 ok_go_ahead_burn_it = FALSE;
3271 log_to_screen("There isn't a writable %s in the drive.",
3272 media_descriptor_string(g_backup_media_type));
3273 }
3274 }
3275
3276/*
3277 if (g_current_media_number > ask_for_one_if_more_than_this)
3278 {
3279 ok_go_ahead_burn_it = FALSE;
3280 log_it("paafcd: %d > %d, so I'll definitely pause.", g_current_media_number > ask_for_one_if_more_than_this);
3281 }
3282*/
3283
3284 if (!ok_go_ahead_burn_it) {
3285 eject_device(cdrom_dev);
3286 sprintf(tmp,
3287 "I am about to burn %s #%d of the backup set. Please insert %s and press Enter.",
3288 media_descriptor_string(g_backup_media_type),
3289 g_current_media_number,
3290 media_descriptor_string(g_backup_media_type));
3291 popup_and_OK(tmp);
3292 goto gotos_make_me_puke;
3293 } else {
3294 log_msg(2, "paafcd: OK, going ahead and burning it.");
3295 }
3296
3297 log_msg(2,
3298 "paafcd: OK, I assume I have a blank/reusable %s in the drive...",
3299 media_descriptor_string(g_backup_media_type));
3300
3301 // if (ask_for_one_if_more_than_this>1) { popup_and_OK(szmsg); }
3302
3303 log_to_screen("Proceeding w/ %s in drive.",
3304 media_descriptor_string(g_backup_media_type));
3305 paranoid_free(tmp);
3306 paranoid_free(szmsg);
3307 paranoid_free(cdrom_dev);
3308 paranoid_free(cdrw_dev);
3309 paranoid_free(mtpt);
3310 paranoid_free(szcdno);
3311 paranoid_free(szserfname);
3312 paranoid_free(our_serial_str);
3313 paranoid_free(szunmount);
3314 if (pmountable) {
3315 if (attempt_to_mount_returned_this) {
3316 *pmountable = FALSE;
3317 } else {
3318 *pmountable = TRUE;
3319 }
3320 }
3321
3322}
3323
3324
3325
3326
3327
3328
3329
3330
3331/**
3332 * Set the <tt>N</tt>th bit of @c array to @c true_or_false.
3333 * @param array The bit array (as a @c char pointer).
3334 * @param N The bit number to set or reset.
3335 * @param true_or_false If TRUE then set bit @c N, if FALSE then reset bit @c N.
3336 * @see get_bit_N_of_array
3337 */
3338void set_bit_N_of_array(char *array, int N, bool true_or_false)
3339{
3340 int bit_number;
3341 int mask, orig_val, to_add;
3342 int element_number;
3343
3344 assert(array != NULL);
3345
3346 element_number = N / 8;
3347 bit_number = N % 8;
3348 to_add = (1 << bit_number);
3349 mask = 255 - to_add;
3350 orig_val = array[element_number] & mask;
3351 // log_it("array[%d]=%02x; %02x&%02x = %02x", element_number, array[element_number], mask, orig_val);
3352 if (true_or_false) {
3353 array[element_number] = orig_val | to_add;
3354 }
3355}
3356
3357/* @} - end of utilityGroup */
3358
3359
3360
3361
3362
3363
3364
3365
3366/**
3367 * Chop up @c filename.
3368 * @param bkpinfo The backup information structure. Fields used:
3369 * - @c backup_media_type
3370 * - @c compression_level
3371 * - @c optimal_set_size
3372 * - @c tmpdir
3373 * - @c use_lzo
3374 * - @c zip_exe
3375 * - @c zip_suffix
3376 *
3377 * @param biggie_filename The file to chop up.
3378 * @param ntfsprog_fifo The FIFO to ntfsclone if this is an imagedev, NULL otherwise.
3379 * @param biggie_file_number The sequence number of this biggie file (starting from 0).
3380 * @param noof_biggie_files The number of biggie files there are total.
3381 * @return The number of errors encountered (0 for success)
3382 * @see make_slices_and_images
3383 * @ingroup LLarchiveGroup
3384 */
3385int
3386slice_up_file_etc(char *biggie_filename,
3387 char *ntfsprog_fifo, long biggie_file_number,
3388 long noof_biggie_files, bool use_ntfsprog)
3389{
3390
3391 /*@ buffers ************************************************** */
3392 char *tmp, *checksum_line, *command;
3393 char *tempblock;
3394 char *curr_slice_fname_uncompressed;
3395 char *curr_slice_fname_compressed;
3396 char *file_to_archive;
3397 char *file_to_openin;
3398 /*@ pointers ************************************************** */
3399 char *pB;
3400 FILE *fin = NULL, *fout = NULL;
3401
3402 /*@ bool ****************************************************** */
3403 bool finished = FALSE;
3404
3405 /*@ long ****************************************************** */
3406 size_t blksize = 0;
3407 long slice_num = 0;
3408 long i;
3409 long optimal_set_size;
3410 bool should_I_compress_slices;
3411 char *suffix; // for compressed slices
3412
3413 /*@ long long ************************************************** */
3414 off_t totalread = (off_t)0;
3415 off_t totallength = (off_t)0;
3416 off_t length;
3417
3418 /*@ int ******************************************************** */
3419 int retval = 0;
3420 int res = 0;
3421
3422 /*@ structures ************************************************** */
3423 struct s_filename_and_lstat_info biggiestruct;
3424// struct stat statbuf;
3425
3426 assert(bkpinfo != NULL);
3427 assert_string_is_neither_NULL_nor_zerolength(biggie_filename);
3428 malloc_string(tmp);
3429 malloc_string(checksum_line);
3430 malloc_string(curr_slice_fname_uncompressed);
3431 malloc_string(curr_slice_fname_compressed);
3432 malloc_string(file_to_archive);
3433 malloc_string(suffix);
3434 command = malloc(MAX_STR_LEN * 8);
3435
3436 biggiestruct.for_backward_compatibility = '\n';
3437 biggiestruct.use_ntfsprog = use_ntfsprog;
3438 if (!(tempblock = (char *) malloc(256 * 1024))) {
3439 fatal_error("malloc error 256*1024");
3440 }
3441 optimal_set_size = bkpinfo->optimal_set_size;
3442 if (is_this_file_compressed(biggie_filename)
3443 || bkpinfo->compression_level == 0) {
3444 suffix[0] = '\0';
3445 // log_it("%s is indeed compressed :-)", filename);
3446 should_I_compress_slices = FALSE;
3447 } else {
3448 strcpy(suffix, bkpinfo->zip_suffix);
3449 should_I_compress_slices = TRUE;
3450 }
3451
3452 if (optimal_set_size < 999) {
3453 fatal_error("bkpinfo->optimal_set_size is insanely small");
3454 }
3455 if (ntfsprog_fifo) {
3456 file_to_openin = ntfsprog_fifo;
3457 strcpy(checksum_line, "IGNORE");
3458 log_msg(2,
3459 "Not calculating checksum for %s: it would take too long",
3460 biggie_filename);
3461 if ( !find_home_of_exe("ntfsresize")) {
3462 fatal_error("ntfsresize not found");
3463 }
3464 sprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", biggie_filename);
3465 log_it("command = %s", command);
3466 strcpy (tmp, call_program_and_get_last_line_of_output(command));
3467 log_it("res of it = %s", tmp);
3468 totallength = (off_t)atoll(tmp);
3469 } else {
3470 file_to_openin = biggie_filename;
3471 if (strchr(biggie_filename,'\'') != NULL) {
3472 sprintf(command, "md5sum \"%s\"", biggie_filename);
3473 } else {
3474 sprintf(command, "md5sum '%s'", biggie_filename);
3475 }
3476 if (!(fin = popen(command, "r"))) {
3477 log_OS_error("Unable to popen-in command");
3478 return (1);
3479 }
3480 (void) fgets(checksum_line, MAX_STR_LEN, fin);
3481 pclose(fin);
3482 totallength = length_of_file (biggie_filename);
3483 }
3484 lstat(biggie_filename, &biggiestruct.properties);
3485 strcpy(biggiestruct.filename, biggie_filename);
3486 pB = strchr(checksum_line, ' ');
3487 if (!pB) {
3488 pB = strchr(checksum_line, '\t');
3489 }
3490 if (pB) {
3491 *pB = '\0';
3492 }
3493 strcpy(biggiestruct.checksum, checksum_line);
3494
3495 strcpy(tmp, slice_fname(biggie_file_number, 0, bkpinfo->tmpdir, ""));
3496 fout = fopen(tmp, "w");
3497 if (fout == NULL) {
3498 log_msg(1, "Unable to open and write to %s\n", tmp);
3499 paranoid_free(tmp);
3500 paranoid_free(suffix);
3501 return (1);
3502 }
3503
3504 (void) fwrite((void *) &biggiestruct, 1, sizeof(biggiestruct), fout);
3505 paranoid_fclose(fout);
3506 length = totallength / optimal_set_size / 1024;
3507 log_msg(1, "Opening in %s; slicing it and writing to CD/tape",
3508 file_to_openin);
3509 if (!(fin = fopen(file_to_openin, "r"))) {
3510 log_OS_error("Unable to openin biggie_filename");
3511 sprintf(tmp, "Cannot archive bigfile '%s': not found",
3512 biggie_filename);
3513 log_to_screen(tmp);
3514 paranoid_free(tempblock);
3515 paranoid_free(tmp);
3516 paranoid_free(checksum_line);
3517 paranoid_free(command);
3518 return (1);
3519 }
3520 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
3521 res =
3522 move_files_to_stream(slice_fname(biggie_file_number, 0,
3523 bkpinfo->tmpdir, ""), NULL);
3524 } else {
3525 res =
3526 move_files_to_cd(slice_fname(biggie_file_number, 0,
3527 bkpinfo->tmpdir, ""), NULL);
3528 }
3529 i = bkpinfo->optimal_set_size / 256;
3530 for (slice_num = 1; !finished; slice_num++) {
3531 strcpy(curr_slice_fname_uncompressed,
3532 slice_fname(biggie_file_number, slice_num, bkpinfo->tmpdir,
3533 ""));
3534 strcpy(curr_slice_fname_compressed,
3535 slice_fname(biggie_file_number, slice_num, bkpinfo->tmpdir,
3536 suffix));
3537
3538 strcpy(tmp, percent_media_full_comment());
3539 update_progress_form(tmp);
3540 if (!(fout = fopen(curr_slice_fname_uncompressed, "w"))) {
3541 log_OS_error(curr_slice_fname_uncompressed);
3542 return (1);
3543 }
3544 if ((i == bkpinfo->optimal_set_size / 256)
3545 && (totalread < 1.1 * totallength)) {
3546 for (i = 0; i < bkpinfo->optimal_set_size / 256; i++) {
3547 blksize = fread(tempblock, 1, 256 * 1024, fin);
3548 if (blksize > 0) {
3549 totalread = totalread + blksize;
3550 (void) fwrite(tempblock, 1, blksize, fout);
3551 } else {
3552 break;
3553 }
3554 }
3555 } else {
3556 i = 0;
3557 }
3558 paranoid_fclose(fout);
3559 if (i > 0) // length_of_file (curr_slice_fname_uncompressed)
3560 {
3561 if (!does_file_exist(curr_slice_fname_uncompressed)) {
3562 log_msg(2,
3563 "Warning - '%s' doesn't exist. How can I compress slice?",
3564 curr_slice_fname_uncompressed);
3565 }
3566 if (should_I_compress_slices && bkpinfo->compression_level > 0) {
3567 sprintf(command, "%s -%d %s", bkpinfo->zip_exe,
3568 bkpinfo->compression_level,
3569 curr_slice_fname_uncompressed);
3570 log_msg(2, command);
3571 if ((res = system(command))) {
3572 log_OS_error(command);
3573 }
3574 // did_I_compress_slice = TRUE;
3575 } else {
3576 sprintf(command, "mv %s %s 2>> %s",
3577 curr_slice_fname_uncompressed,
3578 curr_slice_fname_compressed, MONDO_LOGFILE);
3579 res = 0; // don't do it :)
3580 // did_I_compress_slice = FALSE;
3581 }
3582 retval += res;
3583 if (res) {
3584 log_msg(2, "Failed to compress the slice");
3585 }
3586 if (bkpinfo->use_lzo
3587 && strcmp(curr_slice_fname_compressed,
3588 curr_slice_fname_uncompressed)) {
3589 unlink(curr_slice_fname_uncompressed);
3590 }
3591 if (res) {
3592 sprintf(tmp, "Problem with slice # %ld", slice_num);
3593 } else {
3594 sprintf(tmp,
3595 "%s - Bigfile #%ld, slice #%ld compressed OK ",
3596 biggie_filename, biggie_file_number + 1,
3597 slice_num);
3598 }
3599#ifndef _XWIN
3600 if (!g_text_mode) {
3601 newtDrawRootText(0, g_noof_rows - 2, tmp);
3602 newtRefresh();
3603 } else {
3604 log_msg(2, tmp);
3605 }
3606#else
3607 log_msg(2, tmp);
3608#endif
3609 strcpy(file_to_archive, curr_slice_fname_compressed);
3610 g_current_progress++;
3611 } else { /* if i==0 then ... */
3612
3613 finished = TRUE;
3614 strcpy(file_to_archive, curr_slice_fname_uncompressed);
3615 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
3616 break;
3617 }
3618 }
3619
3620 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
3621 register_in_tape_catalog(biggieslice, biggie_file_number,
3622 slice_num, file_to_archive);
3623 maintain_collection_of_recent_archives(bkpinfo->tmpdir,
3624 file_to_archive);
3625 res = move_files_to_stream(file_to_archive, NULL);
3626 } else {
3627 res = move_files_to_cd(file_to_archive, NULL);
3628 }
3629 retval += res;
3630 if (res) {
3631 sprintf(tmp,
3632 "Failed to add slice %ld of bigfile %ld to scratchdir",
3633 slice_num, biggie_file_number + 1);
3634 log_to_screen(tmp);
3635 fatal_error
3636 ("Hard disk full. You should have bought a bigger one.");
3637 }
3638 }
3639 paranoid_fclose(fin);
3640 sprintf(tmp, "Sliced bigfile #%ld", biggie_file_number + 1);
3641 if (retval) {
3642 strcat(tmp, "...FAILED");
3643 } else {
3644 strcat(tmp, "...OK!");
3645 }
3646 log_msg(1, tmp);
3647 paranoid_free(tempblock);
3648 paranoid_free(tmp);
3649 paranoid_free(checksum_line);
3650 paranoid_free(command);
3651 paranoid_free(curr_slice_fname_uncompressed);
3652 paranoid_free(curr_slice_fname_compressed);
3653 paranoid_free(file_to_archive);
3654 paranoid_free(suffix);
3655 return (retval);
3656}
3657
3658
3659
3660
3661
3662
3663/**
3664 * Remove the archives in @c d.
3665 * This could possibly include any of:
3666 * - all afioballs (compressed and not)
3667 * - all filelists
3668 * - all slices
3669 * - all checksums
3670 * - a zero filler file
3671 *
3672 * @param d The directory to wipe the archives from.
3673 * @ingroup utilityGroup
3674 */
3675void wipe_archives(char *d)
3676{
3677 /*@ buffers ********************************************* */
3678 char *tmp;
3679 char *dir;
3680
3681 malloc_string(tmp);
3682 malloc_string(dir);
3683 assert_string_is_neither_NULL_nor_zerolength(d);
3684
3685 sprintf(dir, "%s/archives", d);
3686 sprintf(tmp, "find %s -name '*.afio*' -exec rm -f '{}' \\;", dir);
3687 run_program_and_log_output(tmp, FALSE);
3688 sprintf(tmp, "find %s -name '*list.[0-9]*' -exec rm -f '{}' \\;", dir);
3689 run_program_and_log_output(tmp, FALSE);
3690 sprintf(tmp, "find %s -name 'slice*' -exec rm -f '{}' \\;", dir);
3691 run_program_and_log_output(tmp, FALSE);
3692 sprintf(tmp, "rm -f %s/cklist*", dir);
3693 run_program_and_log_output(tmp, FALSE);
3694 sprintf(tmp, "rm -f %s/zero", dir);
3695 run_program_and_log_output(tmp, FALSE);
3696 log_msg(1, "Wiped %s's archives", dir);
3697 sprintf(tmp, "ls -l %s", dir);
3698 run_program_and_log_output(tmp, FALSE);
3699 paranoid_free(tmp);
3700 paranoid_free(dir);
3701}
3702
3703
3704
3705/**
3706 * @addtogroup LLarchiveGroup
3707 * @{
3708 */
3709/**
3710 * Write the final ISO image.
3711 * @param bkpinfo The backup information structure. Used only
3712 * in the call to @c write_iso_and_go_on().
3713 * @return The number of errors encountered (0 for success)
3714 * @see write_iso_and_go_on
3715 * @see make_iso_fs
3716 * @bug The final ISO is written even if there are no files on it. In practice,
3717 * however, this occurs rarely.
3718 */
3719int write_final_iso_if_necessary()
3720{
3721 /*@ int ***************************************************** */
3722 int res;
3723
3724 /*@ buffers ************************************************** */
3725 char *tmp;
3726
3727 malloc_string(tmp);
3728 assert(bkpinfo != NULL);
3729
3730// I should really check if there are any slices or tarballs to be copied to CD-R(W)'s; the odds are approx. 1 in a million that there are no files here, so I'll just go ahead & make one more CD anyway
3731
3732 sprintf(tmp, "Writing the final ISO");
3733 log_msg(2, tmp);
3734 center_string(tmp, 80);
3735#ifndef _XWIN
3736 if (!g_text_mode) {
3737 newtPushHelpLine(tmp);
3738 }
3739#endif
3740 res = write_iso_and_go_on(TRUE);
3741#ifndef _XWIN
3742 if (!g_text_mode) {
3743 newtPopHelpLine();
3744 }
3745#endif
3746 log_msg(2, "Returning from writing final ISO (res=%d)", res);
3747 paranoid_free(tmp);
3748 return (res);
3749}
3750
3751
3752/**
3753 * Write an ISO image to <tt>[bkpinfo->isodir]/bkpinfo->prefix-[g_current_media_number].iso</tt>.
3754 * @param bkpinfo The backup information structure. Fields used:
3755 * - @c backup_media_type
3756 * - @c prefix
3757 * - @c isodir
3758 * - @c manual_cd_tray
3759 * - @c media_size
3760 * - @c nfs_mount
3761 * - @c nfs_remote_dir
3762 * - @c scratchdir
3763 * - @c verify_data
3764 *
3765 * @param last_cd If TRUE, this is the last CD to write; if FALSE, it's not.
3766 * @return The number of errors encountered (0 for success)
3767 * @see make_iso_fs
3768 */
3769int write_iso_and_go_on(bool last_cd)
3770{
3771 /*@ pointers **************************************************** */
3772 FILE *fout;
3773
3774 /*@ buffers ***************************************************** */
3775 char *tmp;
3776 char *cdno_fname;
3777 char *lastcd_fname;
3778 char *isofile;
3779
3780 /*@ bool ******************************************************** */
3781 bool that_one_was_ok;
3782 bool using_nfs;
3783 bool orig_vfy_flag_val;
3784
3785 /*@ int *********************************************************** */
3786 int res = 0;
3787
3788 malloc_string(tmp);
3789 malloc_string(cdno_fname);
3790 malloc_string(lastcd_fname);
3791 malloc_string(isofile);
3792
3793 assert(bkpinfo != NULL);
3794 orig_vfy_flag_val = bkpinfo->verify_data;
3795 if (bkpinfo->media_size[g_current_media_number] <= 0) {
3796 fatal_error("write_iso_and_go_on() - unknown media size");
3797 }
3798
3799 if (strlen(bkpinfo->nfs_mount) > 1) {
3800 using_nfs = TRUE;
3801 } else {
3802 using_nfs = FALSE;
3803 }
3804 log_msg(1, "OK, time to make %s #%d",
3805 media_descriptor_string(bkpinfo->backup_media_type),
3806 g_current_media_number);
3807
3808 /* label the ISO with its number */
3809
3810 sprintf(cdno_fname, "%s/archives/THIS-CD-NUMBER", bkpinfo->scratchdir);
3811 fout = fopen(cdno_fname, "w");
3812 fprintf(fout, "%d", g_current_media_number);
3813 paranoid_fclose(fout);
3814
3815 sprintf(tmp, "cp -f %s/autorun %s/", g_mondo_home,
3816 bkpinfo->scratchdir);
3817 if (run_program_and_log_output(tmp, FALSE)) {
3818 log_msg(2, "Warning - unable to copy autorun to scratchdir");
3819 }
3820
3821 /* last CD or not? Label accordingly */
3822 sprintf(lastcd_fname, "%s/archives/NOT-THE-LAST", bkpinfo->scratchdir);
3823 if (last_cd) {
3824 unlink(lastcd_fname);
3825 log_msg(2,
3826 "OK, you're telling me this is the last CD. Fair enough.");
3827 } else {
3828 fout = fopen(lastcd_fname, "w");
3829 fprintf(fout,
3830 "You're listening to 90.3 WPLN, Nashville Public Radio.\n");
3831 paranoid_fclose(fout);
3832 }
3833 if (space_occupied_by_cd(bkpinfo->scratchdir) / 1024 >
3834 bkpinfo->media_size[g_current_media_number]) {
3835 sprintf(tmp,
3836 "Warning! CD is too big. It occupies %ld KB, which is more than the %ld KB allowed.",
3837 (long) space_occupied_by_cd(bkpinfo->scratchdir),
3838 (long) bkpinfo->media_size[g_current_media_number]);
3839 log_to_screen(tmp);
3840 }
3841 sprintf(isofile, "%s/%s/%s-%d.iso", bkpinfo->isodir,
3842 bkpinfo->nfs_remote_dir, bkpinfo->prefix,
3843 g_current_media_number);
3844 for (that_one_was_ok = FALSE; !that_one_was_ok;) {
3845 res = make_iso_fs(isofile);
3846 if (g_current_media_number == 1 && !res
3847 && (bkpinfo->backup_media_type == cdr
3848 || bkpinfo->backup_media_type == cdrw)) {
3849 if (find_cdrom_device(tmp, FALSE)) // make sure find_cdrom_device() finds, records CD-R's loc
3850 {
3851 log_msg(3, "*Sigh* Mike, I hate your computer.");
3852 bkpinfo->manual_cd_tray = TRUE;
3853 } // if it can't be found then force pausing
3854 else {
3855 log_msg(3, "Great. Found Mike's CD-ROM drive.");
3856 }
3857 }
3858 if (bkpinfo->verify_data && !res) {
3859 log_to_screen
3860 ("Please reboot from the 1st %s in Compare Mode, as a precaution.",
3861 media_descriptor_string(g_backup_media_type));
3862 chdir("/");
3863 iamhere("Before calling verify_cd_image()");
3864 res += verify_cd_image();
3865 iamhere("After calling verify_cd_image()");
3866 }
3867 if (!res) {
3868 that_one_was_ok = TRUE;
3869 } else {
3870 sprintf(tmp, "Failed to burn %s #%d. Retry?",
3871 media_descriptor_string(bkpinfo->backup_media_type),
3872 g_current_media_number);
3873 res = ask_me_yes_or_no(tmp);
3874 if (!res) {
3875 if (ask_me_yes_or_no("Abort the backup?")) {
3876 fatal_error("FAILED TO BACKUP");
3877 } else {
3878 break;
3879 }
3880 } else {
3881 log_msg(2, "Retrying, at user's request...");
3882 res = 0;
3883 }
3884 }
3885 }
3886/*
3887 if (using_nfs)
3888 {
3889 sprintf(tmp,"mv -f %s %s/%s/", isofile, bkpinfo->isodir, bkpinfo->nfs_remote_dir);
3890 if (run_program_and_log_output(tmp, FALSE))
3891 { log_to_screen("Unable to move ISO to NFS dir"); }
3892 }
3893*/
3894 g_current_media_number++;
3895 if (g_current_media_number > MAX_NOOF_MEDIA) {
3896 fatal_error("Too many CD-R(W)'s. Use tape or net.");
3897 }
3898 wipe_archives(bkpinfo->scratchdir);
3899 sprintf(tmp, "rm -Rf %s/images/*gz %s/images/*data*img",
3900 bkpinfo->scratchdir, bkpinfo->scratchdir);
3901 if (system(tmp)) {
3902 log_msg
3903 (2,
3904 "Error occurred when I tried to delete the redundant IMGs and GZs");
3905 }
3906
3907 if (last_cd) {
3908 log_msg(2, "This was your last CD.");
3909 } else {
3910 log_msg(2, "Continuing to backup your data...");
3911 }
3912
3913 bkpinfo->verify_data = orig_vfy_flag_val;
3914 paranoid_free(tmp);
3915 paranoid_free(cdno_fname);
3916 paranoid_free(lastcd_fname);
3917 paranoid_free(isofile);
3918 return (0);
3919}
3920
3921/* @} - end of LLarchiveGroup */
3922
3923
3924
3925
3926/**
3927 * Verify the user's data.
3928 * @param bkpinfo The backup information structure. Fields used:
3929 * - @c backup_data
3930 * - @c backup_media_type
3931 * - @c media_device
3932 * - @c verify_data
3933 *
3934 * @return The number of errors encountered (0 for success)
3935 * @ingroup verifyGroup
3936 */
3937int verify_data()
3938{
3939 int res = 0, retval = 0, cdno = 0;
3940 char *tmp;
3941 long diffs = 0;
3942
3943 malloc_string(tmp);
3944 assert(bkpinfo != NULL);
3945 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
3946 chdir("/");
3947 mvaddstr_and_log_it(g_currentY, 0,
3948 "Verifying archives against live filesystem");
3949 if (bkpinfo->backup_media_type == cdstream) {
3950 strcpy(bkpinfo->media_device, "/dev/cdrom");
3951 }
3952 verify_tape_backups();
3953 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
3954 } else if (bkpinfo->backup_data)
3955 //bkpinfo->backup_media_type == cdrw || bkpinfo->backup_media_type == cdr))
3956 {
3957 log_msg(2,
3958 "Not verifying again. Per-CD/ISO verification already carried out.");
3959 sprintf(tmp, "cat %s/changed.files > %s/changed.files 2> /dev/null",bkpinfo->tmpdir, MINDI_CACHE);
3960 paranoid_system(tmp);
3961 } else {
3962 g_current_media_number = cdno;
3963 if (bkpinfo->backup_media_type != iso) {
3964 find_cdrom_device(bkpinfo->media_device, FALSE); // replace 0,0,0 with /dev/cdrom
3965 }
3966 chdir("/");
3967 for (cdno = 1; cdno < 99 && bkpinfo->verify_data; cdno++) {
3968 if (cdno != g_current_media_number) {
3969 log_msg(2,
3970 "Warning - had to change g_current_media_number from %d to %d",
3971 g_current_media_number, cdno);
3972 g_current_media_number = cdno;
3973 }
3974 if (bkpinfo->backup_media_type != iso) {
3975 insist_on_this_cd_number(cdno);
3976 }
3977 res = verify_cd_image(); // sets verify_data to FALSE if it's time to stop verifying
3978 retval += res;
3979 if (res) {
3980 sprintf(tmp,
3981 "Warnings/errors were reported while checking %s #%d",
3982 media_descriptor_string(bkpinfo->backup_media_type),
3983 g_current_media_number);
3984 log_to_screen(tmp);
3985
3986 }
3987 }
3988/*
3989 sprintf (tmp,
3990 "cat %s | grep \"afio: \" | cut -d'\"' -f2 | sort -u | awk '{print \"/\"$0;};' | tr -s '/' '/' | grep -vx \"/afio:.*\" > /tmp/changed.files",
3991 MONDO_LOGFILE);
3992 system (tmp);
3993*/
3994 sprintf(tmp,
3995 "grep 'afio: ' %s | sed 's/afio: //' | grep -vE '^/dev/.*$' >> %s/changed.files",
3996 MONDO_LOGFILE, MINDI_CACHE);
3997 system(tmp);
3998
3999 sprintf(tmp,
4000 "grep 'star: ' %s | sed 's/star: //' | grep -vE '^/dev/.*$' >> %s/changed.files",
4001 MONDO_LOGFILE, MINDI_CACHE);
4002 system(tmp);
4003 run_program_and_log_output("umount " MNT_CDROM, FALSE);
4004// if (bkpinfo->backup_media_type != iso && !bkpinfo->please_dont_eject_when_restoring)
4005//{
4006 eject_device(bkpinfo->media_device);
4007//}
4008 }
4009 sprintf(tmp, "%s/changed.files", MINDI_CACHE);
4010 diffs = count_lines_in_file(tmp);
4011
4012 if (diffs > 0) {
4013 if (retval == 0) {
4014 retval = (int) (-diffs);
4015 }
4016 }
4017 paranoid_free(tmp);
4018 return (retval);
4019}
4020
4021
4022
4023
4024
4025/**
4026 * @addtogroup utilityGroup
4027 * @{
4028 */
4029/**
4030 * Write an image to a real 3.5" floppy disk.
4031 * @param device The device to write to (e.g. @c /dev/fd0)
4032 * @param datafile The image to write to @p device.
4033 * @return The number of errors encountered (0 for success)
4034 * @see write_image_to_floppy
4035 */
4036int write_image_to_floppy_SUB(char *device, char *datafile)
4037{
4038 /*@ int *************************************************************** */
4039 int res = 0;
4040 int percentage = 0;
4041 int blockno = 0;
4042 int maxblocks = 0;
4043
4044 /*@ buffers************************************************************ */
4045 char *tmp;
4046 char blk[1024];
4047 char *title;
4048
4049 /*@ pointers ********************************************************** */
4050 char *p;
4051 FILE *fout, *fin;
4052
4053
4054 malloc_string(tmp);
4055 malloc_string(title);
4056 /* pretty stuff */
4057 if (!(p = strrchr(datafile, '/'))) {
4058 p = datafile;
4059 } else {
4060 p++;
4061 }
4062 sprintf(title, "Writing %s to floppy", p);
4063 open_evalcall_form(title);
4064 /* functional stuff */
4065 for (p = device + strlen(device); p != device && isdigit(*(p - 1));
4066 p--);
4067 maxblocks = atoi(p);
4068 if (!maxblocks) {
4069 maxblocks = 1440;
4070 }
4071 sprintf(tmp, "maxblocks = %d; p=%s", maxblocks, p);
4072 log_msg(2, tmp);
4073 /* copy data from image to floppy */
4074 if (!(fin = fopen(datafile, "r"))) {
4075 log_OS_error("Cannot open img");
4076 return (1);
4077 }
4078 if (!(fout = fopen(device, "w"))) {
4079 log_OS_error("Cannot open fdd");
4080 return (1);
4081 }
4082 for (blockno = 0; blockno < maxblocks; blockno++) {
4083 percentage = blockno * 100 / maxblocks;
4084 if (fread(blk, 1, 1024, fin) != 1024) {
4085 if (feof(fin)) {
4086 log_msg(1,
4087 "img read err - img ended prematurely - non-fatal error");
4088 sleep(3);
4089 return (res);
4090 }
4091 res++;
4092 log_to_screen("img read err");
4093 }
4094 if (fwrite(blk, 1, 1024, fout) != 1024) {
4095 res++;
4096 log_to_screen("fdd write err");
4097 }
4098 if (((blockno + 1) % 128) == 0) {
4099 paranoid_system("sync"); /* fflush doesn't work; dunno why */
4100 update_evalcall_form(percentage);
4101 }
4102 }
4103 paranoid_fclose(fin);
4104 paranoid_fclose(fout);
4105 paranoid_free(tmp);
4106 paranoid_free(title);
4107 close_evalcall_form();
4108 return (res);
4109}
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119/**
4120 * Wrapper around @c write_image_to_floppy_SUB().
4121 * This function, unlike @c write_image_to_floppy_SUB(),
4122 * gives the user the opportunity to retry if the write fails.
4123 * @see write_image_to_floppy_SUB
4124 */
4125int write_image_to_floppy(char *device, char *datafile)
4126{
4127 /*@ int ************************************************************** */
4128 int res = 0;
4129
4130 assert_string_is_neither_NULL_nor_zerolength(device);
4131 assert_string_is_neither_NULL_nor_zerolength(datafile);
4132
4133 while ((res = write_image_to_floppy_SUB(device, datafile))) {
4134 if (!ask_me_yes_or_no("Failed to write image to floppy. Retry?")) {
4135 return (res);
4136 }
4137 }
4138 return (res);
4139}
4140
4141/* @} - end of utilityGroup */
4142
4143void setenv_mondo_share(void) {
4144
4145setenv("MONDO_SHARE", MONDO_SHARE, 1);
4146}
Note: See TracBrowser for help on using the repository browser.