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

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

svn merge -r 1923:1938 $SVN_M/branches/2.2.6

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