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

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

Removal of g_mondo_home useless and used MONDO_SHARE instead

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