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

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

r3337@localhost: bruno | 2009-08-11 20:02:18 +0200
bkpinfo->boot_device and bkpinfo->zip_exe are now dynamically allocated

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