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

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

Cosmetic changes to allow for quality to not giv false positive

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