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

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