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

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

Apply patch from Andree Leidenfrost, modified a bit to use bkpinfo->tmpdir instead of /tmp
or MINDI_CACHE when appropriate. Fix security issues in mondo
Thanks al ot Andree for catching all those issues.
Will not compile needs more work as bkpinfo->tmpdir isn't available everywhere
Should become a global in 3.x when only containing pointers.

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