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

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