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 1079
|
/* copyin.c - extract or list a cpio archive
Copyright (C) 1990,1991,1992,2001,2002,2003,2004 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include <fnmatch.h>
# ifndef DIRECTORY_SEPARATOR
# define DIRECTORY_SEPARATOR '/'
# endif
# ifndef ISSLASH
# define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
# endif
/* Return 1 if an array of N objects, each of size S, cannot exist due
to size arithmetic overflow. S must be positive and N must be
nonnegative. This is a macro, not an inline function, so that it
works correctly even when SIZE_MAX < N.
By gnulib convention, SIZE_MAX represents overflow in size
calculations, so the conservative dividend to use here is
SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
However, malloc (SIZE_MAX) fails on all known hosts where
sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
exactly-SIZE_MAX allocations on such hosts; this avoids a test and
branch when S is known to be 1. */
# define xalloc_oversized(n, s) \
((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
#define DISK_IO_BLOCK_SIZE (512)
char *progname = NULL;
/* If true, print a . for each file processed. (-V) */
char dot_flag = false;
/* Input and output buffers. */
char *input_buffer, *output_buffer;
/* The size of the input buffer. */
long input_buffer_size;
/* Current locations in `input_buffer' and `output_buffer'. */
char *in_buff, *out_buff;
/* Current number of bytes stored at `input_buff' and `output_buff'. */
long input_size, output_size;
/* Block size value, initially 512. -B sets to 5120. */
int io_block_size = 512;
struct new_cpio_header {
unsigned short c_magic;
union {
struct {
unsigned long c_ino;
unsigned long c_mode;
unsigned long c_uid;
unsigned long c_gid;
unsigned long c_nlink;
unsigned long c_mtime;
unsigned long c_filesize;
long c_dev_maj;
long c_dev_min;
long c_rdev_maj;
long c_rdev_min;
unsigned long c_namesize;
unsigned long c_chksum;
};
unsigned long c_hdr[13];
};
char *c_name;
char *c_tar_linkname;
};
/* Total number of bytes read and written for all files.
* Now that many tape drives hold more than 4Gb we need more than 32
* bits to hold input_bytes and output_bytes.
*/
long long input_bytes, output_bytes;
/* Allocate N bytes of memory dynamically, with error checking. */
static void *xmalloc(size_t n)
{
void *p;
if (xalloc_oversized(n, 1) || (!(p = malloc(n)) && n != 0)) {
fprintf(stderr, "%s: memory exhausted\n", progname);
exit(1);
}
return p;
/* return xnmalloc_inline (n, 1); */
}
/* Clone STRING. */
static char *xstrdup(char const *string)
{
size_t s = strlen(string) + 1;
return memcpy(xmalloc(s), string, s);
/* return xmemdup_inline (string, strlen (string) + 1); */
}
/* Copy NUM_BYTES of buffer `in_buff' into IN_BUF.
`in_buff' may be partly full.
When `in_buff' is exhausted, refill it from file descriptor IN_DES. */
static void tape_fill_input_buffer(int in_des, int num_bytes)
{
in_buff = input_buffer;
num_bytes = (num_bytes < io_block_size) ? num_bytes : io_block_size;
input_size = read(in_des, input_buffer, num_bytes);
if (input_size < 0) {
fprintf(stderr, "%s: read error: %s\n", progname,
strerror(errno));
exit(1);
}
if (input_size == 0) {
fprintf(stderr, "%s: premature end of file\n", progname);
exit(1);
}
input_bytes += input_size;
}
/* Write `output_size' bytes of `output_buffer' to file
descriptor OUT_DES and reset `output_size' and `out_buff'.
If `swapping_halfwords' or `swapping_bytes' is set,
do the appropriate swapping first. Our callers have
to make sure to only set these flags if `output_size'
is appropriate (a multiple of 4 for `swapping_halfwords',
2 for `swapping_bytes'). The fact that DISK_IO_BLOCK_SIZE
must always be a multiple of 4 helps us (and our callers)
insure this. */
static void disk_empty_output_buffer(int out_des)
{
int bytes_written;
bytes_written = write(out_des, output_buffer, output_size);
if (bytes_written != output_size) {
fprintf(stderr, "%s: write error: %s\n",
progname, strerror(errno));
exit(1);
}
output_bytes += output_size;
out_buff = output_buffer;
output_size = 0;
}
/* Copy NUM_BYTES of buffer IN_BUF to `out_buff', which may be partly full.
When `out_buff' fills up, flush it to file descriptor OUT_DES. */
static void disk_buffered_write(char *in_buf, int out_des, long num_bytes)
{
register long bytes_left = num_bytes; /* Bytes needing to be copied. */
register long space_left; /* Room left in output buffer. */
while (bytes_left > 0) {
space_left = DISK_IO_BLOCK_SIZE - output_size;
if (space_left == 0)
disk_empty_output_buffer(out_des);
else {
if (bytes_left < space_left)
space_left = bytes_left;
memmove(out_buff, in_buf, (unsigned)space_left);
out_buff += space_left;
output_size += space_left;
in_buf += space_left;
bytes_left -= space_left;
}
}
}
/* Copy a file using the input and output buffers, which may start out
partly full. After the copy, the files are not closed nor the last
block flushed to output, and the input buffer may still be partly
full. If `crc_i_flag' is set, add each byte to `crc'.
IN_DES is the file descriptor for input;
OUT_DES is the file descriptor for output;
NUM_BYTES is the number of bytes to copy. */
static void copy_files_tape_to_disk(int in_des, int out_des, long num_bytes)
{
long size;
while (num_bytes > 0) {
if (input_size == 0)
tape_fill_input_buffer(in_des, io_block_size);
size = (input_size < num_bytes) ? input_size : num_bytes;
disk_buffered_write(in_buff, out_des, size);
num_bytes -= size;
input_size -= size;
in_buff += size;
}
}
/* if IN_BUF is NULL, Skip the next NUM_BYTES bytes of file descriptor IN_DES. */
static void tape_buffered_read(char *in_buf, int in_des, long num_bytes)
{
register long bytes_left = num_bytes; /* Bytes needing to be copied. */
register long space_left; /* Bytes to copy from input buffer. */
while (bytes_left > 0) {
if (input_size == 0)
tape_fill_input_buffer(in_des, io_block_size);
if (bytes_left < input_size)
space_left = bytes_left;
else
space_left = input_size;
if (in_buf != NULL) {
memmove(in_buf, in_buff, (unsigned)space_left);
in_buf += space_left;
}
in_buff += space_left;
input_size -= space_left;
bytes_left -= space_left;
}
}
/* Skip the next NUM_BYTES bytes of file descriptor IN_DES. */
#define tape_toss_input(in_des,num_bytes) \
(tape_buffered_read(NULL,(in_des),(num_bytes)))
struct deferment {
struct deferment *next;
struct new_cpio_header header;
};
static struct deferment *create_deferment(struct new_cpio_header *file_hdr)
{
struct deferment *d;
d = (struct deferment *)xmalloc(sizeof(struct deferment));
d->header = *file_hdr;
d->header.c_name = (char *)xmalloc(strlen(file_hdr->c_name) + 1);
strcpy(d->header.c_name, file_hdr->c_name);
return d;
}
static void free_deferment(struct deferment *d)
{
free(d->header.c_name);
free(d);
}
static int link_to_name(char *link_name, char *link_target)
{
int res = link(link_target, link_name);
return res;
}
struct inode_val {
unsigned long inode;
unsigned long major_num;
unsigned long minor_num;
char *file_name;
};
/* Inode hash table. Allocated by first call to add_inode. */
static struct inode_val **hash_table = NULL;
/* Size of current hash table. Initial size is 47. (47 = 2*22 + 3) */
static int hash_size = 22;
/* Number of elements in current hash table. */
static int hash_num;
/* Do the hash insert. Used in normal inserts and resizing the hash
table. It is guaranteed that there is room to insert the item.
NEW_VALUE is the pointer to the previously allocated inode, file
name association record. */
static void hash_insert(struct inode_val *new_value)
{
int start; /* Home position for the value. */
int temp; /* Used for rehashing. */
/* Hash function is node number modulo the table size. */
start = new_value->inode % hash_size;
/* Do the initial look into the table. */
if (hash_table[start] == NULL) {
hash_table[start] = new_value;
return;
}
/* If we get to here, the home position is full with a different inode
record. Do a linear search for the first NULL pointer and insert
the new item there. */
temp = (start + 1) % hash_size;
while (hash_table[temp] != NULL)
temp = (temp + 1) % hash_size;
/* Insert at the NULL. */
hash_table[temp] = new_value;
}
/* Associate FILE_NAME with the inode NODE_NUM. (Insert into hash table.) */
static void
add_inode(unsigned long node_num, char *file_name, unsigned long major_num,
unsigned long minor_num)
{
struct inode_val *temp;
/* Create new inode record. */
temp = (struct inode_val *)xmalloc(sizeof(struct inode_val));
temp->inode = node_num;
temp->major_num = major_num;
temp->minor_num = minor_num;
temp->file_name = xstrdup(file_name);
/* Do we have to increase the size of (or initially allocate)
the hash table? */
if (hash_num == hash_size || hash_table == NULL) {
struct inode_val **old_table; /* Pointer to old table. */
int i; /* Index for re-insert loop. */
/* Save old table. */
old_table = hash_table;
if (old_table == NULL)
hash_num = 0;
/* Calculate new size of table and allocate it.
Sequence of table sizes is 47, 97, 197, 397, 797, 1597, 3197, 6397 ...
where 3197 and most of the sizes after 6397 are not prime. The other
numbers listed are prime. */
hash_size = 2 * hash_size + 3;
hash_table = (struct inode_val **)
xmalloc(hash_size * sizeof(struct inode_val *));
memset(hash_table, 0, hash_size * sizeof(struct inode_val *));
/* Insert the values from the old table into the new table. */
for (i = 0; i < hash_num; i++)
hash_insert(old_table[i]);
free(old_table);
}
/* Insert the new record and increment the count of elements in the
hash table. */
hash_insert(temp);
hash_num++;
}
static char *find_inode_file(unsigned long node_num, unsigned long major_num,
unsigned long minor_num)
{
int start; /* Initial hash location. */
int temp; /* Rehash search variable. */
if (hash_table != NULL) {
/* Hash function is node number modulo the table size. */
start = node_num % hash_size;
/* Initial look into the table. */
if (hash_table[start] == NULL)
return NULL;
if (hash_table[start]->inode == node_num
&& hash_table[start]->major_num == major_num
&& hash_table[start]->minor_num == minor_num)
return hash_table[start]->file_name;
/* The home position is full with a different inode record.
Do a linear search terminated by a NULL pointer. */
for (temp = (start + 1) % hash_size;
hash_table[temp] != NULL && temp != start;
temp = (temp + 1) % hash_size) {
if (hash_table[temp]->inode == node_num
&& hash_table[start]->major_num == major_num
&& hash_table[start]->minor_num == minor_num)
return hash_table[temp]->file_name;
}
}
return NULL;
}
/* Try and create a hard link from FILE_NAME to another file
with the given major/minor device number and inode. If no other
file with the same major/minor/inode numbers is known, add this file
to the list of known files and associated major/minor/inode numbers
and return -1. If another file with the same major/minor/inode
numbers is found, try and create another link to it using
link_to_name, and return 0 for success and -1 for failure. */
static int
link_to_maj_min_ino(char *file_name, int st_dev_maj, int st_dev_min, int st_ino)
{
int link_res;
char *link_name;
link_res = -1;
/* Is the file a link to a previously copied file? */
link_name = find_inode_file(st_ino, st_dev_maj, st_dev_min);
if (link_name == NULL)
add_inode(st_ino, file_name, st_dev_maj, st_dev_min);
else
link_res = link_to_name(file_name, link_name);
return link_res;
}
static void copyin_regular_file(struct new_cpio_header *file_hdr,
int in_file_des);
static void warn_junk_bytes(long bytes_skipped)
{
fprintf(stderr, "%s: warning: skipped %ld byte(s) of junk\n",
progname, bytes_skipped);
}
/* Skip the padding on IN_FILE_DES after a header or file,
up to the next header.
The number of bytes skipped is based on OFFSET -- the current offset
from the last start of a header (or file) -- and the current
header type. */
static void tape_skip_padding(int in_file_des, int offset)
{
int pad;
pad = (4 - (offset % 4)) % 4;
if (pad != 0)
tape_toss_input(in_file_des, pad);
}
static int
try_existing_file(struct new_cpio_header *file_hdr, int in_file_des,
int *existing_dir)
{
struct stat file_stat;
*existing_dir = false;
if (lstat(file_hdr->c_name, &file_stat) == 0) {
if (S_ISDIR(file_stat.st_mode)
&& ((file_hdr->c_mode & S_IFMT) == S_IFDIR)) {
/* If there is already a directory there that
we are trying to create, don't complain about
it. */
*existing_dir = true;
return 0;
} else if (S_ISDIR(file_stat.st_mode)
? rmdir(file_hdr->c_name)
: unlink(file_hdr->c_name)) {
fprintf(stderr, "%s: cannot remove current %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
tape_toss_input(in_file_des, file_hdr->c_filesize);
tape_skip_padding(in_file_des, file_hdr->c_filesize);
return -1; /* Go to the next file. */
}
}
return 0;
}
/* The newc and crc formats store multiply linked copies of the same file
in the archive only once. The actual data is attached to the last link
in the archive, and the other links all have a filesize of 0. When a
file in the archive has multiple links and a filesize of 0, its data is
probably "attatched" to another file in the archive, so we can't create
it right away. We have to "defer" creating it until we have created
the file that has the data "attatched" to it. We keep a list of the
"defered" links on deferments. */
struct deferment *deferments = NULL;
/* Add a file header to the deferments list. For now they all just
go on one list, although we could optimize this if necessary. */
static void defer_copyin(struct new_cpio_header *file_hdr)
{
struct deferment *d;
d = create_deferment(file_hdr);
d->next = deferments;
deferments = d;
return;
}
/* We just created a file that (probably) has some other links to it
which have been defered. Go through all of the links on the deferments
list and create any which are links to this file. */
static void create_defered_links(struct new_cpio_header *file_hdr)
{
struct deferment *d;
struct deferment *d_prev;
int ino;
int maj;
int min;
int link_res;
ino = file_hdr->c_ino;
maj = file_hdr->c_dev_maj;
min = file_hdr->c_dev_min;
d = deferments;
d_prev = NULL;
while (d != NULL) {
if ((d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
&& (d->header.c_dev_min == min)) {
struct deferment *d_free;
link_res =
link_to_name(d->header.c_name, file_hdr->c_name);
if (link_res < 0) {
fprintf(stderr,
"%s: cannot link %s to %s: %s\n",
progname, d->header.c_name,
file_hdr->c_name, strerror(errno));
}
if (d_prev != NULL)
d_prev->next = d->next;
else
deferments = d->next;
d_free = d;
d = d->next;
free_deferment(d_free);
} else {
d_prev = d;
d = d->next;
}
}
}
/* If we had a multiply linked file that really was empty then we would
have defered all of its links, since we never found any with data
"attached", and they will still be on the deferment list even when
we are done reading the whole archive. Write out all of these
empty links that are still on the deferments list. */
static void create_final_defers(void)
{
struct deferment *d;
int link_res;
int out_file_des;
struct utimbuf times; /* For setting file times. */
/* Initialize this in case it has members we don't know to set. */
memset(×, 0, sizeof(struct utimbuf));
for (d = deferments; d != NULL; d = d->next) {
/* Debian hack: A line, which could cause an endless loop, was
removed (97/1/2). It was reported by Ronald F. Guilmette to
the upstream maintainers. -BEM */
/* Debian hack: This was reported by Horst Knobloch. This bug has
been reported to "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM
*/
link_res = link_to_maj_min_ino(d->header.c_name,
d->header.c_dev_maj,
d->header.c_dev_min,
d->header.c_ino);
if (link_res == 0) {
continue;
}
out_file_des = open(d->header.c_name, O_CREAT | O_WRONLY, 0600);
if (out_file_des < 0) {
fprintf(stderr, "%s: open %s: %s\n",
progname, d->header.c_name, strerror(errno));
continue;
}
/* File is now copied; set attributes. */
if ((fchown(out_file_des, d->header.c_uid, d->header.c_gid) < 0)
&& errno != EPERM)
fprintf(stderr, "%s: fchown %s: %s\n",
progname, d->header.c_name, strerror(errno));
/* chown may have turned off some permissions we wanted. */
if (fchmod(out_file_des, (int)d->header.c_mode) < 0)
fprintf(stderr, "%s: fchmod %s: %s\n",
progname, d->header.c_name, strerror(errno));
if (close(out_file_des) < 0)
fprintf(stderr, "%s: close %s: %s\n",
progname, d->header.c_name, strerror(errno));
}
}
static void
copyin_regular_file(struct new_cpio_header *file_hdr, int in_file_des)
{
int out_file_des; /* Output file descriptor. */
/* Can the current file be linked to a previously copied file? */
if (file_hdr->c_nlink > 1) {
int link_res;
if (file_hdr->c_filesize == 0) {
/* The newc and crc formats store multiply linked copies
of the same file in the archive only once. The
actual data is attached to the last link in the
archive, and the other links all have a filesize
of 0. Since this file has multiple links and a
filesize of 0, its data is probably attatched to
another file in the archive. Save the link, and
process it later when we get the actual data. We
can't just create it with length 0 and add the
data later, in case the file is readonly. We still
lose if its parent directory is readonly (and we aren't
running as root), but there's nothing we can do about
that. */
defer_copyin(file_hdr);
tape_toss_input(in_file_des, file_hdr->c_filesize);
tape_skip_padding(in_file_des, file_hdr->c_filesize);
return;
}
/* If the file has data (filesize != 0), then presumably
any other links have already been defer_copyin'ed(),
but GNU cpio version 2.0-2.2 didn't do that, so we
still have to check for links here (and also in case
the archive was created and later appeneded to). */
/* Debian hack: (97/1/2) This was reported by Ronald
F. Guilmette to the upstream maintainers. -BEM */
link_res = link_to_maj_min_ino(file_hdr->c_name,
file_hdr->c_dev_maj,
file_hdr->c_dev_min,
file_hdr->c_ino);
if (link_res == 0) {
tape_toss_input(in_file_des, file_hdr->c_filesize);
tape_skip_padding(in_file_des, file_hdr->c_filesize);
return;
}
}
/* If not linked, copy the contents of the file. */
out_file_des = open(file_hdr->c_name, O_CREAT | O_WRONLY, 0600);
if (out_file_des < 0) {
fprintf(stderr, "%s: open %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
tape_toss_input(in_file_des, file_hdr->c_filesize);
tape_skip_padding(in_file_des, file_hdr->c_filesize);
return;
}
copy_files_tape_to_disk(in_file_des, out_file_des,
file_hdr->c_filesize);
disk_empty_output_buffer(out_file_des);
if (close(out_file_des) < 0)
fprintf(stderr, "%s: close %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
/* File is now copied; set attributes. */
if ((chown(file_hdr->c_name, file_hdr->c_uid, file_hdr->c_gid) < 0)
&& errno != EPERM)
fprintf(stderr, "%s: chown %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
/* chown may have turned off some permissions we wanted. */
if (chmod(file_hdr->c_name, (int)file_hdr->c_mode) < 0)
fprintf(stderr, "%s: chmod %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
tape_skip_padding(in_file_des, file_hdr->c_filesize);
if (file_hdr->c_nlink > 1) {
/* (see comment above for how the newc and crc formats
store multiple links). Now that we have the data
for this file, create any other links to it which
we defered. */
create_defered_links(file_hdr);
}
}
/* In general, we can't use the builtin `basename' function if available,
since it has different meanings in different environments.
In some environments the builtin `basename' modifies its argument.
Return the address of the last file name component of NAME. If
NAME has no file name components because it is all slashes, return
NAME if it is empty, the address of its last slash otherwise. */
static char *base_name(char const *name)
{
char const *base = name;
char const *p;
for (p = base; *p; p++) {
if (ISSLASH(*p)) {
/* Treat multiple adjacent slashes like a single slash. */
do
p++;
while (ISSLASH(*p));
/* If the file name ends in slash, use the trailing slash as
the basename if no non-slashes have been found. */
if (!*p) {
if (ISSLASH(*base))
base = p - 1;
break;
}
/* *P is a non-slash preceded by a slash. */
base = p;
}
}
return (char *)base;
}
/* Return the length of of the basename NAME. Typically NAME is the
value returned by base_name. Act like strlen (NAME), except omit
redundant trailing slashes. */
static size_t base_len(char const *name)
{
size_t len;
for (len = strlen(name); 1 < len && ISSLASH(name[len - 1]); len--)
continue;
return len;
}
/* Remove trailing slashes from PATH.
Return true if a trailing slash was removed.
This is useful when using filename completion from a shell that
adds a "/" after directory names (such as tcsh and bash), because
the Unix rename and rmdir system calls return an "Invalid argument" error
when given a path that ends in "/" (except for the root directory). */
static bool strip_trailing_slashes(char *path)
{
char *base = base_name(path);
char *base_lim = base + base_len(base);
bool had_slash = (*base_lim != '\0');
*base_lim = '\0';
return had_slash;
}
static void copyin_directory(struct new_cpio_header *file_hdr, int existing_dir)
{
int res; /* Result of various function calls. */
/* Strip any trailing `/'s off the filename; tar puts
them on. We might as well do it here in case anybody
else does too, since they cause strange things to happen. */
strip_trailing_slashes(file_hdr->c_name);
/* Ignore the current directory. It must already exist,
and we don't want to change its permission, ownership
or time. */
if (file_hdr->c_name[0] == '.' && file_hdr->c_name[1] == '\0') {
return;
}
if (!existing_dir)
{
res = mkdir(file_hdr->c_name, file_hdr->c_mode);
} else
res = 0;
if (res < 0) {
/* In some odd cases where the file_hdr->c_name includes `.',
the directory may have actually been created by
create_all_directories(), so the mkdir will fail
because the directory exists. If that's the case,
don't complain about it. */
struct stat file_stat;
if ((errno != EEXIST) ||
(lstat(file_hdr->c_name, &file_stat) != 0) ||
!(S_ISDIR(file_stat.st_mode))) {
fprintf(stderr, "%s: lstat %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
return;
}
}
if ((chown(file_hdr->c_name, file_hdr->c_uid, file_hdr->c_gid) < 0)
&& errno != EPERM)
fprintf(stderr, "%s: chown %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
/* chown may have turned off some permissions we wanted. */
if (chmod(file_hdr->c_name, (int)file_hdr->c_mode) < 0)
fprintf(stderr, "%s: chmod %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
}
static void copyin_device(struct new_cpio_header *file_hdr)
{
int res; /* Result of various function calls. */
if (file_hdr->c_nlink > 1) {
int link_res;
/* Debian hack: This was reported by Horst
Knobloch. This bug has been reported to
"bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM */
link_res = link_to_maj_min_ino(file_hdr->c_name,
file_hdr->c_dev_maj,
file_hdr->c_dev_min,
file_hdr->c_ino);
if (link_res == 0) {
return;
}
}
res = mknod(file_hdr->c_name, file_hdr->c_mode,
makedev(file_hdr->c_rdev_maj, file_hdr->c_rdev_min));
if (res < 0) {
fprintf(stderr, "%s: mknod %s: %s\n", progname,
file_hdr->c_name, strerror(errno));
return;
}
if ((chown(file_hdr->c_name, file_hdr->c_uid, file_hdr->c_gid) < 0)
&& errno != EPERM)
fprintf(stderr, "%s: chown %s: %s\n", progname,
file_hdr->c_name, strerror(errno));
/* chown may have turned off some permissions we wanted. */
if (chmod(file_hdr->c_name, file_hdr->c_mode) < 0)
fprintf(stderr, "%s: chmod %s: %s\n", progname,
file_hdr->c_name, strerror(errno));
}
static void copyin_link(struct new_cpio_header *file_hdr, int in_file_des)
{
char *link_name = NULL; /* Name of hard and symbolic links. */
int res; /* Result of various function calls. */
link_name = (char *)xmalloc(file_hdr->c_filesize + 1);
link_name[file_hdr->c_filesize] = '\0';
tape_buffered_read(link_name, in_file_des, file_hdr->c_filesize);
tape_skip_padding(in_file_des, file_hdr->c_filesize);
res = symlink(link_name, file_hdr->c_name);
if (res < 0) {
fprintf(stderr, "%s: symlink %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
free(link_name);
return;
}
if ((lchown(file_hdr->c_name, file_hdr->c_uid, file_hdr->c_gid) < 0)
&& errno != EPERM) {
fprintf(stderr, "%s: lchown %s: %s\n",
progname, file_hdr->c_name, strerror(errno));
}
free(link_name);
}
static void copyin_file(struct new_cpio_header *file_hdr, int in_file_des)
{
int existing_dir;
if (try_existing_file(file_hdr, in_file_des, &existing_dir) < 0)
return;
/* Do the real copy or link. */
switch (file_hdr->c_mode & S_IFMT) {
case S_IFREG:
copyin_regular_file(file_hdr, in_file_des);
break;
case S_IFDIR:
copyin_directory(file_hdr, existing_dir);
break;
case S_IFCHR:
case S_IFBLK:
case S_IFSOCK:
case S_IFIFO:
copyin_device(file_hdr);
break;
case S_IFLNK:
copyin_link(file_hdr, in_file_des);
break;
default:
fprintf(stderr, "%s: %s: unknown file type\n",
progname, file_hdr->c_name);
tape_toss_input(in_file_des, file_hdr->c_filesize);
tape_skip_padding(in_file_des, file_hdr->c_filesize);
}
}
/* Fill in FILE_HDR by reading a new-format ASCII format cpio header from
file descriptor IN_DES, except for the magic number, which is
already filled in. */
static void read_in_new_ascii(struct new_cpio_header *file_hdr, int in_des)
{
char ascii_header[13*8], *ah, hexbuf[9];
int i;
tape_buffered_read(ascii_header, in_des, 13*8);
ah = ascii_header;
hexbuf[8] = '\0';
for (i = 0; i < 13; i++) {
memcpy(hexbuf, ah, 8);
file_hdr->c_hdr[i] = strtoul(hexbuf, NULL, 16);
ah += 8;
}
/* Sizes > LONG_MAX can currently result in integer overflow
in various places. Fail if name is too large. */
if (file_hdr->c_namesize > LONG_MAX) {
fprintf(stderr, "%s: name size out of range\n",
progname);
exit(1);
}
/* Read file name from input. */
free(file_hdr->c_name);
file_hdr->c_name = (char *)xmalloc(file_hdr->c_namesize);
tape_buffered_read(file_hdr->c_name, in_des,
(long)file_hdr->c_namesize);
/* In SVR4 ASCII format, the amount of space allocated for the header
is rounded up to the next long-word, so we might need to drop
1-3 bytes. */
tape_skip_padding(in_des, file_hdr->c_namesize + 110);
/* Fail if file is too large. We could check this earlier
but it's helpful to report the name. */
if (file_hdr->c_filesize > LONG_MAX) {
fprintf(stderr, "%s: %s: file size out of range\n",
progname, file_hdr->c_name);
exit(1);
}
}
/* Return 16-bit integer I with the bytes swapped. */
#define swab_short(i) ((((i) << 8) & 0xff00) | (((i) >> 8) & 0x00ff))
/* Read the header, including the name of the file, from file
descriptor IN_DES into FILE_HDR. */
static void read_in_header(struct new_cpio_header *file_hdr, int in_des)
{
long bytes_skipped = 0; /* Bytes of junk found before magic number. */
/* Search for a valid magic number. */
file_hdr->c_tar_linkname = NULL;
tape_buffered_read((char *)file_hdr, in_des, 6L);
while (1) {
if (!strncmp((char *)file_hdr, "070702", 6)
|| !strncmp((char *)file_hdr, "070701", 6))
{
if (bytes_skipped > 0)
warn_junk_bytes(bytes_skipped);
read_in_new_ascii(file_hdr, in_des);
break;
}
bytes_skipped++;
memmove((char *)file_hdr, (char *)file_hdr + 1, 5);
tape_buffered_read((char *)file_hdr + 5, in_des, 1L);
}
}
/* Read the collection from standard input and create files
in the file system. */
static void process_copy_in(void)
{
char done = false; /* True if trailer reached. */
struct new_cpio_header file_hdr; /* Output header information. */
int in_file_des; /* Input file descriptor. */
/* Initialize the copy in. */
file_hdr.c_name = NULL;
/* only from stdin */
in_file_des = 0;
/* While there is more input in the collection, process the input. */
while (!done) {
/* Start processing the next file by reading the header. */
read_in_header(&file_hdr, in_file_des);
/* Is this the header for the TRAILER file? */
if (strcmp("TRAILER!!!", file_hdr.c_name) == 0) {
done = true;
break;
}
/* Copy the input file into the directory structure. */
copyin_file(&file_hdr, in_file_des);
if (dot_flag)
fputc('.', stderr);
}
if (dot_flag)
fputc('\n', stderr);
create_final_defers();
}
/* Initialize the input and output buffers to their proper size and
initialize all variables associated with the input and output
buffers. */
static void initialize_buffers(void)
{
int in_buf_size, out_buf_size;
/* Make sure the input buffer can always hold 2 blocks and that it
is big enough to hold 1 tar record (512 bytes) even if it
is not aligned on a block boundary. The extra buffer space
is needed by process_copyin and peek_in_buf to automatically
figure out what kind of archive it is reading. */
if (io_block_size >= 512)
in_buf_size = 2 * io_block_size;
else
in_buf_size = 1024;
out_buf_size = DISK_IO_BLOCK_SIZE;
input_buffer = (char *)xmalloc(in_buf_size);
in_buff = input_buffer;
input_buffer_size = in_buf_size;
input_size = 0;
input_bytes = 0;
output_buffer = (char *)xmalloc(out_buf_size);
out_buff = output_buffer;
output_size = 0;
output_bytes = 0;
}
int main(int argc, char *argv[])
{
int c;
int extract_flag = false;
progname = argv[0];
do {
c = getopt(argc, argv, "iV");
if (c == EOF)
break;
switch (c) {
case 'V':
dot_flag = true;
break;
case 'i':
extract_flag = true;
break;
case '?':
fprintf(stderr,
"%s: not implemented or invalid option -%c\n",
progname, optopt);
exit(1);
}
} while (1);
if (extract_flag) {
initialize_buffers();
process_copy_in();
} else {
fprintf(stderr, "Usage: %s [-V] -i [< archive]\n", progname);
exit(1);
}
return 0;
}
|