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

Last change on this file since 2601 was 2601, checked in by Bruno Cornec, 14 years ago

r3755@localhost: bruno | 2010-03-16 22:03:43 +0100

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