source: MondoRescue/branches/3.2/mondo/src/common/libmondo-archive.c@ 3197

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