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

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