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

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

More compiler fixes

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