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

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

r3335@localhost: bruno | 2009-08-08 23:04:12 +0200

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