1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
|
/*
Time-stamp: <00/01/14 13:35:05 yusuf>
$Id: backup.c,v 1.66 1998/06/07 04:45:15 yusuf Exp $
*/
#ifndef lint
static char vcid[] = "$Id: backup.c,v 1.66 1998/06/07 04:45:15 yusuf Exp $";
#endif /* lint */
/* Tape structure
*
* Taper header (at beginning of each tape)
* ----------------------------------------
* long taper_magic number (identifies taper backups )
* long archive ID (unqiue # that IDs archives )
* int tape number )
* char archive_title
*
*
* Volume
* ------
*
* Volume header information
*
* DATA
* : file_info
* : file_name
* : file (straight or compressed) only if file IS_IFREG
* : S_ISDIR nothing
* : S_ISLNK the name of the symbolic link (# chars first)
*
*
* File info block: 52 bytes
*
* long actual size with uncompressed files, = size
* volume
* position in vol
* rdev )
* uid )
* gid )
* mode ) Taken from stat call
* org mode (mode of original file for links)
* size )
* a time )
* m time )
* c time )
* backup time
* length of name+1
* compressed or not compressed
* checksum
*
*
* Information file structure
*
* long magic
* long archive_id )
* int number_tapes ) in structure
* int number_volumes )
* long size_of_volume_headers
* long no_in_archive )
* char archive_title )
*
* IF COMPRESSED long size_compressed
*
* For each file:
* file_info
* file_name
*
*
* Volume details
* vol header ) for each volume
* files selected ) on archive
*
* Volume/tape information
* volume tape it's on - for each volume
*
* Tape size information
* tape size in blocks
*
*
* The info file is written in little endian format.
*
* The info file can be compressed (using internal gzip).
* If it is, then there is an additional field - length of compressed
* info file
*/
#include "taper.h"
#include "backup.h"
FILE *fdfifo;
char fifo_name[MAX_FNAME];
void backup_stop_child(void)
{
/* Stops child from compressing and deletes whatever files
* are left in temporary buffers */
char *fn, *cf;
if (backup_child) {
kill(backup_child, SIGTERM); /* tell child to stop compressing */
waitpid(backup_child, NULL, 0);
backup_child = 0;
}
while (fifo_receive_file (fdfifo, &fn, &cf) && fn != NULL)
if (fn != fifo_disk_full && cf != NULL)
unlink(cf); /* delete compressed file */
fclose(fdfifo); /* close FIFO */
fdfifo = NULL;
unlink(fifo_name); /* remove file list & FIFO */
}
_errstat backup_dpd(char *full_path, struct stat *b)
{
/* The routine for each file found by process_dir */
struct file_info fi;
dev_t dev;
dev = get_file_info(full_path, &fi, 0, b);
if (dev == 0) return 0; /* error - ignore */
if (S_ISDIR(fi.mode)) /* directories have trailing '/' */
if (full_path[strlen(full_path)-1] != '/')
strcat(full_path, "/");
if (for_backup(full_path))
if (add_to_info(&fi, full_path, FALSE) != -1)
total_selected += sizeof(struct file_info) + fi.name_len;
return 0;
}
_errstat add_actual_files(WINDOW *mes_box)
{
_s32 count;
struct selected_entry *se;
se = sel_files;
for (count=0; count<no_sel; count++) {
if ( (se->selected == 1) || /* only if directly selected */
(se->selected == 4) ) {
process_dir(NULL, 0, se->i.name, se->incremental, backup_dpd, TRUE);
}
se++;
}
return 0;
}
_errstat write_nameinfo(struct file_info *fi, char *cp)
{
/* An error writing to the backup device is a fatal error
Returns 0 if all OK
Returns -1 if error
*/
char l[MAX_FNAME+50];
sprintf(l, "Writing info & filename for %s", cp);
if (tape_write_fi(fi) == -1) {
write_fatal_log(l); /* write info */
return -1;
}
if (tape_write((char *) cp, fi->name_len) == -1) {/* write filename */
write_fatal_log(l);
return -1;
}
return 0;
}
_errstat backup_file(WINDOW *mes, struct info_file_data *i_data)
{
/* Backup the file pointed to by cp and update cp
* An error writing to the backup device is a fatal error
Returns 0 if all OK
Returns -1 if error
*/
char tmpf[MAX_FNAME], *cf, *fn_child;
char *fn, l[MAX_FNAME+50];
int fd;
_s32 x, totalread, totalwrite;
struct stat sbuf, csbuf;
WINDOW *m=NULL;
struct file_info *fi;
i_data->f.volume = (i_data->f.volume < 0) ? -ifd.number_volumes : ifd.number_volumes; /* force to belong to this volume */
fi = &i_data->f; /* fi points to file info */
fn = i_data->name;
if (S_ISLNK(fi->mode)) { /* a soft link */
memset(tmpf, 0, sizeof(tmpf)); /* name of original file */
readlink(fn, tmpf, sizeof(tmpf)); /* is written */
fi->checksum = 0;
fi->act_size = strlen(tmpf)+1+sizeof(_s32);
fi->size = strlen(tmpf)+1+sizeof(_s32);
sprintf(l, "Writing link name for %s [%s]", fn, tmpf);
if (log_level > 1) write_log(l);
if (write_nameinfo( fi, fn) == -1) {
write_fatal_log(l);
return -1;
}
x = strlen(tmpf)+1;
if (tape_write_namelen(tmpf) == -1) {
write_fatal_log(l);
return -1;
}
return 0;
}
if (!(S_ISREG(fi->mode))) { /* if file isn't an ordinary */
fi->checksum = 0;
sprintf(l, "Storing directory %s", fn);
if (log_level > 1) write_log(l);
return write_nameinfo( fi, fn); /* file, only write out name */
}
cf = NULL; /* must be a regular file */
fi->compressed = 0; /* not compressed */
if ((compression) && (!exclude_compression(fn)) && (fdfifo != NULL)) {/* compress if want to and not excluded */
sprintf(l, "Reading filenames from FIFO for %s", fn);
if (log_level > 2) write_log(l);
while (1) {
if (fifo_receive_file (fdfifo, &fn_child, &cf)) {
if (m) {close_statusbox(m); other_mes = 0; touchwin(mes); wrefresh(mes);}
sprintf(l, "Fatal error while reading filenames from FIFO for %s - saving uncompressed", fn);
write_log(l);
cf = NULL;
goto non_compress;
}
if (m) {close_statusbox(m); other_mes = 0; touchwin(mes); wrefresh(mes);}
if (fn_child == NULL) {
sprintf(l, "EOF while reading filenames from FIFO for %s - saving uncompressed", fn);
if (log_level > 2) write_log(l);
cf = NULL;
goto non_compress;
}
if (fn_child == fifo_disk_full) { /* child is waiting for disk space */
if (mes) {
sprintf(l, "Child has no disk space - waiting");
if (log_level > 2) write_log(l);
m = status_box(mes, "Child waiting for disk space", 1, TRUE, 1);
other_mes=1;
continue;
}
}
if (strcmp(fn_child, fn)) { /* checks that FIFO & me agree */
sprintf(l, "Mismatch reading in FIFO output for %s [got %s] - saving uncompressed", fn, fn_child);
if (log_level > 2) write_log(l);
cf = NULL;
goto non_compress;
}
if (cf == NULL) {
sprintf(l, "FIFO didn't compress %s - saving uncompressed", fn);
if (log_level > 2) write_log(l);
goto non_compress; /* assume no compression */
}
break;
}
sprintf(l, "Opening compressed file %s", cf);
if (log_level > 2) write_log(l);
fd = open(cf, O_RDWR); /* open the temp file */
if (fd == -1) {
write_error_log(l);
unlink(cf); /* compressed file, delete it */
fi->checksum = -1; /* mark file as invalid */
return write_nameinfo( fi, fn);
}
sprintf(l, "Getting compressed file status info for %s", cf);
if (log_level > 2) write_log(l);
if (fstat(fd, &csbuf) == -1) { /* get compressed file size */
write_error_log(l);
fi->checksum = -1; /* mark file as invalid */
close(fd);
unlink(cf);
return write_nameinfo( fi, fn);
}
fi->compressed = compression; /* file is compressed */
fi->act_size = csbuf.st_size; /* actual size = size of compressed file */
}
else { /* no compression needed */
non_compress:
sprintf(l, "Opening file %s", fn);
if (log_level > 2) write_log(l);
fd = open(fn, O_RDONLY); /* open source file */
if (fd == -1) {
write_error_log(l);
fi->checksum = -1; /* mark file as invalid */
return write_nameinfo( fi, fn);
}
sprintf(l, "Getting file status info for %s", fn);
if (log_level > 2) write_log(l);
if (fstat(fd, &sbuf) == -1) { /* get actual file size */
write_error_log(l);
fi->checksum = -1; /* mark file as invalid */
close(fd);
return write_nameinfo( fi, fn);
}
fi->act_size = sbuf.st_size; /* update file info */
}
fi->checksum = calc_checksum(fd); /* calculate checksum */
sprintf(l, "Backing up file %s; actual size %d, on tape size %d.",
fn, fi->size, fi->act_size);
if (log_level > 1) write_log(l);
if (write_nameinfo(fi, fn) == -1) { /* couldn't */
write_fatal_log(l);
close(fd);
if (*tmpf) unlink(tmpf);
return -1; /* write info/name - fatal */
}
if (fi->checksum == -1) { /* couldn't get checksum - not fatal */
close(fd);
if (*tmpf) unlink(tmpf);
return 0;
}
totalread = 0;
totalwrite = 0;
while (1) { /* copy file accross */
x = read(fd, tr_buffer, max_tr_size); /* to device file */
if (!x) break;
if (x == -1) {
write_error_log("Error reading file while transferring data to backup device");
while (totalwrite < fi->act_size)
totalwrite += tape_write((char *) tr_buffer, min(max_tr_size,/* pad rubbish data to the end so */
fi->act_size - totalwrite));
totalread = totalwrite;
break; /* so that the archive integrity is maintained */
}
totalread += x;
if (totalwrite < fi->act_size) { /* don't write too much data to buffer */
if ((x = tape_write((char *) tr_buffer, min(x, fi->act_size-totalwrite))) == -1) {
close(fd);
return -1;
}
totalwrite += x;
}
}
close(fd);
if (totalread > totalwrite) {
sprintf(l, "%s grew while writing. Data at end of file not backed up", fn);
write_error_log(l);
}
if (cf != NULL) /* remove temporary files */
unlink(cf);
return 0;
}
_errstat write_volume_header(_s32 no_files)
{
/* Writes the volume header to the tape and appends volume header
* information to the block in memory.
* Returns 0 if OK, -1 if error
*/
_s32 c;
struct selected_entry *se;
int c1, sz;
char *cur_pos;
struct volume_header *vh1, vh;
char null_string=0;
char sname[MAX_FNAME];
if (log_level > 2) write_log("Writing volume header");
vh.volume_magic = VOLUME_MAGIC;
vh.backup_time = time(NULL); /* time backed up */
strcpy(vh.volume_title, volume_title);
vh.no_sels = no_sel+no_exclude;
vh.no_in_volume = no_files;
sz = sizeof(struct volume_header);
se=sel_files; /* work out how much space */
for (c=0; c<no_sel; c++) { /* needed for volume header */
sz += sizeof(_s32) + strlen(se->i.name)+ 1 +
sizeof(_s32)+ 0 + 1; /* room for filter */
se++;
}
se=excluded_files; /* work out how much space */
for (c=0; c<no_exclude; c++) { /* needed for volume header */
sz += sizeof(_s32) + strlen(se->i.name)+ 2 + /* 2 bytes (one for space) */
sizeof(_s32)+ 0 + 1; /* room for filter */
se++;
}
vh.size_header = sz;
ifd.size_volume_headers += sz; /* update size */
if (tape_write_volheader(&vh) == -1) return -1;
vol_headers = my_realloc(vol_headers, ifd.size_volume_headers);
if (vol_headers == NULL)
return do_exit(ERROR_MEMORY);
cur_pos = (char *) vol_headers;
for (c=0; c<ifd.number_volumes-1; c++) { /* skip past existing entries */
vh1 = (struct volume_header *) cur_pos; /* in volume headers */
cur_pos += sizeof(struct volume_header);/* past header */
for (c1=0; c1<vh1->no_sels; c1++) {
sz = *(_s32 *) cur_pos; /* skip selection name */
cur_pos += sz + sizeof(_s32);
sz = *(_s32 *) cur_pos; /* skip filter */
cur_pos += sz + sizeof(_s32);
}
}
*((struct volume_header *) cur_pos) = vh;
cur_pos += sizeof(struct volume_header);
se=sel_files; /* write file selections */
for (c=0; c<no_sel; c++) {
c1 = strlen(se->i.name) + 1; /* file name */
if (tape_write_namelen(se->i.name) == -1)
return -1;
memcpy(cur_pos, &c1, sizeof(_s32)); /* filename length */
cur_pos += sizeof(_s32); /* advance to filename pos */
strcpy(cur_pos, se->i.name);
cur_pos += c1; /* past filename */
c1 = 0+1; /* filter */
if (tape_write_namelen(&null_string) == -1)
return -1;
memcpy(cur_pos, &c1, sizeof(_s32));
cur_pos += sizeof(_s32);
strcpy(cur_pos, ""); /* filter */
cur_pos += c1;
se++;
}
se=excluded_files; /* write file exclusions */
for (c=0; c<no_exclude; c++) {
sname[0] = '!';
strcpy(&sname[1], se->i.name);
c1 = strlen(sname) + 1; /* file name */
if (tape_write_namelen(sname) == -1)
return -1;
memcpy(cur_pos, &c1, sizeof(_s32)); /* filename length */
cur_pos += sizeof(_s32); /* advance to filename pos */
strcpy(cur_pos, sname);
cur_pos += c1; /* past filename */
c1 = 0+1; /* filter */
if (tape_write_namelen(&null_string) == -1)
return -1;
memcpy(cur_pos, &c1, sizeof(_s32));
cur_pos += sizeof(_s32);
strcpy(cur_pos, ""); /* filter */
cur_pos += c1;
se++;
}
return 0;
}
_errstat make_comp_file(char *s)
{
/* Creates a file and gives it a name then writes the names of the files
* to be compressed, filename after filename.
*
Assumes the info file is open
* Returns -1 if error, 0 otherwise
*/
FILE *c1f;
int c;
struct info_file_data i_data;
taper_tmpnam(s);
c1f = fopen(s, "w+"); /* create & open file */
if (c1f == NULL) return -1;
for (c=0; c<ifd.no_in_archive;c++) {
if (read_info_rec(c, &i_data) == -1)
return do_exit(ERROR_READING_INFO);
if (i_data.f.pos_in_archive == 0) /* only if not already in archive */
if (S_ISREG(i_data.f.mode)) { /* only regular files */
fputs(i_data.name, c1f);
fputs("\n", c1f);
}
}
fclose(c1f);
return 0;
}
PRIVATE WINDOW *s_mes=NULL;
PRIVATE _s32 s_count, s_noarchive;
PRIVATE struct info_file_data s_idata;
PRIVATE time_t s_tstart, s_tcurrent;
PRIVATE _u32 bytes_processed, bytes_written;
PRIVATE void backup_alarm(int sig)
{
/* Updates the status box on a SIGALARM */
char s[200], s1[200], s2[200], s3[200];
time_t t_elapsed;
static int prop= 0;
static char propchar[] = "-/|\\";
signal(SIGALRM, SIG_IGN); /* reset alarm */
if (!other_mes) {
t_elapsed=s_tcurrent-s_tstart;
status_box(s_mes, s_idata.name, 0, FALSE, 1);
sprintf(s, "File %d of %d (%c)", s_count, s_noarchive, propchar[prop]);
prop++; prop &=3;
status_box(s_mes, s, 2, FALSE, 1);
sprintf(s, "Processed %s of %s", print_kb(s2, bytes_processed),
print_kb(s3, total_selected));
status_box(s_mes, s, 3, FALSE, 1);
if (bytes_written)
sprintf(s, "Written %s : Ratio %.2f",
print_kb(s2, bytes_written),
(float) bytes_processed/(float) bytes_written);
else
sprintf(s, "Written %s : Ratio 1.0",
print_kb(s2, bytes_written));
status_box(s_mes, s, 4, FALSE, 1);
sprintf(s, "Total on archive %s", print_kb(s2, bytes_written+total_compressed));
status_box(s_mes, s, 5, FALSE, 1);
if (total_selected)
sprintf(s, "%.0f%% done, Elapsed %s, Remaining %s", (float) bytes_processed/(float) total_selected * 100,
convtime(s2, s_tstart, s_tcurrent),
(bytes_processed == 0) ? "" :
convtime(s3, t_elapsed, t_elapsed / ((float) bytes_processed/(float) total_selected)));
else /* if bytes=0, base on # files */
sprintf(s, "%d%% done, Elapsed %s, Remaining %s", 100*s_count/s_noarchive,
convtime(s2, s_tstart, s_tcurrent),
((s_count/s_noarchive) == 0) ? "" :
convtime(s3, t_elapsed, t_elapsed / (s_count/s_noarchive)));
status_box(s_mes, s, 7, FALSE, 1);
if (t_elapsed) {
sprintf(s, "Backup rate %s/min [%s/min]",
print_mb(s1, bytes_written/t_elapsed*60),
print_mb(s2, bytes_processed/t_elapsed*60));
status_box(s_mes, s, 8, FALSE, 1);
}
if (!no_windows) {
touchwin(s_mes); wrefresh(s_mes); /* in case new tape */
}
}
signal(SIGALRM, backup_alarm); /* reset alarm */
retrigger_alarm();
}
_errstat do_backup(void) {
/* For aborting a backup,
*
* fi.checksum = -2 is written - this means a backup
* was aborted. The info file is changed accordingly
*/
_s32 count=0, no_old_files, no_written, org_files;
char s[100], s2[30], s3[30], s4[30], tmpbuf[MAX_FNAME], cfile[MAX_FNAME];
struct volume_tape_info vti;
int x;
_s8 quitting;
struct file_info *fi;
char sa[9][150];
struct volume_header v;
struct info_file_data i_data;
time_t t_current;
if (!no_sel) /* none selected */
return 0;
if (!no_windows)
s_mes = status_box(s_mes, " Opening backup device... ", 3, TRUE, 6);
status_box(s_mes, "Adding to archive directory...", 3, FALSE, 1);
if (!append) {
tdh.magic = TAPER_MAGIC_NUMBER; /* identify our directory volume */
tdh.tape_number = 1; /* first volume */
clear_ifd();
ifd.archive_id = tdh.archive_id;
strcpy(ifd.archive_title, archive_title);
strcpy(tdh.archive_title, archive_title);
no_old_files = 0;
vt_info = my_realloc(vt_info, sizeof(struct volume_tape_info));
if (open_info_file(FALSE, ifd.archive_id, TRUE) == -1) return -1;
}
else {
ifd.magic = INFO_MAGIC; /* uncompressed originally */
no_old_files = ifd.no_in_archive;
vt_info = my_realloc(vt_info, sizeof(struct volume_tape_info)*(ifd.number_volumes+1));
}
org_files = no_old_files;
if (!append) {
if (tape_open(O_RDWR) == -1) return -1;
if (write_tape_header(&tdh) == -1)
return -1;
}
else {
if (use_eom) {
if (tape_eom() == -1) return -1;
}
else {
if (goto_end_vol(s_mes, 3, ifd.number_volumes, 1, TRUE, O_RDWR) == -1)/* goto end of */
return -1; /* previous volume */
}
}
ifd.number_volumes++;
vti.volume = ifd.number_volumes; /* update volume/tape info */
vti.start_tape = ifd.number_tapes;
if (add_actual_files(s_mes) == -1) /* add filenames to info file */
return -1;
status_box(s_mes, "", 3, FALSE, 1);
if (process_info(s_mes, 3, FALSE) == -1) /* add directories that are required */
return -1;
status_box(s_mes, "", 1, FALSE, 1);
if (write_volume_header(ifd.no_in_archive-no_old_files) == -1) return -1;/* write volume header */
/* free superflous memory */
sel_files = my_realloc(sel_files, 1);
bytes_written=0;
bytes_processed=0;
print_kb(s3, total_selected);
fdfifo = NULL;
*cfile=0;
if (compression)
if (make_comp_file(cfile) != -1) {
taper_tmpnam(fifo_name);
if ((mknod(fifo_name, S_IFIFO|S_IREAD|S_IWRITE, 0)) == -1)
return do_exit(ERROR_CREATING_FIFO);
if (backup_child) { /* a process still going on */
kill(backup_child, SIGKILL);
waitpid(backup_child, NULL, 0);
backup_child = 0;
}
if (log_level > 3) write_log("About to fork of backup child");
backup_child = fork();
if (backup_child == 0) { /* we are in child process */
close(dv);
chdir(original_cur_dir);
#ifdef TRIPLE_BUFFER
sprintf(tmpbuf, "B:Executing bg_backup %s %d %s %s %d %d \"%s\" %s %d",
log_file, log_level, cfile, fifo_name, compression, shm_id, exclude_compress, temp_dir, min_free);
sprintf(s2, "%d", shm_id);
#else
sprintf(tmpbuf, "B:Executing bg_backup %s %d %s %s %d %d \"%s\" %s %d",
log_file, log_level, cfile, fifo_name, compression, 0, exclude_compress, temp_dir, min_free);
sprintf(s2, "%d", 0);
#endif
if (log_level > 3) write_log(tmpbuf);
sprintf(s, "%d", compression);
sprintf(s3, "%d", log_level);
sprintf(s4, "%d", min_free);
if (execlp("bg_backup", "bg_backup", log_file, s3, cfile, fifo_name, s, s2, exclude_compress, temp_dir, s4, NULL) == -1) { write_error_log("starting background backup");
fdfifo = fopen(fifo_name, "w");
fifo_send_no_child (fdfifo); /* no child! */
fclose(fdfifo);
exit(-1); /* should never get here */
}
}
if (backup_child == -1) {
unlink(fifo_name);
if (*cfile) unlink(cfile);
backup_child = 0;
return do_exit(ERROR_UNABLE_FORK);
}
if ((fdfifo = fopen(fifo_name, "r")) == NULL) {
if (backup_child) {
kill(backup_child, SIGKILL); /* kill child process */
waitpid(backup_child, NULL, 0);
backup_child = 0;
}
return do_exit(ERROR_OPENING_FIFO);
}
if (fifo_receive_child_status (fdfifo)) {
/* no child, or fatal error */
waitpid(backup_child, NULL, WNOHANG);
return do_exit(ERROR_NO_BACKUP_CHILD);
}
if (comp_head_start) { /* let the compression program */
status_box(s_mes, "Giving compression a head start", 3, FALSE, 1);
sleep(60*comp_head_start); /* have a head start */
s_tstart = time(NULL); /* restart timer */
}
}
if (s_mes) nodelay(s_mes, TRUE);
quitting=0;
no_written=0;
s_tstart = time(NULL);
set_1s_timer(); /* set a 1s timer */
other_mes = 0;
signal(SIGALRM, backup_alarm);
for (count=0; count< ifd.no_in_archive; count++) {
if (read_info_rec(count, &i_data) == -1) return do_exit(ERROR_READING_INFO);
fi = &i_data.f;
*s = (s_mes == NULL) ? 0 : wgetch(s_mes);
if (((*s == 'q') || (*s == 'Q')) && (!quitting)) {
if (message_box("Confirm abort backup", MB_YESNO)) {
touchwin(s_mes); wrefresh(s_mes);
backup_stop_child(); /* stop child from compressing */
fi->act_size = -1;
fi->name_len = 0;
fi->checksum = -2;
if (write_info_rec(count, &i_data) == -1)
return do_exit(ERROR_WRITING_INFO); /* don't need to reindex */
write_nameinfo(fi, ""); /* because haven't changed index field */
ifd.no_in_archive = no_old_files;/* correct info file */
memcpy(&v, get_vh(vol_headers, ifd.number_volumes), sizeof(struct volume_header)); /* this needed to */
v.no_in_volume = no_written; /* ensure aligned access */
memcpy(get_vh(vol_headers, ifd.number_volumes), &v, sizeof(struct volume_header));
write_log("Backup aborted by user");
log_errors++;
break;
}
}
if (fi->pos_in_archive == 0) { /* only need new ones */
no_written++;
s_count = count; s_noarchive = ifd.no_in_archive; /* set up */
s_idata = i_data; /* data for status box */
s_tcurrent = time(NULL);
no_old_files++;
i_data.f.pos_in_archive = no_old_files;
if (backup_file(s_mes, &i_data) == -1) {
tape_close(); /* rewinding device so will auto update headers */
backup_stop_child();
if (*cfile) unlink(cfile);
waitpid(backup_child, NULL, WNOHANG);
return -1; /* error message handled by backup file */
}
if (write_info_rec(count, &i_data) == -1) return do_exit(ERROR_WRITING_INFO);
if (add_to_info_index(&i_data) == -1) return do_exit(ERROR_WRITING_INFO); /* adds to indicies */
bytes_processed += sizeof(struct file_info) + fi->name_len;
bytes_written += sizeof(struct file_info) + fi->name_len;
if ((S_ISREG(fi->mode)) ||
(S_ISLNK(fi->mode)) ) { /* only add this if regular file */
bytes_processed += fi->size;
bytes_written += fi->act_size;
}
}
}
reset_timer();
t_current = time(NULL);
x = t_current - s_tstart;
if (x == 0) x=1;
status_box(s_mes, "", 0, FALSE, 1);
status_box(s_mes, "", 2, FALSE, 1);
status_box(s_mes, "", 4, FALSE, 1);
status_box(s_mes, "", 5, FALSE, 1);
status_box(s_mes, "", 7, FALSE, 1);
status_box(s_mes, "", 8, FALSE, 1);
status_box(s_mes, "Updating header segments", 3, FALSE, 1);
tape_close(); /* rewinding device so will auto update headers */
vti.blocks_on_last_tape = blocks_passed;
vti.end_tape = ifd.number_tapes;
memcpy(vt_info+ifd.number_volumes-1, &vti, sizeof(vti));
/* Code for info file at end here */
status_box(s_mes, "Closing/Compressing info File", 3, FALSE, 1);
if (write_info_file()) return -1; /* write out info file */
if (fdfifo) {
fclose(fdfifo); /* close FIFO */
unlink(fifo_name); /* remove file list & FIFO */
}
if (*cfile) unlink(cfile); /* in case child didn't */
waitpid(backup_child, NULL, 0);
backup_child = 0;
close_info_file();
close_statusbox(s_mes);
if (win_main) {
touchwin(win_main);
wrefresh(win_main);
}
sprintf(sa[0], "Backup Finished");
strcpy(sa[1], "");
sprintf(sa[2], "Backed up: %d files, %s [%s]", ifd.no_in_archive - org_files,
print_mb(s2, bytes_written),
print_mb(s3, bytes_processed));
if (log_level) write_log(sa[2]);
if (mf) {write(mf, sa[2], strlen(sa[2])); write(mf, "\n", 1);}
sprintf(sa[3], "Total on archive %s [%s]. Ratio %.2f",
print_mb(s2, bytes_written+total_compressed),
print_mb(s3, bytes_processed+total_uncompressed),
(total_compressed+bytes_written == 0) ? 1 :
(float) (total_uncompressed+bytes_processed)/
(float) (total_compressed+bytes_written));
if (log_level) write_log(sa[3]);
if (mf) {write(mf, sa[3], strlen(sa[3])); write(mf, "\n", 1);}
strcpy(sa[4], "");
sprintf(sa[5], "Time elapsed %s.", convtime(s2, s_tstart, t_current));
if (log_level) write_log(sa[5]);
if (mf) {write(mf, sa[5], strlen(sa[5])); write(mf, "\n", 1);}
sprintf(sa[6], "Backup rate %s/min [%s/min]",
print_mb(tmpbuf, bytes_written/x*60),
print_mb(s3, bytes_processed/x*60));
if (log_level) write_log(sa[6]);
if (mf) {write(mf, sa[6], strlen(sa[6])); write(mf, "\n", 1);}
strcpy(sa[7], "");
sprintf(sa[8], "%d warnings, %d errors", log_warnings, log_errors);
if (log_level) write_log(sa[8]);
if (mf) {write(mf, sa[8], strlen(sa[8])); write(mf, "\n", 1);}
if (!no_windows) multi_message_box(sa, 9, MB_OK, TRUE);
return 0;
}
_errstat add_to_set(char *ln, char inc_or_exc)
{
/* Adds line 's' to backup set */
struct direntry entry;
if (!chk_sel_excl(ln)) {
if (get_statinfo(ln, &entry.info) != -1) {/* ignore if error */
if (inc_or_exc == ENTRY_INCLUDE) {
select_entry_1(&entry, ln);
write(mf, " Selected ", strlen(" Selected "));
}
else {
exclude_entry_1(&entry, ln);
write(mf, " Excluded ", strlen(" Excluded "));
}
write(mf, ln, strlen(ln));
write(mf, " from fileset\n", strlen(" from fileset\n"));
}
else
return -1;
}
return 0;
}
_errstat read_in_fileset(int argc, char *argv[])
{
/* Reads in the files
Returns 1 if all OK
-1 otherwise
*/
FILE *f;
char ln[MAX_FNAME], s[MAX_FNAME];
_s32 c;
time_t t;
_s8 inc_or_exc;
c = 0;
while (c < argc) {
if ( (!strcmp(argv[c], "-U") ||
(!strcmp(argv[c], "--unattended-file"))) ) {
c++;
if (c == argc) {
strcpy(ln, "Illegal file/directory/set name specified\n");
write(mf, ln, strlen(ln));
strcpy(ln, "BACKUP ABORTED\n");
write(mf, ln, strlen(ln));
return -1;
}
if (*argv[c] == '@') {
strcpy(dir_cur_dir, taper_info_files);
f = backrest_restore_backupset(&argv[c][1]);/* open file set */
if (f==NULL) {
sprintf(ln, "WARNING: Couldn't open file set %s\n", &argv[c][1]);
write(mf, ln, strlen(ln));
continue;
}
sprintf(ln, "Using file set %s\n", &argv[c][1]);
write(mf, ln, strlen(ln));
while (!feof(f)) {
if (get_line(ln, f) == NULL) break; /* EOF */
if ((*ln != 'E') && (*ln != 'I')) {
strcpy(ln, "Illegal line in backup set - using old format?\n");
write(mf, ln, strlen(ln));
strcpy(ln, "BACKUP ABORTED\n");
write(mf, ln, strlen(ln));
fclose(f);
return -1;
}
inc_or_exc = (*ln == 'I') ? ENTRY_INCLUDE : ENTRY_EXCLUDE;
if (get_line(ln, f) == NULL) break;
if (add_to_set(ln, inc_or_exc) == -1) {
sprintf(s, "WARNING: %s is not found in fileset %s\n", ln, &argv[c][1]);
write(mf, s, strlen(s));
}
}
fclose(f);
}
else { /* individual directory specified */
if (add_to_set(argv[c], ENTRY_INCLUDE) == -1) {
sprintf(s, "WARNING: %s is not found\n", ln);
write(mf, s, strlen(s));
}
}
}
c++;
}
time(&t);
sprintf(ln, "\nBackup commenced at %s\n", ctime(&t));
write(mf, ln, strlen(ln));
if ((unattended_id != -1)) { /* unattended backup and user */
if (unattended_id != tdh.archive_id) { /* wants to append to a particular one */
sprintf(s, "Archive %d is in tape drive. I want archive %d\n",
tdh.archive_id, unattended_id);
if (mf) write(mf, s, strlen(s));
write_log(s);
if (mf) write(mf, "Backup NOT made\n", strlen("Backup NOT made\n"));
write_log("Backup NOT made");
}
return -1;
}
return 1;
}
_errstat utils_write_nullheader(WINDOW **mes);
_errstat check_backup(void) {
/* Make sure backup is a valid device and confirm with use about
an erase if it has been requested. */
int x, fd;
WINDOW *mes=NULL;
char info_file[MAX_FNAME];
_s32 oldarch=0;
if ((fd=rmtopen(tape, O_RDONLY, S_IRWXU)) == -1) {
/* if (stat(tape, &buf) == -1) {*/ /* touch file if needed */
if (tape_type == TAPE_TYPE_FILE) {
if (log_level > 2) write_log("Creating backup file");
close(creat(tape, S_IREAD|S_IWRITE));
x = TAPE_EXIST_EMPTY;
}
else
return do_exit(ERROR_OPENING_BACKUP);
}
else {
close(fd);
x= do_read_vol_dir(-1, tape, O_RDWR, append, FALSE, TRUE);
}
if (x==-1) return -1;
if (x == BAD_MAGIC) {
if (!no_windows) {
if (bmessage_box("Unknown tape data. Overwrite?", MB_YESNO)) append = 0;
else return 0;;
}
else { /* unattended mode */
if (!tape_overwrite) {
if (mf) write(mf, "Unknown data on tape. Tape not overwritten.\n",
strlen("Unknown data on tape. Tape not overwritten.\n"));
if (mf) write(mf, "Use --tape-overwrite-on [+O] to force overwriting\n",
strlen("Use --tape-overwrite-on [+O] to force overwriting\n"));
write_warning_log("Unknown data on the tape. Tape not overwritten");
return 0; /* need to have authority to overwrite */
}
append = 0; /* to overwrite tapes in unattended mode */
}
}
if ((x == TAPE_EXIST) && (!append)) {
if (!no_windows) {
if (!bmessage_box("Taper data on tape. Confirm erase", MB_YESNO))
return 0;
}
}
if (x == TAPE_EXIST_EMPTY) { /* empty taper tape */
append = 0;
return 1;
}
if ((x == BAD_MAGIC) || (!append)) { /* no taper data on tape */
append = 0;
if (!no_windows)
mes = status_box(mes, "Erasing data on tape", 1, TRUE, 1);
if (is_regfile(dv)) {
unlink(tape); /* regular files get deleted */
close(creat(tape, S_IREAD|S_IWRITE));/* and re-created to get zero length */
}
else {
oldarch = tdh.archive_id;
utils_write_nullheader(&mes);
tape_close();
}
strcpy(info_file, taper_info_files); /* look for information file */
if (info_file[strlen(info_file)-1] != '/') /* associated with this archive */
strcat(info_file, "/");
sprintf(info_file, "%staper_info.%u", info_file, oldarch);
unlink(info_file); /* attempt to delete info file */
info_fd = 0;
for (x=0; x<=INFO_INDICIES; x++) {
strcpy(info_file, taper_info_files); /* look for information file */
if (info_file[strlen(info_file)-1] != '/') /* associated with this archive */
strcat(info_file, "/");
sprintf(info_file, "%staper_info.%u.%d", info_file, oldarch, x);
unlink(info_file);
info_index[x] = 0;
}
if (mes) close_statusbox(mes);
return 1;
}
if (x == TAPE_NOEXIST) {
append = 0;
return 1;
}
return 1;
}
void taper_backup(int argc, char *argv[])
{
int x;
int old_append;
WINDOW *mes_box=NULL;
old_append = append; /* save value of append for future use */
if (open_logfile("Backup") == -1)
return;
if (!no_windows) {
backrest_init_windows() ; /* Draw windows */
backrest_clear_screen();
}
else { /* open file to mail things to */
taper_tmpnam(mailfn);
mf = open(mailfn, O_CREAT|O_RDWR, S_IREAD|S_IWRITE);
if (mf==-1) {
do_exit(ERROR_OPENING_MAIL);
return;
}
}
if (check_device_names() == -1) goto endbackup;/* check devices & other parms */
if (backrest_do_mallocs() == -1) /* couldn't allocate memory */
goto endbackup;
if (check_backup() > 0) {
if (!append) { /* no archive */
tdh.archive_id = time(NULL); /* set defaults */
ifd.archive_id = tdh.archive_id;
if (!no_windows)
get_string(win_main, archive_title, MAX_ARCHIVETITLE, "Enter archive title");
strcpy(tdh.archive_title, archive_title);
strcpy(ifd.archive_title, archive_title);
}
print_title_line();
chdir(cur_dir);
if (!no_windows) {
if (get_string(win_main, volume_title, MAX_ARCHIVETITLE, "Enter volume title"))
x = backup_select_files(cur_dir); /* select files */
else
x = 0;
}
else /* unattended mode */
x = read_in_fileset(argc, argv);
getcwd(cur_dir, sizeof(cur_dir));
backrest_kill_windows();
print_my_name();
if (x > 0) do_backup();
if (dv > 0) tape_close();
if (!no_windows) close_statusbox(mes_box);
}
else {
backrest_kill_windows();
log_errors = 1; /* the fatal error */
}
backrest_free_memory();
if (mf)
sendmail(); /* if mail file */
append = old_append;
endbackup:;
close_logfile("Backup");
}
|