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

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