source: MondoRescue/branches/3.1/mondo/src/common/libmondo-archive.c@ 3147

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