source: MondoRescue/branches/3.0/mondo/src/common/libmondo-archive.c@ 2985

Last change on this file since 2985 was 2985, checked in by Bruno Cornec, 12 years ago

r4608@localhost: bruno | 2012-03-29 20:28:19 +0200

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