source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-archive.c@ 2323

Last change on this file since 2323 was 2323, checked in by Bruno Cornec, 15 years ago

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

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