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

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

r3336@localhost: bruno | 2009-08-11 16:32:36 +0200

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