source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-archive.c@ 2211

Last change on this file since 2211 was 2211, checked in by Bruno Cornec, 15 years ago

r3089@localhost: bruno | 2009-05-18 06:41:05 +0200

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