git: e64fe029e9d3 - main - libarchive: merge from vendor branch

From: Martin Matuska <mm_at_FreeBSD.org>
Date: Mon, 24 Jul 2023 06:07:39 UTC
The branch main has been updated by mm:

URL: https://cgit.FreeBSD.org/src/commit/?id=e64fe029e9d3ce476e77a478318e0c3cd201ff08

commit e64fe029e9d3ce476e77a478318e0c3cd201ff08
Merge: 7f9318a022ef 14b646f7c3f7
Author:     Martin Matuska <mm@FreeBSD.org>
AuthorDate: 2023-07-24 05:42:43 +0000
Commit:     Martin Matuska <mm@FreeBSD.org>
CommitDate: 2023-07-24 05:42:43 +0000

    libarchive: merge from vendor branch
    
    Libarchive 3.7.0
    
    Important changes (relevant to FreeBSD):
      #1814 Do not account for NULL terminator when comparing with "TRAILER!!!"
      #1818 Add ability to produce multi-frame zstd archives
      #1840 year 2038 fix for pax archives on platforms with 64-bit time_t
      #1860 Make single bit bitfields unsigned to avoid clang 16 warning
      #1869 Fix FreeBSD builds with WARNS=6
      #1873 bsdunzip ported to libarchive from FreeBSD
      #1894 read support for zstd compression in 7zip archives
      #1918 ARM64 filter support in 7zip archives
    
    MFC after:      2 weeks
    PR:             272567 (exp-run)

 contrib/libarchive/NEWS                            |    6 +-
 contrib/libarchive/README.md                       |   16 +-
 contrib/libarchive/SECURITY.md                     |   19 +
 contrib/libarchive/cpio/cpio.c                     |   19 +-
 contrib/libarchive/cpio/test/test_option_J_upper.c |    1 +
 contrib/libarchive/cpio/test/test_option_c.c       |    6 +-
 contrib/libarchive/cpio/test/test_option_t.c       |   17 +-
 contrib/libarchive/libarchive/archive.h            |    4 +-
 contrib/libarchive/libarchive/archive_digest.c     |   60 +
 .../libarchive/libarchive/archive_digest_private.h |   10 +
 contrib/libarchive/libarchive/archive_entry.h      |    2 +-
 contrib/libarchive/libarchive/archive_getdate.c    |  119 +-
 contrib/libarchive/libarchive/archive_hmac.c       |   15 +-
 .../libarchive/libarchive/archive_hmac_private.h   |    2 +
 .../libarchive/archive_openssl_evp_private.h       |    3 +-
 contrib/libarchive/libarchive/archive_random.c     |   35 +-
 .../libarchive/archive_read_data_into_fd.c         |    7 +-
 .../libarchive/archive_read_disk_posix.c           |   28 +
 .../libarchive/libarchive/archive_read_open_file.c |    4 +-
 .../libarchive/archive_read_set_options.3          |   21 +
 .../libarchive/archive_read_support_filter_zstd.c  |   14 +-
 .../libarchive/archive_read_support_format_7zip.c  |  196 +++-
 .../libarchive/archive_read_support_format_cab.c   |    8 +-
 .../libarchive/archive_read_support_format_cpio.c  |    6 +-
 .../archive_read_support_format_iso9660.c          |   12 +-
 .../libarchive/archive_read_support_format_lha.c   |   16 +-
 .../libarchive/archive_read_support_format_mtree.c |    8 +-
 .../libarchive/archive_read_support_format_rar.c   |   17 +-
 .../libarchive/archive_read_support_format_warc.c  |    6 +-
 .../libarchive/archive_read_support_format_xar.c   |    8 +-
 .../libarchive/archive_read_support_format_zip.c   |    2 +-
 contrib/libarchive/libarchive/archive_string.c     |    4 +
 contrib/libarchive/libarchive/archive_util.c       |   57 +-
 contrib/libarchive/libarchive/archive_write.c      |   31 +
 .../libarchive/archive_write_add_filter_compress.c |    2 +-
 .../libarchive/archive_write_add_filter_zstd.c     |  233 ++--
 .../libarchive/archive_write_disk_posix.c          |   65 +-
 .../libarchive/libarchive/archive_write_private.h  |    1 +
 .../libarchive/archive_write_set_format_iso9660.c  |   18 +-
 .../libarchive/archive_write_set_format_pax.c      |   27 +-
 .../libarchive/archive_write_set_format_warc.c     |   25 +-
 .../libarchive/archive_write_set_format_xar.c      |   10 +-
 .../libarchive/archive_write_set_format_zip.c      |   19 +-
 contrib/libarchive/libarchive/test/test_fuzz.c     |    5 +-
 .../test/test_read_disk_directory_traversals.c     |    9 +-
 .../libarchive/test/test_read_format_7zip.c        |  346 ++++++
 .../test/test_read_format_7zip_deflate_arm64.7z.uu |   64 ++
 .../test/test_read_format_7zip_lzma2_arm.7z.uu     |   50 +
 .../test/test_read_format_7zip_lzma2_arm64.7z.uu   |   54 +
 .../test/test_read_format_7zip_solid_zstd.7z.uu    |    9 +
 .../test/test_read_format_7zip_zstd.7z.uu          |   12 +
 .../test/test_read_format_7zip_zstd_arm.7z.uu      |   61 +
 .../test/test_read_format_7zip_zstd_bcj.7z.uu      |   56 +
 .../test/test_read_format_7zip_zstd_nobcj.7z.uu    |   56 +
 .../libarchive/test/test_read_format_rar5.c        |    2 +
 .../libarchive/test/test_read_format_zip.c         |    5 +
 .../libarchive/libarchive/test/test_short_writes.c |    2 +
 .../libarchive/test/test_write_filter_zstd.c       |   27 +
 .../test/test_write_format_zip_compression_store.c |   17 +-
 .../libarchive/test/test_write_format_zip_file.c   |   17 +-
 .../test/test_write_format_zip_file_zip64.c        |   17 +-
 contrib/libarchive/libarchive/xxhash.c             |    4 +
 contrib/libarchive/libarchive_fe/passphrase.c      |    6 +
 contrib/libarchive/tar/bsdtar.1                    |   15 +-
 contrib/libarchive/tar/test/test_option_lzma.c     |    1 +
 contrib/libarchive/tar/util.c                      |   17 +-
 contrib/libarchive/test_utils/test_main.c          |   29 +-
 contrib/libarchive/unzip/CMakeLists.txt            |   37 +
 contrib/libarchive/unzip/bsdunzip.1                |  216 ++++
 contrib/libarchive/unzip/bsdunzip.c                | 1186 ++++++++++++++++++++
 contrib/libarchive/unzip/bsdunzip_platform.h       |   83 ++
 contrib/libarchive/unzip/test/CMakeLists.txt       |   80 ++
 contrib/libarchive/unzip/test/test.h               |   40 +
 contrib/libarchive/unzip/test/test_0.c             |   58 +
 contrib/libarchive/unzip/test/test_C.c             |   41 +
 contrib/libarchive/unzip/test/test_L.c             |   44 +
 contrib/libarchive/unzip/test/test_P_encryption.c  |   41 +
 contrib/libarchive/unzip/test/test_Z1.c            |   40 +
 contrib/libarchive/unzip/test/test_basic.c         |   44 +
 contrib/libarchive/unzip/test/test_basic.zip.uu    |   25 +
 contrib/libarchive/unzip/test/test_d.c             |   44 +
 .../libarchive/unzip/test/test_encrypted.zip.uu    |   13 +
 contrib/libarchive/unzip/test/test_glob.c          |   44 +
 contrib/libarchive/unzip/test/test_j.c             |   44 +
 contrib/libarchive/unzip/test/test_n.c             |   48 +
 contrib/libarchive/unzip/test/test_not_exist.c     |   36 +
 contrib/libarchive/unzip/test/test_o.c             |   47 +
 contrib/libarchive/unzip/test/test_p.c             |   39 +
 contrib/libarchive/unzip/test/test_q.c             |   44 +
 contrib/libarchive/unzip/test/test_singlefile.c    |   41 +
 .../libarchive/unzip/test/test_singlefile.zip.uu   |    8 +
 contrib/libarchive/unzip/test/test_t.c             |   39 +
 contrib/libarchive/unzip/test/test_t_bad.c         |   39 +
 contrib/libarchive/unzip/test/test_t_bad.zip.uu    |   25 +
 contrib/libarchive/unzip/test/test_x.c             |   44 +
 lib/libarchive/tests/Makefile                      |    8 +
 96 files changed, 4122 insertions(+), 436 deletions(-)

diff --cc contrib/libarchive/README.md
index 404076237871,000000000000..727ed49856b6
mode 100644,000000..100644
--- a/contrib/libarchive/README.md
+++ b/contrib/libarchive/README.md
@@@ -1,232 -1,0 +1,244 @@@
 +# Welcome to libarchive!
 +
 +The libarchive project develops a portable, efficient C library that
 +can read and write streaming archives in a variety of formats.  It
 +also includes implementations of the common `tar`, `cpio`, and `zcat`
 +command-line tools that use the libarchive library.
 +
 +## Questions?  Issues?
 +
- * http://www.libarchive.org is the home for ongoing
++* https://www.libarchive.org is the home for ongoing
 +  libarchive development, including documentation,
 +  and links to the libarchive mailing lists.
 +* To report an issue, use the issue tracker at
 +  https://github.com/libarchive/libarchive/issues
 +* To submit an enhancement to libarchive, please
 +  submit a pull request via GitHub: https://github.com/libarchive/libarchive/pulls
 +
 +## Contents of the Distribution
 +
 +This distribution bundle includes the following major components:
 +
 +* **libarchive**: a library for reading and writing streaming archives
 +* **tar**: the 'bsdtar' program is a full-featured 'tar' implementation built on libarchive
 +* **cpio**: the 'bsdcpio' program is a different interface to essentially the same functionality
 +* **cat**: the 'bsdcat' program is a simple replacement tool for zcat, bzcat, xzcat, and such
++* **unzip**: the 'bsdunzip' program is a simple replacement tool for Info-ZIP's unzip
 +* **examples**: Some small example programs that you may find useful.
 +* **examples/minitar**: a compact sample demonstrating use of libarchive.
 +* **contrib**:  Various items sent to me by third parties; please contact the authors with any questions.
 +
 +The top-level directory contains the following information files:
 +
 +* **NEWS** - highlights of recent changes
 +* **COPYING** - what you can do with this
 +* **INSTALL** - installation instructions
 +* **README** - this file
 +* **CMakeLists.txt** - input for "cmake" build tool, see INSTALL
 +* **configure** - configuration script, see INSTALL for details.  If your copy of the source lacks a `configure` script, you can try to construct it by running the script in `build/autogen.sh` (or use `cmake`).
 +
 +The following files in the top-level directory are used by the 'configure' script:
 +
 +* `Makefile.am`, `aclocal.m4`, `configure.ac` - used to build this distribution, only needed by maintainers
 +* `Makefile.in`, `config.h.in` - templates used by configure script
 +
 +## Documentation
 +
 +In addition to the informational articles and documentation
 +in the online [libarchive Wiki](https://github.com/libarchive/libarchive/wiki),
 +the distribution also includes a number of manual pages:
 +
 + * bsdtar.1 explains the use of the bsdtar program
 + * bsdcpio.1 explains the use of the bsdcpio program
 + * bsdcat.1 explains the use of the bsdcat program
 + * libarchive.3 gives an overview of the library as a whole
 + * archive_read.3, archive_write.3, archive_write_disk.3, and
 +   archive_read_disk.3 provide detailed calling sequences for the read
 +   and write APIs
 + * archive_entry.3 details the "struct archive_entry" utility class
 + * archive_internals.3 provides some insight into libarchive's
 +   internal structure and operation.
 + * libarchive-formats.5 documents the file formats supported by the library
 + * cpio.5, mtree.5, and tar.5 provide detailed information about these
 +   popular archive formats, including hard-to-find details about
 +   modern cpio and tar variants.
 +
 +The manual pages above are provided in the 'doc' directory in
 +a number of different formats.
 +
 +You should also read the copious comments in `archive.h` and the
 +source code for the sample programs for more details.  Please let us
 +know about any errors or omissions you find.
 +
 +## Supported Formats
 +
 +Currently, the library automatically detects and reads the following formats:
 +
 +  * Old V7 tar archives
 +  * POSIX ustar
 +  * GNU tar format (including GNU long filenames, long link names, and sparse files)
 +  * Solaris 9 extended tar format (including ACLs)
 +  * POSIX pax interchange format
 +  * POSIX octet-oriented cpio
 +  * SVR4 ASCII cpio
 +  * Binary cpio (big-endian or little-endian)
 +  * PWB binary cpio
 +  * ISO9660 CD-ROM images (with optional Rockridge or Joliet extensions)
 +  * ZIP archives (with uncompressed or "deflate" compressed entries, including support for encrypted Zip archives)
 +  * ZIPX archives (with support for bzip2, ppmd8, lzma and xz compressed entries)
 +  * GNU and BSD 'ar' archives
 +  * 'mtree' format
-   * 7-Zip archives
++  * 7-Zip archives (including archives that use zstandard compression)
 +  * Microsoft CAB format
 +  * LHA and LZH archives
 +  * RAR and RAR 5.0 archives (with some limitations due to RAR's proprietary status)
 +  * XAR archives
 +
 +The library also detects and handles any of the following before evaluating the archive:
 +
 +  * uuencoded files
 +  * files with RPM wrapper
 +  * gzip compression
 +  * bzip2 compression
 +  * compress/LZW compression
 +  * lzma, lzip, and xz compression
 +  * lz4 compression
 +  * lzop compression
 +  * zstandard compression
 +
 +The library can create archives in any of the following formats:
 +
 +  * POSIX ustar
 +  * POSIX pax interchange format
 +  * "restricted" pax format, which will create ustar archives except for
 +    entries that require pax extensions (for long filenames, ACLs, etc).
 +  * Old GNU tar format
 +  * Old V7 tar format
 +  * POSIX octet-oriented cpio
 +  * SVR4 "newc" cpio
 +  * Binary cpio (little-endian)
 +  * PWB binary cpio
 +  * shar archives
 +  * ZIP archives (with uncompressed or "deflate" compressed entries)
 +  * GNU and BSD 'ar' archives
 +  * 'mtree' format
 +  * ISO9660 format
 +  * 7-Zip archives
 +  * XAR archives
 +
 +When creating archives, the result can be filtered with any of the following:
 +
 +  * uuencode
 +  * gzip compression
 +  * bzip2 compression
 +  * compress/LZW compression
 +  * lzma, lzip, and xz compression
 +  * lz4 compression
 +  * lzop compression
 +  * zstandard compression
 +
 +## Notes about the Library Design
 +
 +The following notes address many of the most common
 +questions we are asked about libarchive:
 +
 +* This is a heavily stream-oriented system.  That means that
 +  it is optimized to read or write the archive in a single
 +  pass from beginning to end.  For example, this allows
 +  libarchive to process archives too large to store on disk
 +  by processing them on-the-fly as they are read from or
 +  written to a network or tape drive.  This also makes
 +  libarchive useful for tools that need to produce
 +  archives on-the-fly (such as webservers that provide
 +  archived contents of a users account).
 +
 +* In-place modification and random access to the contents
 +  of an archive are not directly supported.  For some formats,
 +  this is not an issue: For example, tar.gz archives are not
 +  designed for random access.  In some other cases, libarchive
 +  can re-open an archive and scan it from the beginning quickly
 +  enough to provide the needed abilities even without true
 +  random access.  Of course, some applications do require true
 +  random access; those applications should consider alternatives
 +  to libarchive.
 +
 +* The library is designed to be extended with new compression and
 +  archive formats.  The only requirement is that the format be
 +  readable or writable as a stream and that each archive entry be
 +  independent.  There are articles on the libarchive Wiki explaining
 +  how to extend libarchive.
 +
 +* On read, compression and format are always detected automatically.
 +
 +* The same API is used for all formats; it should be very
 +  easy for software using libarchive to transparently handle
 +  any of libarchive's archiving formats.
 +
 +* Libarchive's automatic support for decompression can be used
 +  without archiving by explicitly selecting the "raw" and "empty"
 +  formats.
 +
 +* I've attempted to minimize static link pollution.  If you don't
 +  explicitly invoke a particular feature (such as support for a
 +  particular compression or format), it won't get pulled in to
 +  statically-linked programs.  In particular, if you don't explicitly
 +  enable a particular compression or decompression support, you won't
 +  need to link against the corresponding compression or decompression
 +  libraries.  This also reduces the size of statically-linked
 +  binaries in environments where that matters.
 +
 +* The library is generally _thread safe_ depending on the platform:
 +  it does not define any global variables of its own.  However, some
 +  platforms do not provide fully thread-safe versions of key C library
 +  functions.  On those platforms, libarchive will use the non-thread-safe
 +  functions.  Patches to improve this are of great interest to us.
 +
++* The function `archive_write_disk_header()` is _not_ thread safe on
++  POSIX machines and could lead to security issue resulting in world
++  writeable directories.  Thus it must be mutexed by the calling code.
++  This is due to calling `umask(oldumask = umask(0))`, which sets the
++  umask for the whole process to 0 for a short time frame.
++  In case other thread calls the same function in parallel, it might
++  get interrupted by it and cause the executable to use umask=0 for the
++  remaining execution.
++  This will then lead to implicitely created directories to have 777
++  permissions without sticky bit.
++
 +* In particular, libarchive's modules to read or write a directory
 +  tree do use `chdir()` to optimize the directory traversals.  This
 +  can cause problems for programs that expect to do disk access from
 +  multiple threads.  Of course, those modules are completely
 +  optional and you can use the rest of libarchive without them.
 +
 +* The library is _not_ thread aware, however.  It does no locking
 +  or thread management of any kind.  If you create a libarchive
 +  object and need to access it from multiple threads, you will
 +  need to provide your own locking.
 +
 +* On read, the library accepts whatever blocks you hand it.
 +  Your read callback is free to pass the library a byte at a time
 +  or mmap the entire archive and give it to the library at once.
 +  On write, the library always produces correctly-blocked output.
 +
 +* The object-style approach allows you to have multiple archive streams
 +  open at once.  bsdtar uses this in its "@archive" extension.
 +
 +* The archive itself is read/written using callback functions.
 +  You can read an archive directly from an in-memory buffer or
 +  write it to a socket, if you wish.  There are some utility
 +  functions to provide easy-to-use "open file," etc, capabilities.
 +
 +* The read/write APIs are designed to allow individual entries
 +  to be read or written to any data source:  You can create
 +  a block of data in memory and add it to a tar archive without
 +  first writing a temporary file.  You can also read an entry from
 +  an archive and write the data directly to a socket.  If you want
 +  to read/write entries to disk, there are convenience functions to
 +  make this especially easy.
 +
 +* Note: The "pax interchange format" is a POSIX standard extended tar
 +  format that should be used when the older _ustar_ format is not
 +  appropriate.  It has many advantages over other tar formats
 +  (including the legacy GNU tar format) and is widely supported by
 +  current tar implementations.
 +
diff --cc contrib/libarchive/SECURITY.md
index 000000000000,6ca188b603fe..6ca188b603fe
mode 000000,100644..100644
--- a/contrib/libarchive/SECURITY.md
+++ b/contrib/libarchive/SECURITY.md
diff --cc contrib/libarchive/cpio/cpio.c
index 847c92d31449,000000000000..fbeae4133091
mode 100644,000000..100644
--- a/contrib/libarchive/cpio/cpio.c
+++ b/contrib/libarchive/cpio/cpio.c
@@@ -1,1517 -1,0 +1,1510 @@@
 +/*-
 + * Copyright (c) 2003-2007 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 "cpio_platform.h"
 +__FBSDID("$FreeBSD$");
 +
 +#include <sys/types.h>
 +#include <archive.h>
 +#include <archive_entry.h>
 +
 +#ifdef HAVE_SYS_MKDEV_H
 +#include <sys/mkdev.h>
 +#endif
 +#ifdef HAVE_SYS_STAT_H
 +#include <sys/stat.h>
 +#endif
 +#ifdef HAVE_SYS_TIME_H
 +#include <sys/time.h>
 +#endif
 +#ifdef HAVE_ERRNO_H
 +#include <errno.h>
 +#endif
 +#ifdef HAVE_FCNTL_H
 +#include <fcntl.h>
 +#endif
 +#ifdef HAVE_GRP_H
 +#include <grp.h>
 +#endif
 +#ifdef HAVE_LOCALE_H
 +#include <locale.h>
 +#endif
 +#ifdef HAVE_PWD_H
 +#include <pwd.h>
 +#endif
 +#ifdef HAVE_SIGNAL_H
 +#include <signal.h>
 +#endif
 +#ifdef HAVE_STDARG_H
 +#include <stdarg.h>
 +#endif
 +#ifdef HAVE_STDINT_H
 +#include <stdint.h>
 +#endif
 +#include <stdio.h>
 +#ifdef HAVE_STDLIB_H
 +#include <stdlib.h>
 +#endif
 +#ifdef HAVE_STRING_H
 +#include <string.h>
 +#endif
 +#ifdef HAVE_UNISTD_H
 +#include <unistd.h>
 +#endif
 +#ifdef HAVE_TIME_H
 +#include <time.h>
 +#endif
 +
 +#include "cpio.h"
 +#include "err.h"
 +#include "line_reader.h"
 +#include "passphrase.h"
 +
 +/* Fixed size of uname/gname caches. */
 +#define	name_cache_size 101
 +
 +#ifndef O_BINARY
 +#define O_BINARY 0
 +#endif
 +
 +struct name_cache {
 +	int	probes;
 +	int	hits;
 +	size_t	size;
 +	struct {
 +		id_t id;
 +		char *name;
 +	} cache[name_cache_size];
 +};
 +
 +static int	extract_data(struct archive *, struct archive *);
 +const char *	cpio_i64toa(int64_t);
 +static const char *cpio_rename(const char *name);
 +static int	entry_to_archive(struct cpio *, struct archive_entry *);
 +static int	file_to_archive(struct cpio *, const char *);
 +static void	free_cache(struct name_cache *cache);
 +static void	list_item_verbose(struct cpio *, struct archive_entry *);
 +static void	long_help(void) __LA_DEAD;
 +static const char *lookup_gname(struct cpio *, gid_t gid);
 +static int	lookup_gname_helper(struct cpio *,
 +		    const char **name, id_t gid);
 +static const char *lookup_uname(struct cpio *, uid_t uid);
 +static int	lookup_uname_helper(struct cpio *,
 +		    const char **name, id_t uid);
 +static void	mode_in(struct cpio *) __LA_DEAD;
 +static void	mode_list(struct cpio *) __LA_DEAD;
 +static void	mode_out(struct cpio *);
 +static void	mode_pass(struct cpio *, const char *);
 +static const char *remove_leading_slash(const char *);
 +static int	restore_time(struct cpio *, struct archive_entry *,
 +		    const char *, int fd);
 +static void	usage(void) __LA_DEAD;
 +static void	version(void) __LA_DEAD;
 +static const char * passphrase_callback(struct archive *, void *);
 +static void	passphrase_free(char *);
 +
 +int
 +main(int argc, char *argv[])
 +{
 +	static char buff[16384];
 +	struct cpio _cpio; /* Allocated on stack. */
 +	struct cpio *cpio;
 +	const char *errmsg;
 +	char *tptr;
 +	int uid, gid;
 +	int opt, t;
 +
 +	cpio = &_cpio;
 +	memset(cpio, 0, sizeof(*cpio));
 +	cpio->buff = buff;
 +	cpio->buff_size = sizeof(buff);
 +
 +#if defined(HAVE_SIGACTION) && defined(SIGPIPE)
 +	{ /* Ignore SIGPIPE signals. */
 +		struct sigaction sa;
 +		sigemptyset(&sa.sa_mask);
 +		sa.sa_flags = 0;
 +		sa.sa_handler = SIG_IGN;
 +		sigaction(SIGPIPE, &sa, NULL);
 +	}
 +#endif
 +
 +	/* Set lafe_progname before calling lafe_warnc. */
 +	lafe_setprogname(*argv, "bsdcpio");
 +
 +#if HAVE_SETLOCALE
 +	if (setlocale(LC_ALL, "") == NULL)
 +		lafe_warnc(0, "Failed to set default locale");
 +#endif
 +
 +	cpio->uid_override = -1;
 +	cpio->gid_override = -1;
 +	cpio->argv = argv;
 +	cpio->argc = argc;
 +	cpio->mode = '\0';
 +	cpio->verbose = 0;
 +	cpio->compress = '\0';
 +	cpio->extract_flags = ARCHIVE_EXTRACT_NO_AUTODIR;
 +	cpio->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
 +	cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS;
 +	cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT;
 +	cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
 +	cpio->extract_flags |= ARCHIVE_EXTRACT_PERM;
 +	cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS;
 +	cpio->extract_flags |= ARCHIVE_EXTRACT_ACL;
 +#if !defined(_WIN32) && !defined(__CYGWIN__)
 +	if (geteuid() == 0)
 +		cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER;
 +#endif
 +	cpio->bytes_per_block = 512;
 +	cpio->filename = NULL;
 +
 +	cpio->matching = archive_match_new();
 +	if (cpio->matching == NULL)
 +		lafe_errc(1, 0, "Out of memory");
 +
 +	while ((opt = cpio_getopt(cpio)) != -1) {
 +		switch (opt) {
 +		case '0': /* GNU convention: --null, -0 */
 +			cpio->option_null = 1;
 +			break;
 +		case '6': /* in/out: assume/create 6th edition (PWB) format */
 +			cpio->option_pwb = 1;
 +			break;
 +		case '7': /* out: create archive using 7th Edition binary format */
 +			cpio->format = "bin";
 +			break;
 +		case 'A': /* NetBSD/OpenBSD */
 +			cpio->option_append = 1;
 +			break;
 +		case 'a': /* POSIX 1997 */
 +			cpio->option_atime_restore = 1;
 +			break;
 +		case 'B': /* POSIX 1997 */
 +			cpio->bytes_per_block = 5120;
 +			break;
 +		case OPTION_B64ENCODE:
 +			cpio->add_filter = opt;
 +			break;
 +		case 'C': /* NetBSD/OpenBSD */
 +			errno = 0;
 +			tptr = NULL;
 +			t = (int)strtol(cpio->argument, &tptr, 10);
 +			if (errno || t <= 0 || *(cpio->argument) == '\0' ||
 +			    tptr == NULL || *tptr != '\0') {
 +				lafe_errc(1, 0, "Invalid blocksize: %s",
 +				    cpio->argument);
 +			}
 +			cpio->bytes_per_block = t;
 +			break;
 +		case 'c': /* POSIX 1997 */
 +			cpio->format = "odc";
 +			break;
 +		case 'd': /* POSIX 1997 */
 +			cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR;
 +			break;
 +		case 'E': /* NetBSD/OpenBSD */
 +			if (archive_match_include_pattern_from_file(
 +			    cpio->matching, cpio->argument,
 +			    cpio->option_null) != ARCHIVE_OK)
 +				lafe_errc(1, 0, "Error : %s",
 +				    archive_error_string(cpio->matching));
 +			break;
 +		case 'F': /* NetBSD/OpenBSD/GNU cpio */
 +			cpio->filename = cpio->argument;
 +			break;
 +		case 'f': /* POSIX 1997 */
 +			if (archive_match_exclude_pattern(cpio->matching,
 +			    cpio->argument) != ARCHIVE_OK)
 +				lafe_errc(1, 0, "Error : %s",
 +				    archive_error_string(cpio->matching));
 +			break;
 +		case OPTION_GRZIP:
 +			cpio->compress = opt;
 +			break;
 +		case 'H': /* GNU cpio (also --format) */
 +			cpio->format = cpio->argument;
 +			break;
 +		case 'h':
 +			long_help();
 +			break;
 +		case 'I': /* NetBSD/OpenBSD */
 +			cpio->filename = cpio->argument;
 +			break;
 +		case 'i': /* POSIX 1997 */
 +			if (cpio->mode != '\0')
 +				lafe_errc(1, 0,
 +				    "Cannot use both -i and -%c", cpio->mode);
 +			cpio->mode = opt;
 +			break;
 +		case 'J': /* GNU tar, others */
 +			cpio->compress = opt;
 +			break;
 +		case 'j': /* GNU tar, others */
 +			cpio->compress = opt;
 +			break;
 +		case OPTION_INSECURE:
 +			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS;
 +			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
 +			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
 +			break;
 +		case 'L': /* GNU cpio */
 +			cpio->option_follow_links = 1;
 +			break;
 +		case 'l': /* POSIX 1997 */
 +			cpio->option_link = 1;
 +			break;
 +		case OPTION_LRZIP:
 +		case OPTION_LZ4:
 +		case OPTION_LZMA: /* GNU tar, others */
 +		case OPTION_LZOP: /* GNU tar, others */
 +		case OPTION_ZSTD:
 +			cpio->compress = opt;
 +			break;
 +		case 'm': /* POSIX 1997 */
 +			cpio->extract_flags |= ARCHIVE_EXTRACT_TIME;
 +			break;
 +		case 'n': /* GNU cpio */
 +			cpio->option_numeric_uid_gid = 1;
 +			break;
 +		case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */
 +			cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER;
 +			break;
 +		case 'O': /* GNU cpio */
 +			cpio->filename = cpio->argument;
 +			break;
 +		case 'o': /* POSIX 1997 */
 +			if (cpio->mode != '\0')
 +				lafe_errc(1, 0,
 +				    "Cannot use both -o and -%c", cpio->mode);
 +			cpio->mode = opt;
 +			break;
 +		case 'p': /* POSIX 1997 */
 +			if (cpio->mode != '\0')
 +				lafe_errc(1, 0,
 +				    "Cannot use both -p and -%c", cpio->mode);
 +			cpio->mode = opt;
 +			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT;
 +			cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
 +			break;
 +		case OPTION_PASSPHRASE:
 +			cpio->passphrase = cpio->argument;
 +			break;
 +		case OPTION_PRESERVE_OWNER:
 +			cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER;
 +			break;
 +		case OPTION_QUIET: /* GNU cpio */
 +			cpio->quiet = 1;
 +			break;
 +		case 'R': /* GNU cpio, also --owner */
 +			/* TODO: owner_parse should return uname/gname
 +			 * also; use that to set [ug]name_override. */
 +			errmsg = owner_parse(cpio->argument, &uid, &gid);
 +			if (errmsg) {
 +				lafe_warnc(-1, "%s", errmsg);
 +				usage();
 +			}
 +			if (uid != -1) {
 +				cpio->uid_override = uid;
 +				cpio->uname_override = NULL;
 +			}
 +			if (gid != -1) {
 +				cpio->gid_override = gid;
 +				cpio->gname_override = NULL;
 +			}
 +			break;
 +		case 'r': /* POSIX 1997 */
 +			cpio->option_rename = 1;
 +			break;
 +		case 't': /* POSIX 1997 */
 +			cpio->option_list = 1;
 +			break;
 +		case 'u': /* POSIX 1997 */
 +			cpio->extract_flags
 +			    &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
 +			break;
 +		case OPTION_UUENCODE:
 +			cpio->add_filter = opt;
 +			break;
 +		case 'v': /* POSIX 1997 */
 +			cpio->verbose++;
 +			break;
 +		case 'V': /* GNU cpio */
 +			cpio->dot++;
 +			break;
 +		case OPTION_VERSION: /* GNU convention */
 +			version();
 +			break;
 +#if 0
 +	        /*
 +		 * cpio_getopt() handles -W specially, so it's not
 +		 * available here.
 +		 */
 +		case 'W': /* Obscure, but useful GNU convention. */
 +			break;
 +#endif
 +		case 'y': /* tar convention */
 +			cpio->compress = opt;
 +			break;
 +		case 'Z': /* tar convention */
 +			cpio->compress = opt;
 +			break;
 +		case 'z': /* tar convention */
 +			cpio->compress = opt;
 +			break;
 +		default:
 +			usage();
 +		}
 +	}
 +
 +	/*
 +	 * Sanity-check args, error out on nonsensical combinations.
 +	 */
 +	/* -t implies -i if no mode was specified. */
 +	if (cpio->option_list && cpio->mode == '\0')
 +		cpio->mode = 'i';
 +	/* -t requires -i */
 +	if (cpio->option_list && cpio->mode != 'i')
 +		lafe_errc(1, 0, "Option -t requires -i");
 +	/* -n requires -it */
 +	if (cpio->option_numeric_uid_gid && !cpio->option_list)
 +		lafe_errc(1, 0, "Option -n requires -it");
 +	/* Can only specify format when writing */
 +	if (cpio->format != NULL && cpio->mode != 'o')
 +		lafe_errc(1, 0, "Option --format requires -o");
 +	/* -l requires -p */
 +	if (cpio->option_link && cpio->mode != 'p')
 +		lafe_errc(1, 0, "Option -l requires -p");
 +	/* -v overrides -V */
 +	if (cpio->dot && cpio->verbose)
 +		cpio->dot = 0;
 +	/* TODO: Flag other nonsensical combinations. */
 +
 +	switch (cpio->mode) {
 +	case 'o':
 +		if (cpio->format == NULL) {
 +			if (cpio->option_pwb)
 +				cpio->format = "pwb";
 +			else
 +				cpio->format = "cpio";
 +		}
 +		mode_out(cpio);
 +		break;
 +	case 'i':
 +		while (*cpio->argv != NULL) {
 +			if (archive_match_include_pattern(cpio->matching,
 +			    *cpio->argv) != ARCHIVE_OK)
 +				lafe_errc(1, 0, "Error : %s",
 +				    archive_error_string(cpio->matching));
 +			--cpio->argc;
 +			++cpio->argv;
 +		}
 +		if (cpio->option_list)
 +			mode_list(cpio);
 +		else
 +			mode_in(cpio);
 +		break;
 +	case 'p':
 +		if (*cpio->argv == NULL || **cpio->argv == '\0')
 +			lafe_errc(1, 0,
 +			    "-p mode requires a target directory");
 +		mode_pass(cpio, *cpio->argv);
 +		break;
 +	default:
 +		lafe_errc(1, 0,
 +		    "Must specify at least one of -i, -o, or -p");
 +	}
 +
 +	archive_match_free(cpio->matching);
 +	free_cache(cpio->gname_cache);
 +	free_cache(cpio->uname_cache);
++	archive_read_close(cpio->archive_read_disk);
++	archive_read_free(cpio->archive_read_disk);
 +	free(cpio->destdir);
 +	passphrase_free(cpio->ppbuff);
 +	return (cpio->return_value);
 +}
 +
 +static void
 +usage(void)
 +{
 +	const char	*p;
 +
 +	p = lafe_getprogname();
 +
 +	fprintf(stderr, "Brief Usage:\n");
 +	fprintf(stderr, "  List:    %s -it < archive\n", p);
 +	fprintf(stderr, "  Extract: %s -i < archive\n", p);
 +	fprintf(stderr, "  Create:  %s -o < filenames > archive\n", p);
 +	fprintf(stderr, "  Help:    %s --help\n", p);
 +	exit(1);
 +}
 +
 +static const char *long_help_msg =
 +	"First option must be a mode specifier:\n"
 +	"  -i Input  -o Output  -p Pass\n"
 +	"Common Options:\n"
 +	"  -v Verbose filenames     -V  one dot per file\n"
 +	"Create: %p -o [options]  < [list of files] > [archive]\n"
 +	"  -J,-y,-z,--lzma  Compress archive with xz/bzip2/gzip/lzma\n"
 +	"  --format {pwb|bin|odc|newc|ustar}  Select archive format\n"
 +	"List: %p -it < [archive]\n"
 +	"Extract: %p -i [options] < [archive]\n";
 +
 +
 +/*
 + * Note that the word 'bsdcpio' will always appear in the first line
 + * of output.
 + *
 + * In particular, /bin/sh scripts that need to test for the presence
 + * of bsdcpio can use the following template:
 + *
 + * if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \
 + *          echo bsdcpio; else echo not bsdcpio; fi
 + */
 +static void
 +long_help(void)
 +{
 +	const char	*prog;
 +	const char	*p;
 +
 +	prog = lafe_getprogname();
 +
 +	fflush(stderr);
 +
 +	p = (strcmp(prog,"bsdcpio") != 0) ? "(bsdcpio)" : "";
 +	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);
 +	}
 +	version();
 +}
 +
 +static void
 +version(void)
 +{
 +	fprintf(stdout,"bsdcpio %s - %s \n",
 +	    BSDCPIO_VERSION_STRING,
 +	    archive_version_details());
 +	exit(0);
 +}
 +
 +static void
 +mode_out(struct cpio *cpio)
 +{
 +	struct archive_entry *entry, *spare;
 +	struct lafe_line_reader *lr;
 +	const char *p;
 +	int r;
 +
 +	if (cpio->option_append)
 +		lafe_errc(1, 0, "Append mode not yet supported.");
 +
 +	cpio->archive_read_disk = archive_read_disk_new();
 +	if (cpio->archive_read_disk == NULL)
 +		lafe_errc(1, 0, "Failed to allocate archive object");
 +	if (cpio->option_follow_links)
 +		archive_read_disk_set_symlink_logical(cpio->archive_read_disk);
 +	else
 +		archive_read_disk_set_symlink_physical(cpio->archive_read_disk);
 +	archive_read_disk_set_standard_lookup(cpio->archive_read_disk);
 +
 +	cpio->archive = archive_write_new();
 +	if (cpio->archive == NULL)
 +		lafe_errc(1, 0, "Failed to allocate archive object");
 +	switch (cpio->compress) {
 +	case OPTION_GRZIP:
 +		r = archive_write_add_filter_grzip(cpio->archive);
 +		break;
 +	case 'J':
 +		r = archive_write_add_filter_xz(cpio->archive);
 +		break;
 +	case OPTION_LRZIP:
 +		r = archive_write_add_filter_lrzip(cpio->archive);
 +		break;
 +	case OPTION_LZ4:
 +		r = archive_write_add_filter_lz4(cpio->archive);
 +		break;
 +	case OPTION_LZMA:
 +		r = archive_write_add_filter_lzma(cpio->archive);
 +		break;
 +	case OPTION_LZOP:
 +		r = archive_write_add_filter_lzop(cpio->archive);
 +		break;
 +	case OPTION_ZSTD:
 +		r = archive_write_add_filter_zstd(cpio->archive);
 +		break;
 +	case 'j': case 'y':
 +		r = archive_write_add_filter_bzip2(cpio->archive);
 +		break;
 +	case 'z':
 +		r = archive_write_add_filter_gzip(cpio->archive);
 +		break;
 +	case 'Z':
 +		r = archive_write_add_filter_compress(cpio->archive);
 +		break;
 +	default:
 +		r = archive_write_add_filter_none(cpio->archive);
 +		break;
 +	}
 +	if (r < ARCHIVE_WARN)
 +		lafe_errc(1, 0, "Requested compression not available");
 +	switch (cpio->add_filter) {
 +	case 0:
 +		r = ARCHIVE_OK;
 +		break;
 +	case OPTION_B64ENCODE:
 +		r = archive_write_add_filter_b64encode(cpio->archive);
 +		break;
 +	case OPTION_UUENCODE:
 +		r = archive_write_add_filter_uuencode(cpio->archive);
 +		break;
 +	}
 +	if (r < ARCHIVE_WARN)
 +		lafe_errc(1, 0, "Requested filter not available");
 +	r = archive_write_set_format_by_name(cpio->archive, cpio->format);
 +	if (r != ARCHIVE_OK)
*** 3223 LINES SKIPPED ***