source: MondoRescue/branches/stable/mondo/src/common/libmondo-archive.c@ 1136

Last change on this file since 1136 was 1136, checked in by Bruno Cornec, 17 years ago

Typo + new libmondo-archive.c merges

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