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

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

Modify the get_netx_raitab_line function prototype to reduce static memory allocation

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