source: MondoRescue/trunk/mondo/src/common/libmondo-archive.c@ 1106

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

merge -r1082:1105 $SVN_M/branches/stable

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