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

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

paranoid_free => mr_free after stable merge

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