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

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

merge -r1042:1045 $SVN_M/branches/stable

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