From 14bf48fc3f645a158d5e21a1f470e54e50192b21 Mon Sep 17 00:00:00 2001 From: Joerg Sonnenberger Date: Wed, 10 Nov 2004 18:02:24 +0000 Subject: [PATCH 1/1] Import libarchive and bsdtar. The default tar (/usr/bin/tar) can be choosen at installworld time by setting WITH_BSDTAR. This is not the default yet. Obtained-from: FreeBSD --- contrib/bsdtar/COPYING | 28 + contrib/bsdtar/Makefile | 49 ++ contrib/bsdtar/README.DELETED | 3 + contrib/bsdtar/README.DRAGONFLY | 3 + contrib/bsdtar/bsdtar.1 | 682 +++++++++++++++ contrib/bsdtar/bsdtar.c | 777 +++++++++++++++++ contrib/bsdtar/bsdtar.h | 121 +++ contrib/bsdtar/bsdtar_platform.h | 146 ++++ contrib/bsdtar/fts.c | 1195 ++++++++++++++++++++++++++ contrib/bsdtar/fts.h | 147 ++++ contrib/bsdtar/matching.c | 258 ++++++ contrib/bsdtar/read.c | 430 ++++++++++ contrib/bsdtar/util.c | 382 +++++++++ contrib/bsdtar/write.c | 1365 ++++++++++++++++++++++++++++++ 14 files changed, 5586 insertions(+) create mode 100644 contrib/bsdtar/COPYING create mode 100644 contrib/bsdtar/Makefile create mode 100644 contrib/bsdtar/README.DELETED create mode 100644 contrib/bsdtar/README.DRAGONFLY create mode 100644 contrib/bsdtar/bsdtar.1 create mode 100644 contrib/bsdtar/bsdtar.c create mode 100644 contrib/bsdtar/bsdtar.h create mode 100644 contrib/bsdtar/bsdtar_platform.h create mode 100644 contrib/bsdtar/fts.c create mode 100644 contrib/bsdtar/fts.h create mode 100644 contrib/bsdtar/matching.c create mode 100644 contrib/bsdtar/read.c create mode 100644 contrib/bsdtar/util.c create mode 100644 contrib/bsdtar/write.c diff --git a/contrib/bsdtar/COPYING b/contrib/bsdtar/COPYING new file mode 100644 index 0000000000..555a2818b2 --- /dev/null +++ b/contrib/bsdtar/COPYING @@ -0,0 +1,28 @@ +All of the C source code and documentation in this package is subject +to the following: + +Copyright (c) 2003-2004 Tim Kientzle +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer + in this position and unchanged. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +$FreeBSD: src/usr.bin/tar/COPYING,v 1.1 2004/08/07 03:24:48 kientzle Exp $ diff --git a/contrib/bsdtar/Makefile b/contrib/bsdtar/Makefile new file mode 100644 index 0000000000..d07bac69f3 --- /dev/null +++ b/contrib/bsdtar/Makefile @@ -0,0 +1,49 @@ +# $FreeBSD: src/usr.bin/tar/Makefile,v 1.12 2004/11/05 05:39:37 kientzle Exp $ + +# +# Use "make distfile" to build a tar.gz file suitable for distribution, +# including an autoconf/automake-generated build system. +# + +PROG= bsdtar +VERSION= 1.01.015 +SRCS= bsdtar.c matching.c read.c util.c write.c +WARNS?= 6 +DPADD= ${LIBARCHIVE} ${LIBBZ2} ${LIBZ} +LDADD= -larchive -lbz2 -lz +CFLAGS+= -DPACKAGE_VERSION=\"${VERSION}\" + +.if !defined(WITH_GTAR) +SYMLINKS= ${BINDIR}/bsdtar ${BINDIR}/tar +MLINKS= bsdtar.1 tar.1 +.endif + +DIST_BUILD_DIR= ${.OBJDIR}/${PROG}-${VERSION} +CLEANDIRS+= ${DIST_BUILD_DIR} +DISTFILE= ${PROG}-${VERSION}.tar.gz +# Files that just get copied to the distfile build directory +DIST_FILES= ${SRCS} +DIST_FILES+= ${MAN} +DIST_FILES+= bsdtar.h bsdtar_platform.h +DIST_FILES+= Makefile.am +DIST_FILES+= fts.c fts.h + +distfile: + rm -rf ${DIST_BUILD_DIR} + mkdir ${DIST_BUILD_DIR} + for f in ${DIST_FILES}; \ + do \ + cat ${.CURDIR}/$$f >${DIST_BUILD_DIR}/$$f; \ + done + cat ${.CURDIR}/configure.ac.in | \ + sed 's/@VERSION@/${VERSION}/' | \ + cat > ${DIST_BUILD_DIR}/configure.ac + (cd ${DIST_BUILD_DIR} && aclocal && autoheader && autoconf ) + (cd ${DIST_BUILD_DIR} && automake -a --foreign) + (cd ${DIST_BUILD_DIR} && ./configure && make distcheck && make dist) + mv ${DIST_BUILD_DIR}/${DISTFILE} ${.OBJDIR} + @echo ================================================== + @echo Created ${.OBJDIR}/${DISTFILE} + @echo ================================================== + +.include diff --git a/contrib/bsdtar/README.DELETED b/contrib/bsdtar/README.DELETED new file mode 100644 index 0000000000..50146e641e --- /dev/null +++ b/contrib/bsdtar/README.DELETED @@ -0,0 +1,3 @@ +CVS +Makefile.am +configure.ac.in diff --git a/contrib/bsdtar/README.DRAGONFLY b/contrib/bsdtar/README.DRAGONFLY new file mode 100644 index 0000000000..c104722061 --- /dev/null +++ b/contrib/bsdtar/README.DRAGONFLY @@ -0,0 +1,3 @@ +This directory is a checkout from src/usr.bin/tar of the FreeBSD +CVS repository from Tue Nov 9 22:26:00 CET 2004. See README.DELETED +for files not included here. diff --git a/contrib/bsdtar/bsdtar.1 b/contrib/bsdtar/bsdtar.1 new file mode 100644 index 0000000000..6eb6bf2a44 --- /dev/null +++ b/contrib/bsdtar/bsdtar.1 @@ -0,0 +1,682 @@ +.\" Copyright (c) 2003 Tim Kientzle +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD: src/usr.bin/tar/bsdtar.1,v 1.21 2004/08/27 04:19:28 kientzle Exp $ +.\" +.Dd April 13, 2004 +.Dt BSDTAR 1 +.Os +.Sh NAME +.Nm tar +.Nd manipulate tape archives +.Sh SYNOPSIS +.Nm +.Op Ar bundled-flags Ao args Ac +.Op Ao Ar file Ac | Ao Ar pattern Ac ... +.Nm +.Brq Fl c +.Op Ar options +.Op Ar files | directories +.Nm +.Brq Fl r | Fl u +.Fl f Ar archive-file +.Op Ar options +.Op Ar files | directories +.Nm +.Brq Fl t | Fl x +.Op Ar options +.Op Ar patterns +.Sh DESCRIPTION +.Nm +creates and manipulates streaming archive files. +.Pp +The first synopsis form shows a +.Dq bundled +option word. +This usage is provided for compatibility with historical implementations. +See COMPATIBILITY below for details. +.Pp +The other synopsis forms show the preferred usage. +The first option to +.Nm +is a mode indicator from the following list: +.Bl -tag -compact -width indent +.It Fl c +Create a new archive containing the specified items. +.It Fl r +Like +.Fl c , +but new entries are appended to the archive. +Note that this only works on uncompressed archives stored in regular files. +The +.Fl f +option is required. +.It Fl t +List archive contents to stdout. +.It Fl u +Like +.Fl r , +but new entries are added only if they have a modification date +newer than the corresponding entry in the archive. +Note that this only works on uncompressed archives stored in regular files. +The +.Fl f +option is required. +.It Fl x +Extract to disk from the archive. +If a file with the same name appears more than once in the archive, +each copy will be extracted, with later copies overwriting (replacing) +earlier copies. +.El +.Pp +In +.Fl c , +.Fl r , +or +.Fl u +mode, each specified file or directory is added to the +archive in the order specified on the command line. +By default, the contents of each directory are also archived. +.Pp +In extract or list mode, the entire command line +is read and parsed before the archive is opened. +The pathnames or patterns on the command line indicate +which items in the archive should be processed. +Patterns are shell-style globbing patterns as +documented in XXXX. +.Sh OPTIONS +Unless specifically stated otherwise, options are applicable in +all operating modes. +.Bl -tag -width indent +.It Cm @ Ns Pa archive +(c and r mode only) +The specified archive is opened and the entries +in it will be appended to the current archive. +As a simple example, +.Dl Nm Fl c Fl f Pa - Pa newfile Cm @ Ns Pa original.tar +writes a new archive to standard output containing a file +.Pa newfile +and all of the entries from +.Pa original.tar . +In contrast, +.Dl Nm Fl c Fl f Pa - Pa newfile Pa original.tar +creates a new archive with only two entries. +Similarly, +.Dl Nm Fl czf Pa - Fl -format Cm pax Cm @ Ns Pa - +reads an archive from standard input (whose format will be determined +automatically) and converts it into a gzip-compressed +pax-format archive on stdout. +In this way, +.Nm +can be used to convert archives from one format to another. +.It Fl b Ar blocksize +Specify the block size, in 512-byte records, for tape drive I/O. +As a rule, this argument is only needed when reading from or writing +to tape drives, and usually not even then as the default block size of +20 records (10240 bytes) is very common. +.It Fl C Ar directory +In c and r mode, this changes the directory before adding +the following files. +In x mode, change directories after opening the archive +but before extracting entries from the archive. +.It Fl -check-links ( Fl W Cm check-links ) +(c and r modes only) +Issue a warning message unless all links to each file are archived. +.It Fl -exclude Ar pattern ( Fl W Cm exclude Ns = Ns Ar pattern ) +Do not process files or directories that match the +specified pattern. +Note that exclusions take precedence over patterns or filenames +specified on the command line. +.It Fl -format Ar format ( Fl W Cm format Ns = Ns Ar format ) +(c mode only) +Use the specified format for the created archive. +Supported formats include +.Dq cpio , +.Dq pax , +.Dq shar , +and +.Dq ustar . +Other formats may also be supported; see +.Xr libarchive-formats 5 +for more information about currently-supported formats. +.It Fl f Ar file +Read the archive from or write the archive to the specified file. +The filename can be +.Pa - +for standard input or standard output. +If not specified, the default tape device will be used. +(On FreeBSD, the default tape device is +.Pa /dev/sa0 . ) +.It Fl -fast-read ( Fl W Cm fast-read ) +(x and t mode only) +Extract or list only the first archive entry that matches each pattern +or filename operand. +Exit as soon as each specified pattern or filename has been matched. +By default, the archive is always read to the very end, since +there can be multiple entries with the same name and, by convention, +later entries overwrite earlier entries. +This option is provided as a performance optimization. +.It Fl H +(c and r mode only) +Symbolic links named on the command line will be followed; the +target of the link will be archived, not the link itself. +.It Fl h +(c and r mode only) +Synonym for +.Fl L . +.It Fl -include Ar pattern ( Fl W Cm include Ns = Ns Ar pattern ) +Process only files or directories that match the specified pattern. +Note that exclusions specified with +.Fl -exclude +take precedence over inclusions. +If no inclusions are explicitly specified, all entries are processed by +default. +The +.Fl -include +option is especially useful when filtering archives. +For example, the command +.Dl Nm Fl c Fl f Pa new.tar Fl -include='*foo*' Cm @ Ns Pa old.tgz +creates a new archive +.Pa new.tar +containing only the entries from +.Pa old.tgz +containing the string +.Sq foo . +.It Fl j +(c mode only) +Compress the resulting archive with +.Xr bzip2 1 . +In extract or list modes, this option is ignored. +Note that, unlike other +.Nm tar +implementations, this implementation recognizes bzip2 compression +automatically when reading archives. +.It Fl k +(x mode only) +Do not overwrite existing files. +In particular, if a file appears more than once in an archive, +later copies will not overwrite earlier copies. +.It Fl L +(c and r mode only) +All symbolic links will be followed. +Normally, symbolic links are archived as such. +With this option, the target of the link will be archived instead. +.It Fl l +If +.Ev POSIXLY_CORRECT +is specified in the environment, this is a synonym for the +.Fl -check-links +option. +Otherwise, an error will be displayed. +Users who desire behavior compatible with GNU tar should use +the +.Fl -one-file-system +option instead. +.It Fl m +(x mode only) +Do not extract modification time. +By default, the modification time is set to the time stored in the archive. +.It Fl n +(c, r, u modes only) +Do not recursively archive the contents of directories. +.It Fl -nodump ( Fl W Cm nodump ) +(c and r modes only) +Honor the nodump file flag by skipping this file. +.It Fl O +(x, t modes only) +In extract (-x) mode, files will be written to standard out rather than +being extracted to disk. +In list (-t) mode, the file listing will be written to stderr rather than +the usual stdout. +.It Fl o +(x mode only) +Use the user and group of the user running the program rather +than those specified in the archive. +Note that this has no significance unless +.Fl p +is specified, and the program is being run by the root user. +In this case, the file modes and flags from +the archive will be restored, but ACLs or owner information in +the archive will be discarded. +.It Fl P +Preserve pathnames. +By default, absolute pathnames (those that begin with a / +character) have the leading slash removed both when creating archives +and extracting from them. +Also, +.Nm +will refuse to extract archive entries whose pathnames contain +.Pa .. +or whose target directory would be altered by a symlink. +This option suppresses these behaviors. +.It Fl p +(x mode only) +Preserve file permissions. +Attempt to restore the full permissions, including owner, file modes, file +flags and ACLs, if available, for each item extracted from the archive. +By default, newly-created files are owned by the user running +.Nm , +the file mode is restored for newly-created regular files, and +all other types of entries receive default permissions. +If +.Nm +is being run by root, the default is to restore the owner unless the +.Fl o +option is also specified. +.It Fl T Ar filename +(c mode only) +Read names to be archived from +.Pa filename . +Names are terminated by newlines. +The special name +.Dq -C +will cause the current directory to be changed to the directory +specified on the following line. +.It Fl U +(x mode only) +Unlink files before creating them. +Without this option, +.Nm +overwrites existing files, which preserves existing hardlinks. +With this option, existing hardlinks will be broken, as will any +symlink that would affect the location of an extracted file. +.It Fl v +Produce verbose output. +In create and extract modes, +.Nm +will list each file name as it is read from or written to +the archive. +In list mode, +.Nm +will produce output similar to that of +.Xr ls 1 . +Additional +.Fl v +options will provide additional detail. +.It Fl W Ar longopt=value +Long options (preceded by +.Fl - ) +are only supported directly on systems that have the +.Xr getopt_long 3 +function. +The +.Fl W +option can be used to access long options on systems that +do not support this function. +.It Fl w +Ask for confirmation for every action. +.It Fl X Ar filename +Read a list of exclusion patterns from the specified file. +See +.Fl -exclude +for more information about the handling of exclusions. +.It Fl y +(c mode only) +Compress the resulting archive with +.Xr bzip2 1 . +In extract or list modes, this option is ignored. +Note that, unlike other +.Nm tar +implementations, this implementation recognizes bzip2 compression +automatically when reading archives. +.It Fl z +(c mode only) +Compress the resulting archive with +.Xr gzip 1 . +In extract or list modes, this option is ignored. +Note that, unlike other +.Nm tar +implementations, this implementation recognizes gzip compression +automatically when reading archives. +.El +.Sh EXAMPLES +The following creates a new archive +called +.Ar file.tar +that contains two files +.Ar source.c +and +.Ar source.h : +.Dl Nm Fl czf Pa file.tar Pa source.c Pa source.h +.Pp +To view a detailed table of contents for this +archive: +.Dl Nm Fl tvf Pa file.tar +.Pp +To extract all entries from the archive on +the default tape drive: +.Dl Nm Fl x +.Pp +In create mode, the list of files and directories to be archived +can also include directory change instructions of the form +.Cm -C Ns Pa foo/baz +and archive inclusions of the form +.Cm @ Ns Pa archive-file . +For example, the command line +.Dl Nm Fl c Fl f Pa new.tar Pa foo1 Cm @ Ns Pa old.tgz Cm -C Ns Pa /tmp Pa foo2 +will create a new archive +.Pa new.tar . +.Nm +will read the file +.Pa foo1 +from the current directory and add it to the output archive. +It will then read each entry from +.Pa old.tgz +and add those entries to the output archive. +Finally, it will switch to the +.Pa /tmp +directory and add +.Pa foo2 +to the output archive. +.Sh DIAGNOSTICS +.Ex -std +.Sh ENVIRONMENT +The following environment variables affect the execution of +.Nm : +.Bl -tag -width ".Ev BLOCKSIZE" +.It Ev LANG +The locale to use. +See +.Xr environ 7 +for more information. +.It Ev POSIXLY_CORRECT +If this environment variable is defined, the +.Fl l +option will be interpreted in accordance with +.St -p1003.1-96 . +.It Ev TAPE +The default tape device. +The +.Fl f +option overrides this. +.It Ev TZ +The timezone to use when displaying dates. +See +.Xr environ 7 +for more information. +.El +.Sh FILES +.Bl -tag -width ".Ev BLOCKSIZE" +.It Pa /dev/sa0 +The default tape device, if not overridden by the +.Ev TAPE +environment variable or the +.Fl f +option. +.El +.Sh COMPATIBILITY +The bundled-arguments format is supported for compatibility +with historic implementations. +It consists of an initial word (with no leading - character) in which +each character indicates an option. +Arguments follow as separate words. +The order of the arguments must match the order +of the corresponding characters in the bundled command word. +For example, +.Dl Nm Cm tbf 32 Pa file.tar +specifies three flags +.Cm t , +.Cm b , +and +.Cm f . +The +.Cm b +and +.Cm f +flags both require arguments, +so there must be two additional items +on the command line. +The +.Ar 32 +is the argument to the +.Cm b +flag, and +.Ar file.tar +is the argument to the +.Cm f +flag. +.Pp +The mode options c, r, t, u, and x and the options +b, f, l, m, o, v, and w comply with SUSv2. +.Pp +For maximum portability, scripts that invoke +.Nm tar +should use the bundled-argument format above, should limit +themselves to the +.Cm c , +.Cm t , +and +.Cm x +modes, and the +.Cm b , +.Cm f , +.Cm m , +.Cm v , +and +.Cm w +options. +.Pp +On systems that support getopt_long(), additional long options +are available to improve compatibility with other tar implementations. +.Sh SECURITY +Certain security issues are common to many archiving programs, including +.Nm . +In particular, carefully-crafted archives can request that +.Nm +extract files to locations outside of the target directory. +This can potentially be used to cause unwitting users to overwrite +files they did not intend to overwrite. +If the archive is being extracted by the superuser, any file +on the system can potentially be overwritten. +There are three ways this can happen. +Although +.Nm +has mechanisms to protect against each one, +savvy users should be aware of the implications: +.Bl -bullet -width indent +.It +Archive entries can have absolute pathnames. +By default, +.Nm +removes the leading +.Pa / +character from filenames before restoring them to guard against this problem. +.It +Archive entries can have pathnames that include +.Pa .. +components. +By default, +.Nm +will not extract files containing +.Pa .. +components in their pathname. +.It +Archive entries can exploit symbolic links to restore +files to other directories. +An archive can restore a symbolic link to another directory, +then use that link to restore a file into that directory. +To guard against this, +.Nm +checks each extracted path for symlinks. +If the final path element is a symlink, it will be removed +and replaced with the archive entry. +If +.Fl U +is specified, any intermediate symlink will also be unconditionally removed. +If neither +.Fl U +nor +.Fl P +is specified, +.Nm +will refuse to extract the entry. +.El +To protect yourself, you should be wary of any archives that +come from untrusted sources. +You should examine the contents of an archive with +.Dl Nm Fl tf Pa filename +before extraction. +You should use the +.Fl k +option to ensure that +.Nm +will not overwrite any existing files or the +.Fl U +option to remove any pre-existing files. +You should generally not extract archives while running with super-user +privileges. +Note that the +.Fl P +option to +.Nm +disables the security checks above and allows you to extract +an archive while preserving any absolute pathnames, +.Pa .. +components, or symlinks to other directories. +.Sh SEE ALSO +.Xr bzip2 1 , +.Xr cpio 1 , +.Xr gzip 1 , +.Xr mt 1 , +.Xr pax 1 , +.Xr shar 1 , +.Xr libarchive 3 , +.Xr libarchive-formats 5 , +.Xr tar 5 +.Sh STANDARDS +There is no current POSIX standard for the tar command; it appeared +in +.St -p1003.1-96 +but was dropped from +.St -p1003.1-2001 . +The options used by this implementation were developed by surveying a +number of existing tar implementations as well as the old POSIX specification +for tar and the current POSIX specification for pax. +.Pp +The ustar and pax interchange file formats are defined by +.St -p1003.1-2001 +for the pax command. +.Sh BUGS +POSIX and GNU violently disagree about the meaning of the +.Fl l +option. +Because of the potential for disaster if someone expects +one behavior and gets the other, the +.Fl l +option is deliberately broken in this implementation. +.Pp +The +.Fl C Pa dir +option may differ from historic implementations. +.Pp +All archive output is written in correctly-sized blocks, even +if the output is being compressed. +Whether or not the last output block is padded to a full +block size varies depending on the format and the +output device. +For tar and cpio formats, the last block of output is padded +to a full block size if the output is being +written to standard output or to a character or block device such as +a tape drive. +If the output is being written to a regular file, the last block +will not be padded. +Many compressors, including +.Xr gzip 1 +and +.Xr bzip2 1 , +complain about the null padding when decompressing an archive created by +.Nm , +although they still extract it correctly. +.Pp +The compression and decompression is implemented internally, so +there may be insignificant differences between the compressed output +generated by +.Dl Nm Fl czf Pa - file +and that generated by +.Dl Nm Fl cf Pa - file | Nm gzip +.Pp +The default should be to read and write archives to the standard I/O paths, +but tradition (and POSIX) dictates otherwise. +.Pp +The +.Cm r +and +.Cm u +modes require that the archive be uncompressed +and located in a regular file on disk. +Other archives can be modified using +.Cm c +mode with the +.Pa @archive-file +extension. +.Pp +To archive a file called +.Pa @foo +or +.Pa -foo +you must specify it as +.Pa ./@foo +or +.Pa ./-foo , +respectively. +.Pp +In create mode, a leading +.Pa ./ +is always removed. +A leading +.Pa / +is stripped unless the +.Fl P +option is specified. +.Pp +There needs to be better support for file selection on both create +and extract. +.Pp +There is not yet any support for multi-volume archives or for archiving +sparse files. +.Pp +Converting between dissimilar archive formats (such as tar and cpio) using the +.Cm @ Ns Pa - +convention can cause hard link information to be lost. +(This is a consequence of the incompatible ways that different archive +formats store hardlink information.) +.Pp +There are alternative long options for many of the short options that +are deliberately not documented. +.Sh HISTORY +A +.Nm tar +command appeared in Seventh Edition Unix. +There have been numerous other implementations, +many of which extended the file format. +John Gilmore's +.Nm pdtar +public-domain implementation (circa November, 1987) +was quite influential, and formed the basis of GNU tar. +GNU tar was included as the standard system tar +in FreeBSD beginning with FreeBSD 1.0. +.Pp +This is a complete re-implementation based on the +.Xr libarchive 3 +library. diff --git a/contrib/bsdtar/bsdtar.c b/contrib/bsdtar/bsdtar.c new file mode 100644 index 0000000000..d6c0286e25 --- /dev/null +++ b/contrib/bsdtar/bsdtar.c @@ -0,0 +1,777 @@ +/*- + * Copyright (c) 2003-2004 Tim Kientzle + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "bsdtar_platform.h" +__FBSDID("$FreeBSD: src/usr.bin/tar/bsdtar.c,v 1.56 2004/10/17 23:58:17 kientzle Exp $"); + +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_GETOPT_LONG +#include +#else +struct option { + const char *name; + int has_arg; + int *flag; + int val; +}; +#define no_argument 0 +#define required_argument 1 +#endif +#ifdef HAVE_LANGINFO_H +#include +#endif +#include +#ifdef HAVE_PATHS_H +#include +#endif +#include +#include +#include +#include +#include +#if HAVE_ZLIB_H +#include +#endif + +#include "bsdtar.h" + +#ifdef linux +#define _PATH_DEFTAPE "/dev/st0" +#endif + +#ifndef _PATH_DEFTAPE +#define _PATH_DEFTAPE "/dev/tape" +#endif + +static int bsdtar_getopt(struct bsdtar *, const char *optstring, + const struct option **poption); +static void long_help(struct bsdtar *); +static void only_mode(struct bsdtar *, const char *opt, + const char *valid); +static char ** rewrite_argv(struct bsdtar *, + int *argc, char ** src_argv, + const char *optstring); +static void set_mode(struct bsdtar *, char opt); +static void version(void); + +/* + * The leading '+' here forces the GNU version of getopt() (as well as + * both the GNU and BSD versions of getopt_long) to stop at the first + * non-option. Otherwise, GNU getopt() permutes the arguments and + * screws up -C processing. + */ +static const char *tar_opts = "+Bb:C:cF:f:HhI:jkLlmnOoPprtT:UuvW:wX:xyZz"; + +/* + * Most of these long options are deliberately not documented. They + * are provided only to make life easier for people who also use GNU tar. + * The only long options documented in the manual page are the ones + * with no corresponding short option, such as --exclude, --nodump, + * and --fast-read. + * + * On systems that lack getopt_long, long options can be specified + * using -W longopt and -W longopt=value, e.g. "-W nodump" is the same + * as "--nodump" and "-W exclude=pattern" is the same as "--exclude + * pattern". This does not rely the GNU getopt() "W;" extension, so + * should work correctly on any system with a POSIX-compliant getopt(). + */ + +/* Fake short equivalents for long options that otherwise lack them. */ +#define OPTION_CHECK_LINKS 3 +#define OPTION_EXCLUDE 6 +#define OPTION_FAST_READ 9 +#define OPTION_FORMAT 10 +#define OPTION_HELP 12 +#define OPTION_INCLUDE 15 +#define OPTION_NODUMP 18 +#define OPTION_NO_SAME_PERMISSIONS 21 +#define OPTION_NULL 24 +#define OPTION_ONE_FILE_SYSTEM 27 +#define OPTION_TOTALS 28 +#define OPTION_VERSION 30 + +static const struct option tar_longopts[] = { + { "absolute-paths", no_argument, NULL, 'P' }, + { "append", no_argument, NULL, 'r' }, + { "block-size", required_argument, NULL, 'b' }, + { "bunzip2", no_argument, NULL, 'j' }, + { "bzip", no_argument, NULL, 'j' }, + { "bzip2", no_argument, NULL, 'j' }, + { "cd", required_argument, NULL, 'C' }, + { "check-links", no_argument, NULL, OPTION_CHECK_LINKS }, + { "confirmation", no_argument, NULL, 'w' }, + { "create", no_argument, NULL, 'c' }, + { "dereference", no_argument, NULL, 'L' }, + { "directory", required_argument, NULL, 'C' }, + { "exclude", required_argument, NULL, OPTION_EXCLUDE }, + { "exclude-from", required_argument, NULL, 'X' }, + { "extract", no_argument, NULL, 'x' }, + { "fast-read", no_argument, NULL, OPTION_FAST_READ }, + { "file", required_argument, NULL, 'f' }, + { "files-from", required_argument, NULL, 'T' }, + { "format", required_argument, NULL, OPTION_FORMAT }, + { "gunzip", no_argument, NULL, 'z' }, + { "gzip", no_argument, NULL, 'z' }, + { "help", no_argument, NULL, OPTION_HELP }, + { "include", required_argument, NULL, OPTION_INCLUDE }, + { "interactive", no_argument, NULL, 'w' }, + { "keep-old-files", no_argument, NULL, 'k' }, + { "list", no_argument, NULL, 't' }, + { "modification-time", no_argument, NULL, 'm' }, + { "nodump", no_argument, NULL, OPTION_NODUMP }, + { "norecurse", no_argument, NULL, 'n' }, + { "no-recursion", no_argument, NULL, 'n' }, + { "no-same-owner", no_argument, NULL, 'o' }, + { "no-same-permissions",no_argument, NULL, OPTION_NO_SAME_PERMISSIONS }, + { "null", no_argument, NULL, OPTION_NULL }, + { "one-file-system", no_argument, NULL, OPTION_ONE_FILE_SYSTEM }, + { "preserve-permissions", no_argument, NULL, 'p' }, + { "read-full-blocks", no_argument, NULL, 'B' }, + { "same-permissions", no_argument, NULL, 'p' }, + { "to-stdout", no_argument, NULL, 'O' }, + { "totals", no_argument, NULL, OPTION_TOTALS }, + { "unlink", no_argument, NULL, 'U' }, + { "unlink-first", no_argument, NULL, 'U' }, + { "update", no_argument, NULL, 'u' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, OPTION_VERSION }, + { NULL, 0, NULL, 0 } +}; + +int +main(int argc, char **argv) +{ + struct bsdtar *bsdtar, bsdtar_storage; + const struct option *option; + int opt, t; + char option_o; + char possible_help_request; + char buff[16]; + + /* + * Use a pointer for consistency, but stack-allocated storage + * for ease of cleanup. + */ + bsdtar = &bsdtar_storage; + memset(bsdtar, 0, sizeof(*bsdtar)); + bsdtar->fd = -1; /* Mark as "unused" */ + option_o = 0; + + if (setlocale(LC_ALL, "") == NULL) + bsdtar_warnc(bsdtar, 0, "Failed to set default locale"); +#if defined(HAVE_NL_LANGINFO) && defined(HAVE_D_MD_ORDER) + bsdtar->day_first = (*nl_langinfo(D_MD_ORDER) == 'd'); +#endif + possible_help_request = 0; + + /* Look up uid of current user for future reference */ + bsdtar->user_uid = geteuid(); + + /* Default: open tape drive. */ + bsdtar->filename = getenv("TAPE"); + if (bsdtar->filename == NULL) + bsdtar->filename = _PATH_DEFTAPE; + + /* Default: preserve mod time on extract */ + bsdtar->extract_flags = ARCHIVE_EXTRACT_TIME; + + if (bsdtar->user_uid == 0) + bsdtar->extract_flags |= ARCHIVE_EXTRACT_OWNER; + + if (*argv == NULL) + bsdtar->progname = "bsdtar"; + else { + bsdtar->progname = strrchr(*argv, '/'); + if (bsdtar->progname != NULL) + bsdtar->progname++; + else + bsdtar->progname = *argv; + } + + /* Rewrite traditional-style tar arguments, if used. */ + argv = rewrite_argv(bsdtar, &argc, argv, tar_opts); + + bsdtar->argv = argv; + bsdtar->argc = argc; + + /* Process all remaining arguments now. */ + while ((opt = bsdtar_getopt(bsdtar, tar_opts, &option)) != -1) { + switch (opt) { + case 'B': /* GNU tar */ + /* libarchive doesn't need this; just ignore it. */ + break; + case 'b': /* SUSv2 */ + t = atoi(optarg); + if (t <= 0 || t > 1024) + bsdtar_errc(bsdtar, 1, 0, + "Argument to -b is out of range (1..1024)"); + bsdtar->bytes_per_block = 512 * t; + break; + case 'C': /* GNU tar */ + set_chdir(bsdtar, optarg); + break; + case 'c': /* SUSv2 */ + set_mode(bsdtar, opt); + break; + case OPTION_CHECK_LINKS: /* GNU tar */ + bsdtar->option_warn_links = 1; + break; + case OPTION_EXCLUDE: /* GNU tar */ + if (exclude(bsdtar, optarg)) + bsdtar_errc(bsdtar, 1, 0, + "Couldn't exclude %s\n", optarg); + break; + case OPTION_FORMAT: + bsdtar->create_format = optarg; + break; + case 'f': /* SUSv2 */ + bsdtar->filename = optarg; + if (strcmp(bsdtar->filename, "-") == 0) + bsdtar->filename = NULL; + break; + case OPTION_FAST_READ: /* GNU tar */ + bsdtar->option_fast_read = 1; + break; + case 'H': /* BSD convention */ + bsdtar->symlink_mode = 'H'; + break; + case 'h': /* Linux Standards Base, gtar; synonym for -L */ + bsdtar->symlink_mode = 'L'; + /* Hack: -h by itself is the "help" command. */ + possible_help_request = 1; + break; + case OPTION_HELP: + long_help(bsdtar); + exit(0); + break; + case 'I': /* GNU tar */ + bsdtar->names_from_file = optarg; + break; + case OPTION_INCLUDE: + if (include(bsdtar, optarg)) + bsdtar_errc(bsdtar, 1, 0, + "Failed to add %s to inclusion list", + optarg); + break; + case 'j': /* GNU tar */ +#if HAVE_LIBBZ2 + if (bsdtar->create_compression != '\0') + bsdtar_errc(bsdtar, 1, 0, + "Can't specify both -%c and -%c", opt, + bsdtar->create_compression); + bsdtar->create_compression = opt; +#else + bsdtar_warnc(bsdtar, 0, "-j compression not supported by this version of bsdtar"); + usage(bsdtar); +#endif + break; + case 'k': /* GNU tar */ + bsdtar->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE; + break; + case 'L': /* BSD convention */ + bsdtar->symlink_mode = 'L'; + break; + case 'l': /* SUSv2 and GNU conflict badly here */ + if (getenv("POSIXLY_CORRECT") != NULL) { + /* User has asked for POSIX/SUS behavior. */ + bsdtar->option_warn_links = 1; + } else { + fprintf(stderr, +"Error: -l has different behaviors in different tar programs.\n"); + fprintf(stderr, +" For the GNU behavior, use --one-file-system instead.\n"); + fprintf(stderr, +" For the POSIX behavior, use --check-links instead.\n"); + usage(bsdtar); + } + break; + case 'm': /* SUSv2 */ + bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_TIME; + break; + case 'n': /* GNU tar */ + bsdtar->option_no_subdirs = 1; + break; + case OPTION_NODUMP: /* star */ + bsdtar->option_honor_nodump = 1; + break; + case OPTION_NO_SAME_PERMISSIONS: /* GNU tar */ + /* + * This is always the default in FreeBSD's + * version of GNU tar; it's also the default + * behavior for bsdtar, so treat the + * command-line option as a no-op. + */ + break; + case OPTION_NULL: /* GNU tar */ + bsdtar->option_null++; + break; + case 'O': /* GNU tar */ + bsdtar->option_stdout = 1; + break; + case 'o': /* SUSv2 and GNU conflict here */ + option_o = 1; /* Record it and resolve it later. */ + break; + case OPTION_ONE_FILE_SYSTEM: /* -l in GNU tar */ + bsdtar->option_dont_traverse_mounts = 1; + break; +#if 0 + /* + * The common BSD -P option is not necessary, since + * our default is to archive symlinks, not follow + * them. This is convenient, as -P conflicts with GNU + * tar anyway. + */ + case 'P': /* BSD convention */ + /* Default behavior, no option necessary. */ + break; +#endif + case 'P': /* GNU tar */ + bsdtar->option_absolute_paths = 1; + break; + case 'p': /* GNU tar, star */ + bsdtar->extract_flags |= ARCHIVE_EXTRACT_PERM; + bsdtar->extract_flags |= ARCHIVE_EXTRACT_ACL; + bsdtar->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; + break; + case 'r': /* SUSv2 */ + set_mode(bsdtar, opt); + break; + case 'T': /* GNU tar */ + bsdtar->names_from_file = optarg; + break; + case 't': /* SUSv2 */ + set_mode(bsdtar, opt); + bsdtar->verbose++; + break; + case OPTION_TOTALS: /* GNU tar */ + bsdtar->option_totals++; + break; + case 'U': /* GNU tar */ + bsdtar->extract_flags |= ARCHIVE_EXTRACT_UNLINK; + bsdtar->option_unlink_first = 1; + break; + case 'u': /* SUSv2 */ + set_mode(bsdtar, opt); + break; + case 'v': /* SUSv2 */ + bsdtar->verbose++; + break; + case OPTION_VERSION: + version(); + break; + case 'w': /* SUSv2 */ + bsdtar->option_interactive = 1; + break; + case 'X': /* GNU tar */ + if (exclude_from_file(bsdtar, optarg)) + bsdtar_errc(bsdtar, 1, 0, + "failed to process exclusions from file %s", + optarg); + break; + case 'x': /* SUSv2 */ + set_mode(bsdtar, opt); + break; + case 'y': /* FreeBSD version of GNU tar */ +#if HAVE_LIBBZ2 + if (bsdtar->create_compression != '\0') + bsdtar_errc(bsdtar, 1, 0, + "Can't specify both -%c and -%c", opt, + bsdtar->create_compression); + bsdtar->create_compression = opt; +#else + bsdtar_warnc(bsdtar, 0, "-y compression not supported by this version of bsdtar"); + usage(bsdtar); +#endif + break; + case 'Z': /* GNU tar */ + if (bsdtar->create_compression != '\0') + bsdtar_errc(bsdtar, 1, 0, + "Can't specify both -%c and -%c", opt, + bsdtar->create_compression); + bsdtar->create_compression = opt; + break; + case 'z': /* GNU tar, star, many others */ +#if HAVE_LIBZ + if (bsdtar->create_compression != '\0') + bsdtar_errc(bsdtar, 1, 0, + "Can't specify both -%c and -%c", opt, + bsdtar->create_compression); + bsdtar->create_compression = opt; +#else + bsdtar_warnc(bsdtar, 0, "-z compression not supported by this version of bsdtar"); + usage(bsdtar); +#endif + break; + default: + usage(bsdtar); + } + } + + /* + * Sanity-check options. + */ + if ((bsdtar->mode == '\0') && possible_help_request) { + long_help(bsdtar); + exit(0); + } + + if (bsdtar->mode == '\0') + bsdtar_errc(bsdtar, 1, 0, + "Must specify one of -c, -r, -t, -u, -x"); + + /* Check boolean options only permitted in certain modes. */ + if (bsdtar->option_dont_traverse_mounts) + only_mode(bsdtar, "-X", "cru"); + if (bsdtar->option_fast_read) + only_mode(bsdtar, "--fast-read", "xt"); + if (bsdtar->option_honor_nodump) + only_mode(bsdtar, "--nodump", "cru"); + if (option_o > 0) { + switch (bsdtar->mode) { + case 'c': + /* + * In GNU tar, -o means "old format." The + * "ustar" format is the closest thing + * supported by libarchive. + */ + bsdtar->create_format = "ustar"; + /* TODO: bsdtar->create_format = "v7"; */ + break; + case 'x': + /* POSIX-compatible behavior. */ + bsdtar->option_no_owner = 1; + bsdtar->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; + break; + default: + only_mode(bsdtar, "-o", "xc"); + break; + } + } + if (bsdtar->option_no_subdirs) + only_mode(bsdtar, "-n", "cru"); + if (bsdtar->option_stdout) + only_mode(bsdtar, "-O", "xt"); + if (bsdtar->option_warn_links) + only_mode(bsdtar, "--check-links", "cr"); + + /* Check other parameters only permitted in certain modes. */ + if (bsdtar->create_compression == 'Z' && bsdtar->mode == 'c') { + bsdtar_warnc(bsdtar, 0, ".Z compression not supported"); + usage(bsdtar); + } + if (bsdtar->create_compression != '\0') { + strcpy(buff, "-?"); + buff[1] = bsdtar->create_compression; + only_mode(bsdtar, buff, "cxt"); + } + if (bsdtar->create_format != NULL) + only_mode(bsdtar, "-F", "c"); + if (bsdtar->symlink_mode != '\0') { + strcpy(buff, "-?"); + buff[1] = bsdtar->symlink_mode; + only_mode(bsdtar, buff, "cru"); + } + + bsdtar->argc -= optind; + bsdtar->argv += optind; + + switch(bsdtar->mode) { + case 'c': + tar_mode_c(bsdtar); + break; + case 'r': + tar_mode_r(bsdtar); + break; + case 't': + tar_mode_t(bsdtar); + break; + case 'u': + tar_mode_u(bsdtar); + break; + case 'x': + tar_mode_x(bsdtar); + break; + } + + cleanup_exclusions(bsdtar); + return (bsdtar->return_value); +} + +static void +set_mode(struct bsdtar *bsdtar, char opt) +{ + if (bsdtar->mode != '\0' && bsdtar->mode != opt) + bsdtar_errc(bsdtar, 1, 0, + "Can't specify both -%c and -%c", opt, bsdtar->mode); + bsdtar->mode = opt; +} + +/* + * Verify that the mode is correct. + */ +static void +only_mode(struct bsdtar *bsdtar, const char *opt, const char *valid_modes) +{ + if (strchr(valid_modes, bsdtar->mode) == NULL) + bsdtar_errc(bsdtar, 1, 0, + "Option %s is not permitted in mode -%c", + opt, bsdtar->mode); +} + + +/*- + * Convert traditional tar arguments into new-style. + * For example, + * tar tvfb file.tar 32 --exclude FOO + * will be converted to + * tar -t -v -f file.tar -b 32 --exclude FOO + * + * This requires building a new argv array. The initial bundled word + * gets expanded into a new string that looks like "-t\0-v\0-f\0-b\0". + * The new argv array has pointers into this string intermingled with + * pointers to the existing arguments. Arguments are moved to + * immediately follow their options. + * + * The optstring argument here is the same one passed to getopt(3). + * It is used to determine which option letters have trailing arguments. + */ +char ** +rewrite_argv(struct bsdtar *bsdtar, int *argc, char **src_argv, + const char *optstring) +{ + char **new_argv, **dest_argv; + const char *p; + char *src, *dest; + + if (src_argv[0] == NULL || + src_argv[1] == NULL || src_argv[1][0] == '-') + return (src_argv); + + *argc += strlen(src_argv[1]) - 1; + new_argv = malloc((*argc + 1) * sizeof(new_argv[0])); + if (new_argv == NULL) + bsdtar_errc(bsdtar, 1, errno, "No Memory"); + + dest_argv = new_argv; + *dest_argv++ = *src_argv++; + + dest = malloc(strlen(*src_argv) * 3); + if (dest == NULL) + bsdtar_errc(bsdtar, 1, errno, "No memory"); + for (src = *src_argv++; *src != '\0'; src++) { + *dest_argv++ = dest; + *dest++ = '-'; + *dest++ = *src; + *dest++ = '\0'; + /* If option takes an argument, insert that into the list. */ + for (p = optstring; p != NULL && *p != '\0'; p++) { + if (*p != *src) + continue; + if (p[1] != ':') /* No arg required, done. */ + break; + if (*src_argv == NULL) /* No arg available? Error. */ + bsdtar_errc(bsdtar, 1, 0, + "Option %c requires an argument", + *src); + *dest_argv++ = *src_argv++; + break; + } + } + + /* Copy remaining arguments, including trailing NULL. */ + while ((*dest_argv++ = *src_argv++) != NULL) + ; + + return (new_argv); +} + +void +usage(struct bsdtar *bsdtar) +{ + const char *p; + + p = bsdtar->progname; + + fprintf(stderr, "Usage:\n"); + fprintf(stderr, " List: %s -tf \n", p); + fprintf(stderr, " Extract: %s -xf \n", p); + fprintf(stderr, " Create: %s -cf [filenames...]\n", p); +#ifdef HAVE_GETOPT_LONG + fprintf(stderr, " Help: %s --help\n", p); +#else + fprintf(stderr, " Help: %s -h\n", p); +#endif + exit(1); +} + +static void +version(void) +{ + printf("bsdtar %s, ", PACKAGE_VERSION); + printf("%s\n", archive_version()); + printf("Copyright (C) 2003-2004 Tim Kientzle\n"); + exit(1); +} + +static const char *long_help_msg = + "First option must be a mode specifier:\n" + " -c Create -r Add/Replace -t List -u Update -x Extract\n" + "Common Options:\n" + " -b # Use # 512-byte records per I/O block\n" + " -f Location of archive (default " _PATH_DEFTAPE ")\n" + " -v Verbose\n" + " -w Interactive\n" + "Create: %p -c [options] [ | | @ | -C ]\n" + " , add these items to archive\n" + " -z, -j Compress archive with gzip/bzip2\n" + " --format {ustar|pax|cpio|shar} Select archive format\n" +#ifdef HAVE_GETOPT_LONG + " --exclude Skip files that match pattern\n" +#else + " -W exclude= Skip files that match pattern\n" +#endif + " -C Change to before processing remaining files\n" + " @ Add entries from to output\n" + "List: %p -t [options] []\n" + " If specified, list only entries that match\n" + "Extract: %p -x [options] []\n" + " If specified, extract only entries that match\n" + " -k Keep (don't overwrite) existing files\n" + " -m Don't restore modification times\n" + " -O Write entries to stdout, don't restore to disk\n" + " -p Restore permissions (including ACLs, owner, file flags)\n"; + + +/* + * Note that the word 'bsdtar' will always appear in the first line + * of output. + * + * In particular, /bin/sh scripts that need to test for the presence + * of bsdtar can use the following template: + * + * if (tar --help 2>&1 | grep bsdtar >/dev/null 2>&1 ) then \ + * echo bsdtar; else echo not bsdtar; fi + */ +static void +long_help(struct bsdtar *bsdtar) +{ + const char *prog; + const char *p; + + prog = bsdtar->progname; + + fflush(stderr); + + p = (strcmp(prog,"bsdtar") != 0) ? "(bsdtar)" : ""; + printf("%s%s: manipulate archive files\n", prog, p); + + for (p = long_help_msg; *p != '\0'; p++) { + if (*p == '%') { + if (p[1] == 'p') { + fputs(prog, stdout); + p++; + } else + putchar('%'); + } else + putchar(*p); + } + fprintf(stdout, "\n%s %s\n", PACKAGE_NAME, PACKAGE_VERSION); + fprintf(stdout, "%s\n", archive_version()); +} + +static int +bsdtar_getopt(struct bsdtar *bsdtar, const char *optstring, + const struct option **poption) +{ + char *p, *q; + const struct option *option; + int opt; + int option_index; + size_t option_length; + + option_index = -1; + *poption = NULL; + +#ifdef HAVE_GETOPT_LONG + opt = getopt_long(bsdtar->argc, bsdtar->argv, optstring, + tar_longopts, &option_index); + if (option_index > -1) + *poption = tar_longopts + option_index; +#else + opt = getopt(bsdtar->argc, bsdtar->argv, optstring); +#endif + + /* Support long options through -W longopt=value */ + if (opt == 'W') { + p = optarg; + q = strchr(optarg, '='); + if (q != NULL) { + option_length = (size_t)(q - p); + optarg = q + 1; + } else { + option_length = strlen(p); + optarg = NULL; + } + option = tar_longopts; + while (option->name != NULL && + (strlen(option->name) < option_length || + strncmp(p, option->name, option_length) != 0 )) { + option++; + } + + if (option->name != NULL) { + *poption = option; + opt = option->val; + + /* Check if there's another match. */ + option++; + while (option->name != NULL && + (strlen(option->name) < option_length || + strncmp(p, option->name, option_length) != 0)) { + option++; + } + if (option->name != NULL) + bsdtar_errc(bsdtar, 1, 0, + "Ambiguous option %s " + "(matches both %s and %s)", + p, (*poption)->name, option->name); + + } else { + opt = '?'; + /* TODO: Set up a fake 'struct option' for + * error reporting... ? ? ? */ + } + } + + return (opt); +} diff --git a/contrib/bsdtar/bsdtar.h b/contrib/bsdtar/bsdtar.h new file mode 100644 index 0000000000..2b721cfa73 --- /dev/null +++ b/contrib/bsdtar/bsdtar.h @@ -0,0 +1,121 @@ +/*- + * Copyright (c) 2003-2004 Tim Kientzle + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name(s) of the author(s) may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD: src/usr.bin/tar/bsdtar.h,v 1.20 2004/08/08 05:50:10 kientzle Exp $ + */ + +#include +#include + +#define DEFAULT_BYTES_PER_BLOCK (20*512) + +/* + * The internal state for the "bsdtar" program. + * + * Keeping all of the state in a structure like this simplifies memory + * leak testing (at exit, anything left on the heap is suspect). A + * pointer to this structure is passed to most bsdtar internal + * functions. + */ +struct bsdtar { + /* Options */ + const char *filename; /* -f filename */ + const char *create_format; /* -F format */ + char *pending_chdir; /* -C dir */ + const char *names_from_file; /* -T file */ + int bytes_per_block; /* -b block_size */ + int verbose; /* -v */ + int extract_flags; /* Flags for extract operation */ + char mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */ + char symlink_mode; /* H or L, per BSD conventions */ + char create_compression; /* j, y, or z */ + char option_absolute_paths; /* -P */ + char option_dont_traverse_mounts; /* -X */ + char option_fast_read; /* --fast-read */ + char option_honor_nodump; /* --nodump */ + char option_interactive; /* -w */ + char option_no_owner; /* -o */ + char option_no_subdirs; /* -d */ + char option_null; /* --null */ + char option_stdout; /* -p */ + char option_totals; /* --totals */ + char option_unlink_first; /* -U */ + char option_warn_links; /* -l */ + char day_first; /* show day before month in -tv output */ + + /* If >= 0, then close this when done. */ + int fd; + + /* Miscellaneous state information */ + struct archive *archive; + const char *progname; + int argc; + char **argv; + size_t gs_width; /* For 'list_item' in read.c */ + size_t u_width; /* for 'list_item' in read.c */ + uid_t user_uid; /* UID running this program */ + int return_value; /* Value returned by main() */ + char warned_lead_slash; /* Already displayed warning */ + char next_line_is_dir; /* Used for -C parsing in -cT */ + + /* + * Data for various subsystems. Full definitions are located in + * the file where they are used. + */ + struct archive_dir *archive_dir; /* for write.c */ + struct name_cache *gname_cache; /* for write.c */ + struct links_cache *links_cache; /* for write.c */ + struct matching *matching; /* for matching.c */ + struct security *security; /* for read.c */ + struct name_cache *uname_cache; /* for write.c */ +}; + +void bsdtar_errc(struct bsdtar *, int _eval, int _code, + const char *fmt, ...); +void bsdtar_strmode(struct archive_entry *entry, char *bp); +void bsdtar_warnc(struct bsdtar *, int _code, const char *fmt, ...); +void cleanup_exclusions(struct bsdtar *); +void do_chdir(struct bsdtar *); +int exclude(struct bsdtar *, const char *pattern); +int exclude_from_file(struct bsdtar *, const char *pathname); +int excluded(struct bsdtar *, const char *pathname); +int include(struct bsdtar *, const char *pattern); +int include_from_file(struct bsdtar *, const char *pathname); +int process_lines(struct bsdtar *bsdtar, const char *pathname, + int (*process)(struct bsdtar *, const char *)); +void safe_fprintf(FILE *, const char *fmt, ...); +void set_chdir(struct bsdtar *, const char *newdir); +void tar_mode_c(struct bsdtar *bsdtar); +void tar_mode_r(struct bsdtar *bsdtar); +void tar_mode_t(struct bsdtar *bsdtar); +void tar_mode_u(struct bsdtar *bsdtar); +void tar_mode_x(struct bsdtar *bsdtar); +int unmatched_inclusions(struct bsdtar *bsdtar); +void usage(struct bsdtar *); +int yes(const char *fmt, ...); + diff --git a/contrib/bsdtar/bsdtar_platform.h b/contrib/bsdtar/bsdtar_platform.h new file mode 100644 index 0000000000..e9336d9659 --- /dev/null +++ b/contrib/bsdtar/bsdtar_platform.h @@ -0,0 +1,146 @@ +/*- + * Copyright (c) 2003-2004 Tim Kientzle + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD: src/usr.bin/tar/bsdtar_platform.h,v 1.11 2004/11/06 18:38:13 kientzle Exp $ + */ + +/* + * This header is the first thing included in any of the bsdtar + * source files. As far as possible, platform-specific issues should + * be dealt with here and not within individual source files. + */ + +#ifndef BSDTAR_PLATFORM_H_INCLUDED +#define BSDTAR_PLATFORM_H_INCLUDED + +#if HAVE_CONFIG_H +#include "config.h" +#else + +#ifdef __FreeBSD__ +/* A default configuration for FreeBSD, used if there is no config.h. */ +#define PACKAGE_NAME "bsdtar" + +#define HAVE_BZLIB_H 1 +#define HAVE_CHFLAGS 1 +#define HAVE_DIRENT_H 1 +#define HAVE_D_MD_ORDER 1 +#define HAVE_FCHDIR 1 +#define HAVE_FCNTL_H 1 +#define HAVE_FNMATCH 1 +#define HAVE_FTRUNCATE 1 +#define HAVE_GETOPT_LONG 1 +#define HAVE_INTTYPES_H 1 +#define HAVE_LANGINFO_H 1 +#define HAVE_LIBARCHIVE 1 +#define HAVE_LIBBZ2 1 +#define HAVE_LIBZ 1 +#define HAVE_LIMITS_H 1 +#define HAVE_LOCALE_H 1 +#define HAVE_MALLOC 1 +#define HAVE_MEMMOVE 1 +#define HAVE_MEMORY_H 1 +#define HAVE_MEMSET 1 +#if __FreeBSD_version >= 450002 /* nl_langinfo introduced */ +#define HAVE_NL_LANGINFO 1 +#endif +#define HAVE_PATHS_H 1 +#define HAVE_SETLOCALE 1 +#define HAVE_STDINT_H 1 +#define HAVE_STDLIB_H 1 +#define HAVE_STRCHR 1 +#define HAVE_STRDUP 1 +#define HAVE_STRERROR 1 +#define HAVE_STRFTIME 1 +#define HAVE_STRINGS_H 1 +#define HAVE_STRING_H 1 +#define HAVE_STRRCHR 1 +#define HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC 1 +#define HAVE_STRUCT_STAT_ST_RDEV 1 +#if __FreeBSD__ > 4 +#define HAVE_SYS_ACL_H 1 +#endif +#define HAVE_SYS_IOCTL_H 1 +#define HAVE_SYS_PARAM_H 1 +#define HAVE_SYS_STAT_H 1 +#define HAVE_SYS_TYPES_H 1 +#define HAVE_UINTMAX_T 1 +#define HAVE_UNISTD_H 1 +#define HAVE_VPRINTF 1 +#define HAVE_ZLIB_H 1 +#define STDC_HEADERS 1 + +#else /* !__FreeBSD__ */ +/* Warn if the library hasn't been (automatically or manually) configured. */ +#error Oops: No config.h and no built-in configuration in archive_platform.h. +#endif /* !__FreeBSD__ */ + +#endif /* !HAVE_CONFIG_H */ + +/* No non-FreeBSD platform will have __FBSDID, so just define it here. */ +#ifdef __FreeBSD__ +#include /* For __FBSDID */ +#else +#define __FBSDID(a) /* null */ +#endif + +#ifndef HAVE_LIBARCHIVE +#error Configuration error: did not find libarchive. +#endif + +/* TODO: Test for the functions we use as well... */ +#if HAVE_SYS_ACL_H +#define HAVE_POSIX_ACLS 1 +#endif + +/* + * We need to be able to display a filesize using printf(). The type + * and format string here must be compatible with one another and + * large enough for any file. + */ +#if HAVE_UINTMAX_T +#define BSDTAR_FILESIZE_TYPE uintmax_t +#define BSDTAR_FILESIZE_PRINTF "%ju" +#else +#if HAVE_UNSIGNED_LONG_LONG +#define BSDTAR_FILESIZE_TYPE unsigned long long +#define BSDTAR_FILESIZE_PRINTF "%llu" +#else +#define BSDTAR_FILESIZE_TYPE unsigned long +#define BSDTAR_FILESIZE_PRINTF "%lu" +#endif +#endif + +#if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC +#define ARCHIVE_STAT_MTIME_NANOS(st) (st)->st_mtimespec.tv_nsec +#else +#if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC +#define ARCHIVE_STAT_MTIME_NANOS(st) (st)->st_mtim.tv_nsec +#else +#define ARCHIVE_STAT_MTIME_NANOS(st) (0) +#endif +#endif + +#endif /* !BSDTAR_PLATFORM_H_INCLUDED */ diff --git a/contrib/bsdtar/fts.c b/contrib/bsdtar/fts.c new file mode 100644 index 0000000000..bcb05a4ebb --- /dev/null +++ b/contrib/bsdtar/fts.c @@ -0,0 +1,1195 @@ +/*- + * This file is not used on BSD systems, because the libc version + * works. On Linux, the fts in libc is compiled for a 32-bit + * off_t, which doesn't match the 64-bit off_t used by the rest + * of bsdtar. + * + * bsdtar_platform.h defines _FILE_OFFSET_BITS=64 so this will + * be compiled for 64-bit off_t. + * + * This file is mostly unchanged from: + * FreeBSD: src/lib/libc/gen/fts.c,v 1.22 2003/01/03 23:25:25 tjr Exp + * except for a few minor Linux-isms which are commented with "bsdtar:" + * below. + */ + +/*- + * Copyright (c) 1990, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * From: $OpenBSD: fts.c,v 1.22 1999/10/03 19:22:22 millert Exp $ + * From: $ + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)fts.c 8.6 (Berkeley) 8/14/94"; +#endif /* LIBC_SCCS and not lint */ +#include "bsdtar_platform.h" /* bsdtar: need platform-specific definitions. */ +__FBSDID("$FreeBSD: src/usr.bin/tar/fts.c,v 1.2 2004/07/24 22:13:44 kientzle Exp $"); + +#ifdef linux /* bsdtar: translate certain system calls to Linux names. */ +#define _open open +#define _close close +#define _fstat fstat +#endif + +/* #include "namespace.h" */ /* bsdtar: Don't use namespace.h. */ +#include +#include +#include + +#include +#include +#include +#include +#undef FTS_WHITEOUT /* bsdtar: Not all platforms support this. */ +#include +#include +#include +/* #include "un-namespace.h" */ /* bsdtar: Don't use namespace.h. */ + +static FTSENT *fts_alloc(FTS *, char *, int); +static FTSENT *fts_build(FTS *, int); +static void fts_lfree(FTSENT *); +static void fts_load(FTS *, FTSENT *); +static size_t fts_maxarglen(char * const *); +static void fts_padjust(FTS *, FTSENT *); +static int fts_palloc(FTS *, size_t); +static FTSENT *fts_sort(FTS *, FTSENT *, int); +static u_short fts_stat(FTS *, FTSENT *, int); +static int fts_safe_changedir(FTS *, FTSENT *, int, char *); + +#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) + +#define CLR(opt) (sp->fts_options &= ~(opt)) +#define ISSET(opt) (sp->fts_options & (opt)) +#define SET(opt) (sp->fts_options |= (opt)) + +#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && fchdir(fd)) + +/* fts_build flags */ +#define BCHILD 1 /* fts_children */ +#define BNAMES 2 /* fts_children, names only */ +#define BREAD 3 /* fts_read */ + +#ifndef HAVE_REALLOCF /* bsdtar: Define reallocf for non-BSD platforms. */ +static void * +reallocf(ptr, size) + void *ptr; + size_t size; +{ + void *ret = realloc(ptr, size); + if (!ret) + free(ptr); + return ret; +} +#endif + +FTS * +fts_open(argv, options, compar) + char * const *argv; + int options; + int (*compar)(const FTSENT * const *, const FTSENT * const *); +{ + FTS *sp; + FTSENT *p, *root; + int nitems; + FTSENT *parent, *tmp; + int len; + + /* Options check. */ + if (options & ~FTS_OPTIONMASK) { + errno = EINVAL; + return (NULL); + } + + /* Allocate/initialize the stream */ + if ((sp = malloc(sizeof(FTS))) == NULL) + return (NULL); + memset(sp, 0, sizeof(FTS)); + sp->fts_compar = compar; + sp->fts_options = options; + + /* Shush, GCC. */ + tmp = NULL; + + /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ + if (ISSET(FTS_LOGICAL)) + SET(FTS_NOCHDIR); + + /* + * Start out with 1K of path space, and enough, in any case, + * to hold the user's paths. + */ + if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) + goto mem1; + + /* Allocate/initialize root's parent. */ + if ((parent = fts_alloc(sp, "", 0)) == NULL) + goto mem2; + parent->fts_level = FTS_ROOTPARENTLEVEL; + + /* Allocate/initialize root(s). */ + for (root = NULL, nitems = 0; *argv != NULL; ++argv, ++nitems) { + /* Don't allow zero-length paths. */ + if ((len = strlen(*argv)) == 0) { + errno = ENOENT; + goto mem3; + } + + p = fts_alloc(sp, *argv, len); + p->fts_level = FTS_ROOTLEVEL; + p->fts_parent = parent; + p->fts_accpath = p->fts_name; + p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); + + /* Command-line "." and ".." are real directories. */ + if (p->fts_info == FTS_DOT) + p->fts_info = FTS_D; + + /* + * If comparison routine supplied, traverse in sorted + * order; otherwise traverse in the order specified. + */ + if (compar) { + p->fts_link = root; + root = p; + } else { + p->fts_link = NULL; + if (root == NULL) + tmp = root = p; + else { + tmp->fts_link = p; + tmp = p; + } + } + } + if (compar && nitems > 1) + root = fts_sort(sp, root, nitems); + + /* + * Allocate a dummy pointer and make fts_read think that we've just + * finished the node before the root(s); set p->fts_info to FTS_INIT + * so that everything about the "current" node is ignored. + */ + if ((sp->fts_cur = fts_alloc(sp, "", 0)) == NULL) + goto mem3; + sp->fts_cur->fts_link = root; + sp->fts_cur->fts_info = FTS_INIT; + + /* + * If using chdir(2), grab a file descriptor pointing to dot to ensure + * that we can get back here; this could be avoided for some paths, + * but almost certainly not worth the effort. Slashes, symbolic links, + * and ".." are all fairly nasty problems. Note, if we can't get the + * descriptor we run anyway, just more slowly. + */ + if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = _open(".", O_RDONLY, 0)) < 0) + SET(FTS_NOCHDIR); + + return (sp); + +mem3: fts_lfree(root); + free(parent); +mem2: free(sp->fts_path); +mem1: free(sp); + return (NULL); +} + +static void +fts_load(sp, p) + FTS *sp; + FTSENT *p; +{ + int len; + char *cp; + + /* + * Load the stream structure for the next traversal. Since we don't + * actually enter the directory until after the preorder visit, set + * the fts_accpath field specially so the chdir gets done to the right + * place and the user can access the first node. From fts_open it's + * known that the path will fit. + */ + len = p->fts_pathlen = p->fts_namelen; + memmove(sp->fts_path, p->fts_name, len + 1); + if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) { + len = strlen(++cp); + memmove(p->fts_name, cp, len + 1); + p->fts_namelen = len; + } + p->fts_accpath = p->fts_path = sp->fts_path; + sp->fts_dev = p->fts_dev; +} + +int +fts_close(sp) + FTS *sp; +{ + FTSENT *freep, *p; + int saved_errno; + + /* + * This still works if we haven't read anything -- the dummy structure + * points to the root list, so we step through to the end of the root + * list which has a valid parent pointer. + */ + if (sp->fts_cur) { + for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { + freep = p; + p = p->fts_link != NULL ? p->fts_link : p->fts_parent; + free(freep); + } + free(p); + } + + /* Free up child linked list, sort array, path buffer. */ + if (sp->fts_child) + fts_lfree(sp->fts_child); + if (sp->fts_array) + free(sp->fts_array); + free(sp->fts_path); + + /* Return to original directory, save errno if necessary. */ + if (!ISSET(FTS_NOCHDIR)) { + saved_errno = fchdir(sp->fts_rfd) ? errno : 0; + (void)_close(sp->fts_rfd); + + /* Set errno and return. */ + if (saved_errno != 0) { + /* Free up the stream pointer. */ + free(sp); + errno = saved_errno; + return (-1); + } + } + + /* Free up the stream pointer. */ + free(sp); + return (0); +} + +/* + * Special case of "/" at the end of the path so that slashes aren't + * appended which would cause paths to be written as "....//foo". + */ +#define NAPPEND(p) \ + (p->fts_path[p->fts_pathlen - 1] == '/' \ + ? p->fts_pathlen - 1 : p->fts_pathlen) + +FTSENT * +fts_read(sp) + FTS *sp; +{ + FTSENT *p, *tmp; + int instr; + char *t; + int saved_errno; + + /* If finished or unrecoverable error, return NULL. */ + if (sp->fts_cur == NULL || ISSET(FTS_STOP)) + return (NULL); + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* Save and zero out user instructions. */ + instr = p->fts_instr; + p->fts_instr = FTS_NOINSTR; + + /* Any type of file may be re-visited; re-stat and re-turn. */ + if (instr == FTS_AGAIN) { + p->fts_info = fts_stat(sp, p, 0); + return (p); + } + + /* + * Following a symlink -- SLNONE test allows application to see + * SLNONE and recover. If indirecting through a symlink, have + * keep a pointer to current location. If unable to get that + * pointer, follow fails. + */ + if (instr == FTS_FOLLOW && + (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = _open(".", O_RDONLY, 0)) < 0) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + return (p); + } + + /* Directory in pre-order. */ + if (p->fts_info == FTS_D) { + /* If skipped or crossed mount point, do post-order visit. */ + if (instr == FTS_SKIP || + (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { + if (p->fts_flags & FTS_SYMFOLLOW) + (void)_close(p->fts_symfd); + if (sp->fts_child) { + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + p->fts_info = FTS_DP; + return (p); + } + + /* Rebuild if only read the names and now traversing. */ + if (sp->fts_child != NULL && ISSET(FTS_NAMEONLY)) { + CLR(FTS_NAMEONLY); + fts_lfree(sp->fts_child); + sp->fts_child = NULL; + } + + /* + * Cd to the subdirectory. + * + * If have already read and now fail to chdir, whack the list + * to make the names come out right, and set the parent errno + * so the application will eventually get an error condition. + * Set the FTS_DONTCHDIR flag so that when we logically change + * directories back to the parent we don't do a chdir. + * + * If haven't read do so. If the read fails, fts_build sets + * FTS_STOP or the fts_info field of the node. + */ + if (sp->fts_child != NULL) { + if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { + p->fts_errno = errno; + p->fts_flags |= FTS_DONTCHDIR; + for (p = sp->fts_child; p != NULL; + p = p->fts_link) + p->fts_accpath = + p->fts_parent->fts_accpath; + } + } else if ((sp->fts_child = fts_build(sp, BREAD)) == NULL) { + if (ISSET(FTS_STOP)) + return (NULL); + return (p); + } + p = sp->fts_child; + sp->fts_child = NULL; + goto name; + } + + /* Move to the next node on this level. */ +next: tmp = p; + if ((p = p->fts_link) != NULL) { + free(tmp); + + /* + * If reached the top, return to the original directory (or + * the root of the tree), and load the paths for the next root. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + fts_load(sp, p); + return (sp->fts_cur = p); + } + + /* + * User may have called fts_set on the node. If skipped, + * ignore. If followed, get a file descriptor so we can + * get back if necessary. + */ + if (p->fts_instr == FTS_SKIP) + goto next; + if (p->fts_instr == FTS_FOLLOW) { + p->fts_info = fts_stat(sp, p, 1); + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if ((p->fts_symfd = + _open(".", O_RDONLY, 0)) < 0) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; + } else + p->fts_flags |= FTS_SYMFOLLOW; + } + p->fts_instr = FTS_NOINSTR; + } + +name: t = sp->fts_path + NAPPEND(p->fts_parent); + *t++ = '/'; + memmove(t, p->fts_name, p->fts_namelen + 1); + return (sp->fts_cur = p); + } + + /* Move up to the parent node. */ + p = tmp->fts_parent; + free(tmp); + + if (p->fts_level == FTS_ROOTPARENTLEVEL) { + /* + * Done; free everything up and set errno to 0 so the user + * can distinguish between error and EOF. + */ + free(p); + errno = 0; + return (sp->fts_cur = NULL); + } + + /* NUL terminate the pathname. */ + sp->fts_path[p->fts_pathlen] = '\0'; + + /* + * Return to the parent directory. If at a root node or came through + * a symlink, go back through the file descriptor. Otherwise, cd up + * one directory. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); + return (NULL); + } + } else if (p->fts_flags & FTS_SYMFOLLOW) { + if (FCHDIR(sp, p->fts_symfd)) { + saved_errno = errno; + (void)_close(p->fts_symfd); + errno = saved_errno; + SET(FTS_STOP); + return (NULL); + } + (void)_close(p->fts_symfd); + } else if (!(p->fts_flags & FTS_DONTCHDIR) && + fts_safe_changedir(sp, p->fts_parent, -1, "..")) { + SET(FTS_STOP); + return (NULL); + } + p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; + return (sp->fts_cur = p); +} + +/* + * Fts_set takes the stream as an argument although it's not used in this + * implementation; it would be necessary if anyone wanted to add global + * semantics to fts using fts_set. An error return is allowed for similar + * reasons. + */ +/* ARGSUSED */ +int +fts_set(sp, p, instr) + FTS *sp; + FTSENT *p; + int instr; +{ + if (instr != 0 && instr != FTS_AGAIN && instr != FTS_FOLLOW && + instr != FTS_NOINSTR && instr != FTS_SKIP) { + errno = EINVAL; + return (1); + } + p->fts_instr = instr; + return (0); +} + +FTSENT * +fts_children(sp, instr) + FTS *sp; + int instr; +{ + FTSENT *p; + int fd; + + if (instr != 0 && instr != FTS_NAMEONLY) { + errno = EINVAL; + return (NULL); + } + + /* Set current node pointer. */ + p = sp->fts_cur; + + /* + * Errno set to 0 so user can distinguish empty directory from + * an error. + */ + errno = 0; + + /* Fatal errors stop here. */ + if (ISSET(FTS_STOP)) + return (NULL); + + /* Return logical hierarchy of user's arguments. */ + if (p->fts_info == FTS_INIT) + return (p->fts_link); + + /* + * If not a directory being visited in pre-order, stop here. Could + * allow FTS_DNR, assuming the user has fixed the problem, but the + * same effect is available with FTS_AGAIN. + */ + if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) + return (NULL); + + /* Free up any previous child list. */ + if (sp->fts_child != NULL) + fts_lfree(sp->fts_child); + + if (instr == FTS_NAMEONLY) { + SET(FTS_NAMEONLY); + instr = BNAMES; + } else + instr = BCHILD; + + /* + * If using chdir on a relative path and called BEFORE fts_read does + * its chdir to the root of a traversal, we can lose -- we need to + * chdir into the subdirectory, and we don't know where the current + * directory is, so we can't get back so that the upcoming chdir by + * fts_read will work. + */ + if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' || + ISSET(FTS_NOCHDIR)) + return (sp->fts_child = fts_build(sp, instr)); + + if ((fd = _open(".", O_RDONLY, 0)) < 0) + return (NULL); + sp->fts_child = fts_build(sp, instr); + if (fchdir(fd)) + return (NULL); + (void)_close(fd); + return (sp->fts_child); +} + +#ifndef fts_get_clientptr +#error "fts_get_clientptr not defined" +#endif + +void * +(fts_get_clientptr)(FTS *sp) +{ + + return (fts_get_clientptr(sp)); +} + +#ifndef fts_get_stream +#error "fts_get_stream not defined" +#endif + +FTS * +(fts_get_stream)(FTSENT *p) +{ + return (fts_get_stream(p)); +} + +void +fts_set_clientptr(FTS *sp, void *clientptr) +{ + + sp->fts_clientptr = clientptr; +} + +/* + * This is the tricky part -- do not casually change *anything* in here. The + * idea is to build the linked list of entries that are used by fts_children + * and fts_read. There are lots of special cases. + * + * The real slowdown in walking the tree is the stat calls. If FTS_NOSTAT is + * set and it's a physical walk (so that symbolic links can't be directories), + * we can do things quickly. First, if it's a 4.4BSD file system, the type + * of the file is in the directory entry. Otherwise, we assume that the number + * of subdirectories in a node is equal to the number of links to the parent. + * The former skips all stat calls. The latter skips stat calls in any leaf + * directories and for any files after the subdirectories in the directory have + * been found, cutting the stat calls by about 2/3. + */ +static FTSENT * +fts_build(sp, type) + FTS *sp; + int type; +{ + struct dirent *dp; + FTSENT *p, *head; + int nitems; + FTSENT *cur, *tail; + DIR *dirp; + void *oldaddr; + int cderrno, descend, len, level, maxlen, nlinks, oflag, saved_errno, + nostat, doadjust; + char *cp; + + /* Set current node pointer. */ + cur = sp->fts_cur; + + /* + * Open the directory for reading. If this fails, we're done. + * If being called from fts_read, set the fts_info field. + */ +#ifdef FTS_WHITEOUT + if (ISSET(FTS_WHITEOUT)) + oflag = DTF_NODUP|DTF_REWIND; + else + oflag = DTF_HIDEW|DTF_NODUP|DTF_REWIND; +#else +#define __opendir2(path, flag) opendir(path) +#endif + if ((dirp = __opendir2(cur->fts_accpath, oflag)) == NULL) { + if (type == BREAD) { + cur->fts_info = FTS_DNR; + cur->fts_errno = errno; + } + return (NULL); + } + + /* + * Nlinks is the number of possible entries of type directory in the + * directory if we're cheating on stat calls, 0 if we're not doing + * any stat calls at all, -1 if we're doing stats on everything. + */ + if (type == BNAMES) { + nlinks = 0; + /* Be quiet about nostat, GCC. */ + nostat = 0; + } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { + nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); + nostat = 1; + } else { + nlinks = -1; + nostat = 0; + } + +#ifdef notdef + (void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink); + (void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n", + ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT)); +#endif + /* + * If we're going to need to stat anything or we want to descend + * and stay in the directory, chdir. If this fails we keep going, + * but set a flag so we don't chdir after the post-order visit. + * We won't be able to stat anything, but we can still return the + * names themselves. Note, that since fts_read won't be able to + * chdir into the directory, it will have to return different path + * names than before, i.e. "a/b" instead of "b". Since the node + * has already been visited in pre-order, have to wait until the + * post-order visit to return the error. There is a special case + * here, if there was nothing to stat then it's not an error to + * not be able to stat. This is all fairly nasty. If a program + * needed sorted entries or stat information, they had better be + * checking FTS_NS on the returned nodes. + */ + cderrno = 0; + if (nlinks || type == BREAD) { + if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) { + if (nlinks && type == BREAD) + cur->fts_errno = errno; + cur->fts_flags |= FTS_DONTCHDIR; + descend = 0; + cderrno = errno; + (void)closedir(dirp); + dirp = NULL; + } else + descend = 1; + } else + descend = 0; + + /* + * Figure out the max file name length that can be stored in the + * current path -- the inner loop allocates more path as necessary. + * We really wouldn't have to do the maxlen calculations here, we + * could do them in fts_read before returning the path, but it's a + * lot easier here since the length is part of the dirent structure. + * + * If not changing directories set a pointer so that can just append + * each new name into the path. + */ + len = NAPPEND(cur); + if (ISSET(FTS_NOCHDIR)) { + cp = sp->fts_path + len; + *cp++ = '/'; + } else { + /* GCC, you're too verbose. */ + cp = NULL; + } + len++; + maxlen = sp->fts_pathlen - len; + + level = cur->fts_level + 1; + + /* Read the directory, attaching each entry to the `link' pointer. */ + doadjust = 0; + for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) { +#ifdef HAVE_STRUCT_DIRENT_D_NAMLEN /* bsdtar: Not everyone has d_namlen. */ +#define dnamlen dp->d_namlen +#else + int dnamlen = strlen(dp->d_name); +#endif + if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) + continue; + + if ((p = fts_alloc(sp, dp->d_name, dnamlen)) == NULL) + goto mem1; + if (dnamlen >= maxlen) { /* include space for NUL */ + oldaddr = sp->fts_path; + if (fts_palloc(sp, dnamlen + len + 1)) { + /* + * No more memory for path or structures. Save + * errno, free up the current structure and the + * structures already allocated. + */ +mem1: saved_errno = errno; + if (p) + free(p); + fts_lfree(head); + (void)closedir(dirp); + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = saved_errno; + return (NULL); + } + /* Did realloc() change the pointer? */ + if (oldaddr != sp->fts_path) { + doadjust = 1; + if (ISSET(FTS_NOCHDIR)) + cp = sp->fts_path + len; + } + maxlen = sp->fts_pathlen - len; + } + + if (len + dnamlen >= USHRT_MAX) { + /* + * In an FTSENT, fts_pathlen is a u_short so it is + * possible to wraparound here. If we do, free up + * the current structure and the structures already + * allocated, then error out with ENAMETOOLONG. + */ + free(p); + fts_lfree(head); + (void)closedir(dirp); + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = ENAMETOOLONG; + return (NULL); + } + p->fts_level = level; + p->fts_parent = sp->fts_cur; + p->fts_pathlen = len + dnamlen; + +#ifdef FTS_WHITEOUT + if (dp->d_type == DT_WHT) + p->fts_flags |= FTS_ISW; +#endif + + if (cderrno) { + if (nlinks) { + p->fts_info = FTS_NS; + p->fts_errno = cderrno; + } else + p->fts_info = FTS_NSOK; + p->fts_accpath = cur->fts_accpath; + } else if (nlinks == 0 +#ifdef DT_DIR + || (nostat && + dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) +#endif + ) { + p->fts_accpath = + ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; + p->fts_info = FTS_NSOK; + } else { + /* Build a file name for fts_stat to stat. */ + if (ISSET(FTS_NOCHDIR)) { + p->fts_accpath = p->fts_path; + memmove(cp, p->fts_name, p->fts_namelen + 1); + } else + p->fts_accpath = p->fts_name; + /* Stat it. */ + p->fts_info = fts_stat(sp, p, 0); + + /* Decrement link count if applicable. */ + if (nlinks > 0 && (p->fts_info == FTS_D || + p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) + --nlinks; + } + + /* We walk in directory order so "ls -f" doesn't get upset. */ + p->fts_link = NULL; + if (head == NULL) + head = tail = p; + else { + tail->fts_link = p; + tail = p; + } + ++nitems; + } + if (dirp) + (void)closedir(dirp); + + /* + * If realloc() changed the address of the path, adjust the + * addresses for the rest of the tree and the dir list. + */ + if (doadjust) + fts_padjust(sp, head); + + /* + * If not changing directories, reset the path back to original + * state. + */ + if (ISSET(FTS_NOCHDIR)) { + if (len == sp->fts_pathlen || nitems == 0) + --cp; + *cp = '\0'; + } + + /* + * If descended after called from fts_children or after called from + * fts_read and nothing found, get back. At the root level we use + * the saved fd; if one of fts_open()'s arguments is a relative path + * to an empty directory, we wind up here with no other way back. If + * can't get back, we're done. + */ + if (descend && (type == BCHILD || !nitems) && + (cur->fts_level == FTS_ROOTLEVEL ? + FCHDIR(sp, sp->fts_rfd) : + fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + return (NULL); + } + + /* If didn't find anything, return NULL. */ + if (!nitems) { + if (type == BREAD) + cur->fts_info = FTS_DP; + return (NULL); + } + + /* Sort the entries. */ + if (sp->fts_compar && nitems > 1) + head = fts_sort(sp, head, nitems); + return (head); +} + +static u_short +fts_stat(sp, p, follow) + FTS *sp; + FTSENT *p; + int follow; +{ + FTSENT *t; + dev_t dev; + ino_t ino; + struct stat *sbp, sb; + int saved_errno; + + /* If user needs stat info, stat buffer already allocated. */ + sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; + +#ifdef FTS_WHITEOUT + /* check for whiteout */ + if (p->fts_flags & FTS_ISW) { + if (sbp != &sb) { + memset(sbp, '\0', sizeof (*sbp)); + sbp->st_mode = S_IFWHT; + } + return (FTS_W); + } +#endif + + /* + * If doing a logical walk, or application requested FTS_FOLLOW, do + * a stat(2). If that fails, check for a non-existent symlink. If + * fail, set the errno from the stat call. + */ + if (ISSET(FTS_LOGICAL) || follow) { + if (stat(p->fts_accpath, sbp)) { + saved_errno = errno; + if (!lstat(p->fts_accpath, sbp)) { + errno = 0; + return (FTS_SLNONE); + } + p->fts_errno = saved_errno; + goto err; + } + } else if (lstat(p->fts_accpath, sbp)) { + p->fts_errno = errno; +err: memset(sbp, 0, sizeof(struct stat)); + return (FTS_NS); + } + + if (S_ISDIR(sbp->st_mode)) { + /* + * Set the device/inode. Used to find cycles and check for + * crossing mount points. Also remember the link count, used + * in fts_build to limit the number of stat calls. It is + * understood that these fields are only referenced if fts_info + * is set to FTS_D. + */ + dev = p->fts_dev = sbp->st_dev; + ino = p->fts_ino = sbp->st_ino; + p->fts_nlink = sbp->st_nlink; + + if (ISDOT(p->fts_name)) + return (FTS_DOT); + + /* + * Cycle detection is done by brute force when the directory + * is first encountered. If the tree gets deep enough or the + * number of symbolic links to directories is high enough, + * something faster might be worthwhile. + */ + for (t = p->fts_parent; + t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) + if (ino == t->fts_ino && dev == t->fts_dev) { + p->fts_cycle = t; + return (FTS_DC); + } + return (FTS_D); + } + if (S_ISLNK(sbp->st_mode)) + return (FTS_SL); + if (S_ISREG(sbp->st_mode)) + return (FTS_F); + return (FTS_DEFAULT); +} + +/* + * The comparison function takes pointers to pointers to FTSENT structures. + * Qsort wants a comparison function that takes pointers to void. + * (Both with appropriate levels of const-poisoning, of course!) + * Use a trampoline function to deal with the difference. + */ +static int +fts_compar(const void *a, const void *b) +{ + FTS *parent; + + parent = (*(const FTSENT * const *)a)->fts_fts; + return (*parent->fts_compar)(a, b); +} + +static FTSENT * +fts_sort(sp, head, nitems) + FTS *sp; + FTSENT *head; + int nitems; +{ + FTSENT **ap, *p; + + /* + * Construct an array of pointers to the structures and call qsort(3). + * Reassemble the array in the order returned by qsort. If unable to + * sort for memory reasons, return the directory entries in their + * current order. Allocate enough space for the current needs plus + * 40 so don't realloc one entry at a time. + */ + if (nitems > sp->fts_nitems) { + sp->fts_nitems = nitems + 40; + if ((sp->fts_array = reallocf(sp->fts_array, + sp->fts_nitems * sizeof(FTSENT *))) == NULL) { + sp->fts_nitems = 0; + return (head); + } + } + for (ap = sp->fts_array, p = head; p; p = p->fts_link) + *ap++ = p; + qsort(sp->fts_array, nitems, sizeof(FTSENT *), fts_compar); + for (head = *(ap = sp->fts_array); --nitems; ++ap) + ap[0]->fts_link = ap[1]; + ap[0]->fts_link = NULL; + return (head); +} + +static FTSENT * +fts_alloc(sp, name, namelen) + FTS *sp; + char *name; + int namelen; +{ + FTSENT *p; + size_t len; + + struct ftsent_withstat { + FTSENT ent; + struct stat statbuf; + }; + + /* + * The file name is a variable length array and no stat structure is + * necessary if the user has set the nostat bit. Allocate the FTSENT + * structure, the file name and the stat structure in one chunk, but + * be careful that the stat structure is reasonably aligned. + */ + if (ISSET(FTS_NOSTAT)) + len = sizeof(FTSENT) + namelen + 1; + else + len = sizeof(struct ftsent_withstat) + namelen + 1; + + if ((p = malloc(len)) == NULL) + return (NULL); + + if (ISSET(FTS_NOSTAT)) { + p->fts_name = (char *)(p + 1); + p->fts_statp = NULL; + } else { + p->fts_name = (char *)((struct ftsent_withstat *)p + 1); + p->fts_statp = &((struct ftsent_withstat *)p)->statbuf; + } + + /* Copy the name and guarantee NUL termination. */ + memcpy(p->fts_name, name, namelen); + p->fts_name[namelen] = '\0'; + p->fts_namelen = namelen; + p->fts_path = sp->fts_path; + p->fts_errno = 0; + p->fts_flags = 0; + p->fts_instr = FTS_NOINSTR; + p->fts_number = 0; + p->fts_pointer = NULL; + p->fts_fts = sp; + return (p); +} + +static void +fts_lfree(head) + FTSENT *head; +{ + FTSENT *p; + + /* Free a linked list of structures. */ + while ((p = head)) { + head = head->fts_link; + free(p); + } +} + +/* + * Allow essentially unlimited paths; find, rm, ls should all work on any tree. + * Most systems will allow creation of paths much longer than MAXPATHLEN, even + * though the kernel won't resolve them. Add the size (not just what's needed) + * plus 256 bytes so don't realloc the path 2 bytes at a time. + */ +static int +fts_palloc(sp, more) + FTS *sp; + size_t more; +{ + + sp->fts_pathlen += more + 256; + /* + * Check for possible wraparound. In an FTS, fts_pathlen is + * a signed int but in an FTSENT it is an unsigned short. + * We limit fts_pathlen to USHRT_MAX to be safe in both cases. + */ + if (sp->fts_pathlen < 0 || sp->fts_pathlen >= USHRT_MAX) { + if (sp->fts_path) + free(sp->fts_path); + sp->fts_path = NULL; + errno = ENAMETOOLONG; + return (1); + } + sp->fts_path = reallocf(sp->fts_path, sp->fts_pathlen); + return (sp->fts_path == NULL); +} + +/* + * When the path is realloc'd, have to fix all of the pointers in structures + * already returned. + */ +static void +fts_padjust(sp, head) + FTS *sp; + FTSENT *head; +{ + FTSENT *p; + char *addr = sp->fts_path; + +#define ADJUST(p) do { \ + if ((p)->fts_accpath != (p)->fts_name) { \ + (p)->fts_accpath = \ + (char *)addr + ((p)->fts_accpath - (p)->fts_path); \ + } \ + (p)->fts_path = addr; \ +} while (0) + /* Adjust the current set of children. */ + for (p = sp->fts_child; p; p = p->fts_link) + ADJUST(p); + + /* Adjust the rest of the tree, including the current level. */ + for (p = head; p->fts_level >= FTS_ROOTLEVEL;) { + ADJUST(p); + p = p->fts_link ? p->fts_link : p->fts_parent; + } +} + +static size_t +fts_maxarglen(argv) + char * const *argv; +{ + size_t len, max; + + for (max = 0; *argv; ++argv) + if ((len = strlen(*argv)) > max) + max = len; + return (max + 1); +} + +/* + * Change to dir specified by fd or p->fts_accpath without getting + * tricked by someone changing the world out from underneath us. + * Assumes p->fts_dev and p->fts_ino are filled in. + */ +static int +fts_safe_changedir(sp, p, fd, path) + FTS *sp; + FTSENT *p; + int fd; + char *path; +{ + int ret, oerrno, newfd; + struct stat sb; + + newfd = fd; + if (ISSET(FTS_NOCHDIR)) + return (0); + if (fd < 0 && (newfd = _open(path, O_RDONLY, 0)) < 0) + return (-1); + if (_fstat(newfd, &sb)) { + ret = -1; + goto bail; + } + if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) { + errno = ENOENT; /* disinformation */ + ret = -1; + goto bail; + } + ret = fchdir(newfd); +bail: + oerrno = errno; + if (fd < 0) + (void)_close(newfd); + errno = oerrno; + return (ret); +} diff --git a/contrib/bsdtar/fts.h b/contrib/bsdtar/fts.h new file mode 100644 index 0000000000..cbe54a81ca --- /dev/null +++ b/contrib/bsdtar/fts.h @@ -0,0 +1,147 @@ +/*- + * This file is not used on BSD systems, because the libc version + * works. On Linux, the fts in libc is compiled for a 32-bit + * off_t, which doesn't match the 64-bit off_t used by the rest + * of bsdtar. + * + * The remainder of this file is an exact copy of: + * FreeBSD: src/include/fts.h,v 1.7 2002/09/21 01:28:36 wollman Exp + */ + +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)fts.h 8.3 (Berkeley) 8/14/94 + * $FreeBSD: src/usr.bin/tar/fts.h,v 1.1 2004/05/04 17:21:01 kientzle Exp $ + */ + +#ifndef _FTS_H_ +#define _FTS_H_ + +typedef struct { + struct _ftsent *fts_cur; /* current node */ + struct _ftsent *fts_child; /* linked list of children */ + struct _ftsent **fts_array; /* sort array */ + dev_t fts_dev; /* starting device # */ + char *fts_path; /* path for this descent */ + int fts_rfd; /* fd for root */ + int fts_pathlen; /* sizeof(path) */ + int fts_nitems; /* elements in the sort array */ + int (*fts_compar) /* compare function */ + (const struct _ftsent * const *, const struct _ftsent * const *); + +#define FTS_COMFOLLOW 0x001 /* follow command line symlinks */ +#define FTS_LOGICAL 0x002 /* logical walk */ +#define FTS_NOCHDIR 0x004 /* don't change directories */ +#define FTS_NOSTAT 0x008 /* don't get stat info */ +#define FTS_PHYSICAL 0x010 /* physical walk */ +#define FTS_SEEDOT 0x020 /* return dot and dot-dot */ +#define FTS_XDEV 0x040 /* don't cross devices */ +#define FTS_WHITEOUT 0x080 /* return whiteout information */ +#define FTS_OPTIONMASK 0x0ff /* valid user option mask */ + +#define FTS_NAMEONLY 0x100 /* (private) child names only */ +#define FTS_STOP 0x200 /* (private) unrecoverable error */ + int fts_options; /* fts_open options, global flags */ + void *fts_clientptr; /* thunk for sort function */ +} FTS; + +typedef struct _ftsent { + struct _ftsent *fts_cycle; /* cycle node */ + struct _ftsent *fts_parent; /* parent directory */ + struct _ftsent *fts_link; /* next file in directory */ + long fts_number; /* local numeric value */ + void *fts_pointer; /* local address value */ + char *fts_accpath; /* access path */ + char *fts_path; /* root path */ + int fts_errno; /* errno for this node */ + int fts_symfd; /* fd for symlink */ + u_short fts_pathlen; /* strlen(fts_path) */ + u_short fts_namelen; /* strlen(fts_name) */ + + ino_t fts_ino; /* inode */ + dev_t fts_dev; /* device */ + nlink_t fts_nlink; /* link count */ + +#define FTS_ROOTPARENTLEVEL -1 +#define FTS_ROOTLEVEL 0 + short fts_level; /* depth (-1 to N) */ + +#define FTS_D 1 /* preorder directory */ +#define FTS_DC 2 /* directory that causes cycles */ +#define FTS_DEFAULT 3 /* none of the above */ +#define FTS_DNR 4 /* unreadable directory */ +#define FTS_DOT 5 /* dot or dot-dot */ +#define FTS_DP 6 /* postorder directory */ +#define FTS_ERR 7 /* error; errno is set */ +#define FTS_F 8 /* regular file */ +#define FTS_INIT 9 /* initialized only */ +#define FTS_NS 10 /* stat(2) failed */ +#define FTS_NSOK 11 /* no stat(2) requested */ +#define FTS_SL 12 /* symbolic link */ +#define FTS_SLNONE 13 /* symbolic link without target */ +#define FTS_W 14 /* whiteout object */ + u_short fts_info; /* user flags for FTSENT structure */ + +#define FTS_DONTCHDIR 0x01 /* don't chdir .. to the parent */ +#define FTS_SYMFOLLOW 0x02 /* followed a symlink to get here */ +#define FTS_ISW 0x04 /* this is a whiteout object */ + u_short fts_flags; /* private flags for FTSENT structure */ + +#define FTS_AGAIN 1 /* read node again */ +#define FTS_FOLLOW 2 /* follow symbolic link */ +#define FTS_NOINSTR 3 /* no instructions */ +#define FTS_SKIP 4 /* discard node */ + u_short fts_instr; /* fts_set() instructions */ + + struct stat *fts_statp; /* stat(2) information */ + char *fts_name; /* file name */ + FTS *fts_fts; /* back pointer to main FTS */ +} FTSENT; + +#include + +__BEGIN_DECLS +FTSENT *fts_children(FTS *, int); +int fts_close(FTS *); +void *fts_get_clientptr(FTS *); +#define fts_get_clientptr(fts) ((fts)->fts_clientptr) +FTS *fts_get_stream(FTSENT *); +#define fts_get_stream(ftsent) ((ftsent)->fts_fts) +FTS *fts_open(char * const *, int, + int (*)(const FTSENT * const *, const FTSENT * const *)); +FTSENT *fts_read(FTS *); +int fts_set(FTS *, FTSENT *, int); +void fts_set_clientptr(FTS *, void *); +__END_DECLS + +#endif /* !_FTS_H_ */ diff --git a/contrib/bsdtar/matching.c b/contrib/bsdtar/matching.c new file mode 100644 index 0000000000..fb6573b970 --- /dev/null +++ b/contrib/bsdtar/matching.c @@ -0,0 +1,258 @@ +/*- + * Copyright (c) 2003-2004 Tim Kientzle + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "bsdtar_platform.h" +__FBSDID("$FreeBSD: src/usr.bin/tar/matching.c,v 1.7 2004/07/24 22:13:44 kientzle Exp $"); + +#include +#include +#include +#include + +#include "bsdtar.h" + +struct match { + struct match *next; + int matches; + char pattern[1]; +}; + +struct matching { + struct match *exclusions; + int exclusions_count; + struct match *inclusions; + int inclusions_count; + int inclusions_unmatched_count; +}; + + +static void add_pattern(struct bsdtar *, struct match **list, + const char *pattern); +static void initialize_matching(struct bsdtar *); +static int match_exclusion(struct match *, const char *pathname); +static int match_inclusion(struct match *, const char *pathname); + +/* + * The matching logic here needs to be re-thought. I started out to + * try to mimic gtar's matching logic, but it's not entirely + * consistent. In particular 'tar -t' and 'tar -x' interpret patterns + * on the command line as anchored, but --exclude doesn't. + */ + +/* + * Utility functions to manage exclusion/inclusion patterns + */ + +int +exclude(struct bsdtar *bsdtar, const char *pattern) +{ + struct matching *matching; + + if (bsdtar->matching == NULL) + initialize_matching(bsdtar); + matching = bsdtar->matching; + add_pattern(bsdtar, &(matching->exclusions), pattern); + matching->exclusions_count++; + return (0); +} + +int +exclude_from_file(struct bsdtar *bsdtar, const char *pathname) +{ + return (process_lines(bsdtar, pathname, &exclude)); +} + +int +include(struct bsdtar *bsdtar, const char *pattern) +{ + struct matching *matching; + + if (bsdtar->matching == NULL) + initialize_matching(bsdtar); + matching = bsdtar->matching; + add_pattern(bsdtar, &(matching->inclusions), pattern); + matching->inclusions_count++; + matching->inclusions_unmatched_count++; + return (0); +} + +int +include_from_file(struct bsdtar *bsdtar, const char *pathname) +{ + return (process_lines(bsdtar, pathname, &include)); +} + +static void +add_pattern(struct bsdtar *bsdtar, struct match **list, const char *pattern) +{ + struct match *match; + + match = malloc(sizeof(*match) + strlen(pattern) + 1); + if (match == NULL) + bsdtar_errc(bsdtar, 1, errno, "Out of memory"); + if (pattern[0] == '/') + pattern++; + strcpy(match->pattern, pattern); + /* Both "foo/" and "foo" should match "foo/bar". */ + if (match->pattern[strlen(match->pattern)-1] == '/') + match->pattern[strlen(match->pattern)-1] = '\0'; + match->next = *list; + *list = match; + match->matches = 0; +} + + +int +excluded(struct bsdtar *bsdtar, const char *pathname) +{ + struct matching *matching; + struct match *match; + struct match *matched; + + matching = bsdtar->matching; + if (matching == NULL) + return (0); + + /* Exclusions take priority */ + for (match = matching->exclusions; match != NULL; match = match->next){ + if (match_exclusion(match, pathname)) + return (1); + } + + /* Then check for inclusions */ + matched = NULL; + for (match = matching->inclusions; match != NULL; match = match->next){ + if (match_inclusion(match, pathname)) { + /* + * If this pattern has never been matched, + * then we're done. + */ + if (match->matches == 0) { + match->matches++; + matching->inclusions_unmatched_count++; + return (0); + } + /* + * Otherwise, remember the match but keep checking + * in case we can tick off an unmatched pattern. + */ + matched = match; + } + } + /* + * We didn't find a pattern that had never been matched, but + * we did find a match, so count it and exit. + */ + if (matched != NULL) { + matched->matches++; + return (0); + } + + /* If there were inclusions, default is to exclude. */ + if (matching->inclusions != NULL) + return (1); + + /* No explicit inclusions, default is to match. */ + return (0); +} + +/* + * This is a little odd, but it matches the default behavior of + * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar' + * + * XXX TODO: fnmatch isn't the most portable thing around, and even + * worse, FNM_LEADING_DIR is a non-POSIX extension. Thus, the + * following two functions need to eventually be replaced with code + * that does not rely on fnmatch(). + */ +int +match_exclusion(struct match *match, const char *pathname) +{ + const char *p; + + if (*match->pattern == '*' || *match->pattern == '/') + return (fnmatch(match->pattern, pathname, FNM_LEADING_DIR) == 0); + + for (p = pathname; p != NULL; p = strchr(p, '/')) { + if (*p == '/') + p++; + if (fnmatch(match->pattern, p, FNM_LEADING_DIR) == 0) + return (1); + } + return (0); +} + +/* + * Again, mimic gtar: inclusions are always anchored (have to match + * the beginning of the path) even though exclusions are not anchored. + */ +int +match_inclusion(struct match *match, const char *pathname) +{ + return (fnmatch(match->pattern, pathname, FNM_LEADING_DIR) == 0); +} + +void +cleanup_exclusions(struct bsdtar *bsdtar) +{ + struct match *p, *q; + + if (bsdtar->matching) { + p = bsdtar->matching->inclusions; + while (p != NULL) { + q = p; + p = p->next; + free(q); + } + p = bsdtar->matching->exclusions; + while (p != NULL) { + q = p; + p = p->next; + free(q); + } + free(bsdtar->matching); + } +} + +static void +initialize_matching(struct bsdtar *bsdtar) +{ + bsdtar->matching = malloc(sizeof(*bsdtar->matching)); + if (bsdtar->matching == NULL) + bsdtar_errc(bsdtar, 1, errno, "No memory"); + memset(bsdtar->matching, 0, sizeof(*bsdtar->matching)); +} + +int +unmatched_inclusions(struct bsdtar *bsdtar) +{ + struct matching *matching; + + matching = bsdtar->matching; + if (matching == NULL) + return (0); + return (matching->inclusions_unmatched_count); +} diff --git a/contrib/bsdtar/read.c b/contrib/bsdtar/read.c new file mode 100644 index 0000000000..343512ff33 --- /dev/null +++ b/contrib/bsdtar/read.c @@ -0,0 +1,430 @@ +/*- + * Copyright (c) 2003-2004 Tim Kientzle + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "bsdtar_platform.h" +__FBSDID("$FreeBSD: src/usr.bin/tar/read.c,v 1.21 2004/10/17 23:57:10 kientzle Exp $"); + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bsdtar.h" + +static void cleanup_security(struct bsdtar *); +static void list_item_verbose(struct bsdtar *, FILE *, + struct archive_entry *); +static void read_archive(struct bsdtar *bsdtar, char mode); +static int security_problem(struct bsdtar *, struct archive_entry *); + +void +tar_mode_t(struct bsdtar *bsdtar) +{ + read_archive(bsdtar, 't'); +} + +void +tar_mode_x(struct bsdtar *bsdtar) +{ + read_archive(bsdtar, 'x'); +} + +/* + * Handle 'x' and 't' modes. + */ +static void +read_archive(struct bsdtar *bsdtar, char mode) +{ + FILE *out; + struct archive *a; + struct archive_entry *entry; + int r; + + while (*bsdtar->argv) { + include(bsdtar, *bsdtar->argv); + bsdtar->argv++; + } + + if (bsdtar->names_from_file != NULL) + include_from_file(bsdtar, bsdtar->names_from_file); + + a = archive_read_new(); + archive_read_support_compression_all(a); + archive_read_support_format_all(a); + if (archive_read_open_file(a, bsdtar->filename, + bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block : + DEFAULT_BYTES_PER_BLOCK)) + bsdtar_errc(bsdtar, 1, 0, "Error opening archive: %s", + archive_error_string(a)); + + do_chdir(bsdtar); + for (;;) { + /* Support --fast-read option */ + if (bsdtar->option_fast_read && + unmatched_inclusions(bsdtar) == 0) + break; + + r = archive_read_next_header(a, &entry); + if (r == ARCHIVE_EOF) + break; + if (r == ARCHIVE_WARN) + bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a)); + if (r == ARCHIVE_FATAL) { + bsdtar->return_value = 1; + bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a)); + break; + } + if (r == ARCHIVE_RETRY) { + /* Retryable error: try again */ + bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(a)); + bsdtar_warnc(bsdtar, 0, "Retrying..."); + continue; + } + + if (excluded(bsdtar, archive_entry_pathname(entry))) + continue; + + if (mode == 't') { + /* Perversely, gtar uses -O to mean "send to stderr" + * when used with -t. */ + out = bsdtar->option_stdout ? stderr : stdout; + + if (bsdtar->verbose < 2) + safe_fprintf(out, "%s", + archive_entry_pathname(entry)); + else + list_item_verbose(bsdtar, out, entry); + fflush(out); + r = archive_read_data_skip(a); + if (r == ARCHIVE_WARN) { + fprintf(out, "\n"); + bsdtar_warnc(bsdtar, 0, "%s", + archive_error_string(a)); + } + if (r == ARCHIVE_RETRY) { + fprintf(out, "\n"); + bsdtar_warnc(bsdtar, 0, "%s", + archive_error_string(a)); + } + if (r == ARCHIVE_FATAL) { + fprintf(out, "\n"); + bsdtar_warnc(bsdtar, 0, "%s", + archive_error_string(a)); + break; + } + fprintf(out, "\n"); + } else { + if (bsdtar->option_interactive && + !yes("extract '%s'", archive_entry_pathname(entry))) + continue; + + if (security_problem(bsdtar, entry)) + continue; + + /* + * Format here is from SUSv2, including the + * deferred '\n'. + */ + if (bsdtar->verbose) { + safe_fprintf(stderr, "x %s", + archive_entry_pathname(entry)); + fflush(stderr); + } + if (bsdtar->option_stdout) { + /* TODO: Catch/recover any errors here. */ + archive_read_data_into_fd(a, 1); + } else if (archive_read_extract(a, entry, + bsdtar->extract_flags)) { + if (!bsdtar->verbose) + safe_fprintf(stderr, "%s", + archive_entry_pathname(entry)); + safe_fprintf(stderr, ": %s", + archive_error_string(a)); + if (!bsdtar->verbose) + fprintf(stderr, "\n"); + /* + * TODO: Decide how to handle + * extraction error... + */ + bsdtar->return_value = 1; + } + if (bsdtar->verbose) + fprintf(stderr, "\n"); + } + } + + if (bsdtar->verbose > 2) + fprintf(stdout, "Archive Format: %s, Compression: %s\n", + archive_format_name(a), archive_compression_name(a)); + + archive_read_finish(a); + cleanup_security(bsdtar); +} + + +/* + * Display information about the current file. + * + * The format here roughly duplicates the output of 'ls -l'. + * This is based on SUSv2, where 'tar tv' is documented as + * listing additional information in an "unspecified format," + * and 'pax -l' is documented as using the same format as 'ls -l'. + */ +static void +list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry) +{ + const struct stat *st; + char tmp[100]; + size_t w; + const char *p; + const char *fmt; + time_t tim; + static time_t now; + + st = archive_entry_stat(entry); + + /* + * We avoid collecting the entire list in memory at once by + * listing things as we see them. However, that also means we can't + * just pre-compute the field widths. Instead, we start with guesses + * and just widen them as necessary. These numbers are completely + * arbitrary. + */ + if (!bsdtar->u_width) { + bsdtar->u_width = 6; + bsdtar->gs_width = 13; + } + if (!now) + time(&now); + bsdtar_strmode(entry, tmp); + fprintf(out, "%s %d ", tmp, st->st_nlink); + + /* Use uname if it's present, else uid. */ + p = archive_entry_uname(entry); + if ((p == NULL) || (*p == '\0')) { + sprintf(tmp, "%d ", st->st_uid); + p = tmp; + } + w = strlen(p); + if (w > bsdtar->u_width) + bsdtar->u_width = w; + fprintf(out, "%-*s ", (int)bsdtar->u_width, p); + + /* Use gname if it's present, else gid. */ + p = archive_entry_gname(entry); + if (p != NULL && p[0] != '\0') { + fprintf(out, "%s", p); + w = strlen(p); + } else { + sprintf(tmp, "%d", st->st_gid); + w = strlen(tmp); + fprintf(out, "%s", tmp); + } + + /* + * Print device number or file size, right-aligned so as to make + * total width of group and devnum/filesize fields be gs_width. + * If gs_width is too small, grow it. + */ + if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { + sprintf(tmp, "%d,%u", + major(st->st_rdev), + (unsigned)minor(st->st_rdev)); /* ls(1) also casts here. */ + } else { + /* + * Note the use of platform-dependent macros to format + * the filesize here. We need the format string and the + * corresponding type for the cast. + */ + sprintf(tmp, BSDTAR_FILESIZE_PRINTF, + (BSDTAR_FILESIZE_TYPE)st->st_size); + } + if (w + strlen(tmp) >= bsdtar->gs_width) + bsdtar->gs_width = w+strlen(tmp)+1; + fprintf(out, "%*s", (int)(bsdtar->gs_width - w), tmp); + + /* Format the time using 'ls -l' conventions. */ + tim = (time_t)st->st_mtime; + if (abs(tim - now) > (365/2)*86400) + fmt = bsdtar->day_first ? "%e %b %Y" : "%b %e %Y"; + else + fmt = bsdtar->day_first ? "%e %b %R" : "%b %e %R"; + strftime(tmp, sizeof(tmp), fmt, localtime(&tim)); + fprintf(out, " %s ", tmp); + safe_fprintf(out, "%s", archive_entry_pathname(entry)); + + /* Extra information for links. */ + if (archive_entry_hardlink(entry)) /* Hard link */ + safe_fprintf(out, " link to %s", + archive_entry_hardlink(entry)); + else if (S_ISLNK(st->st_mode)) /* Symbolic link */ + safe_fprintf(out, " -> %s", archive_entry_symlink(entry)); +} + +struct security { + char *path; + size_t path_size; +}; + +/* + * Check for a variety of security issues. Fix what we can here, + * generate warnings as appropriate, return non-zero to prevent + * this entry from being extracted. + */ +static int +security_problem(struct bsdtar *bsdtar, struct archive_entry *entry) +{ + struct stat st; + const char *name, *pn; + char *p; + int r; + + /* -P option forces us to just accept all pathnames. */ + if (bsdtar->option_absolute_paths) + return (0); + + /* Strip leading '/'. */ + name = archive_entry_pathname(entry); + if (name[0] == '/') { + /* Generate a warning the first time this happens. */ + if (!bsdtar->warned_lead_slash) { + bsdtar_warnc(bsdtar, 0, + "Removing leading '/' from member names"); + bsdtar->warned_lead_slash = 1; + } + while (name[0] == '/') + name++; + archive_entry_set_pathname(entry, name); + } + + /* Reject any archive entry with '..' as a path element. */ + pn = name; + while (pn != NULL && pn[0] != '\0') { + if (pn[0] == '.' && pn[1] == '.' && + (pn[2] == '\0' || pn[2] == '/')) { + bsdtar_warnc(bsdtar, 0, + "Skipping pathname containing .."); + bsdtar->return_value = 1; + return (1); + } + pn = strchr(pn, '/'); + if (pn != NULL) + pn++; + } + + /* + * Gaurd against symlink tricks. Reject any archive entry whose + * destination would be altered by a symlink. + */ + /* XXX TODO: Make this faster by comparing current path to + * prefix of last successful check to avoid duplicate lstat() + * calls. XXX */ + pn = name; + if (bsdtar->security == NULL) { + bsdtar->security = malloc(sizeof(*bsdtar->security)); + if (bsdtar->security == NULL) + bsdtar_errc(bsdtar, 1, errno, "No Memory"); + bsdtar->security->path_size = MAXPATHLEN + 1; + bsdtar->security->path = malloc(bsdtar->security->path_size); + if (bsdtar->security->path == NULL) + bsdtar_errc(bsdtar, 1, errno, "No Memory"); + } + if (strlen(name) >= bsdtar->security->path_size) { + free(bsdtar->security->path); + while (strlen(name) >= bsdtar->security->path_size) + bsdtar->security->path_size *= 2; + bsdtar->security->path = malloc(bsdtar->security->path_size); + if (bsdtar->security->path == NULL) + bsdtar_errc(bsdtar, 1, errno, "No Memory"); + } + p = bsdtar->security->path; + while (pn != NULL && pn[0] != '\0') { + *p++ = *pn++; + while (*pn != '\0' && *pn != '/') + *p++ = *pn++; + p[0] = '\0'; + r = lstat(bsdtar->security->path, &st); + if (r != 0) { + if (errno == ENOENT) + break; + } else if (S_ISLNK(st.st_mode)) { + if (pn[0] == '\0') { + /* + * Last element is symlink; remove it + * so we can overwrite it with the + * item being extracted. + */ + if (!S_ISLNK(archive_entry_mode(entry))) { + /* + * Warn only if the symlink is being + * replaced with a non-symlink. + */ + bsdtar_warnc(bsdtar, 0, + "Removing symlink %s", + bsdtar->security->path); + } + if (unlink(bsdtar->security->path)) + bsdtar_errc(bsdtar, 1, errno, + "Unlink failed"); + /* Symlink gone. No more problem! */ + return (0); + } else if (bsdtar->option_unlink_first) { + /* User asked us to remove problems. */ + if (unlink(bsdtar->security->path)) + bsdtar_errc(bsdtar, 1, errno, + "Unlink failed"); + } else { + bsdtar_warnc(bsdtar, 0, + "Cannot extract %s through symlink %s", + name, bsdtar->security->path); + bsdtar->return_value = 1; + return (1); + } + } + } + + return (0); +} + +static void +cleanup_security(struct bsdtar *bsdtar) +{ + if (bsdtar->security != NULL) { + free(bsdtar->security->path); + free(bsdtar->security); + } +} diff --git a/contrib/bsdtar/util.c b/contrib/bsdtar/util.c new file mode 100644 index 0000000000..cb7ec77b81 --- /dev/null +++ b/contrib/bsdtar/util.c @@ -0,0 +1,382 @@ +/*- + * Copyright (c) 2003-2004 Tim Kientzle + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "bsdtar_platform.h" +__FBSDID("$FreeBSD: src/usr.bin/tar/util.c,v 1.11 2004/08/08 05:50:10 kientzle Exp $"); + +#include +#include /* Linux doesn't define mode_t, etc. in sys/stat.h. */ +#include +#include +#include +#include +#include +#include +#include + +#include "bsdtar.h" + +static void bsdtar_vwarnc(struct bsdtar *, int code, + const char *fmt, va_list ap); + +/* + * Print a string, taking care with any non-printable characters. + */ + +void +safe_fprintf(FILE *f, const char *fmt, ...) +{ + char *buff; + char *buff_heap; + int buff_length; + int length; + va_list ap; + char *p; + unsigned i; + char buff_stack[256]; + char copy_buff[256]; + + /* Use a stack-allocated buffer if we can, for speed and safety. */ + buff_heap = NULL; + buff_length = sizeof(buff_stack); + buff = buff_stack; + + va_start(ap, fmt); + length = vsnprintf(buff, buff_length, fmt, ap); + va_end(ap); + /* If the result is too large, allocate a buffer on the heap. */ + if (length >= buff_length) { + buff_length = length+1; + buff_heap = malloc(buff_length); + /* Failsafe: use the truncated string if malloc fails. */ + if (buff_heap != NULL) { + buff = buff_heap; + va_start(ap, fmt); + length = vsnprintf(buff, buff_length, fmt, ap); + va_end(ap); + } + } + + /* Write data, expanding unprintable characters. */ + p = buff; + i = 0; + while (*p != '\0') { + unsigned char c = *p++; + + if (isprint(c) && c != '\\') + copy_buff[i++] = c; + else { + copy_buff[i++] = '\\'; + switch (c) { + case '\a': copy_buff[i++] = 'a'; break; + case '\b': copy_buff[i++] = 'b'; break; + case '\f': copy_buff[i++] = 'f'; break; + case '\n': copy_buff[i++] = 'n'; break; +#if '\r' != '\n' + /* On some platforms, \n and \r are the same. */ + case '\r': copy_buff[i++] = 'r'; break; +#endif + case '\t': copy_buff[i++] = 't'; break; + case '\v': copy_buff[i++] = 'v'; break; + case '\\': copy_buff[i++] = '\\'; break; + default: + sprintf(copy_buff + i, "%03o", c); + i += 3; + } + } + + /* If our temp buffer is full, dump it and keep going. */ + if (i > (sizeof(copy_buff) - 8)) { + copy_buff[i++] = '\0'; + fprintf(f, "%s", copy_buff); + i = 0; + } + } + copy_buff[i++] = '\0'; + fprintf(f, "%s", copy_buff); + + /* If we allocated a heap-based buffer, free it now. */ + if (buff_heap != NULL) + free(buff_heap); +} + +static void +bsdtar_vwarnc(struct bsdtar *bsdtar, int code, const char *fmt, va_list ap) +{ + fprintf(stderr, "%s: ", bsdtar->progname); + vfprintf(stderr, fmt, ap); + if (code != 0) + fprintf(stderr, ": %s", strerror(code)); + fprintf(stderr, "\n"); +} + +void +bsdtar_warnc(struct bsdtar *bsdtar, int code, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + bsdtar_vwarnc(bsdtar, code, fmt, ap); + va_end(ap); +} + +void +bsdtar_errc(struct bsdtar *bsdtar, int eval, int code, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + bsdtar_vwarnc(bsdtar, code, fmt, ap); + va_end(ap); + exit(eval); +} + +int +yes(const char *fmt, ...) +{ + char buff[32]; + char *p; + ssize_t l; + + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, " (y/N)? "); + fflush(stderr); + + l = read(2, buff, sizeof(buff)); + if (l <= 0) + return (0); + buff[l] = 0; + + for (p = buff; *p != '\0'; p++) { + if (isspace(0xff & (int)*p)) + continue; + switch(*p) { + case 'y': case 'Y': + return (1); + case 'n': case 'N': + return (0); + default: + return (0); + } + } + + return (0); +} + +void +bsdtar_strmode(struct archive_entry *entry, char *bp) +{ + static const char *perms = "?rwxrwxrwx "; + static const mode_t permbits[] = + { S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, + S_IROTH, S_IWOTH, S_IXOTH }; + mode_t mode; + int i; + + /* Fill in a default string, then selectively override. */ + strcpy(bp, perms); + + mode = archive_entry_mode(entry); + switch (mode & S_IFMT) { + case S_IFREG: bp[0] = '-'; break; + case S_IFBLK: bp[0] = 'b'; break; + case S_IFCHR: bp[0] = 'c'; break; + case S_IFDIR: bp[0] = 'd'; break; + case S_IFLNK: bp[0] = 'l'; break; + case S_IFSOCK: bp[0] = 's'; break; +#ifdef S_IFIFO + case S_IFIFO: bp[0] = 'p'; break; +#endif +#ifdef S_IFWHT + case S_IFWHT: bp[0] = 'w'; break; +#endif + } + + for (i = 0; i < 9; i++) + if (!(mode & permbits[i])) + bp[i+1] = '-'; + + if (mode & S_ISUID) { + if (mode & S_IXUSR) bp[3] = 's'; + else bp[3] = 'S'; + } + if (mode & S_ISGID) { + if (mode & S_IXGRP) bp[6] = 's'; + else bp[6] = 'S'; + } + if (mode & S_ISVTX) { + if (mode & S_IXOTH) bp[9] = 't'; + else bp[9] = 'T'; + } + if (archive_entry_acl_count(entry, ARCHIVE_ENTRY_ACL_TYPE_ACCESS)) + bp[10] = '+'; +} + + +/* + * Read lines from file and do something with each one. If option_null + * is set, lines are terminated with zero bytes; otherwise, they're + * terminated with newlines. + * + * This uses a self-sizing buffer to handle arbitrarily-long lines. + * If the "process" function returns non-zero for any line, this + * function will return non-zero after attempting to process all + * remaining lines. + */ +int +process_lines(struct bsdtar *bsdtar, const char *pathname, + int (*process)(struct bsdtar *, const char *)) +{ + FILE *f; + char *buff, *buff_end, *line_start, *line_end, *p; + size_t buff_length, bytes_read, bytes_wanted; + int separator; + int ret; + + separator = bsdtar->option_null ? '\0' : '\n'; + ret = 0; + + if (strcmp(pathname, "-") == 0) + f = stdin; + else + f = fopen(pathname, "r"); + if (f == NULL) + bsdtar_errc(bsdtar, 1, errno, "Couldn't open %s", pathname); + buff_length = 8192; + buff = malloc(buff_length); + if (buff == NULL) + bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read %s", pathname); + line_start = line_end = buff_end = buff; + for (;;) { + /* Get some more data into the buffer. */ + bytes_wanted = buff + buff_length - buff_end; + bytes_read = fread(buff_end, 1, bytes_wanted, f); + buff_end += bytes_read; + /* Process all complete lines in the buffer. */ + while (line_end < buff_end) { + if (*line_end == separator) { + *line_end = '\0'; + if ((*process)(bsdtar, line_start) != 0) + ret = -1; + line_start = line_end + 1; + line_end = line_start; + } else + line_end++; + } + if (feof(f)) + break; + if (ferror(f)) + bsdtar_errc(bsdtar, 1, errno, + "Can't read %s", pathname); + if (line_start > buff) { + /* Move a leftover fractional line to the beginning. */ + memmove(buff, line_start, buff_end - line_start); + buff_end -= line_start - buff; + line_end -= line_start - buff; + line_start = buff; + } else { + /* Line is too big; enlarge the buffer. */ + p = realloc(buff, buff_length *= 2); + if (p == NULL) + bsdtar_errc(bsdtar, 1, ENOMEM, + "Line too long in %s", pathname); + buff_end = p + (buff_end - buff); + line_end = p + (line_end - buff); + line_start = buff = p; + } + } + /* At end-of-file, handle the final line. */ + if (line_end > line_start) { + *line_end = '\0'; + if ((*process)(bsdtar, line_start) != 0) + ret = -1; + } + free(buff); + if (f != stdin) + fclose(f); + return (ret); +} + +/*- + * The logic here for -C attempts to avoid + * chdir() as long as possible. For example: + * "-C /foo -C /bar file" needs chdir("/bar") but not chdir("/foo") + * "-C /foo -C bar file" needs chdir("/foo/bar") + * "-C /foo -C bar /file1" does not need chdir() + * "-C /foo -C bar /file1 file2" needs chdir("/foo/bar") before file2 + * + * The only correct way to handle this is to record a "pending" chdir + * request and combine multiple requests intelligently until we + * need to process a non-absolute file. set_chdir() adds the new dir + * to the pending list; do_chdir() actually executes any pending chdir. + * + * This way, programs that build tar command lines don't have to worry + * about -C with non-existent directories; such requests will only + * fail if the directory must be accessed. + */ +void +set_chdir(struct bsdtar *bsdtar, const char *newdir) +{ + if (newdir[0] == '/') { + /* The -C /foo -C /bar case; dump first one. */ + free(bsdtar->pending_chdir); + bsdtar->pending_chdir = NULL; + } + if (bsdtar->pending_chdir == NULL) + /* Easy case: no previously-saved dir. */ + bsdtar->pending_chdir = strdup(newdir); + else { + /* The -C /foo -C bar case; concatenate */ + char *old_pending = bsdtar->pending_chdir; + size_t old_len = strlen(old_pending); + bsdtar->pending_chdir = malloc(old_len + strlen(newdir) + 2); + if (old_pending[old_len - 1] == '/') + old_pending[old_len - 1] = '\0'; + if (bsdtar->pending_chdir != NULL) + sprintf(bsdtar->pending_chdir, "%s/%s", + old_pending, newdir); + free(old_pending); + } + if (bsdtar->pending_chdir == NULL) + bsdtar_errc(bsdtar, 1, errno, "No memory"); +} + +void +do_chdir(struct bsdtar *bsdtar) +{ + if (bsdtar->pending_chdir == NULL) + return; + + if (chdir(bsdtar->pending_chdir) != 0) { + bsdtar_errc(bsdtar, 1, 0, "could not chdir to '%s'\n", + bsdtar->pending_chdir); + } + free(bsdtar->pending_chdir); + bsdtar->pending_chdir = NULL; +} diff --git a/contrib/bsdtar/write.c b/contrib/bsdtar/write.c new file mode 100644 index 0000000000..36ebc8376b --- /dev/null +++ b/contrib/bsdtar/write.c @@ -0,0 +1,1365 @@ +/*- + * Copyright (c) 2003-2004 Tim Kientzle + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "bsdtar_platform.h" +__FBSDID("$FreeBSD: src/usr.bin/tar/write.c,v 1.35 2004/11/05 05:39:37 kientzle Exp $"); + +#include +#include +#ifdef HAVE_POSIX_ACL +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef linux +#include +#include +#endif + +#include "bsdtar.h" + +/* Fixed size of uname/gname caches. */ +#define name_cache_size 101 + +static const char * const NO_NAME = "(noname)"; + +/* Initial size of link cache. */ +#define links_cache_initial_size 1024 + +struct archive_dir_entry { + struct archive_dir_entry *next; + time_t mtime_sec; + int mtime_nsec; + char *name; +}; + +struct archive_dir { + struct archive_dir_entry *head, *tail; +}; + +struct links_cache { + unsigned long number_entries; + size_t number_buckets; + struct links_entry **buckets; +}; + +struct links_entry { + struct links_entry *next; + struct links_entry *previous; + int links; + dev_t dev; + ino_t ino; + char *name; +}; + +struct name_cache { + int probes; + int hits; + size_t size; + struct { + id_t id; + const char *name; + } cache[name_cache_size]; +}; + +static void add_dir_list(struct bsdtar *bsdtar, const char *path, + time_t mtime_sec, int mtime_nsec); +static int append_archive(struct bsdtar *, struct archive *, + const char *fname); +static void archive_names_from_file(struct bsdtar *bsdtar, + struct archive *a); +static int archive_names_from_file_helper(struct bsdtar *bsdtar, + const char *line); +static void create_cleanup(struct bsdtar *); +static void free_buckets(struct bsdtar *, struct links_cache *); +static void free_cache(struct name_cache *cache); +static const char * lookup_gname(struct bsdtar *bsdtar, gid_t gid); +static int lookup_gname_helper(struct bsdtar *bsdtar, + const char **name, id_t gid); +static void lookup_hardlink(struct bsdtar *, + struct archive_entry *entry, const struct stat *); +static const char * lookup_uname(struct bsdtar *bsdtar, uid_t uid); +static int lookup_uname_helper(struct bsdtar *bsdtar, + const char **name, id_t uid); +static int new_enough(struct bsdtar *, const char *path, + time_t mtime_sec, int mtime_nsec); +static void setup_acls(struct bsdtar *, struct archive_entry *, + const char *path); +static void test_for_append(struct bsdtar *); +static void write_archive(struct archive *, struct bsdtar *); +static void write_entry(struct bsdtar *, struct archive *, + struct stat *, const char *pathname, + unsigned pathlen, const char *accpath); +static int write_file_data(struct bsdtar *, struct archive *, + int fd); +static void write_heirarchy(struct bsdtar *, struct archive *, + const char *); + +void +tar_mode_c(struct bsdtar *bsdtar) +{ + struct archive *a; + int r; + + if (*bsdtar->argv == NULL && bsdtar->names_from_file == NULL) + bsdtar_errc(bsdtar, 1, 0, "no files or directories specified"); + + a = archive_write_new(); + + /* Support any format that the library supports. */ + if (bsdtar->create_format == NULL) { + r = archive_write_set_format_pax_restricted(a); + bsdtar->create_format = "pax restricted"; + } else { + r = archive_write_set_format_by_name(a, bsdtar->create_format); + } + if (r != ARCHIVE_OK) { + fprintf(stderr, "Can't use format %s: %s\n", + bsdtar->create_format, + archive_error_string(a)); + usage(bsdtar); + } + + /* + * If user explicitly set the block size, then assume they + * want the last block padded as well. Otherwise, use the + * default block size and accept archive_write_open_file()'s + * default padding decisions. + */ + if (bsdtar->bytes_per_block != 0) { + archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block); + archive_write_set_bytes_in_last_block(a, + bsdtar->bytes_per_block); + } else + archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK); + + switch (bsdtar->create_compression) { + case 0: + break; +#ifdef HAVE_LIBBZ2 + case 'j': case 'y': + archive_write_set_compression_bzip2(a); + break; +#endif +#ifdef HAVE_LIBZ + case 'z': + archive_write_set_compression_gzip(a); + break; +#endif + default: + bsdtar_errc(bsdtar, 1, 0, + "Unrecognized compression option -%c", + bsdtar->create_compression); + } + + r = archive_write_open_file(a, bsdtar->filename); + if (r != ARCHIVE_OK) + bsdtar_errc(bsdtar, 1, 0, archive_error_string(a)); + + write_archive(a, bsdtar); + + if (bsdtar->option_totals) { + fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n", + (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a)); + } + + archive_write_finish(a); +} + +/* + * Same as 'c', except we only support tar formats in uncompressed + * files on disk. + */ +void +tar_mode_r(struct bsdtar *bsdtar) +{ + off_t end_offset; + int format; + struct archive *a; + struct archive_entry *entry; + + /* Sanity-test some arguments and the file. */ + test_for_append(bsdtar); + + format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED; + + bsdtar->fd = open(bsdtar->filename, O_RDWR); + if (bsdtar->fd < 0) + bsdtar_errc(bsdtar, 1, errno, + "Cannot open %s", bsdtar->filename); + + a = archive_read_new(); + archive_read_support_compression_all(a); + archive_read_support_format_tar(a); + archive_read_support_format_gnutar(a); + archive_read_open_fd(a, bsdtar->fd, 10240); + while (0 == archive_read_next_header(a, &entry)) { + if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) { + archive_read_finish(a); + close(bsdtar->fd); + bsdtar_errc(bsdtar, 1, 0, + "Cannot append to compressed archive."); + } + /* Keep going until we hit end-of-archive */ + format = archive_format(a); + } + + end_offset = archive_read_header_position(a); + archive_read_finish(a); + + /* Re-open archive for writing */ + a = archive_write_new(); + archive_write_set_compression_none(a); + /* + * Set format to same one auto-detected above, except use + * ustar for appending to GNU tar, since the library doesn't + * write GNU tar format. + */ + if (format == ARCHIVE_FORMAT_TAR_GNUTAR) + format = ARCHIVE_FORMAT_TAR_USTAR; + archive_write_set_format(a, format); + lseek(bsdtar->fd, end_offset, SEEK_SET); /* XXX check return val XXX */ + archive_write_open_fd(a, bsdtar->fd); /* XXX check return val XXX */ + + write_archive(a, bsdtar); /* XXX check return val XXX */ + + if (bsdtar->option_totals) { + fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n", + (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a)); + } + + archive_write_finish(a); + close(bsdtar->fd); + bsdtar->fd = -1; +} + +void +tar_mode_u(struct bsdtar *bsdtar) +{ + off_t end_offset; + struct archive *a; + struct archive_entry *entry; + const char *filename; + int format; + struct archive_dir_entry *p; + struct archive_dir archive_dir; + + bsdtar->archive_dir = &archive_dir; + memset(&archive_dir, 0, sizeof(archive_dir)); + + filename = NULL; + format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED; + + /* Sanity-test some arguments and the file. */ + test_for_append(bsdtar); + + bsdtar->fd = open(bsdtar->filename, O_RDWR); + if (bsdtar->fd < 0) + bsdtar_errc(bsdtar, 1, errno, + "Cannot open %s", bsdtar->filename); + + a = archive_read_new(); + archive_read_support_compression_all(a); + archive_read_support_format_tar(a); + archive_read_support_format_gnutar(a); + archive_read_open_fd(a, bsdtar->fd, + bsdtar->bytes_per_block != 0 ? bsdtar->bytes_per_block : + DEFAULT_BYTES_PER_BLOCK); + + /* Build a list of all entries and their recorded mod times. */ + while (0 == archive_read_next_header(a, &entry)) { + if (archive_compression(a) != ARCHIVE_COMPRESSION_NONE) { + archive_read_finish(a); + close(bsdtar->fd); + bsdtar_errc(bsdtar, 1, 0, + "Cannot append to compressed archive."); + } + add_dir_list(bsdtar, archive_entry_pathname(entry), + archive_entry_mtime(entry), + archive_entry_mtime_nsec(entry)); + /* Record the last format determination we see */ + format = archive_format(a); + /* Keep going until we hit end-of-archive */ + } + + end_offset = archive_read_header_position(a); + archive_read_finish(a); + + /* Re-open archive for writing. */ + a = archive_write_new(); + archive_write_set_compression_none(a); + /* + * Set format to same one auto-detected above, except that + * we don't write GNU tar format, so use ustar instead. + */ + if (format == ARCHIVE_FORMAT_TAR_GNUTAR) + format = ARCHIVE_FORMAT_TAR_USTAR; + archive_write_set_format(a, format); + if (bsdtar->bytes_per_block != 0) { + archive_write_set_bytes_per_block(a, bsdtar->bytes_per_block); + archive_write_set_bytes_in_last_block(a, + bsdtar->bytes_per_block); + } else + archive_write_set_bytes_per_block(a, DEFAULT_BYTES_PER_BLOCK); + lseek(bsdtar->fd, end_offset, SEEK_SET); + ftruncate(bsdtar->fd, end_offset); + archive_write_open_fd(a, bsdtar->fd); + + write_archive(a, bsdtar); + + if (bsdtar->option_totals) { + fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n", + (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a)); + } + + archive_write_finish(a); + close(bsdtar->fd); + bsdtar->fd = -1; + + while (bsdtar->archive_dir->head != NULL) { + p = bsdtar->archive_dir->head->next; + free(bsdtar->archive_dir->head->name); + free(bsdtar->archive_dir->head); + bsdtar->archive_dir->head = p; + } + bsdtar->archive_dir->tail = NULL; +} + + +/* + * Write user-specified files/dirs to opened archive. + */ +static void +write_archive(struct archive *a, struct bsdtar *bsdtar) +{ + const char *arg; + + if (bsdtar->names_from_file != NULL) + archive_names_from_file(bsdtar, a); + + while (*bsdtar->argv) { + arg = *bsdtar->argv; + if (arg[0] == '-' && arg[1] == 'C') { + arg += 2; + if (*arg == '\0') { + bsdtar->argv++; + arg = *bsdtar->argv; + if (arg == NULL) { + bsdtar_warnc(bsdtar, 1, 0, + "Missing argument for -C"); + bsdtar->return_value = 1; + return; + } + } + set_chdir(bsdtar, arg); + } else { + if (*arg != '/' || (arg[0] == '@' && arg[1] != '/')) + do_chdir(bsdtar); /* Handle a deferred -C */ + if (*arg == '@') { + if (append_archive(bsdtar, a, arg + 1) != 0) + break; + } else + write_heirarchy(bsdtar, a, arg); + } + bsdtar->argv++; + } + + create_cleanup(bsdtar); + archive_write_close(a); +} + +/* + * Archive names specified in file. + * + * Unless --null was specified, a line containing exactly "-C" will + * cause the next line to be a directory to pass to chdir(). If + * --null is specified, then a line "-C" is just another filename. + */ +void +archive_names_from_file(struct bsdtar *bsdtar, struct archive *a) +{ + bsdtar->archive = a; + + bsdtar->next_line_is_dir = 0; + process_lines(bsdtar, bsdtar->names_from_file, + archive_names_from_file_helper); + if (bsdtar->next_line_is_dir) + bsdtar_errc(bsdtar, 1, errno, + "Unexpected end of filename list; " + "directory expected after -C"); +} + +static int +archive_names_from_file_helper(struct bsdtar *bsdtar, const char *line) +{ + if (bsdtar->next_line_is_dir) { + set_chdir(bsdtar, line); + bsdtar->next_line_is_dir = 0; + } else if (!bsdtar->option_null && strcmp(line, "-C") == 0) + bsdtar->next_line_is_dir = 1; + else { + if (*line != '/') + do_chdir(bsdtar); /* Handle a deferred -C */ + write_heirarchy(bsdtar, bsdtar->archive, line); + } + return (0); +} + +/* + * Copy from specified archive to current archive. + * Returns non-zero on fatal error (i.e., output errors). Errors + * reading the input archive set bsdtar->return_value, but this + * function will still return zero. + */ +static int +append_archive(struct bsdtar *bsdtar, struct archive *a, const char *filename) +{ + struct archive *ina; + struct archive_entry *in_entry; + int bytes_read, bytes_written; + char buff[8192]; + + if (strcmp(filename, "-") == 0) + filename = NULL; /* Library uses NULL for stdio. */ + + ina = archive_read_new(); + archive_read_support_format_all(ina); + archive_read_support_compression_all(ina); + if (archive_read_open_file(ina, filename, 10240)) { + bsdtar_warnc(bsdtar, 0, "%s", archive_error_string(ina)); + bsdtar->return_value = 1; + return (0); + } + while (0 == archive_read_next_header(ina, &in_entry)) { + if (!new_enough(bsdtar, archive_entry_pathname(in_entry), + archive_entry_mtime(in_entry), + archive_entry_mtime_nsec(in_entry))) + continue; + if (excluded(bsdtar, archive_entry_pathname(in_entry))) + continue; + if (bsdtar->option_interactive && + !yes("copy '%s'", archive_entry_pathname(in_entry))) + continue; + if (bsdtar->verbose) + safe_fprintf(stderr, "a %s", + archive_entry_pathname(in_entry)); + /* XXX handle/report errors XXX */ + if (archive_write_header(a, in_entry)) { + bsdtar_warnc(bsdtar, 0, "%s", + archive_error_string(ina)); + bsdtar->return_value = 1; + return (-1); + } + bytes_read = archive_read_data(ina, buff, sizeof(buff)); + while (bytes_read > 0) { + bytes_written = + archive_write_data(a, buff, bytes_read); + if (bytes_written < bytes_read) { + bsdtar_warnc(bsdtar, archive_errno(a), "%s", + archive_error_string(a)); + return (-1); + } + bytes_read = + archive_read_data(ina, buff, sizeof(buff)); + } + if (bsdtar->verbose) + fprintf(stderr, "\n"); + + } + if (archive_errno(ina)) { + bsdtar_warnc(bsdtar, 0, "Error reading archive %s: %s", + filename, archive_error_string(ina)); + bsdtar->return_value = 1; + } + + return (0); /* TODO: Return non-zero on error */ +} + +/* + * Add the file or dir heirarchy named by 'path' to the archive + */ +static void +write_heirarchy(struct bsdtar *bsdtar, struct archive *a, const char *path) +{ + FTS *fts; + FTSENT *ftsent; + int ftsoptions; + char *fts_argv[2]; +#ifdef linux + int fd, r; + unsigned long fflags; +#endif + + /* + * Sigh: fts_open modifies it's first parameter, so we have to + * copy 'path' to mutable storage. + */ + fts_argv[0] = strdup(path); + if (fts_argv[0] == NULL) + bsdtar_errc(bsdtar, 1, ENOMEM, "Can't open %s", path); + fts_argv[1] = NULL; + ftsoptions = FTS_PHYSICAL; + switch (bsdtar->symlink_mode) { + case 'H': + ftsoptions |= FTS_COMFOLLOW; + break; + case 'L': + ftsoptions = FTS_COMFOLLOW | FTS_LOGICAL; + break; + } + if (bsdtar->option_dont_traverse_mounts) + ftsoptions |= FTS_XDEV; + + fts = fts_open(fts_argv, ftsoptions, NULL); + + + if (!fts) { + bsdtar_warnc(bsdtar, errno, "%s: Cannot open", path); + bsdtar->return_value = 1; + return; + } + + while ((ftsent = fts_read(fts))) { + switch (ftsent->fts_info) { + case FTS_NS: + bsdtar_warnc(bsdtar, ftsent->fts_errno, + "%s: Could not stat", ftsent->fts_path); + bsdtar->return_value = 1; + break; + case FTS_ERR: + bsdtar_warnc(bsdtar, ftsent->fts_errno, "%s", + ftsent->fts_path); + bsdtar->return_value = 1; + break; + case FTS_DNR: + bsdtar_warnc(bsdtar, ftsent->fts_errno, + "%s: Cannot read directory contents", + ftsent->fts_path); + bsdtar->return_value = 1; + break; + case FTS_W: /* Skip Whiteout entries */ + break; + case FTS_DC: /* Directory that causes cycle */ + /* XXX Does this need special handling ? */ + break; + case FTS_D: + /* + * If this dir is flagged "nodump" and we're + * honoring such flags, tell FTS to skip the + * entire tree and don't write the entry for the + * directory itself. + */ +#ifdef HAVE_CHFLAGS + if (bsdtar->option_honor_nodump && + (ftsent->fts_statp->st_flags & UF_NODUMP)) { + fts_set(fts, ftsent, FTS_SKIP); + break; + } +#endif + +#ifdef linux + /* + * Linux has a nodump flag too but to read it + * we have to open() the dir and do an ioctl on it... + */ + if (bsdtar->option_honor_nodump && + ((fd = open(ftsent->fts_name, O_RDONLY|O_NONBLOCK)) >= 0) && + ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags)), + close(fd), r) >= 0 && + (fflags & EXT2_NODUMP_FL)) { + fts_set(fts, ftsent, FTS_SKIP); + break; + } +#endif + + /* + * In -u mode, we need to check whether this + * is newer than what's already in the archive. + */ + if (!new_enough(bsdtar, ftsent->fts_path, + ftsent->fts_statp->st_mtime, + ARCHIVE_STAT_MTIME_NANOS(ftsent->fts_statp))) + break; + /* + * If this dir is excluded by a filename + * pattern, tell FTS to skip the entire tree + * and don't write the entry for the directory + * itself. + */ + if (excluded(bsdtar, ftsent->fts_path)) { + fts_set(fts, ftsent, FTS_SKIP); + break; + } + + /* + * If the user vetoes the directory, skip + * the whole thing. + */ + if (bsdtar->option_interactive && + !yes("add '%s'", ftsent->fts_path)) { + fts_set(fts, ftsent, FTS_SKIP); + break; + } + + /* + * If we're not recursing, tell FTS to skip the + * tree but do fall through and write the entry + * for the dir itself. + */ + if (bsdtar->option_no_subdirs) + fts_set(fts, ftsent, FTS_SKIP); + write_entry(bsdtar, a, ftsent->fts_statp, + ftsent->fts_path, ftsent->fts_pathlen, + ftsent->fts_accpath); + break; + case FTS_F: + case FTS_SL: + case FTS_SLNONE: + case FTS_DEFAULT: + /* + * Skip this file if it's flagged "nodump" and we're + * honoring that flag. + */ +#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP) + if (bsdtar->option_honor_nodump && + (ftsent->fts_statp->st_flags & UF_NODUMP)) + break; +#endif + +#ifdef linux + /* + * Linux has a nodump flag too but to read it + * we have to open() the file and do an ioctl on it... + */ + if (bsdtar->option_honor_nodump && + S_ISREG(ftsent->fts_statp->st_mode) && + ((fd = open(ftsent->fts_name, O_RDONLY|O_NONBLOCK)) >= 0) && + ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &fflags)), + close(fd), r) >= 0 && + (fflags & EXT2_NODUMP_FL)) + break; +#endif + + /* + * Skip this file if it's excluded by a + * filename pattern. + */ + if (excluded(bsdtar, ftsent->fts_path)) + break; + + /* + * In -u mode, we need to check whether this + * is newer than what's already in the archive. + */ + if (!new_enough(bsdtar, ftsent->fts_path, + ftsent->fts_statp->st_mtime, + ARCHIVE_STAT_MTIME_NANOS(ftsent->fts_statp))) + break; + + if (bsdtar->option_interactive && + !yes("add '%s'", ftsent->fts_path)) { + break; + } + + write_entry(bsdtar, a, ftsent->fts_statp, + ftsent->fts_path, ftsent->fts_pathlen, + ftsent->fts_accpath); + break; + case FTS_DP: + break; + default: + bsdtar_warnc(bsdtar, 0, + "%s: Heirarchy traversal error %d\n", + ftsent->fts_path, + ftsent->fts_info); + break; + } + + } + if (errno) + bsdtar_warnc(bsdtar, errno, "%s", path); + if (fts_close(fts)) + bsdtar_warnc(bsdtar, errno, "fts_close failed"); + free(fts_argv[0]); +} + +/* + * Add a single filesystem object to the archive. + */ +static void +write_entry(struct bsdtar *bsdtar, struct archive *a, struct stat *st, + const char *pathname, unsigned pathlen, const char *accpath) +{ + struct archive_entry *entry; + int e; + int fd; +#ifdef linux + int r; + unsigned long stflags; +#endif + static char linkbuffer[PATH_MAX+1]; + + (void)pathlen; /* UNUSED */ + + fd = -1; + entry = archive_entry_new(); + + /* Non-regular files get archived with zero size. */ + if (!S_ISREG(st->st_mode)) + st->st_size = 0; + + /* Strip redundant "./" from start of filename. */ + if (pathname != NULL && pathname[0] == '.' && pathname[1] == '/') { + pathname += 2; + if (*pathname == '\0') /* This is the "./" directory. */ + goto cleanup; /* Don't archive it ever. */ + } + + /* Strip leading '/' unless user has asked us not to. */ + if (pathname && pathname[0] == '/' && !bsdtar->option_absolute_paths) { + /* Generate a warning the first time this happens. */ + if (!bsdtar->warned_lead_slash) { + bsdtar_warnc(bsdtar, 0, + "Removing leading '/' from member names"); + bsdtar->warned_lead_slash = 1; + } + pathname++; + } + + archive_entry_set_pathname(entry, pathname); + + if (!S_ISDIR(st->st_mode) && (st->st_nlink > 1)) + lookup_hardlink(bsdtar, entry, st); + + /* Display entry as we process it. This format is required by SUSv2. */ + if (bsdtar->verbose) + safe_fprintf(stderr, "a %s", pathname); + + /* Read symbolic link information. */ + if ((st->st_mode & S_IFMT) == S_IFLNK) { + int lnklen; + + lnklen = readlink(accpath, linkbuffer, PATH_MAX); + if (lnklen < 0) { + if (!bsdtar->verbose) + bsdtar_warnc(bsdtar, errno, + "%s: Couldn't read symbolic link", + pathname); + else + safe_fprintf(stderr, + ": Couldn't read symbolic link: %s", + strerror(errno)); + goto cleanup; + } + linkbuffer[lnklen] = 0; + archive_entry_set_symlink(entry, linkbuffer); + } + + /* Look up username and group name. */ + archive_entry_set_uname(entry, lookup_uname(bsdtar, st->st_uid)); + archive_entry_set_gname(entry, lookup_gname(bsdtar, st->st_gid)); + +#ifdef HAVE_CHFLAGS + if (st->st_flags != 0) + archive_entry_set_fflags(entry, st->st_flags, 0); +#endif + +#ifdef linux + if ((S_ISREG(st->st_mode) || S_ISDIR(st->st_mode)) && + ((fd = open(accpath, O_RDONLY|O_NONBLOCK)) >= 0) && + ((r = ioctl(fd, EXT2_IOC_GETFLAGS, &stflags)), close(fd), r) >= 0 && + stflags) { + archive_entry_set_fflags(entry, stflags, 0); + } +#endif + + archive_entry_copy_stat(entry, st); + setup_acls(bsdtar, entry, accpath); + + /* + * If it's a regular file (and non-zero in size) make sure we + * can open it before we start to write. In particular, note + * that we can always archive a zero-length file, even if we + * can't read it. + */ + if (S_ISREG(st->st_mode) && st->st_size > 0) { + fd = open(accpath, O_RDONLY); + if (fd < 0) { + if (!bsdtar->verbose) + bsdtar_warnc(bsdtar, errno, "%s", pathname); + else + fprintf(stderr, ": %s", strerror(errno)); + goto cleanup; + } + } + + e = archive_write_header(a, entry); + if (e != ARCHIVE_OK) { + if (!bsdtar->verbose) + bsdtar_warnc(bsdtar, 0, "%s: %s", pathname, + archive_error_string(a)); + else + fprintf(stderr, ": %s", archive_error_string(a)); + } + + if (e == ARCHIVE_FATAL) + exit(1); + + /* + * If we opened a file earlier, write it out now. Note that + * the format handler might have reset the size field to zero + * to inform us that the archive body won't get stored. In + * that case, just skip the write. + */ + if (fd >= 0 && archive_entry_size(entry) > 0) + write_file_data(bsdtar, a, fd); + +cleanup: + if (fd >= 0) + close(fd); + + if (entry != NULL) + archive_entry_free(entry); + + if (bsdtar->verbose) + fprintf(stderr, "\n"); +} + + +/* Helper function to copy file to archive, with stack-allocated buffer. */ +static int +write_file_data(struct bsdtar *bsdtar, struct archive *a, int fd) +{ + char buff[64*1024]; + ssize_t bytes_read; + ssize_t bytes_written; + + /* XXX TODO: Allocate buffer on heap and store pointer to + * it in bsdtar structure; arrange cleanup as well. XXX */ + (void)bsdtar; + + bytes_read = read(fd, buff, sizeof(buff)); + while (bytes_read > 0) { + bytes_written = archive_write_data(a, buff, bytes_read); + if (bytes_written <= 0) + return (-1); /* Write failed; this is bad */ + bytes_read = read(fd, buff, sizeof(buff)); + } + return 0; +} + + +static void +create_cleanup(struct bsdtar *bsdtar) +{ + /* Free inode->pathname map used for hardlink detection. */ + if (bsdtar->links_cache != NULL) { + free_buckets(bsdtar, bsdtar->links_cache); + free(bsdtar->links_cache); + bsdtar->links_cache = NULL; + } + + free_cache(bsdtar->uname_cache); + bsdtar->uname_cache = NULL; + free_cache(bsdtar->gname_cache); + bsdtar->gname_cache = NULL; +} + + +static void +free_buckets(struct bsdtar *bsdtar, struct links_cache *links_cache) +{ + size_t i; + + if (links_cache->buckets == NULL) + return; + + for (i = 0; i < links_cache->number_buckets; i++) { + while (links_cache->buckets[i] != NULL) { + struct links_entry *lp = links_cache->buckets[i]->next; + if (bsdtar->option_warn_links) + bsdtar_warnc(bsdtar, 0, "Missing links to %s", + links_cache->buckets[i]->name); + if (links_cache->buckets[i]->name != NULL) + free(links_cache->buckets[i]->name); + free(links_cache->buckets[i]); + links_cache->buckets[i] = lp; + } + } + free(links_cache->buckets); + links_cache->buckets = NULL; +} + +static void +lookup_hardlink(struct bsdtar *bsdtar, struct archive_entry *entry, + const struct stat *st) +{ + struct links_cache *links_cache; + struct links_entry *le, **new_buckets; + int hash; + size_t i, new_size; + + /* If necessary, initialize the links cache. */ + links_cache = bsdtar->links_cache; + if (links_cache == NULL) { + bsdtar->links_cache = malloc(sizeof(struct links_cache)); + if (bsdtar->links_cache == NULL) + bsdtar_errc(bsdtar, 1, ENOMEM, + "No memory for hardlink detection."); + links_cache = bsdtar->links_cache; + memset(links_cache, 0, sizeof(struct links_cache)); + links_cache->number_buckets = links_cache_initial_size; + links_cache->buckets = malloc(links_cache->number_buckets * + sizeof(links_cache->buckets[0])); + if (links_cache->buckets == NULL) { + bsdtar_errc(bsdtar, 1, ENOMEM, + "No memory for hardlink detection."); + } + for (i = 0; i < links_cache->number_buckets; i++) + links_cache->buckets[i] = NULL; + } + + /* If the links cache overflowed and got flushed, don't bother. */ + if (links_cache->buckets == NULL) + return; + + /* If the links cache is getting too full, enlarge the hash table. */ + if (links_cache->number_entries > links_cache->number_buckets * 2) + { + int count; + + new_size = links_cache->number_buckets * 2; + new_buckets = malloc(new_size * sizeof(struct links_entry *)); + + count = 0; + + if (new_buckets != NULL) { + memset(new_buckets, 0, + new_size * sizeof(struct links_entry *)); + for (i = 0; i < links_cache->number_buckets; i++) { + while (links_cache->buckets[i] != NULL) { + /* Remove entry from old bucket. */ + le = links_cache->buckets[i]; + links_cache->buckets[i] = le->next; + + /* Add entry to new bucket. */ + hash = (le->dev ^ le->ino) % new_size; + + if (new_buckets[hash] != NULL) + new_buckets[hash]->previous = + le; + le->next = new_buckets[hash]; + le->previous = NULL; + new_buckets[hash] = le; + } + } + free(links_cache->buckets); + links_cache->buckets = new_buckets; + links_cache->number_buckets = new_size; + } else { + free_buckets(bsdtar, links_cache); + bsdtar_warnc(bsdtar, ENOMEM, + "No more memory for recording hard links"); + bsdtar_warnc(bsdtar, 0, + "Remaining links will be dumped as full files"); + } + } + + /* Try to locate this entry in the links cache. */ + hash = ( st->st_dev ^ st->st_ino ) % links_cache->number_buckets; + for (le = links_cache->buckets[hash]; le != NULL; le = le->next) { + if (le->dev == st->st_dev && le->ino == st->st_ino) { + archive_entry_copy_hardlink(entry, le->name); + + /* + * Decrement link count each time and release + * the entry if it hits zero. This saves + * memory and is necessary for proper -l + * implementation. + */ + if (--le->links <= 0) { + if (le->previous != NULL) + le->previous->next = le->next; + if (le->next != NULL) + le->next->previous = le->previous; + if (le->name != NULL) + free(le->name); + if (links_cache->buckets[hash] == le) + links_cache->buckets[hash] = le->next; + links_cache->number_entries--; + free(le); + } + + return; + } + } + + /* Add this entry to the links cache. */ + le = malloc(sizeof(struct links_entry)); + if (le != NULL) + le->name = strdup(archive_entry_pathname(entry)); + if ((le == NULL) || (le->name == NULL)) { + free_buckets(bsdtar, links_cache); + bsdtar_warnc(bsdtar, ENOMEM, + "No more memory for recording hard links"); + bsdtar_warnc(bsdtar, 0, + "Remaining hard links will be dumped as full files"); + if (le != NULL) + free(le); + return; + } + if (links_cache->buckets[hash] != NULL) + links_cache->buckets[hash]->previous = le; + links_cache->number_entries++; + le->next = links_cache->buckets[hash]; + le->previous = NULL; + links_cache->buckets[hash] = le; + le->dev = st->st_dev; + le->ino = st->st_ino; + le->links = st->st_nlink - 1; +} + +#ifdef HAVE_POSIX_ACL +void setup_acl(struct bsdtar *bsdtar, + struct archive_entry *entry, const char *accpath, + int acl_type, int archive_entry_acl_type); + +void +setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry, + const char *accpath) +{ + archive_entry_acl_clear(entry); + + setup_acl(bsdtar, entry, accpath, + ACL_TYPE_ACCESS, ARCHIVE_ENTRY_ACL_TYPE_ACCESS); + /* Only directories can have default ACLs. */ + if (S_ISDIR(archive_entry_mode(entry))) + setup_acl(bsdtar, entry, accpath, + ACL_TYPE_DEFAULT, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT); +} + +void +setup_acl(struct bsdtar *bsdtar, struct archive_entry *entry, + const char *accpath, int acl_type, int archive_entry_acl_type) +{ + acl_t acl; + acl_tag_t acl_tag; + acl_entry_t acl_entry; + acl_permset_t acl_permset; + int s, ae_id, ae_tag, ae_perm; + const char *ae_name; + + /* Retrieve access ACL from file. */ + acl = acl_get_file(accpath, acl_type); + if (acl != NULL) { + s = acl_get_entry(acl, ACL_FIRST_ENTRY, &acl_entry); + while (s == 1) { + ae_id = -1; + ae_name = NULL; + + acl_get_tag_type(acl_entry, &acl_tag); + if (acl_tag == ACL_USER) { + ae_id = (int)*(uid_t *)acl_get_qualifier(acl_entry); + ae_name = lookup_uname(bsdtar, ae_id); + ae_tag = ARCHIVE_ENTRY_ACL_USER; + } else if (acl_tag == ACL_GROUP) { + ae_id = (int)*(gid_t *)acl_get_qualifier(acl_entry); + ae_name = lookup_gname(bsdtar, ae_id); + ae_tag = ARCHIVE_ENTRY_ACL_GROUP; + } else if (acl_tag == ACL_MASK) { + ae_tag = ARCHIVE_ENTRY_ACL_MASK; + } else if (acl_tag == ACL_USER_OBJ) { + ae_tag = ARCHIVE_ENTRY_ACL_USER_OBJ; + } else if (acl_tag == ACL_GROUP_OBJ) { + ae_tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ; + } else if (acl_tag == ACL_OTHER) { + ae_tag = ARCHIVE_ENTRY_ACL_OTHER; + } else { + /* Skip types that libarchive can't support. */ + continue; + } + + acl_get_permset(acl_entry, &acl_permset); + ae_perm = 0; + if (acl_get_perm_np(acl_permset, ACL_EXECUTE)) + ae_perm |= ARCHIVE_ENTRY_ACL_EXECUTE; + if (acl_get_perm_np(acl_permset, ACL_READ)) + ae_perm |= ARCHIVE_ENTRY_ACL_READ; + if (acl_get_perm_np(acl_permset, ACL_WRITE)) + ae_perm |= ARCHIVE_ENTRY_ACL_WRITE; + + archive_entry_acl_add_entry(entry, + archive_entry_acl_type, ae_perm, ae_tag, + ae_id, ae_name); + + s = acl_get_entry(acl, ACL_NEXT_ENTRY, &acl_entry); + } + acl_free(acl); + } +} +#else +void +setup_acls(struct bsdtar *bsdtar, struct archive_entry *entry, + const char *accpath) +{ + (void)bsdtar; + (void)entry; + (void)accpath; +} +#endif + +static void +free_cache(struct name_cache *cache) +{ + size_t i; + + if (cache != NULL) { + for(i = 0; i < cache->size; i++) { + if (cache->cache[i].name != NULL && + cache->cache[i].name != NO_NAME) + free((void *)(uintptr_t)cache->cache[i].name); + } + free(cache); + } +} + +/* + * Lookup uid/gid from uname/gname, return NULL if no match. + */ +static const char * +lookup_name(struct bsdtar *bsdtar, struct name_cache **name_cache_variable, + int (*lookup_fn)(struct bsdtar *, const char **, id_t), id_t id) +{ + struct name_cache *cache; + const char *name; + int slot; + + + if (*name_cache_variable == NULL) { + *name_cache_variable = malloc(sizeof(struct name_cache)); + if (*name_cache_variable == NULL) + bsdtar_errc(bsdtar, 1, ENOMEM, "No more memory"); + memset(*name_cache_variable, 0, sizeof(struct name_cache)); + (*name_cache_variable)->size = name_cache_size; + } + + cache = *name_cache_variable; + cache->probes++; + + slot = id % cache->size; + if (cache->cache[slot].name != NULL) { + if (cache->cache[slot].id == id) { + cache->hits++; + if (cache->cache[slot].name == NO_NAME) + return (NULL); + return (cache->cache[slot].name); + } + if (cache->cache[slot].name != NO_NAME) + free((void *)(uintptr_t)cache->cache[slot].name); + cache->cache[slot].name = NULL; + } + + if (lookup_fn(bsdtar, &name, id) == 0) { + if (name == NULL || name[0] == '\0') { + /* Cache the negative response. */ + cache->cache[slot].name = NO_NAME; + cache->cache[slot].id = id; + } else { + cache->cache[slot].name = strdup(name); + if (cache->cache[slot].name != NULL) { + cache->cache[slot].id = id; + return (cache->cache[slot].name); + } + /* + * Conveniently, NULL marks an empty slot, so + * if the strdup() fails, we've just failed to + * cache it. No recovery necessary. + */ + } + } + return (NULL); +} + +static const char * +lookup_uname(struct bsdtar *bsdtar, uid_t uid) +{ + return (lookup_name(bsdtar, &bsdtar->uname_cache, + &lookup_uname_helper, (id_t)uid)); +} + +static int +lookup_uname_helper(struct bsdtar *bsdtar, const char **name, id_t id) +{ + struct passwd *pwent; + + (void)bsdtar; /* UNUSED */ + + errno = 0; + pwent = getpwuid((uid_t)id); + if (pwent == NULL) { + *name = NULL; + if (errno != 0) + bsdtar_warnc(bsdtar, errno, "getpwuid(%d) failed", id); + return (errno); + } + + *name = pwent->pw_name; + return (0); +} + +static const char * +lookup_gname(struct bsdtar *bsdtar, gid_t gid) +{ + return (lookup_name(bsdtar, &bsdtar->gname_cache, + &lookup_gname_helper, (id_t)gid)); +} + +static int +lookup_gname_helper(struct bsdtar *bsdtar, const char **name, id_t id) +{ + struct group *grent; + + (void)bsdtar; /* UNUSED */ + + errno = 0; + grent = getgrgid((gid_t)id); + if (grent == NULL) { + *name = NULL; + if (errno != 0) + bsdtar_warnc(bsdtar, errno, "getgrgid(%d) failed", id); + return (errno); + } + + *name = grent->gr_name; + return (0); +} + +/* + * Test if the specified file is newer than what's already + * in the archive. + */ +int +new_enough(struct bsdtar *bsdtar, const char *path, + time_t mtime_sec, int mtime_nsec) +{ + struct archive_dir_entry *p; + + if (path[0] == '.' && path[1] == '/' && path[2] != '\0') + path += 2; + + if (bsdtar->archive_dir == NULL || + bsdtar->archive_dir->head == NULL) + return (1); + + for (p = bsdtar->archive_dir->head; p != NULL; p = p->next) { + if (strcmp(path, p->name)==0) + return (p->mtime_sec < mtime_sec || + (p->mtime_sec == mtime_sec && + p->mtime_nsec < mtime_nsec)); + } + return (1); +} + +/* + * Add an entry to the dir list for 'u' mode. + * + * XXX TODO: Make this fast. + */ +static void +add_dir_list(struct bsdtar *bsdtar, const char *path, + time_t mtime_sec, int mtime_nsec) +{ + struct archive_dir_entry *p; + + if (path[0] == '.' && path[1] == '/' && path[2] != '\0') + path += 2; + + /* + * Search entire list to see if this file has appeared before. + * If it has, override the timestamp data. + */ + p = bsdtar->archive_dir->head; + while (p != NULL) { + if (strcmp(path, p->name)==0) { + p->mtime_sec = mtime_sec; + p->mtime_nsec = mtime_nsec; + return; + } + p = p->next; + } + + p = malloc(sizeof(*p)); + if (p == NULL) + bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory"); + + p->name = strdup(path); + if (p->name == NULL) + bsdtar_errc(bsdtar, 1, ENOMEM, "Can't read archive directory"); + p->mtime_sec = mtime_sec; + p->mtime_nsec = mtime_nsec; + p->next = NULL; + if (bsdtar->archive_dir->tail == NULL) { + bsdtar->archive_dir->head = bsdtar->archive_dir->tail = p; + } else { + bsdtar->archive_dir->tail->next = p; + bsdtar->archive_dir->tail = p; + } +} + +void +test_for_append(struct bsdtar *bsdtar) +{ + struct stat s; + + if (*bsdtar->argv == NULL) + bsdtar_errc(bsdtar, 1, 0, "no files or directories specified"); + if (bsdtar->filename == NULL) + bsdtar_errc(bsdtar, 1, 0, "Cannot append to stdout."); + + if (bsdtar->create_compression != 0) + bsdtar_errc(bsdtar, 1, 0, + "Cannot append to %s with compression", bsdtar->filename); + + if (stat(bsdtar->filename, &s) != 0) + bsdtar_errc(bsdtar, 1, errno, + "Cannot stat %s", bsdtar->filename); + + if (!S_ISREG(s.st_mode)) + bsdtar_errc(bsdtar, 1, 0, + "Cannot append to %s: not a regular file.", + bsdtar->filename); +} -- 2.41.0