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

Last change on this file since 1769 was 1769, checked in by Bruno Cornec, 16 years ago

Continue on configuration file items (compression)

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