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

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