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

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