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

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

r3369@localhost: bruno | 2009-08-18 16:57:27 +0200

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