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

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

r3329@localhost: bruno | 2009-08-05 00:33:17 +0200
bkpinfo->exclude_paths is now dynamically allocated

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