source: MondoRescue/branches/3.3/mondo/src/common/libmondo-archive.c@ 3870

Last change on this file since 3870 was 3870, checked in by Bruno Cornec, 4 months ago

Rewrite find_tape_device ans start for other find_*dev functions

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