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

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

Do not try to copy follpy images anymore (removes a warning)

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