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

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

Remove useless conf files created under /tmp and never used

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