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

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

mr_gettext.h added where necessary
mr_conf is now a pointer on a struct aveywhere

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