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

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