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

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