source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-archive.c@ 2850

Last change on this file since 2850 was 2850, checked in by Bruno Cornec, 13 years ago

svn merge -r 2773:2849 2.2.9 in 2.2.10

  • Adds 3 binaries called potentially by udev o support USB key mount at restore time (Victor Gattegno)
  • Really support both mkisofs and genisoimage everywhere
  • Try to handle netfs_user better in all cases (NFS and SSHFS)
    • Improve logging in init script
    • Format improvement
    • Removes a warning when trying to launch udevadm and it doesn't exist (RHEL 5 e.g.)
    • Fix syntax description in mondoarchive man page for -E & -I with |
  • Adds download entries for new distro supported (Mageia, Fedora 15, Ubuntu 11.04)

-Fix mindi-get-perl-modules when perl dirs in @INC are symlinks (case on Ubuntu 11.04)

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