diff options
author | Tim Kientzle <kientzle@acm.org> | 2021-12-04 10:56:33 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-04 10:56:33 -0800 |
commit | fdb493d9789c3426d603d15d0c438e4b273f4ec8 (patch) | |
tree | ea531e45504d15fb3cdc5f6de559cb4551a916eb /tar/bsdtar.c | |
parent | 56c920eab3352f7877ee0cf9e472c1ab376c7e3e (diff) | |
parent | b6b423f06c4b8389493726be2c83ce8675be5354 (diff) | |
download | libarchive-fdb493d9789c3426d603d15d0c438e4b273f4ec8.tar.gz |
Merge pull request #1632 from wlozano0collabora/default-archive-file
Have `bsdtar` default to stdout if this system has no tape device. This uses an `access()` check to see if the default tape device (e.g., `/dev/tape` on FreeBSD) exists and will use stdout as the default if it doesn't exist. If the system does have a tape device, there is no change to the existing behavior.
For libarchive 4.0, we'll change the default behavior of `bsdtar`:
* The `TAPE` environment variable will still be honored at runtime
* The `_PATH_DEFTAPE` preprocessor macro will still be honored at build time
* But `_PATH_DEFTAPE` will no longer be set by libarchive's default build, with the effect that for most people, bsdtar will default to stdout if there is no `-f` option provided.
Diffstat (limited to 'tar/bsdtar.c')
-rw-r--r-- | tar/bsdtar.c | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/tar/bsdtar.c b/tar/bsdtar.c index 8a88f882..b55b4c6c 100644 --- a/tar/bsdtar.c +++ b/tar/bsdtar.c @@ -70,24 +70,20 @@ __FBSDID("$FreeBSD: src/usr.bin/tar/bsdtar.c,v 1.93 2008/11/08 04:43:24 kientzle #include "bsdtar.h" #include "err.h" -/* - * Per POSIX.1-1988, tar defaults to reading/writing archives to/from - * the default tape device for the system. Pick something reasonable here. - */ -#ifdef __linux -#define _PATH_DEFTAPE "/dev/st0" +#if ARCHIVE_VERSION_NUMBER < 4000000 && !defined(_PATH_DEFTAPE) +// Libarchive 4.0 and later will NOT define _PATH_DEFTAPE +// but will honor it if it's set in the build. +// Until then, we'll continue to set it by default on certain platforms: +#if defined(__linux) +#define _PATH_DEFTAPE "/dev/st0" +#elif defined(_WIN32) && !defined(__CYGWIN__) +#define _PATH_DEFTAPE "\\\\.\\tape0" +#elif !defined(__APPLE__) +#define _PATH_DEFTAPE "/dev/tape" #endif -#if defined(_WIN32) && !defined(__CYGWIN__) -#define _PATH_DEFTAPE "\\\\.\\tape0" -#endif -#if defined(__APPLE__) -#undef _PATH_DEFTAPE -#define _PATH_DEFTAPE "-" /* Mac OS has no tape support, default to stdio. */ #endif -#ifndef _PATH_DEFTAPE -#define _PATH_DEFTAPE "/dev/tape" -#endif +#define _PATH_STDIO "-" #ifdef __MINGW32__ int _CRT_glob = 0; /* Disable broken CRT globbing. */ @@ -217,8 +213,21 @@ main(int argc, char **argv) /* Default: open tape drive. */ bsdtar->filename = getenv("TAPE"); - if (bsdtar->filename == NULL) - bsdtar->filename = _PATH_DEFTAPE; +#if defined(_PATH_DEFTAPE) + if (bsdtar->filename == NULL) { +#if defined(_WIN32) && !defined(__CYGWIN__) + int tapeExists = _access(_PATH_DEFTAPE, 0); +#else + int tapeExists = access(_PATH_DEFTAPE, F_OK); +#endif + if (tapeExists) { + bsdtar->filename = _PATH_DEFTAPE; + } + } +#endif + if (bsdtar->filename == NULL) { + bsdtar->filename = _PATH_STDIO; + } /* Default block size settings. */ bsdtar->bytes_per_block = DEFAULT_BYTES_PER_BLOCK; |