source: MondoRescue/branches/stable/mondo/src/common/libmondo-archive.c@ 1581

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

Continue to remove floppy support

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