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

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

-c means now optical backup for CD/DVD. Removed dvd special mode -r

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