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

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