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

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