/* copyin.c - extract or list a cpio archive
Copyright (C) 1990,1991,1992,2001,2002,2003 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. */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "filetypes.h"
#include "system.h"
#include "cpiohdr.h"
#include "dstring.h"
#include "extern.h"
#include "defer.h"
#include "rmt.h"
#ifndef FNM_PATHNAME
#include <fnmatch.h>
#endif
#ifndef HAVE_LCHOWN
#define lchown chown
#endif
static void read_pattern_file ();
static void tape_skip_padding ();
static void defer_copyin ();
static void create_defered_links ();
static void create_final_defers ();
static int create_defered_links_to_skipped ();
static int query_rename();
static void list_file();
static void copyin_file();
static int try_existing_file();
static void copyin_regular_file();
static void copyin_directory();
static void copyin_device();
static void copyin_link();
/* 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. */
void
read_in_header (file_hdr, in_des)
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. */
if (archive_format == arf_unknown)
{
char tmpbuf[512];
int check_tar;
int peeked_bytes;
while (archive_format == arf_unknown)
{
peeked_bytes = tape_buffered_peek (tmpbuf, in_des, 512);
if (peeked_bytes < 6)
error (1, 0, "premature end of archive");
if (!strncmp (tmpbuf, "070701", 6))
archive_format = arf_newascii;
else if (!strncmp (tmpbuf, "070707", 6))
archive_format = arf_oldascii;
else if (!strncmp (tmpbuf, "070702", 6))
{
archive_format = arf_crcascii;
crc_i_flag = TRUE;
}
else if ((*((unsigned short *) tmpbuf) == 070707) ||
(*((unsigned short *) tmpbuf) == swab_short ((unsigned short) 070707)))
archive_format = arf_binary;
else if (peeked_bytes >= 512
&& (check_tar = is_tar_header (tmpbuf)))
{
if (check_tar == 2)
archive_format = arf_ustar;
else
archive_format = arf_tar;
}
else
{
tape_buffered_read ((char *) tmpbuf, in_des, 1L);
++bytes_skipped;
}
}
}
if (archive_format == arf_tar || archive_format == arf_ustar)
{
if (append_flag)
last_header_start = input_bytes - io_block_size +
(in_buff - input_buffer);
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_tar_header (file_hdr, in_des);
return;
}
file_hdr->c_tar_linkname = NULL;
tape_buffered_read ((char *) file_hdr, in_des, 6L);
while (1)
{
if (append_flag)
last_header_start = input_bytes - io_block_size
+ (in_buff - input_buffer) - 6;
if (archive_format == arf_newascii
&& !strncmp ((char *) file_hdr, "070701", 6))
{
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_new_ascii (file_hdr, in_des);
break;
}
if (archive_format == arf_crcascii
&& !strncmp ((char *) file_hdr, "070702", 6))
{
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_new_ascii (file_hdr, in_des);
break;
}
if ( (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
&& !strncmp ((char *) file_hdr, "070707", 6))
{
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_old_ascii (file_hdr, in_des);
break;
}
if ( (archive_format == arf_binary || archive_format == arf_hpbinary)
&& (file_hdr->c_magic == 070707
|| file_hdr->c_magic == swab_short ((unsigned short) 070707)))
{
/* Having to skip 1 byte because of word alignment is normal. */
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_binary (file_hdr, in_des);
break;
}
bytes_skipped++;
bcopy ((char *) file_hdr + 1, (char *) file_hdr, 5);
tape_buffered_read ((char *) file_hdr + 5, in_des, 1L);
}
}
/* Fill in FILE_HDR by reading an old-format ASCII format cpio header from
file descriptor IN_DES, except for the magic number, which is
already filled in. */
void
read_in_old_ascii (file_hdr, in_des)
struct new_cpio_header *file_hdr;
int in_des;
{
char ascii_header[78];
unsigned long dev;
unsigned long rdev;
tape_buffered_read (ascii_header, in_des, 70L);
ascii_header[70] = '\0';
sscanf (ascii_header,
"%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo",
&dev, &file_hdr->c_ino,
&file_hdr->c_mode, &file_hdr->c_uid, &file_hdr->c_gid,
&file_hdr->c_nlink, &rdev, &file_hdr->c_mtime,
&file_hdr->c_namesize, &file_hdr->c_filesize);
file_hdr->c_dev_maj = major (dev);
file_hdr->c_dev_min = minor (dev);
file_hdr->c_rdev_maj = major (rdev);
file_hdr->c_rdev_min = minor (rdev);
/* Read file name from input. */
if (file_hdr->c_name != NULL)
free (file_hdr->c_name);
file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize + 1);
tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
#ifndef __MSDOS__
/* HP/UX cpio creates archives that look just lik
|