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

Last change on this file since 3858 was 3858, checked in by Bruno Cornec, 2 months ago

remove center_string function to keep only mr_center_string

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