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

Last change on this file since 1878 was 1878, checked in by Bruno Cornec, 16 years ago

fix an issue in the kill procedure

  • Property svn:keywords set to Id
File size: 118.9 KB
Line 
1/* libmondo-archive.c
2 $Id: libmondo-archive.c 1878 2008-01-24 17:04:44Z 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 1878 2008-01-24 17:04:44Z 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#ifdef __IA64__
820 strcpy(bootdev, call_program_and_get_last_line_of_output
821 ("mount | grep ' /boot/efi ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
822#else
823 strcpy(bootdev, call_program_and_get_last_line_of_output
824 ("mount | grep ' /boot ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
825#endif
826 if (strstr(bootdev, "/dev/cciss/")) {
827#ifdef __IA64__
828 strcpy(bootdev, call_program_and_get_last_line_of_output
829 ("mount | grep ' /boot/efi ' | head -1 | cut -d' ' -f1 | cut -dp -f1"));
830#else
831 strcpy(bootdev, call_program_and_get_last_line_of_output
832 ("mount | grep ' /boot ' | head -1 | cut -d' ' -f1 | cut -dp -f1"));
833#endif
834 }
835 if (!bootdev[0]) {
836 strcpy(bootdev, call_program_and_get_last_line_of_output
837 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
838 if (strstr(bootdev, "/dev/cciss/")) {
839 strcpy(bootdev, call_program_and_get_last_line_of_output
840 ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | cut -dp -f1"));
841 }
842 }
843#endif
844 if (bootdev[0])
845 ch = which_boot_loader(bootdev);
846 else
847 ch = 'U';
848 if (bkpinfo->boot_loader != '\0') {
849 sprintf(tmp, "User specified boot loader. It is '%c'.",
850 bkpinfo->boot_loader);
851 log_msg(2, tmp);
852 } else {
853 bkpinfo->boot_loader = ch;
854 }
855 if (bkpinfo->boot_device[0] != '\0') {
856 sprintf(tmp, "User specified boot device. It is '%s'.",
857 bkpinfo->boot_device);
858 log_msg(2, tmp);
859 } else {
860 strcpy(bkpinfo->boot_device, bootdev);
861 }
862 }
863
864 if (
865#ifdef __FreeBSD__
866 bkpinfo->boot_loader != 'B' && bkpinfo->boot_loader != 'D' &&
867#endif
868#ifdef __IA64__
869 bkpinfo->boot_loader != 'E' &&
870#endif
871 bkpinfo->boot_loader != 'L' && bkpinfo->boot_loader != 'G'
872 && bkpinfo->boot_loader != 'R'
873 && !bkpinfo->nonbootable_backup) {
874 fatal_error
875 ("Please specify your boot loader and device, e.g. -l GRUB -f /dev/hda. Type 'man mondoarchive' to read the manual.");
876 }
877 if (bkpinfo->boot_loader == 'L') {
878 strcpy(bootldr_str, "LILO");
879 if (!does_file_exist("/etc/lilo.conf")) {
880 fatal_error
881 ("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?");
882 }
883 } else if (bkpinfo->boot_loader == 'G') {
884 strcpy(bootldr_str, "GRUB");
885 if (!does_file_exist("/etc/grub.conf")
886 && does_file_exist("/boot/grub/grub.conf")) {
887 run_program_and_log_output
888 ("ln -sf /boot/grub/grub.conf /etc/grub.conf", 5);
889 }
890 /* Detect Debian's grub config file */
891 else if (!does_file_exist("/etc/grub.conf")
892 && does_file_exist("/boot/grub/menu.lst")) {
893 run_program_and_log_output
894 ("ln -s /boot/grub/menu.lst /etc/grub.conf", 5);
895 }
896 if (!does_file_exist("/etc/grub.conf")) {
897 fatal_error
898 ("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'...");
899 }
900 } else if (bkpinfo->boot_loader == 'E') {
901 strcpy(bootldr_str, "ELILO");
902 /* BCO: fix it for Debian, Mandrake, ... */
903 if (!does_file_exist("/etc/elilo.conf")
904 && does_file_exist("/boot/efi/efi/redhat/elilo.conf")) {
905 run_program_and_log_output
906 ("ln -sf /boot/efi/efi/redhat/elilo.conf /etc/elilo.conf",
907 5);
908 }
909 if (!does_file_exist("/etc/elilo.conf")
910 && does_file_exist("/boot/efi/efi/SuSE/elilo.conf")) {
911 run_program_and_log_output
912 ("ln -sf /boot/efi/efi/SuSE/elilo.conf /etc/elilo.conf",
913 5);
914 }
915 if (!does_file_exist("/etc/elilo.conf")
916 && does_file_exist("/boot/efi/efi/debian/elilo.conf")) {
917 run_program_and_log_output
918 ("ln -sf /boot/efi/efi/debian/elilo.conf /etc/elilo.conf",
919 5);
920 }
921 if (!does_file_exist("/etc/elilo.conf")
922 && does_file_exist("/boot/efi/debian/elilo.conf")) {
923 run_program_and_log_output
924 ("ln -sf /boot/efi/debian/elilo.conf /etc/elilo.conf",
925 5);
926 }
927 if (!does_file_exist("/etc/elilo.conf")) {
928 fatal_error
929 ("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'");
930 }
931 } else if (bkpinfo->boot_loader == 'R') {
932 strcpy(bootldr_str, "RAW");
933 }
934#ifdef __FreeBSD__
935 else if (bkpinfo->boot_loader == 'D') {
936 strcpy(bootldr_str, "DD");
937 }
938
939 else if (bkpinfo->boot_loader == 'B') {
940 strcpy(bootldr_str, "BOOT0");
941 }
942#endif
943 else {
944 strcpy(bootldr_str, "unknown");
945 }
946 sprintf(tmp, "Your boot loader is %s and it boots from %s",
947 bootldr_str, bkpinfo->boot_device);
948 log_to_screen(tmp);
949 sprintf(tmp, "%s/BOOTLOADER.DEVICE", bkpinfo->tmpdir);
950 if (write_one_liner_data_file(tmp, bkpinfo->boot_device)) {
951 log_msg(1, "%ld: Unable to write one-liner boot device", __LINE__);
952 }
953 switch (bkpinfo->backup_media_type) {
954 case cdr:
955 strcpy(value, "cdr");
956 break;
957 case cdrw:
958 strcpy(value, "cdrw");
959 break;
960 case cdstream:
961 strcpy(value, "cdstream");
962 break;
963 case tape:
964 strcpy(value, "tape");
965 break;
966 case udev:
967 strcpy(value, "udev");
968 break;
969 case iso:
970 strcpy(value, "iso");
971 break;
972 case nfs:
973 strcpy(value, "nfs");
974 break;
975 case dvd:
976 strcpy(value, "dvd");
977 break;
978 case usb:
979 strcpy(value, "usb");
980 break;
981 default:
982 fatal_error("Unknown backup_media_type");
983 }
984 if (bkpinfo->backup_media_type == usb) {
985 asprintf(&tmp2, "--usb %s", bkpinfo->media_device);
986 } else {
987 asprintf(&tmp2," ");
988 }
989
990 sprintf(tmp, "%s/BACKUP-MEDIA-TYPE", bkpinfo->tmpdir);
991 if (write_one_liner_data_file(tmp, value)) {
992 res++;
993 log_msg(1, "%ld: Unable to write one-liner backup-media-type",
994 __LINE__);
995 }
996 sprintf(tmp, "%s/BOOTLOADER.NAME", bkpinfo->tmpdir);
997 if (write_one_liner_data_file(tmp, bootldr_str)) {
998 res++;
999 log_msg(1, "%ld: Unable to write one-liner bootloader.name",
1000 __LINE__);
1001 }
1002 sprintf(tmp, "%s/DIFFERENTIAL", bkpinfo->tmpdir);
1003 if (bkpinfo->differential) {
1004 res += write_one_liner_data_file(tmp, "1");
1005 } else {
1006 res += write_one_liner_data_file(tmp, "0");
1007 }
1008
1009 if (g_getfattr) {
1010 asprintf(&tmp1, "%s/XATTR", bkpinfo->tmpdir);
1011 if (write_one_liner_data_file(tmp1, "TRUE")) {
1012 log_msg(1, "%ld: Unable to write one-liner XATTR",
1013 __LINE__);
1014 }
1015 paranoid_free(tmp1);
1016 }
1017 if (g_getfacl) {
1018 asprintf(&tmp1, "%s/ACL", bkpinfo->tmpdir);
1019 if (write_one_liner_data_file(tmp1, "TRUE")) {
1020 log_msg(1, "%ld: Unable to write one-liner ACL",
1021 __LINE__);
1022 }
1023 paranoid_free(tmp1);
1024 }
1025
1026 estimated_total_noof_slices =
1027 size_of_all_biggiefiles_K(bkpinfo) / bkpinfo->optimal_set_size + 1;
1028/* add nfs stuff here? */
1029 sprintf(command, "mkdir -p %s/images", bkpinfo->scratchdir);
1030 if (system(command)) {
1031 res++;
1032 log_OS_error("Unable to make images directory");
1033 }
1034 sprintf(command, "mkdir -p %s%s", bkpinfo->scratchdir, MNT_FLOPPY);
1035 if (system(command)) {
1036 res++;
1037 log_OS_error("Unable to make mnt floppy directory");
1038 }
1039 sprintf(tmp, "BTW, I'm telling Mindi your kernel is '%s'",
1040 bkpinfo->kernel_path);
1041
1042 log_msg(1, "lines_in_filelist = %ld", lines_in_filelist);
1043
1044 sprintf(command,
1045/* "mindi --custom 2=%s 3=%s/images 4=\"%s\" 5=\"%s\" \
10466=\"%s\" 7=%ld 8=\"%s\" 9=\"%s\" 10=\"%s\" \
104711=\"%s\" 12=%s 13=%ld 14=\"%s\" 15=\"%s\" 16=\"%s\" 17=\"%s\" 18=%ld 19=%d",*/
1048 "mindi %s --custom %s %s/images '%s' '%s' \
1049'%s' %ld '%s' '%s' '%s' \
1050'%s' %s %ld '%s' '%s' '%s' '%s' %ld %d '%s'", tmp2, bkpinfo->tmpdir, // parameter #2
1051 bkpinfo->scratchdir, // parameter #3
1052 bkpinfo->kernel_path, // parameter #4
1053 tape_device, // parameter #5
1054 tape_size_sz, // parameter #6
1055 lines_in_filelist, // parameter #7 (INT)
1056 use_lzo_sz, // parameter #8
1057 cd_recovery_sz, // parameter #9
1058 bkpinfo->image_devs, // parameter #10
1059 broken_bios_sz, // parameter #11
1060 last_filelist_number, // parameter #12 (STRING)
1061 estimated_total_noof_slices, // parameter #13 (INT)
1062 devs_to_exclude, // parameter #14
1063 use_comp_sz, // parameter #15
1064 use_lilo_sz, // parameter #16
1065 use_star_sz, // parameter #17
1066 bkpinfo->internal_tape_block_size, // parameter #18 (LONG)
1067 bkpinfo->differential, // parameter #19 (INT)
1068 use_gzip_sz); // parameter #20 (STRING)
1069
1070// Watch it! This next line adds a parameter...
1071 if (bkpinfo->nonbootable_backup) {
1072 strcat(command, " NONBOOTABLE");
1073 }
1074 log_msg(2, command);
1075
1076// popup_and_OK("Pausing");
1077
1078 res =
1079 run_program_and_log_to_screen(command,
1080 "Generating boot+data disks");
1081 if (bkpinfo->nonbootable_backup) {
1082 res = 0;
1083 } // hack
1084 if (!res) {
1085 log_to_screen("Boot+data disks were created OK");
1086 sprintf(command, "cp -f %s/images/mindi.iso %s/mondorescue.iso",
1087 bkpinfo->scratchdir, MINDI_CACHE);
1088 log_msg(2, command);
1089 run_program_and_log_output(command, FALSE);
1090 if (bkpinfo->nonbootable_backup) {
1091 sprintf(command, "cp -f %s/all.tar.gz %s/images",
1092 bkpinfo->tmpdir, bkpinfo->scratchdir);
1093 if (system(command)) {
1094 fatal_error("Unable to create temporary all tarball");
1095 }
1096 }
1097 /* For USB we already have everything on the key */
1098 if (bkpinfo->backup_media_type == usb) {
1099 sprintf(command, "rm -rf %s/images", bkpinfo->scratchdir);
1100 run_program_and_log_output(command, FALSE);
1101 } else {
1102 sprintf(command, "cp -f %s/mindi-*oot*.img %s/images",
1103 bkpinfo->tmpdir, bkpinfo->scratchdir);
1104 if (system(command)) {
1105 log_msg(2, "Unable to copy mindi images");
1106 }
1107 sprintf(tmp, "cp -f %s/images/all.tar.gz %s", bkpinfo->scratchdir,
1108 bkpinfo->tmpdir);
1109 if (system(tmp)) {
1110 fatal_error("Cannot find all.tar.gz in tmpdir");
1111 }
1112 }
1113 if (res) {
1114 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
1115 } else {
1116 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1117 }
1118 } else {
1119 log_to_screen("Mindi failed to create your boot+data disks.");
1120 strcpy(command, "grep 'Fatal error' /var/log/mindi.log");
1121 strcpy(tmp, call_program_and_get_last_line_of_output(command));
1122 if (strlen(tmp) > 1) {
1123 popup_and_OK(tmp);
1124 }
1125 }
1126 paranoid_free(tmp);
1127 paranoid_free(use_lzo_sz);
1128 paranoid_free(use_gzip_sz);
1129 paranoid_free(scratchdir);
1130 paranoid_free(use_comp_sz);
1131 paranoid_free(bootldr_str);
1132 paranoid_free(tape_device);
1133 paranoid_free(last_filelist_number);
1134 paranoid_free(broken_bios_sz);
1135 paranoid_free(cd_recovery_sz);
1136 paranoid_free(tape_size_sz);
1137 paranoid_free(devs_to_exclude);
1138 paranoid_free(use_lilo_sz);
1139 paranoid_free(value);
1140 paranoid_free(bootdev);
1141 paranoid_free(command);
1142 paranoid_free(use_star_sz);
1143 return (res);
1144}
1145
1146
1147
1148/**
1149 * Maximum number of filesets allowed in this function.
1150 */
1151#define MAX_NOOF_SETS_HERE 32767
1152
1153/**
1154 * Offset of the bkpinfo pointer (in bytes) from the
1155 * buffer passed to create_afio_files_in_background.
1156 */
1157#define BKPINFO_LOC_OFFSET (16+MAX_NOOF_SETS_HERE/8+16)
1158
1159/**
1160 * Main function for each @c afio thread.
1161 * @param inbuf A transfer block containing:
1162 * - @c p_last_set_archived: [offset 0] pointer to an @c int
1163 * containing the last set archived.
1164 * - @c p_archival_threads_running: [offset 4] pointer to an @c int
1165 * containing the number of archival threads currently running.
1166 * - @c p_next_set_to_archive: [offset 8] pointer to an @c int containing
1167 * the next set that should be archived.
1168 * - @c p_list_of_fileset_flags: [offset 12] @c char pointer pointing to a
1169 * bit array, where each bit corresponds to a filelist (1=needs
1170 * to be archived, 0=archived).
1171 * - @c bkpinfo: [offset BKPINFO_LOC_OFFSET] pointer to backup information
1172 * structure. Fields used:
1173 * - @c tmpdir
1174 * - @c zip_suffix
1175 *
1176 * Any of the above may be modified by the caller at any time.
1177 *
1178 * @bug Assumes @c int pointers are 4 bytes.
1179 * @see archive_this_fileset
1180 * @see make_afioballs_and_images
1181 * @return NULL, always.
1182 * @ingroup LLarchiveGroup
1183 */
1184void *create_afio_files_in_background(void *inbuf)
1185{
1186 long int archiving_set_no;
1187 char *archiving_filelist_fname;
1188 char *archiving_afioball_fname;
1189 char *curr_xattr_list_fname = NULL;
1190 char *curr_acl_list_fname;
1191
1192 struct s_bkpinfo *bkpinfo_bis;
1193 char *tmp;
1194 int res = 0, retval = 0;
1195 int *p_archival_threads_running;
1196 int *p_last_set_archived;
1197 int *p_next_set_to_archive;
1198 char *p_list_of_fileset_flags;
1199 int this_thread_no = g_current_thread_no++;
1200
1201 malloc_string(curr_xattr_list_fname);
1202 malloc_string(curr_acl_list_fname);
1203 malloc_string(archiving_filelist_fname);
1204 malloc_string(archiving_afioball_fname);
1205 malloc_string(tmp);
1206 p_last_set_archived = (int *) inbuf;
1207 p_archival_threads_running = (int *) (inbuf + 4);
1208 p_next_set_to_archive = (int *) (inbuf + 8);
1209 p_list_of_fileset_flags = (char *) (inbuf + 12);
1210 bkpinfo_bis = (struct s_bkpinfo *) (inbuf + BKPINFO_LOC_OFFSET);
1211
1212 sprintf(archiving_filelist_fname, FILELIST_FNAME_RAW_SZ,
1213 bkpinfo->tmpdir, 0L);
1214 for (archiving_set_no = 0; does_file_exist(archiving_filelist_fname);
1215 sprintf(archiving_filelist_fname, FILELIST_FNAME_RAW_SZ,
1216 bkpinfo->tmpdir, archiving_set_no)) {
1217 if (g_exiting) {
1218 fatal_error("Execution run aborted (pthread)");
1219 }
1220 if (archiving_set_no >= MAX_NOOF_SETS_HERE) {
1221 fatal_error
1222 ("Maximum number of filesets exceeded. Adjust MAX_NOOF_SETS_HERE, please.");
1223 }
1224 if (!semaphore_p()) {
1225 log_msg(3, "P sem failed (pid=%d)", (int) getpid());
1226 fatal_error("Cannot get semaphore P");
1227 }
1228 if (archiving_set_no < *p_next_set_to_archive) {
1229 archiving_set_no = *p_next_set_to_archive;
1230 }
1231 *p_next_set_to_archive = *p_next_set_to_archive + 1;
1232 if (!semaphore_v()) {
1233 fatal_error("Cannot get semaphore V");
1234 }
1235
1236 /* backup this set of files */
1237 sprintf(archiving_afioball_fname, AFIOBALL_FNAME_RAW_SZ,
1238 bkpinfo->tmpdir, archiving_set_no, bkpinfo->zip_suffix);
1239 sprintf(archiving_filelist_fname, FILELIST_FNAME_RAW_SZ,
1240 bkpinfo->tmpdir, archiving_set_no);
1241 if (!does_file_exist(archiving_filelist_fname)) {
1242 log_msg(3,
1243 "%s[%d:%d] - well, I would archive %d, except that it doesn't exist. I'll stop now.",
1244 FORTY_SPACES, getpid(), this_thread_no,
1245 archiving_set_no);
1246 break;
1247 }
1248
1249 sprintf(tmp, AFIOBALL_FNAME_RAW_SZ, bkpinfo->tmpdir,
1250 archiving_set_no - ARCH_BUFFER_NUM, bkpinfo->zip_suffix);
1251 if (does_file_exist(tmp)) {
1252 log_msg(4, "%s[%d:%d] - waiting for storer", FORTY_SPACES,
1253 getpid(), this_thread_no);
1254 while (does_file_exist(tmp)) {
1255 sleep(1);
1256 }
1257 log_msg(4, "[%d] - continuing", getpid());
1258 }
1259
1260 log_msg(4, "%s[%d:%d] - EXATing %d...", FORTY_SPACES, getpid(),
1261 this_thread_no, archiving_set_no);
1262 if (g_getfattr) {
1263 sprintf(curr_xattr_list_fname, XATTR_LIST_FNAME_RAW_SZ,
1264 bkpinfo->tmpdir, archiving_set_no);
1265 get_fattr_list(archiving_filelist_fname, curr_xattr_list_fname);
1266 }
1267 if (g_getfacl) {
1268 sprintf(curr_acl_list_fname, ACL_LIST_FNAME_RAW_SZ,
1269 bkpinfo->tmpdir, archiving_set_no);
1270 get_acl_list(archiving_filelist_fname, curr_acl_list_fname);
1271 }
1272
1273 log_msg(4, "%s[%d:%d] - archiving %d...", FORTY_SPACES, getpid(),
1274 this_thread_no, archiving_set_no);
1275 res =
1276 archive_this_fileset(archiving_filelist_fname,
1277 archiving_afioball_fname,
1278 archiving_set_no);
1279 retval += res;
1280
1281 if (res) {
1282 sprintf(tmp,
1283 "Errors occurred while archiving set %ld. Please review logs.",
1284 archiving_set_no);
1285 log_to_screen(tmp);
1286 }
1287 if (!semaphore_p()) {
1288 fatal_error("Cannot get semaphore P");
1289 }
1290
1291 set_bit_N_of_array(p_list_of_fileset_flags, archiving_set_no, 5);
1292
1293 if (*p_last_set_archived < archiving_set_no) {
1294 *p_last_set_archived = archiving_set_no;
1295 } // finished archiving this one
1296
1297 if (!semaphore_v()) {
1298 fatal_error("Cannot get semaphore V");
1299 }
1300 log_msg(4, "%s[%d:%d] - archived %d OK", FORTY_SPACES, getpid(),
1301 this_thread_no, archiving_set_no);
1302 archiving_set_no++;
1303 }
1304 if (!semaphore_p()) {
1305 fatal_error("Cannot get semaphore P");
1306 }
1307 (*p_archival_threads_running)--;
1308 if (!semaphore_v()) {
1309 fatal_error("Cannot get semaphore V");
1310 }
1311 log_msg(3, "%s[%d:%d] - exiting", FORTY_SPACES, getpid(),
1312 this_thread_no);
1313 paranoid_free(archiving_filelist_fname);
1314 paranoid_free(archiving_afioball_fname);
1315 paranoid_free(curr_xattr_list_fname);
1316 paranoid_free(curr_acl_list_fname);
1317 paranoid_free(tmp);
1318 pthread_exit(NULL);
1319}
1320
1321
1322
1323
1324
1325/**
1326 * Finalize the backup.
1327 * For streaming backups, this writes the closing block
1328 * to the stream. For CD-based backups, this creates
1329 * the final ISO image.
1330 * @param bkpinfo The backup information structure, used only
1331 * for the @c backup_media_type.
1332 * @ingroup MLarchiveGroup
1333 */
1334int do_that_final_phase()
1335{
1336
1337 /*@ int ************************************** */
1338 int res = 0;
1339 int retval = 0;
1340
1341 /*@ buffers ********************************** */
1342
1343 assert(bkpinfo != NULL);
1344 mvaddstr_and_log_it(g_currentY, 0,
1345 "Writing any remaining data to media ");
1346
1347 log_msg(1, "Closing tape/CD/USB ... ");
1348 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1349 /* write tape/cdstream */
1350 closeout_tape();
1351 } else {
1352 /* write final ISO/USB */
1353 res = write_final_iso_if_necessary();
1354 retval += res;
1355 if (res) {
1356 log_msg(1, "write_final_iso_if_necessary returned an error");
1357 }
1358 }
1359 log_msg(2, "Fork is exiting ... ");
1360
1361 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1362
1363 /* final stuff */
1364 if (retval) {
1365 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
1366 } else {
1367 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1368 }
1369
1370 return (retval);
1371}
1372
1373
1374/**
1375 * Initialize the backup.
1376 * Does the following:
1377 * - Sets up the serial number.
1378 * - For streaming backups, opens the tape stream and writes the data disks
1379 * and backup headers.
1380 * - For CD-based backups, wipes the ISOs in the target directory.
1381 *
1382 * @param bkpinfo The backup information structure. Fields used:
1383 * - @c backup_media_type
1384 * - @c cdrw_speed
1385 * - @c prefix
1386 * - @c isodir
1387 * - @c media_device
1388 * - @c scratchdir
1389 * - @c tmpdir
1390 * @return The number of errors encountered (0 for success).
1391 * @ingroup MLarchiveGroup
1392 */
1393int do_that_initial_phase()
1394{
1395 /*@ int *************************************** */
1396 int retval = 0;
1397
1398 /*@ buffers *********************************** */
1399 char *command, *tmpfile, *data_disks_file;
1400
1401 assert(bkpinfo != NULL);
1402 malloc_string(command);
1403 malloc_string(tmpfile);
1404 malloc_string(data_disks_file);
1405 sprintf(data_disks_file, "%s/all.tar.gz", bkpinfo->tmpdir);
1406
1407 snprintf(g_serial_string, MAX_STR_LEN - 1,
1408 call_program_and_get_last_line_of_output("dd \
1409if=/dev/urandom bs=16 count=1 2> /dev/null | \
1410hexdump | tr -s ' ' '0' | head -n1"));
1411 strip_spaces(g_serial_string);
1412 strcat(g_serial_string, "...word.");
1413 log_msg(2, "g_serial_string = '%s'", g_serial_string);
1414 assert(strlen(g_serial_string) < MAX_STR_LEN);
1415
1416 sprintf(tmpfile, "%s/archives/SERIAL-STRING", bkpinfo->scratchdir);
1417 if (write_one_liner_data_file(tmpfile, g_serial_string)) {
1418 log_msg(1, "%ld: Failed to write serial string", __LINE__);
1419 }
1420
1421 mvaddstr_and_log_it(g_currentY, 0, "Preparing to archive your data");
1422 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1423 if (bkpinfo->backup_media_type == cdstream) {
1424 openout_cdstream(bkpinfo->media_device, bkpinfo->cdrw_speed);
1425 } else {
1426 openout_tape(bkpinfo->media_device, bkpinfo->internal_tape_block_size); /* sets g_tape_stream */
1427 }
1428 if (!g_tape_stream) {
1429 fatal_error("Cannot open backup (streaming) device");
1430 }
1431 log_msg(1, "Backup (stream) opened OK");
1432 write_data_disks_to_stream(data_disks_file);
1433 } else {
1434 if (bkpinfo->backup_media_type == usb) {
1435 log_msg(1, "Backing up to USB's");
1436 } else {
1437 log_msg(1, "Backing up to CD's");
1438 }
1439 }
1440
1441 sprintf(command, "rm -f %s/%s/%s-[1-9]*.iso", bkpinfo->isodir,
1442 bkpinfo->nfs_remote_dir, bkpinfo->prefix);
1443 paranoid_system(command);
1444 wipe_archives(bkpinfo->scratchdir);
1445 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1446 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1447 write_header_block_to_stream((off_t)0, "start-of-tape",
1448 BLK_START_OF_TAPE);
1449 write_header_block_to_stream((off_t)0, "start-of-backup",
1450 BLK_START_OF_BACKUP);
1451 }
1452 paranoid_free(command);
1453 paranoid_free(tmpfile);
1454 paranoid_free(data_disks_file);
1455 return (retval);
1456}
1457
1458
1459/**
1460 * Calls floppy-formatting @c cmd and tracks its progress if possible.
1461 *
1462 * @param cmd The command to run (e.g. @c fdformat @c /dev/fd0).
1463 * @param title The human-friendly description of the floppy you are writing.
1464 * This will be used as the title in the progress bar window. Example:
1465 * "Formatting disk /dev/fd0".
1466 * @see format_disk
1467 * @return The exit code of fdformat/superformat.
1468 */
1469int format_disk_SUB(char *cmd, char *title)
1470{
1471
1472 /*@ int *************************************************************** */
1473 int res = 0;
1474 int percentage = 0;
1475 int maxtracks = 0;
1476 int trackno = 0;
1477 int last_trkno = 0;
1478
1479 /*@ buffers *********************************************************** */
1480 char *command;
1481 char *tempfile;
1482
1483 /*@ pointers ********************************************************** */
1484 FILE *pin;
1485
1486 assert_string_is_neither_NULL_nor_zerolength(cmd);
1487 assert_string_is_neither_NULL_nor_zerolength(title);
1488
1489 malloc_string(command);
1490 malloc_string(tempfile);
1491#ifdef __FreeBSD__
1492/* Ugh. FreeBSD fdformat prints out this pretty progress indicator that's
1493 impossible to parse. It looks like
1494 VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVF-------------------
1495 where V means verified, E means error, F means formatted, and - means
1496 not done yet.
1497*/
1498 return (run_program_and_log_to_screen(cmd, title));
1499#endif
1500
1501/* if Debian then do bog-standard superformat; don't be pretty */
1502 if (strstr(cmd, "superformat")) {
1503 return (run_program_and_log_to_screen(cmd, title));
1504 }
1505/* if not Debian then go ahead & use fdformat */
1506 sprintf(tempfile, "%s/mondo.floppy.log", bkpinfo->tmpdir);
1507 sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
1508 tempfile);
1509 log_msg(3, command);
1510 open_evalcall_form(title);
1511 if (!(pin = popen(command, "r"))) {
1512 log_OS_error("fmt err");
1513 return (1);
1514 }
1515 if (strstr(command, "1722")) {
1516 maxtracks = 82;
1517 } else {
1518 maxtracks = 80;
1519 }
1520 for (sleep(1); does_file_exist(tempfile); sleep(1)) {
1521 trackno = get_trackno_from_logfile(tempfile);
1522 if (trackno < 0 || trackno > 80) {
1523 log_msg(1, "Weird track#");
1524 continue;
1525 }
1526 percentage = trackno * 100 / maxtracks;
1527 if (trackno <= 5 && last_trkno > 40) {
1528 close_evalcall_form();
1529 strcpy(title, "Verifying format");
1530 open_evalcall_form(title);
1531 }
1532 last_trkno = trackno;
1533 update_evalcall_form(percentage);
1534 }
1535 close_evalcall_form();
1536 if (pclose(pin)) {
1537 res++;
1538 log_OS_error("Unable to pclose");
1539 }
1540 unlink(tempfile);
1541 paranoid_free(command);
1542 paranoid_free(tempfile);
1543 return (res);
1544}
1545
1546/**
1547 * Wrapper around @c format_disk_SUB().
1548 * This function calls @c format_disk_SUB() with a @c device of its @c device
1549 * parameter and a @c title of Formatting disk @c device. If the format
1550 * fails, the user will be given the option of retrying.
1551 *
1552 * @param device The floppy drive to write to.
1553 * @see format_disk_SUB
1554 * @return The exit code of fdformat/superformat.
1555 * @ingroup deviceGroup
1556 */
1557int format_disk(char *device)
1558{
1559
1560 /*@ int ************************************************************** */
1561 int res = 0;
1562
1563 /*@ buffer *********************************************************** */
1564 char *command;
1565 char *title;
1566
1567
1568 assert_string_is_neither_NULL_nor_zerolength(device);
1569 malloc_string(title);
1570 command = malloc(1000);
1571 if (!system("which superformat > /dev/null 2> /dev/null")) {
1572 sprintf(command, "superformat %s", device);
1573 } else {
1574#ifdef __FreeBSD__
1575 sprintf(command, "fdformat -y %s", device);
1576#else
1577 sprintf(command, "fdformat %s", device);
1578#endif
1579 }
1580 sprintf(title, "Formatting disk %s", device);
1581 while ((res = format_disk_SUB(command, title))) {
1582 if (!ask_me_yes_or_no("Failed to format disk. Retry?")) {
1583 return (res);
1584 }
1585 }
1586 paranoid_free(title);
1587 paranoid_free(command);
1588 return (res);
1589}
1590
1591/**
1592 * Get the <tt>N</tt>th bit of @c array.
1593 * @param array The bit-array (as a @c char pointer).
1594 * @param N The number of the bit you want.
1595 * @return TRUE (bit is set) or FALSE (bit is not set).
1596 * @see set_bit_N_of_array
1597 * @ingroup utilityGroup
1598 */
1599bool get_bit_N_of_array(char *array, int N)
1600{
1601 int element_number;
1602 int bit_number;
1603 int mask;
1604
1605 element_number = N / 8;
1606 bit_number = N % 8;
1607 mask = 1 << bit_number;
1608 if (array[element_number] & mask) {
1609 return (TRUE);
1610 } else {
1611 return (FALSE);
1612 }
1613}
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623/**
1624 * @addtogroup LLarchiveGroup
1625 * @{
1626 */
1627/**
1628 * Start up threads to archive your files.
1629 *
1630 * This function starts @c ARCH_THREADS threads,
1631 * each starting execution in @c create_afio_files_in_background().
1632 * Each thread will archive individual filesets, based on the
1633 * pointers passed to it and continually updated, until all files
1634 * have been backed up. This function coordinates the threads
1635 * and copies their output to the @c scratchdir.
1636 *
1637 * @param bkpinfo The backup information structure. Fields used:
1638 * - @c backup_media_type
1639 * - @c scratchdir
1640 * - @c tmpdir
1641 * - @c zip_suffix
1642 *
1643 * @return The number of errors encountered (0 for success)
1644 */
1645int make_afioballs_and_images()
1646{
1647
1648 /*@ int ************************************************** */
1649 int retval = 0;
1650 long int storing_set_no = 0;
1651 int res = 0;
1652 bool done_storing = FALSE;
1653 char *result_str;
1654 char *transfer_block;
1655 void *vp;
1656 void **pvp;
1657
1658 /*@ buffers ********************************************** */
1659 char *storing_filelist_fname;
1660 char *storing_afioball_fname;
1661 char *tmp;
1662 char *media_usage_comment;
1663 pthread_t archival_thread[ARCH_THREADS];
1664 char *p_list_of_fileset_flags;
1665 int *p_archival_threads_running;
1666 int *p_last_set_archived;
1667 int *p_next_set_to_archive;
1668 int noof_threads;
1669 int i;
1670 char *curr_xattr_list_fname = NULL;
1671 char *curr_acl_list_fname;
1672 int misc_counter_that_is_not_important = 0;
1673
1674 log_msg(8, "here");
1675 assert(bkpinfo != NULL);
1676 tmp = malloc(MAX_STR_LEN * 2);
1677 malloc_string(result_str);
1678 malloc_string(curr_xattr_list_fname);
1679 malloc_string(curr_acl_list_fname);
1680 malloc_string(storing_filelist_fname);
1681 malloc_string(media_usage_comment);
1682 malloc_string(storing_afioball_fname);
1683 transfer_block =
1684 malloc(sizeof(struct s_bkpinfo) + BKPINFO_LOC_OFFSET + 64);
1685 memset((void *) transfer_block, 0,
1686 sizeof(struct s_bkpinfo) + BKPINFO_LOC_OFFSET + 64);
1687 p_last_set_archived = (int *) transfer_block;
1688 p_archival_threads_running = (int *) (transfer_block + 4);
1689 p_next_set_to_archive = (int *) (transfer_block + 8);
1690 p_list_of_fileset_flags = (char *) (transfer_block + 12);
1691 memcpy((void *) (transfer_block + BKPINFO_LOC_OFFSET),
1692 (void *) bkpinfo, sizeof(struct s_bkpinfo));
1693 pvp = &vp;
1694 vp = (void *) result_str;
1695 *p_archival_threads_running = 0;
1696 *p_last_set_archived = -1;
1697 *p_next_set_to_archive = 0;
1698 sprintf(tmp, "%s/archives/filelist.full", bkpinfo->scratchdir);
1699 log_to_screen("Archiving regular files");
1700 log_msg(5, "Go, Shorty. It's your birthday.");
1701 open_progress_form("Backing up filesystem",
1702 "I am backing up your live filesystem now.",
1703 "Please wait. This may take a couple of hours.",
1704 "Working...",
1705 get_last_filelist_number() + 1);
1706
1707 log_msg(5, "We're gonna party like it's your birthday.");
1708
1709 srand((unsigned int) getpid());
1710 g_sem_key = 1234 + random() % 30000;
1711 if ((g_sem_id =
1712 semget((key_t) g_sem_key, 1,
1713 IPC_CREAT | S_IREAD | S_IWRITE)) == -1) {
1714 fatal_error("MABAI - unable to semget");
1715 }
1716 if (!set_semvalue()) {
1717 fatal_error("Unable to init semaphore");
1718 } // initialize semaphore
1719 for (noof_threads = 0; noof_threads < ARCH_THREADS; noof_threads++) {
1720 log_msg(8, "Creating thread #%d", noof_threads);
1721 (*p_archival_threads_running)++;
1722 if ((res =
1723 pthread_create(&archival_thread[noof_threads], NULL,
1724 create_afio_files_in_background,
1725 (void *) transfer_block))) {
1726 fatal_error("Unable to create an archival thread");
1727 }
1728 }
1729
1730 log_msg(8, "About to enter while() loop");
1731 while (!done_storing) {
1732 if (g_exiting) {
1733 fatal_error("Execution run aborted (main loop)");
1734 }
1735 if (*p_archival_threads_running == 0
1736 && *p_last_set_archived == storing_set_no - 1) {
1737 log_msg(2,
1738 "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... :-)",
1739 *p_last_set_archived, storing_set_no);
1740 done_storing = TRUE;
1741 } else
1742 if (!get_bit_N_of_array
1743 (p_list_of_fileset_flags, storing_set_no)) {
1744 misc_counter_that_is_not_important =
1745 (misc_counter_that_is_not_important + 1) % 5;
1746 if (!misc_counter_that_is_not_important) {
1747 update_progress_form(media_usage_comment);
1748 }
1749 sleep(1);
1750 } else
1751 // store set N
1752 {
1753 sprintf(storing_filelist_fname, FILELIST_FNAME_RAW_SZ,
1754 bkpinfo->tmpdir, storing_set_no);
1755 sprintf(storing_afioball_fname, AFIOBALL_FNAME_RAW_SZ,
1756 bkpinfo->tmpdir, storing_set_no, bkpinfo->zip_suffix);
1757 if (g_getfattr) {
1758 sprintf(curr_xattr_list_fname, XATTR_LIST_FNAME_RAW_SZ,
1759 bkpinfo->tmpdir, storing_set_no);
1760 }
1761 if (g_getfacl) {
1762 sprintf(curr_acl_list_fname, ACL_LIST_FNAME_RAW_SZ,
1763 bkpinfo->tmpdir, storing_set_no);
1764 }
1765
1766 log_msg(2, "Storing set %d", storing_set_no);
1767 while (!does_file_exist(storing_filelist_fname)
1768 || !does_file_exist(storing_afioball_fname)) {
1769 log_msg(2,
1770 "Warning - either %s or %s doesn't exist yet. I'll pause 5 secs.",
1771 storing_filelist_fname, storing_afioball_fname);
1772 sleep(5);
1773 }
1774 strcpy(media_usage_comment, percent_media_full_comment());
1775 /* copy to CD (scratchdir) ... and an actual CD-R if necessary */
1776 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1777 register_in_tape_catalog(fileset, storing_set_no, -1,
1778 storing_afioball_fname);
1779 maintain_collection_of_recent_archives(bkpinfo->tmpdir,
1780 storing_afioball_fname);
1781 iamhere("Writing EXAT files");
1782 res +=
1783 write_EXAT_files_to_tape(curr_xattr_list_fname,
1784 curr_acl_list_fname);
1785// archives themselves
1786 res +=
1787 move_files_to_stream(storing_afioball_fname,
1788 NULL);
1789 } else {
1790 if (g_getfacl) {
1791 if (g_getfattr) {
1792 res = move_files_to_cd(storing_filelist_fname,
1793 curr_xattr_list_fname,
1794 curr_acl_list_fname,
1795 storing_afioball_fname, NULL);
1796 } else {
1797 res = move_files_to_cd(storing_filelist_fname,
1798 curr_acl_list_fname,
1799 storing_afioball_fname, NULL);
1800 }
1801 } else {
1802 if (g_getfattr) {
1803 res = move_files_to_cd(storing_filelist_fname,
1804 curr_xattr_list_fname,
1805 storing_afioball_fname, NULL);
1806 } else {
1807 res = move_files_to_cd(storing_filelist_fname,
1808 storing_afioball_fname, NULL);
1809 }
1810 }
1811 }
1812 retval += res;
1813 g_current_progress++;
1814 update_progress_form(media_usage_comment);
1815 if (res) {
1816 sprintf(tmp,
1817 "Failed to add archive %ld's files to CD dir\n",
1818 storing_set_no);
1819 log_to_screen(tmp);
1820 fatal_error
1821 ("Is your hard disk full? If not, please send the author the logfile.");
1822 }
1823 storing_set_no++;
1824 // sleep(2);
1825 }
1826 }
1827 close_progress_form();
1828
1829 sprintf(tmp, "Your regular files have been archived ");
1830 log_msg(2, "Joining background threads to foreground thread");
1831 for (i = 0; i < noof_threads; i++) {
1832 pthread_join(archival_thread[i], pvp);
1833 log_msg(3, "Thread %d of %d: closed OK", i + 1, noof_threads);
1834 }
1835 del_semvalue();
1836 log_msg(2, "Done.");
1837 if (retval) {
1838 strcat(tmp, "(with errors).");
1839 } else {
1840 strcat(tmp, "successfully.");
1841 }
1842 log_to_screen(tmp);
1843 paranoid_free(transfer_block);
1844 paranoid_free(result_str);
1845 paranoid_free(storing_filelist_fname);
1846 paranoid_free(media_usage_comment);
1847 paranoid_free(storing_afioball_fname);
1848 paranoid_free(curr_xattr_list_fname);
1849 paranoid_free(curr_acl_list_fname);
1850 return (retval);
1851}
1852
1853
1854void pause_for_N_seconds(int how_long, char *msg)
1855{
1856 int i;
1857 open_evalcall_form(msg);
1858 for (i = 0; i < how_long; i++) {
1859 update_evalcall_form((int) ((100.0 / (float) (how_long) * i)));
1860 sleep(1);
1861 }
1862 close_evalcall_form();
1863}
1864
1865
1866
1867
1868/**
1869 * Create a USB image in @c destfile, from files in @c bkpinfo->scratchdir.
1870 *
1871 * @param bkpinfo The backup information structure. Fields used:
1872 * - @c backup_media_type
1873 * - @c nonbootable_backup
1874 * - @c scratchdir
1875 *
1876 * @param destfile Where to put the generated USB image.
1877 * @return The number of errors encountered (0 for success)
1878 */
1879int make_usb_fs()
1880{
1881 /*@ int ********************************************** */
1882 int retval = 0;
1883 int res;
1884
1885 /*@ buffers ****************************************** */
1886 char *tmp = NULL;
1887 char *tmp1 = NULL;
1888 char *result_sz = NULL;
1889 char *message_to_screen = NULL;
1890 char *old_pwd;
1891
1892 malloc_string(old_pwd);
1893 assert(bkpinfo != NULL);
1894
1895 log_msg(2, "make_usb_fs --- scratchdir=%s", bkpinfo->scratchdir);
1896 (void) getcwd(old_pwd, MAX_STR_LEN - 1);
1897 asprintf(&tmp, "chmod 755 %s", bkpinfo->scratchdir);
1898 run_program_and_log_output(tmp, FALSE);
1899 paranoid_free(tmp);
1900 chdir(bkpinfo->scratchdir);
1901
1902 asprintf(&message_to_screen, "Copying data to make %s #%d",
1903 media_descriptor_string(bkpinfo->backup_media_type),
1904 g_current_media_number);
1905 log_msg(1, message_to_screen);
1906
1907 asprintf(&tmp1, "%s1", bkpinfo->media_device);
1908 if (is_this_device_mounted(tmp1)) {
1909 log_msg(1, "USB device mounted. Remounting it at the right place");
1910 asprintf(&tmp, "umount %s", tmp1);
1911 run_program_and_log_output(tmp, FALSE);
1912 paranoid_free(tmp);
1913 }
1914 paranoid_free(tmp);
1915
1916 log_msg(1, "Mounting USB device.");
1917 asprintf(&tmp1, "%s/usb", bkpinfo->tmpdir);
1918 asprintf(&tmp, "mkdir -p %s", tmp1);
1919 run_program_and_log_output(tmp, FALSE);
1920 paranoid_free(tmp);
1921 /* Mindi always create one single parition on the USB dev */
1922 asprintf(&tmp, "mount %s1 %s", bkpinfo->media_device, tmp1);
1923 run_program_and_log_output(tmp, FALSE);
1924 paranoid_free(tmp);
1925
1926 if (bkpinfo->nonbootable_backup) {
1927 log_msg(1, "Making nonbootable USB backup not implemented yet");
1928 } else {
1929 log_msg(1, "Making bootable backup");
1930
1931 /* Command to execute */
1932 asprintf(&tmp,"mv %s/* %s", bkpinfo->scratchdir, tmp1);
1933 res = eval_call_to_make_USB(tmp, message_to_screen);
1934 if (res) {
1935 asprintf(&result_sz, "%s ...failed",tmp);
1936 } else {
1937 asprintf(&result_sz, "%s ...OK",tmp);
1938 }
1939 log_to_screen(result_sz);
1940 paranoid_free(result_sz);
1941 paranoid_free(tmp);
1942 retval += res;
1943 }
1944 paranoid_free(message_to_screen);
1945 paranoid_free(tmp1);
1946
1947 if (is_this_device_mounted(bkpinfo->media_device)) {
1948 asprintf(&tmp, "umount %s1", bkpinfo->media_device);
1949 run_program_and_log_output(tmp, FALSE);
1950 paranoid_free(tmp);
1951 }
1952
1953 chdir(old_pwd);
1954 if (retval) {
1955 log_msg(1, "WARNING - make_usb_fs returned an error");
1956 }
1957 paranoid_free(old_pwd);
1958 return (retval);
1959}
1960
1961
1962/**
1963 * Create an ISO image in @c destfile, from files in @c bkpinfo->scratchdir.
1964 *
1965 * @param bkpinfo The backup information structure. Fields used:
1966 * - @c backup_media_type
1967 * - @c call_after_iso
1968 * - @c call_before_iso
1969 * - @c call_burn_iso
1970 * - @c call_make_iso
1971 * - @c make_cd_use_lilo
1972 * - @c manual_cd_tray
1973 * - @c nonbootable_backup
1974 * - @c scratchdir
1975 *
1976 * @param destfile Where to put the generated ISO image.
1977 * @return The number of errors encountered (0 for success)
1978 */
1979int make_iso_fs(char *destfile)
1980{
1981 /*@ int ********************************************** */
1982 int retval = 0;
1983 int res;
1984
1985 /*@ buffers ****************************************** */
1986 char *tmp;
1987 char *old_pwd;
1988 char *result_sz;
1989 char *message_to_screen;
1990 char *sz_blank_disk;
1991 char *fnam;
1992 char *tmp2;
1993 char *tmp3;
1994 bool cd_is_mountable;
1995
1996 malloc_string(old_pwd);
1997 malloc_string(result_sz);
1998 malloc_string(message_to_screen);
1999 malloc_string(sz_blank_disk);
2000 malloc_string(fnam);
2001 tmp = malloc(1200);
2002 tmp2 = malloc(1200);
2003 tmp3 = malloc(1200);
2004 assert(bkpinfo != NULL);
2005 assert_string_is_neither_NULL_nor_zerolength(destfile);
2006
2007 sprintf(tmp, "%s/isolinux.bin", bkpinfo->scratchdir);
2008 sprintf(tmp2, "%s/isolinux.bin", bkpinfo->tmpdir);
2009 if (does_file_exist(tmp)) {
2010 sprintf(tmp3, "cp -f %s %s", tmp, tmp2);
2011 paranoid_system(tmp3);
2012 }
2013 if (!does_file_exist(tmp) && does_file_exist(tmp2)) {
2014 sprintf(tmp3, "cp -f %s %s", tmp2, tmp);
2015 paranoid_system(tmp3);
2016 }
2017 free(tmp2);
2018 free(tmp3);
2019 tmp2 = NULL;
2020 tmp3 = NULL;
2021 if (bkpinfo->backup_media_type == iso && bkpinfo->manual_cd_tray) {
2022 popup_and_OK("Please insert new media and press Enter.");
2023 }
2024
2025 log_msg(2, "make_iso_fs --- scratchdir=%s --- destfile=%s",
2026 bkpinfo->scratchdir, destfile);
2027 (void) getcwd(old_pwd, MAX_STR_LEN - 1);
2028 sprintf(tmp, "chmod 744 %s", bkpinfo->scratchdir);
2029 run_program_and_log_output(tmp, FALSE);
2030 chdir(bkpinfo->scratchdir);
2031
2032 if (bkpinfo->call_before_iso[0] != '\0') {
2033 sprintf(message_to_screen, "Running pre-ISO call for CD#%d",
2034 g_current_media_number);
2035 res =
2036 eval_call_to_make_ISO(bkpinfo->call_before_iso,
2037 destfile, g_current_media_number,
2038 MONDO_LOGFILE, message_to_screen);
2039 if (res) {
2040 strcat(message_to_screen, "...failed");
2041 } else {
2042 strcat(message_to_screen, "...OK");
2043 }
2044 log_to_screen(message_to_screen);
2045 retval += res;
2046 }
2047
2048 if (bkpinfo->call_make_iso[0] != '\0') {
2049 log_msg(2, "bkpinfo->call_make_iso = %s", bkpinfo->call_make_iso);
2050 sprintf(tmp, "%s/archives/NOT-THE-LAST", bkpinfo->scratchdir);
2051 sprintf(message_to_screen, "Making an ISO (%s #%d)",
2052 media_descriptor_string(bkpinfo->backup_media_type),
2053 g_current_media_number);
2054
2055 pause_and_ask_for_cdr(2, &cd_is_mountable); /* if g_current_media_number >= 2 then pause & ask */
2056 if (retval) {
2057 log_to_screen
2058 ("Serious error(s) occurred already. I shan't try to write to media.");
2059 } else {
2060 res =
2061 eval_call_to_make_ISO(bkpinfo->call_make_iso,
2062 bkpinfo->scratchdir,
2063 g_current_media_number,
2064 MONDO_LOGFILE, message_to_screen);
2065 if (res) {
2066 log_to_screen("%s...failed to write", message_to_screen);
2067 } else {
2068 log_to_screen("%s...OK", message_to_screen);
2069 sprintf(tmp, "tail -n10 %s | grep -F ':-('", MONDO_LOGFILE);
2070 if (!run_program_and_log_output(tmp, 1)) {
2071 log_to_screen
2072 ("Despite nonfatal errors, growisofs confirms the write was successful.");
2073 }
2074 }
2075 retval += res;
2076#ifdef DVDRWFORMAT
2077 sprintf(tmp,
2078 "tail -n8 %s | grep 'blank=full.*dvd-compat.*DAO'",
2079 MONDO_LOGFILE);
2080 if (g_backup_media_type == dvd
2081 && (res || !run_program_and_log_output(tmp, 1))) {
2082 log_to_screen
2083 ("Failed to write to disk. I shall blank it and then try again.");
2084 sleep(5);
2085 system("sync");
2086 pause_for_N_seconds(5, "Letting DVD drive settle");
2087
2088// dvd+rw-format --- OPTION 2
2089 if (!bkpinfo->please_dont_eject) {
2090 log_to_screen("Ejecting media to clear drive status.");
2091 eject_device(bkpinfo->media_device);
2092 inject_device(bkpinfo->media_device);
2093 }
2094 pause_for_N_seconds(5, "Letting DVD drive settle");
2095 sprintf(sz_blank_disk, "dvd+rw-format -force %s",
2096 bkpinfo->media_device);
2097 log_msg(3, "sz_blank_disk = '%s'", sz_blank_disk);
2098 res =
2099 run_external_binary_with_percentage_indicator_NEW
2100 ("Blanking DVD disk", sz_blank_disk);
2101 if (res) {
2102 log_to_screen
2103 ("Warning - format failed. (Was it a DVD-R?) Sleeping for 5 seconds to take a breath...");
2104 pause_for_N_seconds(5,
2105 "Letting DVD drive settle... and trying again.");
2106 res =
2107 run_external_binary_with_percentage_indicator_NEW
2108 ("Blanking DVD disk", sz_blank_disk);
2109 if (res) {
2110 log_to_screen("Format failed a second time.");
2111 }
2112 } else {
2113 log_to_screen
2114 ("Format succeeded. Sleeping for 5 seconds to take a breath...");
2115 }
2116 pause_for_N_seconds(5, "Letting DVD drive settle");
2117 if (!bkpinfo->please_dont_eject) {
2118 log_to_screen("Ejecting media to clear drive status.");
2119 eject_device(bkpinfo->media_device);
2120 inject_device(bkpinfo->media_device);
2121 }
2122 pause_for_N_seconds(5, "Letting DVD drive settle");
2123 res =
2124 eval_call_to_make_ISO(bkpinfo->call_make_iso,
2125 bkpinfo->scratchdir,
2126 g_current_media_number,
2127 MONDO_LOGFILE,
2128 message_to_screen);
2129 retval += res;
2130 if (!bkpinfo->please_dont_eject) {
2131 log_to_screen("Ejecting media.");
2132 eject_device(bkpinfo->media_device);
2133 }
2134 if (res) {
2135 log_to_screen("Dagnabbit. It still failed.");
2136 } else {
2137 log_to_screen
2138 ("OK, this time I successfully backed up to DVD.");
2139 }
2140 }
2141#endif
2142 if (g_backup_media_type == dvd && !bkpinfo->please_dont_eject) {
2143 eject_device(bkpinfo->media_device);
2144 }
2145 }
2146 } else {
2147 sprintf(message_to_screen, "Running mkisofs to make %s #%d",
2148 media_descriptor_string(bkpinfo->backup_media_type),
2149 g_current_media_number);
2150 log_msg(1, message_to_screen);
2151 sprintf(result_sz, "Call to mkisofs to make ISO (%s #%d) ",
2152 media_descriptor_string(bkpinfo->backup_media_type),
2153 g_current_media_number);
2154 if (bkpinfo->nonbootable_backup) {
2155 log_msg(1, "Making nonbootable backup");
2156// FIXME --- change mkisofs string to MONDO_MKISOFS_NONBOOTABLE and add ' .' at end
2157 res =
2158 eval_call_to_make_ISO("mkisofs -o '_ISO_' -r -p MondoRescue -publisher www.mondorescue.org -A MondoRescue_GPL -V _CD#_ .",
2159 destfile, g_current_media_number,
2160 MONDO_LOGFILE, message_to_screen);
2161 } else {
2162 log_msg(1, "Making bootable backup");
2163
2164#ifdef __FreeBSD__
2165 bkpinfo->make_cd_use_lilo = TRUE;
2166#endif
2167
2168
2169 log_msg(1, "make_cd_use_lilo is actually %d",
2170 bkpinfo->make_cd_use_lilo);
2171 if (bkpinfo->make_cd_use_lilo) {
2172 log_msg(1, "make_cd_use_lilo = TRUE");
2173// FIXME --- change mkisofs string to MONDO_MKISOFS_REGULAR_SYSLINUX/LILO depending on bkpinfo->make_cd_usE_lilo
2174// and add ' .' at end
2175#ifdef __IA64__
2176 log_msg(1, "IA64 --> elilo");
2177 res = eval_call_to_make_ISO("mkisofs -no-emul-boot -b images/mindi-bootroot."
2178 IA64_BOOT_SIZE
2179 ".img -c boot.cat -o '_ISO_' -J -r -p MondoRescue -publisher www.mondorescue.org -A Mondo_Rescue_GPL -V _CD#_ .",
2180 destfile,
2181 g_current_media_number,
2182 MONDO_LOGFILE,
2183 message_to_screen);
2184#else
2185// FIXME --- change mkisofs string to MONDO_MKISOFS_REGULAR_SYSLINUX/LILO depending on bkpinfo->make_cd_usE_lilo
2186// and add ' .' at end
2187 log_msg(1, "Non-ia64 --> lilo");
2188 res =
2189 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#_ .",
2190 destfile, g_current_media_number,
2191 MONDO_LOGFILE,
2192 message_to_screen);
2193#endif
2194 } else {
2195 log_msg(1, "make_cd_use_lilo = FALSE");
2196 log_msg(1, "Isolinux");
2197 res =
2198 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#_ .",
2199 destfile, g_current_media_number,
2200 MONDO_LOGFILE,
2201 message_to_screen);
2202 }
2203 }
2204 if (res) {
2205 strcat(result_sz, "...failed");
2206 } else {
2207 strcat(result_sz, "...OK");
2208 }
2209 log_to_screen(result_sz);
2210 retval += res;
2211 }
2212
2213 if (bkpinfo->backup_media_type == cdr
2214 || bkpinfo->backup_media_type == cdrw) {
2215 if (is_this_device_mounted(bkpinfo->media_device)) {
2216 log_msg(2,
2217 "Warning - %s mounted. I'm unmounting it before I burn to it.",
2218 bkpinfo->media_device);
2219 sprintf(tmp, "umount %s", bkpinfo->media_device);
2220 run_program_and_log_output(tmp, FALSE);
2221 }
2222 }
2223
2224 if (bkpinfo->call_burn_iso[0] != '\0') {
2225 log_msg(2, "bkpinfo->call_burn_iso = %s", bkpinfo->call_burn_iso);
2226 sprintf(message_to_screen, "Burning %s #%d",
2227 media_descriptor_string(bkpinfo->backup_media_type),
2228 g_current_media_number);
2229 pause_and_ask_for_cdr(2, &cd_is_mountable);
2230 res =
2231 eval_call_to_make_ISO(bkpinfo->call_burn_iso,
2232 destfile, g_current_media_number,
2233 MONDO_LOGFILE, message_to_screen);
2234 if (res) {
2235 strcat(message_to_screen, "...failed");
2236 } else {
2237 strcat(message_to_screen, "...OK");
2238 }
2239 log_to_screen(message_to_screen);
2240 retval += res;
2241 }
2242
2243 if (bkpinfo->call_after_iso[0] != '\0') {
2244 sprintf(message_to_screen, "Running post-ISO call (%s #%d)",
2245 media_descriptor_string(bkpinfo->backup_media_type),
2246 g_current_media_number);
2247 res =
2248 eval_call_to_make_ISO(bkpinfo->call_after_iso,
2249 destfile, g_current_media_number,
2250 MONDO_LOGFILE, message_to_screen);
2251 if (res) {
2252 strcat(message_to_screen, "...failed");
2253 } else {
2254 strcat(message_to_screen, "...OK");
2255 }
2256 log_to_screen(message_to_screen);
2257 retval += res;
2258 }
2259
2260 chdir(old_pwd);
2261 if (retval) {
2262 log_msg(1, "WARNING - make_iso_fs returned an error");
2263 }
2264 paranoid_free(old_pwd);
2265 paranoid_free(result_sz);
2266 paranoid_free(message_to_screen);
2267 paranoid_free(sz_blank_disk);
2268 paranoid_free(fnam);
2269 paranoid_free(tmp);
2270 return (retval);
2271}
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281bool is_dev_an_NTFS_dev(char *bigfile_fname)
2282{
2283 char *tmp;
2284 char *command;
2285 malloc_string(tmp);
2286 malloc_string(command);
2287 sprintf(command,
2288 "dd if=%s bs=512 count=1 2> /dev/null | strings | head -n1",
2289 bigfile_fname);
2290 log_msg(1, "command = '%s'", command);
2291 strcpy(tmp, call_program_and_get_last_line_of_output(command));
2292 log_msg(1, "--> tmp = '%s'", tmp);
2293 if (strstr(tmp, "NTFS")) {
2294 iamhere("TRUE");
2295 return (TRUE);
2296 } else {
2297 iamhere("FALSE");
2298 return (FALSE);
2299 }
2300}
2301
2302
2303/**
2304 * Back up big files by chopping them up.
2305 * This function backs up all "big" files (where "big" depends
2306 * on your backup media) in "chunks" (whose size again depends
2307 * on your media).
2308 *
2309 * @param bkpinfo The backup information structure. Fields used:
2310 * - @c backup_media_type
2311 * - @c optimal_set_size
2312 * @param biggielist_fname The path to a file containing a list of
2313 * all "big" files.
2314 * @return The number of errors encountered (0 for success)
2315 * @see slice_up_file_etc
2316 */
2317int
2318make_slices_and_images(char *biggielist_fname)
2319{
2320
2321 /*@ pointers ******************************************* */
2322 FILE *fin;
2323 char *p;
2324
2325 /*@ buffers ******************************************** */
2326 char *tmp;
2327 char *bigfile_fname;
2328 char *sz_devfile;
2329 char *ntfsprog_fifo = NULL;
2330 /*@ long *********************************************** */
2331 long biggie_file_number = 0;
2332 long noof_biggie_files = 0;
2333 long estimated_total_noof_slices = 0;
2334
2335 /*@ int ************************************************ */
2336 int retval = 0;
2337 int res = 0;
2338 pid_t pid;
2339 FILE *ftmp = NULL;
2340 bool delete_when_done;
2341 bool use_ntfsprog;
2342 off_t biggie_fsize;
2343
2344 assert(bkpinfo != NULL);
2345 assert_string_is_neither_NULL_nor_zerolength(biggielist_fname);
2346
2347 malloc_string(tmp);
2348 malloc_string(bigfile_fname);
2349 malloc_string(sz_devfile);
2350 estimated_total_noof_slices =
2351 size_of_all_biggiefiles_K() / bkpinfo->optimal_set_size + 1;
2352
2353 log_msg(1, "size of all biggiefiles = %ld",
2354 size_of_all_biggiefiles_K());
2355 log_msg(1, "estimated_total_noof_slices = %ld KB / %ld KB = %ld",
2356 size_of_all_biggiefiles_K(), bkpinfo->optimal_set_size,
2357 estimated_total_noof_slices);
2358
2359 if (length_of_file(biggielist_fname) < 6) {
2360 log_msg(1, "No biggiefiles; fair enough...");
2361 return (0);
2362 }
2363 sprintf(tmp, "I am now backing up all large files.");
2364 log_to_screen(tmp);
2365 noof_biggie_files = count_lines_in_file(biggielist_fname);
2366 open_progress_form("Backing up big files", tmp,
2367 "Please wait. This may take some time.", "",
2368 estimated_total_noof_slices);
2369 if (!(fin = fopen(biggielist_fname, "r"))) {
2370 log_OS_error("Unable to openin biggielist");
2371 return (1);
2372 }
2373 for (fgets(bigfile_fname, MAX_STR_LEN, fin); !feof(fin);
2374 fgets(bigfile_fname, MAX_STR_LEN, fin), biggie_file_number++) {
2375 use_ntfsprog = FALSE;
2376 if (bigfile_fname[strlen(bigfile_fname) - 1] < 32) {
2377 bigfile_fname[strlen(bigfile_fname) - 1] = '\0';
2378 }
2379 biggie_fsize = length_of_file(bigfile_fname);
2380 delete_when_done = FALSE;
2381
2382 if (!does_file_exist(bigfile_fname)) {
2383 ftmp = fopen(bigfile_fname, "w");
2384 if (ftmp == NULL) {
2385 log_msg(3, "Unable to write to %s", bigfile_fname);
2386 // So skip it as it doesn't exist
2387 continue;
2388 } else {
2389 paranoid_fclose(ftmp);
2390 }
2391 delete_when_done = TRUE;
2392 } else {
2393 // Call ntfsclone (formerly partimagehack) if it's a /dev entry
2394 // (i.e. a partition to be imaged)
2395 log_msg(2, "bigfile_fname = %s", bigfile_fname);
2396 use_ntfsprog = FALSE;
2397 if (!strncmp(bigfile_fname, "/dev/", 5)
2398 && is_dev_an_NTFS_dev(bigfile_fname)) {
2399 use_ntfsprog = TRUE;
2400 log_msg(2,
2401 "Calling ntfsclone in background because %s is an NTFS partition",
2402 bigfile_fname);
2403 sprintf(sz_devfile, "%s/%d.%d.000",
2404 bkpinfo->tmpdir,
2405 (int) (random() % 32768),
2406 (int) (random() % 32768));
2407 mkfifo(sz_devfile, 0x770);
2408 ntfsprog_fifo = sz_devfile;
2409 switch (pid = fork()) {
2410 case -1:
2411 fatal_error("Fork failure");
2412 case 0:
2413 log_msg(2,
2414 "CHILD - fip - calling feed_into_ntfsprog(%s, %s)",
2415 bigfile_fname, sz_devfile);
2416 res = feed_into_ntfsprog(bigfile_fname, sz_devfile);
2417 exit(res);
2418 break;
2419 default:
2420 log_msg(2,
2421 "feed_into_ntfsprog() called in background --- pid=%ld",
2422 (long int) (pid));
2423 }
2424 }
2425// Otherwise, use good old 'dd' and 'bzip2'
2426 else {
2427 sz_devfile[0] = '\0';
2428 ntfsprog_fifo = NULL;
2429 }
2430
2431// Whether partition or biggiefile, just do your thang :-)
2432 sprintf(tmp, "Bigfile #%ld is '%s' (%ld KB)",
2433 biggie_file_number + 1, bigfile_fname,
2434 (long) biggie_fsize >> 10);
2435 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2436 write_header_block_to_stream(biggie_fsize, bigfile_fname,
2437 use_ntfsprog ?
2438 BLK_START_A_PIHBIGGIE :
2439 BLK_START_A_NORMBIGGIE);
2440 }
2441 res =
2442 slice_up_file_etc(bigfile_fname,
2443 ntfsprog_fifo, biggie_file_number,
2444 noof_biggie_files, use_ntfsprog);
2445 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2446 write_header_block_to_stream((off_t)0,
2447 calc_checksum_of_file
2448 (bigfile_fname),
2449 BLK_STOP_A_BIGGIE);
2450 }
2451 retval += res;
2452 p = strrchr(bigfile_fname, '/');
2453 if (p) {
2454 p++;
2455 } else {
2456 p = bigfile_fname;
2457 }
2458 sprintf(tmp, "Archiving %s ... ", bigfile_fname);
2459 if (res) {
2460 strcat(tmp, "Failed!");
2461 } else {
2462 strcat(tmp, "OK");
2463 }
2464 if (delete_when_done) {
2465 unlink(bigfile_fname);
2466 delete_when_done = FALSE;
2467 }
2468 }
2469#ifndef _XWIN
2470 if (!g_text_mode) {
2471 newtDrawRootText(0, g_noof_rows - 2, tmp);
2472 newtRefresh();
2473 }
2474#endif
2475 }
2476 log_msg(1, "Finished backing up bigfiles");
2477 log_msg(1, "estimated slices = %ld; actual slices = %ld",
2478 estimated_total_noof_slices, g_current_progress);
2479 close_progress_form();
2480 paranoid_fclose(fin);
2481 paranoid_free(tmp);
2482 paranoid_free(bigfile_fname);
2483 paranoid_free(sz_devfile);
2484 return (retval);
2485}
2486
2487
2488
2489
2490/**
2491 * Single-threaded version of @c make_afioballs_and_images().
2492 * @see make_afioballs_and_images
2493 */
2494int make_afioballs_and_images_OLD()
2495{
2496
2497 /*@ int ************************************************** */
2498 int retval = 0;
2499 long int curr_set_no = 0;
2500 int res = 0;
2501
2502 /*@ buffers ********************************************** */
2503 char *curr_filelist_fname;
2504 char *curr_afioball_fname;
2505 char *curr_xattr_list_fname;
2506 char *curr_acl_list_fname;
2507 char *tmp;
2508 char *media_usage_comment;
2509
2510 malloc_string(curr_afioball_fname);
2511 malloc_string(media_usage_comment);
2512 malloc_string(curr_filelist_fname);
2513 malloc_string(curr_xattr_list_fname);
2514 malloc_string(curr_acl_list_fname);
2515
2516 tmp = malloc(MAX_STR_LEN * 2);
2517
2518 sprintf(tmp, "%s/archives/filelist.full", bkpinfo->scratchdir);
2519
2520 log_to_screen("Archiving regular files");
2521
2522 open_progress_form("Backing up filesystem",
2523 "I am backing up your live filesystem now.",
2524 "Please wait. This may take a couple of hours.",
2525 "Working...",
2526 get_last_filelist_number() + 1);
2527
2528 sprintf(curr_filelist_fname, FILELIST_FNAME_RAW_SZ, bkpinfo->tmpdir,
2529 0L);
2530
2531 for (curr_set_no = 0; does_file_exist(curr_filelist_fname);
2532 sprintf(curr_filelist_fname, FILELIST_FNAME_RAW_SZ,
2533 bkpinfo->tmpdir, ++curr_set_no)) {
2534 /* backup this set of files */
2535 sprintf(curr_filelist_fname, FILELIST_FNAME_RAW_SZ,
2536 bkpinfo->tmpdir, curr_set_no);
2537 sprintf(curr_afioball_fname, AFIOBALL_FNAME_RAW_SZ,
2538 bkpinfo->tmpdir, curr_set_no, bkpinfo->zip_suffix);
2539
2540 log_msg(1, "EXAT'g set %ld", curr_set_no);
2541 if (g_getfattr) {
2542 sprintf(curr_xattr_list_fname, XATTR_LIST_FNAME_RAW_SZ,
2543 bkpinfo->tmpdir, curr_set_no);
2544 get_fattr_list(curr_filelist_fname, curr_xattr_list_fname);
2545 }
2546 if (g_getfacl) {
2547 sprintf(curr_acl_list_fname, ACL_LIST_FNAME_RAW_SZ,
2548 bkpinfo->tmpdir, curr_set_no);
2549 get_acl_list(curr_filelist_fname, curr_acl_list_fname);
2550 }
2551
2552 log_msg(1, "Archiving set %ld", curr_set_no);
2553 res =
2554 archive_this_fileset(curr_filelist_fname,
2555 curr_afioball_fname, curr_set_no);
2556 retval += res;
2557 if (res) {
2558 sprintf(tmp,
2559 "Errors occurred while archiving set %ld. Perhaps your live filesystem changed?",
2560 curr_set_no);
2561 log_to_screen(tmp);
2562 }
2563
2564 strcpy(media_usage_comment, percent_media_full_comment());
2565
2566 /* copy to CD (scratchdir) ... and an actual CD-R if necessary */
2567 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2568 register_in_tape_catalog(fileset, curr_set_no, -1,
2569 curr_afioball_fname);
2570 maintain_collection_of_recent_archives(bkpinfo->tmpdir,
2571 curr_afioball_fname);
2572 iamhere("Writing EXAT files");
2573 res +=
2574 write_EXAT_files_to_tape(curr_xattr_list_fname,
2575 curr_acl_list_fname);
2576// archives themselves
2577 res = move_files_to_stream(curr_afioball_fname, NULL);
2578 } else {
2579 if (g_getfacl) {
2580 if (g_getfattr) {
2581 res = move_files_to_cd(curr_filelist_fname,
2582 curr_xattr_list_fname,
2583 curr_acl_list_fname,
2584 curr_afioball_fname, NULL);
2585 } else {
2586 res = move_files_to_cd(curr_filelist_fname,
2587 curr_acl_list_fname,
2588 curr_afioball_fname, NULL);
2589 }
2590 } else {
2591 if (g_getfattr) {
2592 res = move_files_to_cd(curr_filelist_fname,
2593 curr_xattr_list_fname,
2594 curr_afioball_fname, NULL);
2595 } else {
2596 res = move_files_to_cd(curr_filelist_fname,
2597 curr_afioball_fname, NULL);
2598 }
2599 }
2600 }
2601 retval += res;
2602 g_current_progress++;
2603 update_progress_form(media_usage_comment);
2604
2605 if (res) {
2606 sprintf(tmp, "Failed to add archive %ld's files to CD dir\n",
2607 curr_set_no);
2608 log_to_screen(tmp);
2609 fatal_error
2610 ("Is your hard disk is full? If not, please send the author the logfile.");
2611 }
2612 }
2613 close_progress_form();
2614 sprintf(tmp, "Your regular files have been archived ");
2615 if (retval) {
2616 strcat(tmp, "(with errors).");
2617 } else {
2618 strcat(tmp, "successfully.");
2619 }
2620 log_to_screen(tmp);
2621 paranoid_free(tmp);
2622 paranoid_free(curr_filelist_fname);
2623 paranoid_free(curr_afioball_fname);
2624 paranoid_free(media_usage_comment);
2625 paranoid_free(curr_xattr_list_fname);
2626 paranoid_free(curr_acl_list_fname);
2627 return (retval);
2628}
2629
2630/* @} - end of LLarchiveGroup */
2631
2632
2633/**
2634 * Wrapper around @c make_afioballs_and_images().
2635 * @param bkpinfo the backup information structure. Only the
2636 * @c backup_media_type field is used within this function.
2637 * @return return code of make_afioballs_and_images
2638 * @see make_afioballs_and_images
2639 * @ingroup MLarchiveGroup
2640 */
2641int make_those_afios_phase()
2642{
2643 /*@ int ******************************************* */
2644 int res = 0;
2645 int retval = 0;
2646
2647 assert(bkpinfo != NULL);
2648
2649 mvaddstr_and_log_it(g_currentY, 0,
2650 "Archiving regular files to media ");
2651
2652 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2653 write_header_block_to_stream((off_t)0, "start-of-afioballs",
2654 BLK_START_AFIOBALLS);
2655#if __FreeBSD__ == 5
2656 log_msg(1,
2657 "Using single-threaded make_afioballs_and_images() to suit b0rken FreeBSD 5.0");
2658 res = make_afioballs_and_images_OLD();
2659#else
2660 res = make_afioballs_and_images_OLD();
2661#endif
2662 write_header_block_to_stream((off_t)0, "stop-afioballs",
2663 BLK_STOP_AFIOBALLS);
2664 } else {
2665 res = make_afioballs_and_images();
2666 }
2667
2668 retval += res;
2669 if (res) {
2670 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2671 log_msg(1, "make_afioballs_and_images returned an error");
2672 } else {
2673 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2674 }
2675 return (retval);
2676}
2677
2678/**
2679 * Wrapper around @c make_slices_and_images().
2680 * @param bkpinfo The backup information structure. Fields used:
2681 * - @c backup_media_type
2682 * - @c scratchdir
2683 * - @c tmpdir
2684 * @return The number of errors encountered (0 for success)
2685 * @ingroup MLarchiveGroup
2686 */
2687int make_those_slices_phase()
2688{
2689
2690 /*@ int ***************************************************** */
2691 int res = 0;
2692 int retval = 0;
2693
2694 /*@ buffers ************************************************** */
2695 char *biggielist;
2696 char *command;
2697 char *blah;
2698 char *xattr_fname;
2699 char *acl_fname;
2700
2701 assert(bkpinfo != NULL);
2702 /* slice big files */
2703 malloc_string(biggielist);
2704 malloc_string(xattr_fname);
2705 malloc_string(acl_fname);
2706 mvaddstr_and_log_it(g_currentY, 0,
2707 "Archiving large files to media ");
2708 sprintf(biggielist, "%s/archives/biggielist.txt", bkpinfo->scratchdir);
2709 if (g_getfattr) {
2710 sprintf(xattr_fname, XATTR_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
2711 }
2712 if (g_getfacl) {
2713 sprintf(acl_fname, ACL_BIGGLST_FNAME_RAW_SZ, bkpinfo->tmpdir);
2714 }
2715
2716 asprintf(&command, "cp %s/biggielist.txt %s", bkpinfo->tmpdir,
2717 biggielist);
2718 paranoid_system(command);
2719 paranoid_free(command);
2720
2721 asprintf(&blah, "biggielist = %s", biggielist);
2722 log_msg(2, blah);
2723 paranoid_free(blah);
2724
2725 if (!does_file_exist(biggielist)) {
2726 log_msg(1, "BTW, the biggielist does not exist");
2727 }
2728
2729 if (g_getfattr) {
2730 get_fattr_list(biggielist, xattr_fname);
2731 asprintf(&command, "cp %s %s/archives/", xattr_fname,
2732 bkpinfo->scratchdir);
2733 paranoid_system(command);
2734 paranoid_free(command);
2735 }
2736 if (g_getfacl) {
2737 get_acl_list(biggielist, acl_fname);
2738 asprintf(&command, "cp %s %s/archives/", acl_fname,
2739 bkpinfo->scratchdir);
2740 paranoid_system(command);
2741 paranoid_free(command);
2742 }
2743
2744 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2745 res += write_EXAT_files_to_tape(xattr_fname, acl_fname);
2746 asprintf(&blah, "%ld", count_lines_in_file(biggielist));
2747 write_header_block_to_stream((off_t)0, blah, BLK_START_BIGGIEFILES);
2748 paranoid_free(blah);
2749 }
2750 res = make_slices_and_images(biggielist);
2751 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
2752 write_header_block_to_stream((off_t)0, "end-of-biggiefiles",
2753 BLK_STOP_BIGGIEFILES);
2754 }
2755 retval += res;
2756 if (res) {
2757 log_msg(1, "make_slices_and_images returned an error");
2758 mvaddstr_and_log_it(g_currentY++, 74, "Errors.");
2759 } else {
2760 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
2761 }
2762 paranoid_free(blah);
2763 paranoid_free(biggielist);
2764 paranoid_free(command);
2765 paranoid_free(xattr_fname);
2766 paranoid_free(acl_fname);
2767 return (retval);
2768}
2769
2770
2771/**
2772 * @addtogroup LLarchiveGroup
2773 * @{
2774 */
2775/**
2776 * Function pointer to an appropriate @c move_files_to_cd routine.
2777 * You can set this to your own function (for example, one to
2778 * transfer files over the network) or leave it as is.
2779 */
2780int (*move_files_to_cd) (char *, ...) =
2781 _move_files_to_cd;
2782
2783/**
2784 * Move some files to the ISO scratch directory.
2785 * This function moves files specified as parameters, into the directory
2786 * @c bkpinfo->scratchdir, where the files that will be stored on the next
2787 * CD are waiting.
2788 *
2789 * @param bkpinfo The backup information structure. Fields used:
2790 * - @c media_size
2791 * - @c scratchdir
2792 * @param files_to_add The files to add to the scratchdir.
2793 * @warning The list of @c files_to_add must be terminated with @c NULL.
2794 * @note If and when the space occupied by the scratchdir would exceed
2795 * the capacity of the current CD,
2796 * <tt>write_iso_and_go_on(bkpinfo, FALSE)</tt> is called and the
2797 * scratchdir is emptied.
2798 *
2799 * @return The number of errors encountered (0 for success)
2800 */
2801int _move_files_to_cd(char *files_to_add, ...)
2802{
2803
2804 /*@ int ************************************************************ */
2805 int retval = 0;
2806 int res = 0;
2807
2808 /*@ buffers ******************************************************** */
2809 char *tmp, *curr_file, *cf;
2810
2811 /*@ long ************************************************************ */
2812 va_list ap;
2813 long long would_occupy;
2814
2815 assert(bkpinfo != NULL);
2816 malloc_string(curr_file);
2817 tmp = malloc(1200);
2818 would_occupy = space_occupied_by_cd(bkpinfo->scratchdir);
2819 va_start(ap, files_to_add); // initialize the variable arguments
2820 for (cf = files_to_add; cf != NULL; cf = va_arg(ap, char *)) {
2821 if (!cf) {
2822 continue;
2823 }
2824 strcpy(curr_file, cf);
2825 if (!does_file_exist(curr_file)) {
2826 log_msg(1,
2827 "Warning - you're trying to add a non-existent file - '%s' to the CD",
2828 curr_file);
2829 } else {
2830 log_msg(8, "Trying to add file %s to CD", curr_file);
2831 would_occupy += length_of_file(curr_file) / 1024;
2832 }
2833 }
2834 va_end(ap);
2835
2836 if (bkpinfo->media_size[g_current_media_number] <= 0) {
2837 fatal_error("move_files_to_cd() - unknown media size");
2838 }
2839 if (would_occupy / 1024 > bkpinfo->media_size[g_current_media_number]) {
2840 res = write_iso_and_go_on(FALSE); /* FALSE because this is not the last CD we'll write */
2841 retval += res;
2842 if (res) {
2843 log_msg(1, "WARNING - write_iso_and_go_on returned an error");
2844 }
2845 }
2846
2847 va_start(ap, files_to_add); // initialize the variable arguments
2848 for (cf = files_to_add; cf != NULL; cf = va_arg(ap, char *)) {
2849 if (!cf) {
2850 continue;
2851 }
2852 strcpy(curr_file, cf);
2853
2854 sprintf(tmp, "mv -f %s %s/archives/", curr_file,
2855 bkpinfo->scratchdir);
2856 res = run_program_and_log_output(tmp, 5);
2857 retval += res;
2858 if (res) {
2859 log_msg(1, "(move_files_to_cd) '%s' failed", tmp);
2860 } else {
2861 log_msg(8, "Moved %s to CD OK", tmp);
2862 }
2863 // unlink (curr_file);
2864 }
2865 va_end(ap);
2866
2867 if (retval) {
2868 log_msg(1,
2869 "Warning - errors occurred while I was adding files to CD dir");
2870 }
2871 paranoid_free(tmp);
2872 paranoid_free(curr_file);
2873 return (retval);
2874}
2875
2876/* @} - end of LLarchiveGroup */
2877
2878
2879
2880
2881
2882
2883
2884
2885/**
2886 * Offer to write boot and data disk images to 3.5" floppy disks.
2887 * @param bkpinfo The backup information structure. Only the
2888 * @c backup_media_type field is used in this function.
2889 * @param imagesdir The directory containing the floppy images (usually
2890 * /root/images/mindi).
2891 *
2892 * @return The number of errors encountered (0 for success)
2893 * @see write_image_to_floppy
2894 * @see format_disk
2895 * @ingroup MLarchiveGroup
2896 */
2897int offer_to_write_floppies(char *imagesdir)
2898{
2899 /*@ buffer ************************************************************ */
2900 char *tmp;
2901 char *comment;
2902 char *bootdisk_dev;
2903 char *datadisk_dev;
2904 char *bootdisk_file;
2905 char *rootdisk_file;
2906
2907 /*@ int *************************************************************** */
2908 int i = 0;
2909 int res = 0;
2910
2911 /*@ bool ************************************************************** */
2912 bool format_first;
2913 bool root_disk_exists = FALSE;
2914
2915 malloc_string(tmp);
2916 malloc_string(comment);
2917 malloc_string(bootdisk_dev);
2918 malloc_string(datadisk_dev);
2919 malloc_string(rootdisk_file);
2920 malloc_string(bootdisk_file);
2921 assert(bkpinfo != NULL);
2922 assert_string_is_neither_NULL_nor_zerolength(imagesdir);
2923
2924
2925 if (!ask_me_yes_or_no
2926 ("Write boot and data disk images to 3.5\" floppy disks?")) {
2927 return (0);
2928 }
2929 if (does_device_exist(DEFAULT_1722MB_DISK)) {
2930#ifdef __FreeBSD__
2931 // tell the system that this is a 1.72m floppy
2932 system("/usr/sbin/fdcontrol -F 1722 /dev/fd0.1722");
2933#endif
2934 strcpy(bootdisk_dev, DEFAULT_1722MB_DISK);
2935 } else if (does_device_exist(BACKUP_1722MB_DISK)) {
2936 sprintf(bootdisk_dev, "/dev/fd0H1722");
2937 } else {
2938 log_msg(1, "Warning - can't find a 1.72MB floppy device *sigh*");
2939 strcpy(bootdisk_dev, DEFAULT_1722MB_DISK);
2940// return (1);
2941 }
2942 strcpy(datadisk_dev, "/dev/fd0");
2943 if (!does_device_exist(datadisk_dev)) {
2944 log_msg(1, "Warning - can't find a 1.44MB floppy device *sigh*");
2945 strcpy(datadisk_dev, "/dev/fd0");
2946// return (1);
2947 }
2948 format_first =
2949 ask_me_yes_or_no
2950 ("Do you want me to format the disks before I write to them?");
2951
2952/* boot disk */
2953 if (ask_me_OK_or_cancel("About to write boot disk")) {
2954 log_to_screen("Writing boot floppy");
2955#ifdef __FreeBSD__
2956 sprintf(tmp, "%s/mindi-kern.1722.img", imagesdir);
2957 if (format_first) {
2958 format_disk(bootdisk_dev);
2959 }
2960 res += write_image_to_floppy(bootdisk_dev, tmp);
2961 if (ask_me_OK_or_cancel("About to write 1.44MB mfsroot disk")) {
2962 log_to_screen("Writing mfsroot floppy");
2963 if (format_first) {
2964 format_disk(datadisk_dev);
2965 }
2966 sprintf(tmp, "%s/mindi-mfsroot.1440.img", imagesdir);
2967 write_image_to_floppy(datadisk_dev, tmp);
2968 }
2969#else
2970 sprintf(bootdisk_file, "%s/mindi-bootroot.1722.img", imagesdir);
2971 if (does_file_exist(bootdisk_file)) {
2972 if (format_first) {
2973 format_disk(bootdisk_dev);
2974 }
2975 res += write_image_to_floppy(bootdisk_dev, bootdisk_file);
2976 } else {
2977 sprintf(bootdisk_file, "%s/mindi-boot.1440.img", imagesdir);
2978 sprintf(rootdisk_file, "%s/mindi-root.1440.img", imagesdir);
2979 root_disk_exists = TRUE;
2980 if (!does_file_exist(rootdisk_file)
2981 || !does_file_exist(bootdisk_file)) {
2982 popup_and_OK
2983 ("Cannot write boot/root floppies. Files not found.");
2984 log_to_screen
2985 ("Failed to find boot/root floppy images. Oh dear.");
2986 return (1);
2987 }
2988 if (format_first) {
2989 format_disk(datadisk_dev);
2990 }
2991 /*
2992 sprintf(tmp, "cat %s > %s", bootdisk_file, datadisk_dev);
2993 res += run_external_binary_with_percentage_indicator_NEW("Writing boot floppy", tmp);
2994 */
2995 res += write_image_to_floppy(datadisk_dev, bootdisk_file);
2996 if (ask_me_OK_or_cancel("About to write root disk")) {
2997 log_to_screen("Writing root floppy");
2998 if (format_first) {
2999 format_disk(datadisk_dev);
3000 }
3001 sprintf(tmp, "cat %s > %s", rootdisk_file, datadisk_dev);
3002 log_msg(1, "tmp = '%s'", tmp);
3003 res +=
3004 run_external_binary_with_percentage_indicator_NEW
3005 ("Writing root floppy", tmp);
3006// res += write_image_to_floppy (datadisk_dev, rootdisk_file);
3007 }
3008 }
3009#endif
3010 }
3011 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
3012 log_to_screen
3013 ("FYI, the data disks are stored on tape/CD for your convenience.");
3014 return (0);
3015 }
3016 for (i = 1; i < 99; i++) {
3017 sprintf(tmp, "%s/mindi-data-%d.img", imagesdir, i);
3018 log_msg(3, tmp);
3019 if (!does_file_exist(tmp)) {
3020 log_msg(3, "...not found");
3021 break;
3022 }
3023 sprintf(comment, "About to write data disk #%d", i);
3024 if (ask_me_OK_or_cancel(comment)) {
3025 sprintf(comment, "Writing data disk #%3d", i);
3026 log_to_screen(comment);
3027 if (format_first) {
3028 res += format_disk(datadisk_dev);
3029 }
3030 res += write_image_to_floppy(datadisk_dev, tmp);
3031 }
3032 }
3033 paranoid_free(tmp);
3034 paranoid_free(comment);
3035 paranoid_free(bootdisk_dev);
3036 paranoid_free(datadisk_dev);
3037 return (res);
3038}
3039
3040
3041
3042
3043
3044
3045
3046
3047/**
3048 * Wrapper around @c offer_to_write_floppies().
3049 * @param bkpinfo The backup information structure. Used only
3050 * in the call to @c offer_to_write_floppies().
3051 * @return 0 if the boot floppies were found (not necessarily written OK),
3052 * 1 otherwise.
3053 * @see offer_to_write_floppies
3054 * @ingroup MLarchiveGroup
3055 */
3056
3057int offer_to_write_boot_floppies_to_physical_disks()
3058{
3059 int res = 0;
3060
3061 assert(bkpinfo != NULL);
3062
3063 mvaddstr_and_log_it(g_currentY, 0,
3064 "Writing boot+data floppy images to disk");
3065
3066 if (!bkpinfo->nonbootable_backup) {
3067#ifdef __FreeBSD__
3068 if (!does_file_exist(MINDI_CACHE"/mindi-kern.1722.img"))
3069#else
3070 if (!does_file_exist(MINDI_CACHE"/mindi-bootroot.1722.img") && !does_file_exist(MINDI_CACHE"/mindi-boot.1440.img"))
3071#endif
3072 {
3073 mvaddstr_and_log_it(g_currentY++, 74, "No Imgs");
3074 if (does_file_exist(MINDI_CACHE"/mondorescue.iso")) {
3075 popup_and_OK
3076 ("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.");
3077 res++;
3078 }
3079 } else {
3080 offer_to_write_floppies(MINDI_CACHE);
3081 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
3082 }
3083 } else {
3084 popup_and_OK
3085 ("Since you opted for a nonbootable backup, no boot floppies were created.");
3086 }
3087
3088 return (res);
3089}
3090
3091
3092
3093
3094/**
3095 * @addtogroup LLarchiveGroup
3096 * @{
3097 */
3098/**
3099 * Function pointer to an appropriate @c move_files_to_stream routine.
3100 * You can set this to your own function (for example, one to
3101 * transfer files over the network) or leave it as is.
3102 */
3103int (*move_files_to_stream) (char *, ...) =
3104 _move_files_to_stream;
3105
3106/**
3107 * Copy some files to tape.
3108 * This function copies the files specified as parameters into the tape stream.
3109 *
3110 * @param bkpinfo The backup information structure. Used only in the call to
3111 * @c write_file_to_stream_from_file().
3112 *
3113 * @param files_to_add The files to copy to the tape stream.
3114 * @warning The list of @c files_to_add must be terminated with @c NULL.
3115 * @note Files may be split across multiple tapes if necessary.
3116 *
3117 * @return The number of errors encountered (0 for success)
3118 */
3119int
3120_move_files_to_stream(char *files_to_add, ...)
3121{
3122
3123 /*@ int ************************************************************ */
3124 int retval = 0;
3125 int res = 0;
3126 /*@ buffers ******************************************************** */
3127
3128 /*@ char *********************************************************** */
3129 char start_chr;
3130 char stop_chr;
3131 char *curr_file, *cf;
3132 /*@ long long ****************************************************** */
3133 off_t length_of_incoming_file = (off_t)0;
3134 t_archtype type;
3135 va_list ap;
3136
3137 assert(bkpinfo != NULL);
3138 malloc_string(curr_file);
3139 va_start(ap, files_to_add);
3140 for (cf = files_to_add; cf != NULL; cf = va_arg(ap, char *)) {
3141 if (!cf) {
3142 continue;
3143 }
3144 strcpy(curr_file, cf);
3145 if (!does_file_exist(curr_file)) {
3146 log_msg(1,
3147 "Warning - you're trying to add a non-existent file - '%s' to the tape",
3148 curr_file);
3149 }
3150/* create header chars */
3151 start_chr = BLK_START_AN_AFIO_OR_SLICE;
3152 stop_chr = BLK_STOP_AN_AFIO_OR_SLICE;
3153/* ask for new tape if necessary */
3154 length_of_incoming_file = length_of_file(curr_file);
3155 write_header_block_to_stream(length_of_incoming_file, curr_file,
3156 start_chr);
3157 if (strstr(curr_file, ".afio.") || strstr(curr_file, ".star.")) {
3158 type = fileset;
3159 } else if (strstr(curr_file, "slice")) {
3160 type = biggieslice;
3161 } else {
3162 type = other;
3163 }
3164 res = write_file_to_stream_from_file(curr_file);
3165 retval += res;
3166 unlink(curr_file);
3167/* write closing header */
3168 write_header_block_to_stream((off_t)0, "finished-writing-file", stop_chr);
3169 }
3170 va_end(ap);
3171
3172 if (retval) {
3173 log_msg(1,
3174 "Warning - errors occurred while I was adding file to tape");
3175 }
3176 paranoid_free(curr_file);
3177 return (retval);
3178}
3179
3180/* @} - end of LLarchiveGroup */
3181
3182
3183
3184/**
3185 * @addtogroup utilityGroup
3186 * @{
3187 */
3188/**
3189 * Make sure the user has a valid CD-R(W) in the CD drive.
3190 * @param cdrw_dev Set to the CD-R(W) device checked.
3191 * @param keep_looping If TRUE, keep pestering user until they insist
3192 * or insert a correct CD; if FALSE, only check once.
3193 * @return 0 (there was an OK CD in the drive) or 1 (there wasn't).
3194 */
3195int interrogate_disk_currently_in_cdrw_drive(char *cdrw_dev,
3196 bool keep_looping)
3197{
3198 char *tmp;
3199 int res = 0;
3200 char *bkp;
3201 char *cdrecord;
3202
3203 malloc_string(tmp);
3204 malloc_string(bkp);
3205 malloc_string(cdrecord);
3206 strcpy(bkp, cdrw_dev);
3207 if (find_cdrw_device(cdrw_dev)) {
3208 strcpy(cdrw_dev, bkp);
3209 } else {
3210 if (!system("which cdrecord > /dev/null 2> /dev/null")) {
3211 sprintf(cdrecord, "cdrecord dev=%s -atip", cdrw_dev);
3212 } else if (!system("which dvdrecord > /dev/null 2> /dev/null")) {
3213 sprintf(cdrecord, "cdrecord dev=%s -atip", cdrw_dev);
3214 } else {
3215 cdrecord[0] = '\0';
3216 log_msg(2, "Oh well. I guess I'll just pray then.");
3217 }
3218 if (cdrecord[0]) {
3219 if (!keep_looping) {
3220 retract_CD_tray_and_defeat_autorun();
3221 res = run_program_and_log_output(cdrecord, 5);
3222 } else {
3223 while ((res = run_program_and_log_output(cdrecord, 5))) {
3224 retract_CD_tray_and_defeat_autorun();
3225 if (ask_me_yes_or_no
3226 ("Unable to examine CD. Are you sure this is a valid CD-R(W) CD?"))
3227 {
3228 log_msg(1, "Well, he insisted...");
3229 break;
3230 }
3231 }
3232 }
3233 }
3234 }
3235// retract_CD_tray_and_defeat_autorun();
3236 paranoid_free(tmp);
3237 paranoid_free(cdrecord);
3238 paranoid_free(bkp);
3239 return (res);
3240}
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250/**
3251 * Asks the user to put a CD-R(W) in the drive.
3252 * @param ask_for_one_if_more_than_this (unused)
3253 * @param pmountable If non-NULL, pointed-to value is set to TRUE if the CD is mountable, FALSE otherwise.
3254 */
3255void
3256pause_and_ask_for_cdr(int ask_for_one_if_more_than_this, bool * pmountable)
3257{
3258
3259 /*@ buffers ********************************************* */
3260 char *tmp;
3261 char *szmsg;
3262 char *cdrom_dev;
3263 char *cdrw_dev;
3264 char *our_serial_str;
3265 bool ok_go_ahead_burn_it;
3266 int cd_number = -1;
3267 int attempt_to_mount_returned_this = 999;
3268 char *mtpt;
3269 char *szcdno;
3270 char *szserfname;
3271 char *szunmount;
3272
3273 malloc_string(tmp);
3274 malloc_string(szmsg);
3275 malloc_string(cdrom_dev);
3276 malloc_string(cdrw_dev);
3277 malloc_string(mtpt);
3278 malloc_string(szcdno);
3279 malloc_string(szserfname);
3280 malloc_string(our_serial_str);
3281 malloc_string(szunmount);
3282
3283 sprintf(szmsg, "I am about to burn %s #%d",
3284 media_descriptor_string(g_backup_media_type),
3285 g_current_media_number);
3286 log_to_screen(szmsg);
3287 if (g_current_media_number < ask_for_one_if_more_than_this) {
3288 return;
3289 }
3290 log_to_screen("Scanning CD-ROM drive...");
3291 sprintf(mtpt, "%s/cd.mtpt", bkpinfo->tmpdir);
3292 make_hole_for_dir(mtpt);
3293
3294 gotos_make_me_puke:
3295 ok_go_ahead_burn_it = TRUE;
3296 if (!find_cdrom_device(cdrom_dev, FALSE)) {
3297/* When enabled, it made CD eject-and-retract when wrong CD inserted.. Weird
3298 log_msg(2, "paafcd: Retracting CD-ROM drive if possible" );
3299 retract_CD_tray_and_defeat_autorun();
3300*/
3301 sprintf(tmp, "umount %s", cdrom_dev);
3302 run_program_and_log_output(tmp, 1);
3303 sprintf(szcdno, "%s/archives/THIS-CD-NUMBER", mtpt);
3304 sprintf(szserfname, "%s/archives/SERIAL-STRING", mtpt);
3305 sprintf(szunmount, "umount %s", mtpt);
3306 cd_number = -1;
3307 our_serial_str[0] = '\0';
3308 sprintf(tmp, "mount %s %s", cdrom_dev, mtpt);
3309 if ((attempt_to_mount_returned_this =
3310 run_program_and_log_output(tmp, 1))) {
3311 log_msg(4, "Failed to mount %s at %s", cdrom_dev, mtpt);
3312 log_to_screen("If there's a CD/DVD in the drive, it's blank.");
3313 /*
3314 if (interrogate_disk_currently_in_cdrw_drive(cdrw_dev, FALSE))
3315 {
3316 ok_go_ahead_burn_it = FALSE;
3317 log_to_screen("There isn't a writable CD/DVD in the drive.");
3318 }
3319 else
3320 {
3321 log_to_screen("Confirmed. There is a blank CD/DVD in the drive.");
3322 }
3323 */
3324 } else if (!does_file_exist(szcdno)
3325 || !does_file_exist(szserfname)) {
3326 log_to_screen
3327 ("%s has data on it but it's probably not a Mondo CD.",
3328 media_descriptor_string(g_backup_media_type));
3329 } else {
3330 log_to_screen("%s found in drive. It's a Mondo disk.",
3331 media_descriptor_string(g_backup_media_type));
3332 cd_number = atoi(last_line_of_file(szcdno));
3333 sprintf(tmp, "cat %s 2> /dev/null", szserfname);
3334 strcpy(our_serial_str,
3335 call_program_and_get_last_line_of_output(tmp));
3336 // FIXME - should be able to use last_line_of_file(), surely?
3337 }
3338 run_program_and_log_output(szunmount, 1);
3339 log_msg(2, "paafcd: cd_number = %d", cd_number);
3340 log_msg(2, "our serial str = %s; g_serial_string = %s",
3341 our_serial_str, g_serial_string);
3342 if (cd_number > 0 && !strcmp(our_serial_str, g_serial_string)) {
3343 log_msg(2, "This %s is part of this backup set!",
3344 media_descriptor_string(g_backup_media_type));
3345 ok_go_ahead_burn_it = FALSE;
3346 if (cd_number == g_current_media_number - 1) {
3347 log_to_screen
3348 ("I think you've left the previous %s in the drive.",
3349 media_descriptor_string(g_backup_media_type));
3350 } else {
3351 log_to_screen
3352 ("Please remove this %s. It is part of the backup set you're making now.",
3353 media_descriptor_string(g_backup_media_type));
3354 }
3355 } else {
3356 log_to_screen("...but not part of _our_ backup set.");
3357 }
3358 } else {
3359 log_msg(2,
3360 "paafcd: Can't find CD-ROM drive. Perhaps it has a blank %s in it?",
3361 media_descriptor_string(g_backup_media_type));
3362 if (interrogate_disk_currently_in_cdrw_drive(cdrw_dev, FALSE)) {
3363 ok_go_ahead_burn_it = FALSE;
3364 log_to_screen("There isn't a writable %s in the drive.",
3365 media_descriptor_string(g_backup_media_type));
3366 }
3367 }
3368
3369/*
3370 if (g_current_media_number > ask_for_one_if_more_than_this)
3371 {
3372 ok_go_ahead_burn_it = FALSE;
3373 log_it("paafcd: %d > %d, so I'll definitely pause.", g_current_media_number > ask_for_one_if_more_than_this);
3374 }
3375*/
3376
3377 if (!ok_go_ahead_burn_it) {
3378 eject_device(cdrom_dev);
3379 sprintf(tmp,
3380 "I am about to burn %s #%d of the backup set. Please insert %s and press Enter.",
3381 media_descriptor_string(g_backup_media_type),
3382 g_current_media_number,
3383 media_descriptor_string(g_backup_media_type));
3384 popup_and_OK(tmp);
3385 goto gotos_make_me_puke;
3386 } else {
3387 log_msg(2, "paafcd: OK, going ahead and burning it.");
3388 }
3389
3390 log_msg(2,
3391 "paafcd: OK, I assume I have a blank/reusable %s in the drive...",
3392 media_descriptor_string(g_backup_media_type));
3393
3394 // if (ask_for_one_if_more_than_this>1) { popup_and_OK(szmsg); }
3395
3396 log_to_screen("Proceeding w/ %s in drive.",
3397 media_descriptor_string(g_backup_media_type));
3398 paranoid_free(tmp);
3399 paranoid_free(szmsg);
3400 paranoid_free(cdrom_dev);
3401 paranoid_free(cdrw_dev);
3402 paranoid_free(mtpt);
3403 paranoid_free(szcdno);
3404 paranoid_free(szserfname);
3405 paranoid_free(our_serial_str);
3406 paranoid_free(szunmount);
3407 if (pmountable) {
3408 if (attempt_to_mount_returned_this) {
3409 *pmountable = FALSE;
3410 } else {
3411 *pmountable = TRUE;
3412 }
3413 }
3414
3415}
3416
3417
3418
3419
3420
3421
3422
3423
3424/**
3425 * Set the <tt>N</tt>th bit of @c array to @c true_or_false.
3426 * @param array The bit array (as a @c char pointer).
3427 * @param N The bit number to set or reset.
3428 * @param true_or_false If TRUE then set bit @c N, if FALSE then reset bit @c N.
3429 * @see get_bit_N_of_array
3430 */
3431void set_bit_N_of_array(char *array, int N, bool true_or_false)
3432{
3433 int bit_number;
3434 int mask, orig_val, to_add;
3435 int element_number;
3436
3437 assert(array != NULL);
3438
3439 element_number = N / 8;
3440 bit_number = N % 8;
3441 to_add = (1 << bit_number);
3442 mask = 255 - to_add;
3443 orig_val = array[element_number] & mask;
3444 // log_it("array[%d]=%02x; %02x&%02x = %02x", element_number, array[element_number], mask, orig_val);
3445 if (true_or_false) {
3446 array[element_number] = orig_val | to_add;
3447 }
3448}
3449
3450/* @} - end of utilityGroup */
3451
3452
3453
3454
3455
3456
3457
3458
3459/**
3460 * Chop up @c filename.
3461 * @param bkpinfo The backup information structure. Fields used:
3462 * - @c backup_media_type
3463 * - @c compression_level
3464 * - @c optimal_set_size
3465 * - @c tmpdir
3466 * - @c use_lzo
3467 * - @c zip_exe
3468 * - @c zip_suffix
3469 *
3470 * @param biggie_filename The file to chop up.
3471 * @param ntfsprog_fifo The FIFO to ntfsclone if this is an imagedev, NULL otherwise.
3472 * @param biggie_file_number The sequence number of this biggie file (starting from 0).
3473 * @param noof_biggie_files The number of biggie files there are total.
3474 * @return The number of errors encountered (0 for success)
3475 * @see make_slices_and_images
3476 * @ingroup LLarchiveGroup
3477 */
3478int
3479slice_up_file_etc(char *biggie_filename,
3480 char *ntfsprog_fifo, long biggie_file_number,
3481 long noof_biggie_files, bool use_ntfsprog)
3482{
3483
3484 /*@ buffers ************************************************** */
3485 char *tmp, *checksum_line, *command;
3486 char *tempblock;
3487 char *curr_slice_fname_uncompressed;
3488 char *curr_slice_fname_compressed;
3489 char *file_to_archive;
3490 char *file_to_openin;
3491 /*@ pointers ************************************************** */
3492 char *pB;
3493 FILE *fin = NULL, *fout = NULL;
3494
3495 /*@ bool ****************************************************** */
3496 bool finished = FALSE;
3497
3498 /*@ long ****************************************************** */
3499 size_t blksize = 0;
3500 long slice_num = 0;
3501 long i;
3502 long optimal_set_size;
3503 bool should_I_compress_slices;
3504 char *suffix; // for compressed slices
3505
3506 /*@ long long ************************************************** */
3507 off_t totalread = (off_t)0;
3508 off_t totallength = (off_t)0;
3509 off_t length;
3510
3511 /*@ int ******************************************************** */
3512 int retval = 0;
3513 int res = 0;
3514
3515 /*@ structures ************************************************** */
3516 struct s_filename_and_lstat_info biggiestruct;
3517// struct stat statbuf;
3518
3519 assert(bkpinfo != NULL);
3520 assert_string_is_neither_NULL_nor_zerolength(biggie_filename);
3521 malloc_string(tmp);
3522 malloc_string(checksum_line);
3523 malloc_string(curr_slice_fname_uncompressed);
3524 malloc_string(curr_slice_fname_compressed);
3525 malloc_string(file_to_archive);
3526 malloc_string(suffix);
3527 command = malloc(MAX_STR_LEN * 8);
3528
3529 biggiestruct.for_backward_compatibility = '\n';
3530 biggiestruct.use_ntfsprog = use_ntfsprog;
3531 if (!(tempblock = (char *) malloc(256 * 1024))) {
3532 fatal_error("malloc error 256*1024");
3533 }
3534 optimal_set_size = bkpinfo->optimal_set_size;
3535 if (is_this_file_compressed(biggie_filename)
3536 || bkpinfo->compression_level == 0) {
3537 suffix[0] = '\0';
3538 // log_it("%s is indeed compressed :-)", filename);
3539 should_I_compress_slices = FALSE;
3540 } else {
3541 strcpy(suffix, bkpinfo->zip_suffix);
3542 should_I_compress_slices = TRUE;
3543 }
3544
3545 if (optimal_set_size < 999) {
3546 fatal_error("bkpinfo->optimal_set_size is insanely small");
3547 }
3548 if (ntfsprog_fifo) {
3549 file_to_openin = ntfsprog_fifo;
3550 strcpy(checksum_line, "IGNORE");
3551 log_msg(2,
3552 "Not calculating checksum for %s: it would take too long",
3553 biggie_filename);
3554 if ( !find_home_of_exe("ntfsresize")) {
3555 fatal_error("ntfsresize not found");
3556 }
3557 sprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", biggie_filename);
3558 log_it("command = %s", command);
3559 strcpy (tmp, call_program_and_get_last_line_of_output(command));
3560 log_it("res of it = %s", tmp);
3561 totallength = (off_t)atoll(tmp);
3562 } else {
3563 file_to_openin = biggie_filename;
3564 if (strchr(biggie_filename,'\'') != NULL) {
3565 sprintf(command, "md5sum \"%s\"", biggie_filename);
3566 } else {
3567 sprintf(command, "md5sum '%s'", biggie_filename);
3568 }
3569 if (!(fin = popen(command, "r"))) {
3570 log_OS_error("Unable to popen-in command");
3571 return (1);
3572 }
3573 (void) fgets(checksum_line, MAX_STR_LEN, fin);
3574 pclose(fin);
3575 totallength = length_of_file (biggie_filename);
3576 }
3577 lstat(biggie_filename, &biggiestruct.properties);
3578 strcpy(biggiestruct.filename, biggie_filename);
3579 pB = strchr(checksum_line, ' ');
3580 if (!pB) {
3581 pB = strchr(checksum_line, '\t');
3582 }
3583 if (pB) {
3584 *pB = '\0';
3585 }
3586 strcpy(biggiestruct.checksum, checksum_line);
3587
3588 strcpy(tmp, slice_fname(biggie_file_number, 0, bkpinfo->tmpdir, ""));
3589 fout = fopen(tmp, "w");
3590 if (fout == NULL) {
3591 log_msg(1, "Unable to open and write to %s\n", tmp);
3592 paranoid_free(tmp);
3593 paranoid_free(suffix);
3594 return (1);
3595 }
3596
3597 (void) fwrite((void *) &biggiestruct, 1, sizeof(biggiestruct), fout);
3598 paranoid_fclose(fout);
3599 length = totallength / optimal_set_size / 1024;
3600 log_msg(1, "Opening in %s; slicing it and writing to CD/tape",
3601 file_to_openin);
3602 if (!(fin = fopen(file_to_openin, "r"))) {
3603 log_OS_error("Unable to openin biggie_filename");
3604 sprintf(tmp, "Cannot archive bigfile '%s': not found",
3605 biggie_filename);
3606 log_to_screen(tmp);
3607 paranoid_free(tempblock);
3608 paranoid_free(tmp);
3609 paranoid_free(checksum_line);
3610 paranoid_free(command);
3611 return (1);
3612 }
3613 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
3614 res =
3615 move_files_to_stream(slice_fname(biggie_file_number, 0,
3616 bkpinfo->tmpdir, ""), NULL);
3617 } else {
3618 res =
3619 move_files_to_cd(slice_fname(biggie_file_number, 0,
3620 bkpinfo->tmpdir, ""), NULL);
3621 }
3622 i = bkpinfo->optimal_set_size / 256;
3623 for (slice_num = 1; !finished; slice_num++) {
3624 strcpy(curr_slice_fname_uncompressed,
3625 slice_fname(biggie_file_number, slice_num, bkpinfo->tmpdir,
3626 ""));
3627 strcpy(curr_slice_fname_compressed,
3628 slice_fname(biggie_file_number, slice_num, bkpinfo->tmpdir,
3629 suffix));
3630
3631 strcpy(tmp, percent_media_full_comment());
3632 update_progress_form(tmp);
3633 if (!(fout = fopen(curr_slice_fname_uncompressed, "w"))) {
3634 log_OS_error(curr_slice_fname_uncompressed);
3635 return (1);
3636 }
3637 if ((i == bkpinfo->optimal_set_size / 256)
3638 && (totalread < 1.1 * totallength)) {
3639 for (i = 0; i < bkpinfo->optimal_set_size / 256; i++) {
3640 blksize = fread(tempblock, 1, 256 * 1024, fin);
3641 if (blksize > 0) {
3642 totalread = totalread + blksize;
3643 (void) fwrite(tempblock, 1, blksize, fout);
3644 } else {
3645 break;
3646 }
3647 }
3648 } else {
3649 i = 0;
3650 }
3651 paranoid_fclose(fout);
3652 if (i > 0) // length_of_file (curr_slice_fname_uncompressed)
3653 {
3654 if (!does_file_exist(curr_slice_fname_uncompressed)) {
3655 log_msg(2,
3656 "Warning - '%s' doesn't exist. How can I compress slice?",
3657 curr_slice_fname_uncompressed);
3658 }
3659 if (should_I_compress_slices && bkpinfo->compression_level > 0) {
3660 sprintf(command, "%s -%d %s", bkpinfo->zip_exe,
3661 bkpinfo->compression_level,
3662 curr_slice_fname_uncompressed);
3663 log_msg(2, command);
3664 if ((res = system(command))) {
3665 log_OS_error(command);
3666 }
3667 // did_I_compress_slice = TRUE;
3668 } else {
3669 sprintf(command, "mv %s %s 2>> %s",
3670 curr_slice_fname_uncompressed,
3671 curr_slice_fname_compressed, MONDO_LOGFILE);
3672 res = 0; // don't do it :)
3673 // did_I_compress_slice = FALSE;
3674 }
3675 retval += res;
3676 if (res) {
3677 log_msg(2, "Failed to compress the slice");
3678 }
3679 if (bkpinfo->use_lzo
3680 && strcmp(curr_slice_fname_compressed,
3681 curr_slice_fname_uncompressed)) {
3682 unlink(curr_slice_fname_uncompressed);
3683 }
3684 if (res) {
3685 sprintf(tmp, "Problem with slice # %ld", slice_num);
3686 } else {
3687 sprintf(tmp,
3688 "%s - Bigfile #%ld, slice #%ld compressed OK ",
3689 biggie_filename, biggie_file_number + 1,
3690 slice_num);
3691 }
3692#ifndef _XWIN
3693 if (!g_text_mode) {
3694 newtDrawRootText(0, g_noof_rows - 2, tmp);
3695 newtRefresh();
3696 } else {
3697 log_msg(2, tmp);
3698 }
3699#else
3700 log_msg(2, tmp);
3701#endif
3702 strcpy(file_to_archive, curr_slice_fname_compressed);
3703 g_current_progress++;
3704 } else { /* if i==0 then ... */
3705
3706 finished = TRUE;
3707 strcpy(file_to_archive, curr_slice_fname_uncompressed);
3708 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
3709 break;
3710 }
3711 }
3712
3713 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
3714 register_in_tape_catalog(biggieslice, biggie_file_number,
3715 slice_num, file_to_archive);
3716 maintain_collection_of_recent_archives(bkpinfo->tmpdir,
3717 file_to_archive);
3718 res = move_files_to_stream(file_to_archive, NULL);
3719 } else {
3720 res = move_files_to_cd(file_to_archive, NULL);
3721 }
3722 retval += res;
3723 if (res) {
3724 sprintf(tmp,
3725 "Failed to add slice %ld of bigfile %ld to scratchdir",
3726 slice_num, biggie_file_number + 1);
3727 log_to_screen(tmp);
3728 fatal_error
3729 ("Hard disk full. You should have bought a bigger one.");
3730 }
3731 }
3732 paranoid_fclose(fin);
3733 sprintf(tmp, "Sliced bigfile #%ld", biggie_file_number + 1);
3734 if (retval) {
3735 strcat(tmp, "...FAILED");
3736 } else {
3737 strcat(tmp, "...OK!");
3738 }
3739 log_msg(1, tmp);
3740 paranoid_free(tempblock);
3741 paranoid_free(tmp);
3742 paranoid_free(checksum_line);
3743 paranoid_free(command);
3744 paranoid_free(curr_slice_fname_uncompressed);
3745 paranoid_free(curr_slice_fname_compressed);
3746 paranoid_free(file_to_archive);
3747 paranoid_free(suffix);
3748 return (retval);
3749}
3750
3751
3752
3753
3754
3755
3756/**
3757 * Remove the archives in @c d.
3758 * This could possibly include any of:
3759 * - all afioballs (compressed and not)
3760 * - all filelists
3761 * - all slices
3762 * - all checksums
3763 * - a zero filler file
3764 *
3765 * @param d The directory to wipe the archives from.
3766 * @ingroup utilityGroup
3767 */
3768void wipe_archives(char *d)
3769{
3770 /*@ buffers ********************************************* */
3771 char *tmp;
3772 char *dir;
3773
3774 malloc_string(tmp);
3775 malloc_string(dir);
3776 assert_string_is_neither_NULL_nor_zerolength(d);
3777
3778 sprintf(dir, "%s/archives", d);
3779 sprintf(tmp, "find %s -name '*.afio*' -exec rm -f '{}' \\;", dir);
3780 run_program_and_log_output(tmp, FALSE);
3781 sprintf(tmp, "find %s -name '*list.[0-9]*' -exec rm -f '{}' \\;", dir);
3782 run_program_and_log_output(tmp, FALSE);
3783 sprintf(tmp, "find %s -name 'slice*' -exec rm -f '{}' \\;", dir);
3784 run_program_and_log_output(tmp, FALSE);
3785 sprintf(tmp, "rm -f %s/cklist*", dir);
3786 run_program_and_log_output(tmp, FALSE);
3787 sprintf(tmp, "rm -f %s/zero", dir);
3788 run_program_and_log_output(tmp, FALSE);
3789 log_msg(1, "Wiped %s's archives", dir);
3790 sprintf(tmp, "ls -l %s", dir);
3791 run_program_and_log_output(tmp, FALSE);
3792 paranoid_free(tmp);
3793 paranoid_free(dir);
3794}
3795
3796
3797
3798/**
3799 * @addtogroup LLarchiveGroup
3800 * @{
3801 */
3802/**
3803 * Write the final ISO image.
3804 * @param bkpinfo The backup information structure. Used only
3805 * in the call to @c write_iso_and_go_on().
3806 * @return The number of errors encountered (0 for success)
3807 * @see write_iso_and_go_on
3808 * @see make_iso_fs
3809 * @bug The final ISO is written even if there are no files on it. In practice,
3810 * however, this occurs rarely.
3811 */
3812int write_final_iso_if_necessary()
3813{
3814 /*@ int ***************************************************** */
3815 int res;
3816
3817 /*@ buffers ************************************************** */
3818 char *tmp;
3819
3820 malloc_string(tmp);
3821 assert(bkpinfo != NULL);
3822
3823// 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
3824
3825 sprintf(tmp, "Writing the final ISO");
3826 log_msg(2, tmp);
3827 center_string(tmp, 80);
3828#ifndef _XWIN
3829 if (!g_text_mode) {
3830 newtPushHelpLine(tmp);
3831 }
3832#endif
3833 res = write_iso_and_go_on(TRUE);
3834#ifndef _XWIN
3835 if (!g_text_mode) {
3836 newtPopHelpLine();
3837 }
3838#endif
3839 log_msg(2, "Returning from writing final ISO (res=%d)", res);
3840 paranoid_free(tmp);
3841 return (res);
3842}
3843
3844
3845/**
3846 * Write an ISO image to <tt>[bkpinfo->isodir]/bkpinfo->prefix-[g_current_media_number].iso</tt>.
3847 * @param bkpinfo The backup information structure. Fields used:
3848 * - @c backup_media_type
3849 * - @c prefix
3850 * - @c isodir
3851 * - @c manual_cd_tray
3852 * - @c media_size
3853 * - @c nfs_mount
3854 * - @c nfs_remote_dir
3855 * - @c scratchdir
3856 * - @c verify_data
3857 *
3858 * @param last_cd If TRUE, this is the last CD to write; if FALSE, it's not.
3859 * @return The number of errors encountered (0 for success)
3860 * @see make_iso_fs
3861 */
3862int write_iso_and_go_on(bool last_cd)
3863{
3864 /*@ pointers **************************************************** */
3865 FILE *fout;
3866
3867 /*@ buffers ***************************************************** */
3868 char *tmp;
3869 char *cdno_fname;
3870 char *lastcd_fname;
3871 char *isofile;
3872
3873 /*@ bool ******************************************************** */
3874 bool that_one_was_ok;
3875 bool orig_vfy_flag_val;
3876
3877 /*@ int *********************************************************** */
3878 int res = 0;
3879
3880 malloc_string(tmp);
3881 malloc_string(cdno_fname);
3882 malloc_string(lastcd_fname);
3883 malloc_string(isofile);
3884
3885 assert(bkpinfo != NULL);
3886 orig_vfy_flag_val = bkpinfo->verify_data;
3887 if (bkpinfo->media_size[g_current_media_number] <= 0) {
3888 fatal_error("write_iso_and_go_on() - unknown media size");
3889 }
3890
3891 log_msg(1, "OK, time to make %s #%d",
3892 media_descriptor_string(bkpinfo->backup_media_type),
3893 g_current_media_number);
3894
3895 /* label the ISO with its number */
3896
3897 sprintf(cdno_fname, "%s/archives/THIS-CD-NUMBER", bkpinfo->scratchdir);
3898 fout = fopen(cdno_fname, "w");
3899 fprintf(fout, "%d", g_current_media_number);
3900 paranoid_fclose(fout);
3901
3902 sprintf(tmp, "cp -f %s/autorun %s/", g_mondo_home,
3903 bkpinfo->scratchdir);
3904 if (run_program_and_log_output(tmp, FALSE)) {
3905 log_msg(2, "Warning - unable to copy autorun to scratchdir");
3906 }
3907
3908 /* last CD or not? Label accordingly */
3909 sprintf(lastcd_fname, "%s/archives/NOT-THE-LAST", bkpinfo->scratchdir);
3910 if (last_cd) {
3911 unlink(lastcd_fname);
3912 log_msg(2,
3913 "OK, you're telling me this is the last CD. Fair enough.");
3914 } else {
3915 fout = fopen(lastcd_fname, "w");
3916 fprintf(fout,
3917 "You're listening to 90.3 WPLN, Nashville Public Radio.\n");
3918 paranoid_fclose(fout);
3919 }
3920 if (space_occupied_by_cd(bkpinfo->scratchdir) / 1024 >
3921 bkpinfo->media_size[g_current_media_number]) {
3922 sprintf(tmp,
3923 "Warning! CD is too big. It occupies %ld KB, which is more than the %ld KB allowed.",
3924 (long) space_occupied_by_cd(bkpinfo->scratchdir),
3925 (long) bkpinfo->media_size[g_current_media_number]);
3926 log_to_screen(tmp);
3927 }
3928 sprintf(isofile, "%s/%s/%s-%d.iso", bkpinfo->isodir,
3929 bkpinfo->nfs_remote_dir, bkpinfo->prefix,
3930 g_current_media_number);
3931 for (that_one_was_ok = FALSE; !that_one_was_ok;) {
3932 if (bkpinfo->backup_media_type != usb) {
3933 res = make_iso_fs(isofile);
3934 } else {
3935 res = make_usb_fs();
3936 }
3937 if (g_current_media_number == 1 && !res
3938 && (bkpinfo->backup_media_type == cdr
3939 || bkpinfo->backup_media_type == cdrw)) {
3940 if (find_cdrom_device(tmp, FALSE)) // make sure find_cdrom_device() finds, records CD-R's loc
3941 {
3942 log_msg(3, "*Sigh* Mike, I hate your computer.");
3943 bkpinfo->manual_cd_tray = TRUE;
3944 } // if it can't be found then force pausing
3945 else {
3946 log_msg(3, "Great. Found Mike's CD-ROM drive.");
3947 }
3948 }
3949 if (bkpinfo->verify_data && !res) {
3950 log_to_screen
3951 ("Please reboot from the 1st %s in Compare Mode, as a precaution.",
3952 media_descriptor_string(g_backup_media_type));
3953 chdir("/");
3954 iamhere("Before calling verification of image()");
3955 if (bkpinfo->backup_media_type == usb) {
3956 res += verify_usb_image();
3957 } else {
3958 res += verify_cd_image();
3959 }
3960 iamhere("After calling verification of image()");
3961 }
3962 if (!res) {
3963 that_one_was_ok = TRUE;
3964 } else {
3965 sprintf(tmp, "Failed to create %s #%d. Retry?",
3966 media_descriptor_string(bkpinfo->backup_media_type),
3967 g_current_media_number);
3968 res = ask_me_yes_or_no(tmp);
3969 if (!res) {
3970 if (ask_me_yes_or_no("Abort the backup?")) {
3971 fatal_error("FAILED TO BACKUP");
3972 } else {
3973 break;
3974 }
3975 } else {
3976 log_msg(2, "Retrying, at user's request...");
3977 res = 0;
3978 }
3979 }
3980 }
3981 g_current_media_number++;
3982 if (g_current_media_number > MAX_NOOF_MEDIA) {
3983 fatal_error("Too many media. Use tape or net.");
3984 }
3985 wipe_archives(bkpinfo->scratchdir);
3986 sprintf(tmp, "rm -Rf %s/images/*gz %s/images/*data*img",
3987 bkpinfo->scratchdir, bkpinfo->scratchdir);
3988 if (system(tmp)) {
3989 log_msg
3990 (2,
3991 "Error occurred when I tried to delete the redundant IMGs and GZs");
3992 }
3993
3994 if (last_cd) {
3995 log_msg(2, "This was your last media.");
3996 } else {
3997 log_msg(2, "Continuing to backup your data...");
3998 }
3999
4000 bkpinfo->verify_data = orig_vfy_flag_val;
4001 paranoid_free(tmp);
4002 paranoid_free(cdno_fname);
4003 paranoid_free(lastcd_fname);
4004 paranoid_free(isofile);
4005 return (0);
4006}
4007
4008/* @} - end of LLarchiveGroup */
4009
4010
4011
4012
4013/**
4014 * Verify the user's data.
4015 * @param bkpinfo The backup information structure. Fields used:
4016 * - @c backup_data
4017 * - @c backup_media_type
4018 * - @c media_device
4019 * - @c verify_data
4020 *
4021 * @return The number of errors encountered (0 for success)
4022 * @ingroup verifyGroup
4023 */
4024int verify_data()
4025{
4026 int res = 0, retval = 0, cdno = 0;
4027 char *tmp;
4028 long diffs = 0;
4029
4030 malloc_string(tmp);
4031 assert(bkpinfo != NULL);
4032 if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
4033 chdir("/");
4034 mvaddstr_and_log_it(g_currentY, 0,
4035 "Verifying archives against live filesystem");
4036 if (bkpinfo->backup_media_type == cdstream) {
4037 strcpy(bkpinfo->media_device, "/dev/cdrom");
4038 }
4039 verify_tape_backups();
4040 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
4041 } else if (bkpinfo->backup_data)
4042 //bkpinfo->backup_media_type == cdrw || bkpinfo->backup_media_type == cdr))
4043 {
4044 log_msg(2,
4045 "Not verifying again. Per-CD/ISO verification already carried out.");
4046 sprintf(tmp, "cat %s/changed.files > %s/changed.files 2> /dev/null",bkpinfo->tmpdir, MONDO_CACHE);
4047 paranoid_system(tmp);
4048 } else {
4049 g_current_media_number = cdno;
4050 if (bkpinfo->backup_media_type != iso) {
4051 find_cdrom_device(bkpinfo->media_device, FALSE); // replace 0,0,0 with /dev/cdrom
4052 }
4053 chdir("/");
4054 for (cdno = 1; cdno < 99 && bkpinfo->verify_data; cdno++) {
4055 if (cdno != g_current_media_number) {
4056 log_msg(2,
4057 "Warning - had to change g_current_media_number from %d to %d",
4058 g_current_media_number, cdno);
4059 g_current_media_number = cdno;
4060 }
4061 if (bkpinfo->backup_media_type != iso) {
4062 insist_on_this_cd_number(cdno);
4063 }
4064 res = verify_cd_image(); // sets verify_data to FALSE if it's time to stop verifying
4065 retval += res;
4066 if (res) {
4067 sprintf(tmp,
4068 "Warnings/errors were reported while checking %s #%d",
4069 media_descriptor_string(bkpinfo->backup_media_type),
4070 g_current_media_number);
4071 log_to_screen(tmp);
4072
4073 }
4074 }
4075/*
4076 sprintf (tmp,
4077 "cat %s | grep \"afio: \" | cut -d'\"' -f2 | sort -u | awk '{print \"/\"$0;};' | tr -s '/' '/' | grep -vx \"/afio:.*\" > /tmp/changed.files",
4078 MONDO_LOGFILE);
4079 system (tmp);
4080*/
4081 sprintf(tmp,
4082 "grep 'afio: ' %s | sed 's/afio: //' | grep -vE '^/dev/.*$' >> %s/changed.files",
4083 MONDO_LOGFILE, MONDO_CACHE);
4084 system(tmp);
4085
4086 sprintf(tmp,
4087 "grep 'star: ' %s | sed 's/star: //' | grep -vE '^/dev/.*$' >> %s/changed.files",
4088 MONDO_LOGFILE, MONDO_CACHE);
4089 system(tmp);
4090 run_program_and_log_output("umount " MNT_CDROM, FALSE);
4091// if (bkpinfo->backup_media_type != iso && !bkpinfo->please_dont_eject_when_restoring)
4092//{
4093 eject_device(bkpinfo->media_device);
4094//}
4095 }
4096 sprintf(tmp, "%s/changed.files", MONDO_CACHE);
4097 diffs = count_lines_in_file(tmp);
4098
4099 if (diffs > 0) {
4100 if (retval == 0) {
4101 retval = (int) (-diffs);
4102 }
4103 }
4104 paranoid_free(tmp);
4105 return (retval);
4106}
4107
4108
4109
4110
4111
4112/**
4113 * @addtogroup utilityGroup
4114 * @{
4115 */
4116/**
4117 * Write an image to a real 3.5" floppy disk.
4118 * @param device The device to write to (e.g. @c /dev/fd0)
4119 * @param datafile The image to write to @p device.
4120 * @return The number of errors encountered (0 for success)
4121 * @see write_image_to_floppy
4122 */
4123int write_image_to_floppy_SUB(char *device, char *datafile)
4124{
4125 /*@ int *************************************************************** */
4126 int res = 0;
4127 int percentage = 0;
4128 int blockno = 0;
4129 int maxblocks = 0;
4130
4131 /*@ buffers************************************************************ */
4132 char *tmp;
4133 char blk[1024];
4134 char *title;
4135
4136 /*@ pointers ********************************************************** */
4137 char *p;
4138 FILE *fout, *fin;
4139
4140
4141 malloc_string(tmp);
4142 malloc_string(title);
4143 /* pretty stuff */
4144 if (!(p = strrchr(datafile, '/'))) {
4145 p = datafile;
4146 } else {
4147 p++;
4148 }
4149 sprintf(title, "Writing %s to floppy", p);
4150 open_evalcall_form(title);
4151 /* functional stuff */
4152 for (p = device + strlen(device); p != device && isdigit(*(p - 1));
4153 p--);
4154 maxblocks = atoi(p);
4155 if (!maxblocks) {
4156 maxblocks = 1440;
4157 }
4158 sprintf(tmp, "maxblocks = %d; p=%s", maxblocks, p);
4159 log_msg(2, tmp);
4160 /* copy data from image to floppy */
4161 if (!(fin = fopen(datafile, "r"))) {
4162 log_OS_error("Cannot open img");
4163 return (1);
4164 }
4165 if (!(fout = fopen(device, "w"))) {
4166 log_OS_error("Cannot open fdd");
4167 return (1);
4168 }
4169 for (blockno = 0; blockno < maxblocks; blockno++) {
4170 percentage = blockno * 100 / maxblocks;
4171 if (fread(blk, 1, 1024, fin) != 1024) {
4172 if (feof(fin)) {
4173 log_msg(1,
4174 "img read err - img ended prematurely - non-fatal error");
4175 sleep(3);
4176 return (res);
4177 }
4178 res++;
4179 log_to_screen("img read err");
4180 }
4181 if (fwrite(blk, 1, 1024, fout) != 1024) {
4182 res++;
4183 log_to_screen("fdd write err");
4184 }
4185 if (((blockno + 1) % 128) == 0) {
4186 paranoid_system("sync"); /* fflush doesn't work; dunno why */
4187 update_evalcall_form(percentage);
4188 }
4189 }
4190 paranoid_fclose(fin);
4191 paranoid_fclose(fout);
4192 paranoid_free(tmp);
4193 paranoid_free(title);
4194 close_evalcall_form();
4195 return (res);
4196}
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206/**
4207 * Wrapper around @c write_image_to_floppy_SUB().
4208 * This function, unlike @c write_image_to_floppy_SUB(),
4209 * gives the user the opportunity to retry if the write fails.
4210 * @see write_image_to_floppy_SUB
4211 */
4212int write_image_to_floppy(char *device, char *datafile)
4213{
4214 /*@ int ************************************************************** */
4215 int res = 0;
4216
4217 assert_string_is_neither_NULL_nor_zerolength(device);
4218 assert_string_is_neither_NULL_nor_zerolength(datafile);
4219
4220 while ((res = write_image_to_floppy_SUB(device, datafile))) {
4221 if (!ask_me_yes_or_no("Failed to write image to floppy. Retry?")) {
4222 return (res);
4223 }
4224 }
4225 return (res);
4226}
4227
4228/* @} - end of utilityGroup */
4229
4230void setenv_mondo_share(void) {
4231
4232setenv("MONDO_SHARE", MONDO_SHARE, 1);
4233}
Note: See TracBrowser for help on using the repository browser.