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

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

Update trunk with the latest feedback from stable (obtained with meld) - First pass

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