]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Add path fallback in tar 1632/head
authorWalter Lozano <walter.lozano@collabora.com>
Sat, 27 Nov 2021 00:23:20 +0000 (21:23 -0300)
committerWalter Lozano <walter.lozano@collabora.com>
Fri, 3 Dec 2021 14:55:09 +0000 (11:55 -0300)
Since current tar defaults to tape devices that are rare nowadays add an
additional step to fallback to "-" if tape devices are not found.

This is a clean way to have a default to "-" on those systems that tape
devices are not present while keeping the current behavior for other
cases. Additionally prepare for future releases where this kind of defaults
will be dropped.

Signed-off-by: Walter Lozano <walter.lozano@collabora.com>
tar/bsdtar.c

index df0930ae09c2aeb01915f871881fbfe1a0949452..521b6a43d5ae179548ce6942b9d54ed9f4ea7008 100644 (file)
@@ -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;