source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-archive.c@ 2241

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

r3144@localhost: bruno | 2009-06-26 12:18:08 +0200

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