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

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