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

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