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

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

r3263@localhost: bruno | 2009-07-13 02:25:52 +0200

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