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

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

Remove the possibility to have non bootable media

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