source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-archive.c@ 2301

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