source: MondoRescue/branches/2.2.6/mondo/src/common/libmondo-archive.c@ 1956

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

Use non-rewinding tape name earlier to pass it to mindi so that at restore time we have the right device name for OBDR

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