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

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