source: MondoRescue/branches/2.2.8/mondo/src/common/libmondo-archive.c@ 2791

Last change on this file since 2791 was 2791, checked in by Bruno Cornec, 13 years ago

r2161@localhost (orig r2160): bruno | 2009-03-06 10:53:57 +0100

  • Exclude filesystems type ptmpfs and devpts from being parsed. Should solve aproblem seen with large /dev/shm being backed up.


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