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

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

r3342@localhost: bruno | 2009-08-14 00:46:51 +0200

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