source: MondoRescue/branches/3.1/mondo/src/common/libmondo-archive.c@ 3148

Last change on this file since 3148 was 3148, checked in by Bruno Cornec, 11 years ago

2nd phase for svn merge -r 2935:3146 ../3.0

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