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

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

r3341@localhost: bruno | 2009-08-13 22:36:24 +0200

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