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

Last change on this file since 1693 was 1693, checked in by Bruno Cornec, 17 years ago
  • Remove useless copy from mindi to mondo at end of USB handling
  • Fix PB macro vs tools/*2build issue
  • make_usb_fs change of interface (doesn't need a parameter)
  • Fix USB support in mondo to avoid multiple copies of files
  • Use first partiion in mondo for USB device
  • Fixes for USB CLI for both mondo/mindi
  • Try to add USB support for mondoarchive with new functions
  • du => deb for similarity with other distro type under pbconf
  • migrate gento build files under pb
  • remove now obsolete rpm spec file and gentoo build files from distributions
  • Remove DOCDIR usage in mindi + various build fixes

(merge -r1680:1692 $SVN_M/branches/2.2.5)

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