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

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

removal of g_skip_floppies now useless

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