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

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