2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2009-06-26 20:00:35 +02:00
|
|
|
/// \file file_io.c
|
2007-12-08 23:42:33 +01:00
|
|
|
/// \brief File opening, unlinking, and closing
|
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "private.h"
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
#include <fcntl.h>
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
#ifdef TUKLIB_DOSLIKE
|
2009-02-13 16:29:02 +01:00
|
|
|
# include <io.h>
|
2009-09-19 08:47:30 +02:00
|
|
|
#else
|
2013-06-28 21:51:02 +02:00
|
|
|
# include <poll.h>
|
2009-09-19 08:47:30 +02:00
|
|
|
static bool warn_fchown;
|
2009-02-13 16:29:02 +01:00
|
|
|
#endif
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMES)
|
|
|
|
# include <sys/time.h>
|
2016-06-30 19:27:36 +02:00
|
|
|
#elif defined(HAVE__FUTIME)
|
|
|
|
# include <sys/utime.h>
|
2008-11-19 19:46:52 +01:00
|
|
|
#elif defined(HAVE_UTIME)
|
|
|
|
# include <utime.h>
|
2007-12-08 23:42:33 +01:00
|
|
|
#endif
|
|
|
|
|
2015-03-31 21:19:34 +02:00
|
|
|
#ifdef HAVE_CAPSICUM
|
|
|
|
# ifdef HAVE_SYS_CAPSICUM_H
|
|
|
|
# include <sys/capsicum.h>
|
|
|
|
# else
|
|
|
|
# include <sys/capability.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
#include "tuklib_open_stdxxx.h"
|
|
|
|
|
2009-02-05 08:12:57 +01:00
|
|
|
#ifndef O_BINARY
|
|
|
|
# define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef O_NOCTTY
|
|
|
|
# define O_NOCTTY 0
|
|
|
|
#endif
|
|
|
|
|
2016-06-16 21:46:02 +02:00
|
|
|
// Using this macro to silence a warning from gcc -Wlogical-op.
|
|
|
|
#if EAGAIN == EWOULDBLOCK
|
|
|
|
# define IS_EAGAIN_OR_EWOULDBLOCK(e) ((e) == EAGAIN)
|
|
|
|
#else
|
|
|
|
# define IS_EAGAIN_OR_EWOULDBLOCK(e) \
|
|
|
|
((e) == EAGAIN || (e) == EWOULDBLOCK)
|
|
|
|
#endif
|
|
|
|
|
2009-02-05 08:12:57 +01:00
|
|
|
|
2013-07-04 13:18:46 +02:00
|
|
|
typedef enum {
|
|
|
|
IO_WAIT_MORE, // Reading or writing is possible.
|
|
|
|
IO_WAIT_ERROR, // Error or user_abort
|
|
|
|
IO_WAIT_TIMEOUT, // poll() timed out
|
|
|
|
} io_wait_ret;
|
|
|
|
|
|
|
|
|
2009-11-25 10:19:20 +01:00
|
|
|
/// If true, try to create sparse files when decompressing.
|
|
|
|
static bool try_sparse = true;
|
|
|
|
|
2015-03-31 21:19:34 +02:00
|
|
|
#ifdef ENABLE_SANDBOX
|
|
|
|
/// True if the conditions for sandboxing (described in main()) have been met.
|
|
|
|
static bool sandbox_allowed = false;
|
|
|
|
#endif
|
|
|
|
|
2009-12-07 20:46:53 +01:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2013-06-28 21:51:02 +02:00
|
|
|
/// File status flags of standard input. This is used by io_open_src()
|
|
|
|
/// and io_close_src().
|
|
|
|
static int stdin_flags;
|
|
|
|
static bool restore_stdin_flags = false;
|
|
|
|
|
2013-06-28 16:36:47 +02:00
|
|
|
/// Original file status flags of standard output. This is used by
|
|
|
|
/// io_open_dest() and io_close_dest() to save and restore the flags.
|
|
|
|
static int stdout_flags;
|
|
|
|
static bool restore_stdout_flags = false;
|
2013-06-28 22:48:05 +02:00
|
|
|
|
|
|
|
/// Self-pipe used together with the user_abort variable to avoid
|
|
|
|
/// race conditions with signal handling.
|
|
|
|
static int user_abort_pipe[2];
|
2009-12-07 20:46:53 +01:00
|
|
|
#endif
|
2009-11-25 10:19:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
static bool io_write_buf(file_pair *pair, const uint8_t *buf, size_t size);
|
|
|
|
|
|
|
|
|
2009-02-05 08:12:57 +01:00
|
|
|
extern void
|
|
|
|
io_init(void)
|
|
|
|
{
|
2011-04-12 10:59:49 +02:00
|
|
|
// Make sure that stdin, stdout, and stderr are connected to
|
2010-02-12 12:16:15 +01:00
|
|
|
// a valid file descriptor. Exit immediately with exit code ERROR
|
2009-02-05 08:12:57 +01:00
|
|
|
// if we cannot make the file descriptors valid. Maybe we should
|
|
|
|
// print an error message, but our stderr could be screwed anyway.
|
2009-09-19 08:47:30 +02:00
|
|
|
tuklib_open_stdxxx(E_ERROR);
|
2009-02-05 08:12:57 +01:00
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2009-02-05 08:12:57 +01:00
|
|
|
// If fchown() fails setting the owner, we warn about it only if
|
|
|
|
// we are root.
|
|
|
|
warn_fchown = geteuid() == 0;
|
2013-06-28 22:48:05 +02:00
|
|
|
|
2015-04-20 18:59:18 +02:00
|
|
|
// Create a pipe for the self-pipe trick.
|
2015-02-21 22:00:19 +01:00
|
|
|
if (pipe(user_abort_pipe))
|
2013-06-28 22:48:05 +02:00
|
|
|
message_fatal(_("Error creating a pipe: %s"),
|
|
|
|
strerror(errno));
|
2015-02-21 22:00:19 +01:00
|
|
|
|
|
|
|
// Make both ends of the pipe non-blocking.
|
|
|
|
for (unsigned i = 0; i < 2; ++i) {
|
|
|
|
int flags = fcntl(user_abort_pipe[i], F_GETFL);
|
|
|
|
if (flags == -1 || fcntl(user_abort_pipe[i], F_SETFL,
|
|
|
|
flags | O_NONBLOCK) == -1)
|
|
|
|
message_fatal(_("Error creating a pipe: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2009-02-05 08:12:57 +01:00
|
|
|
#endif
|
|
|
|
|
2009-02-13 16:29:02 +01:00
|
|
|
#ifdef __DJGPP__
|
|
|
|
// Avoid doing useless things when statting files.
|
|
|
|
// This isn't important but doesn't hurt.
|
2011-04-10 11:47:47 +02:00
|
|
|
_djstat_flags = _STAT_EXEC_EXT | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
|
2009-02-13 16:29:02 +01:00
|
|
|
#endif
|
|
|
|
|
2009-02-05 08:12:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2013-06-28 22:48:05 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
|
|
|
extern void
|
|
|
|
io_write_to_user_abort_pipe(void)
|
|
|
|
{
|
2013-07-01 13:34:11 +02:00
|
|
|
// If the write() fails, it's probably due to the pipe being full.
|
|
|
|
// Failing in that case is fine. If the reason is something else,
|
|
|
|
// there's not much we can do since this is called in a signal
|
|
|
|
// handler. So ignore the errors and try to avoid warnings with
|
|
|
|
// GCC and glibc when _FORTIFY_SOURCE=2 is used.
|
2013-06-28 22:48:05 +02:00
|
|
|
uint8_t b = '\0';
|
2013-07-01 13:34:11 +02:00
|
|
|
const int ret = write(user_abort_pipe[1], &b, 1);
|
|
|
|
(void)ret;
|
2013-06-28 22:48:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-11-25 10:19:20 +01:00
|
|
|
extern void
|
|
|
|
io_no_sparse(void)
|
|
|
|
{
|
|
|
|
try_sparse = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-31 21:19:34 +02:00
|
|
|
#ifdef ENABLE_SANDBOX
|
|
|
|
extern void
|
|
|
|
io_allow_sandbox(void)
|
|
|
|
{
|
|
|
|
sandbox_allowed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Enables operating-system-specific sandbox if it is possible.
|
|
|
|
/// src_fd is the file descriptor of the input file.
|
|
|
|
static void
|
|
|
|
io_sandbox_enter(int src_fd)
|
|
|
|
{
|
|
|
|
if (!sandbox_allowed) {
|
2020-02-05 19:33:50 +01:00
|
|
|
// This message is more often annoying than useful so
|
|
|
|
// it's commented out. It can be useful when developing
|
|
|
|
// the sandboxing code.
|
|
|
|
//message(V_DEBUG, _("Sandbox is disabled due "
|
|
|
|
// "to incompatible command line arguments"));
|
2015-03-31 21:19:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char dummy_str[] = "x";
|
|
|
|
|
|
|
|
// Try to ensure that both libc and xz locale files have been
|
|
|
|
// loaded when NLS is enabled.
|
|
|
|
snprintf(NULL, 0, "%s%s", _(dummy_str), strerror(EINVAL));
|
|
|
|
|
|
|
|
// Try to ensure that iconv data files needed for handling multibyte
|
|
|
|
// characters have been loaded. This is needed at least with glibc.
|
|
|
|
tuklib_mbstr_width(dummy_str, NULL);
|
|
|
|
|
|
|
|
#ifdef HAVE_CAPSICUM
|
|
|
|
// Capsicum needs FreeBSD 10.0 or later.
|
|
|
|
cap_rights_t rights;
|
|
|
|
|
|
|
|
if (cap_rights_limit(src_fd, cap_rights_init(&rights,
|
|
|
|
CAP_EVENT, CAP_FCNTL, CAP_LOOKUP, CAP_READ, CAP_SEEK)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (cap_rights_limit(STDOUT_FILENO, cap_rights_init(&rights,
|
|
|
|
CAP_EVENT, CAP_FCNTL, CAP_FSTAT, CAP_LOOKUP,
|
|
|
|
CAP_WRITE, CAP_SEEK)))
|
|
|
|
goto error;
|
|
|
|
|
2015-04-01 13:45:25 +02:00
|
|
|
if (cap_rights_limit(user_abort_pipe[0], cap_rights_init(&rights,
|
|
|
|
CAP_EVENT)))
|
|
|
|
goto error;
|
|
|
|
|
2015-03-31 21:19:34 +02:00
|
|
|
if (cap_rights_limit(user_abort_pipe[1], cap_rights_init(&rights,
|
2015-04-01 13:45:25 +02:00
|
|
|
CAP_WRITE)))
|
2015-03-31 21:19:34 +02:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (cap_enter())
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
#else
|
|
|
|
# error ENABLE_SANDBOX is defined but no sandboxing method was found.
|
|
|
|
#endif
|
|
|
|
|
2020-02-05 19:33:50 +01:00
|
|
|
// This message is annoying in xz -lvv.
|
|
|
|
//message(V_DEBUG, _("Sandbox was successfully enabled"));
|
2015-03-31 21:19:34 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
message(V_DEBUG, _("Failed to enable the sandbox"));
|
|
|
|
}
|
|
|
|
#endif // ENABLE_SANDBOX
|
|
|
|
|
|
|
|
|
2013-06-28 21:51:02 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2013-06-28 22:48:05 +02:00
|
|
|
/// \brief Waits for input or output to become available or for a signal
|
|
|
|
///
|
|
|
|
/// This uses the self-pipe trick to avoid a race condition that can occur
|
|
|
|
/// if a signal is caught after user_abort has been checked but before e.g.
|
|
|
|
/// read() has been called. In that situation read() could block unless
|
|
|
|
/// non-blocking I/O is used. With non-blocking I/O something like select()
|
|
|
|
/// or poll() is needed to avoid a busy-wait loop, and the same race condition
|
|
|
|
/// pops up again. There are pselect() (POSIX-1.2001) and ppoll() (not in
|
|
|
|
/// POSIX) but neither is portable enough in 2013. The self-pipe trick is
|
|
|
|
/// old and very portable.
|
2013-07-04 13:18:46 +02:00
|
|
|
static io_wait_ret
|
|
|
|
io_wait(file_pair *pair, int timeout, bool is_reading)
|
2013-06-28 21:51:02 +02:00
|
|
|
{
|
2013-06-28 22:48:05 +02:00
|
|
|
struct pollfd pfd[2];
|
2013-06-28 21:51:02 +02:00
|
|
|
|
|
|
|
if (is_reading) {
|
|
|
|
pfd[0].fd = pair->src_fd;
|
|
|
|
pfd[0].events = POLLIN;
|
|
|
|
} else {
|
|
|
|
pfd[0].fd = pair->dest_fd;
|
|
|
|
pfd[0].events = POLLOUT;
|
|
|
|
}
|
|
|
|
|
2013-06-28 22:48:05 +02:00
|
|
|
pfd[1].fd = user_abort_pipe[0];
|
|
|
|
pfd[1].events = POLLIN;
|
2013-06-28 21:51:02 +02:00
|
|
|
|
2013-06-28 22:48:05 +02:00
|
|
|
while (true) {
|
2013-07-04 13:18:46 +02:00
|
|
|
const int ret = poll(pfd, 2, timeout);
|
2013-06-28 21:51:02 +02:00
|
|
|
|
2013-06-28 22:48:05 +02:00
|
|
|
if (user_abort)
|
2013-07-04 13:18:46 +02:00
|
|
|
return IO_WAIT_ERROR;
|
2013-06-28 21:51:02 +02:00
|
|
|
|
2013-06-28 22:48:05 +02:00
|
|
|
if (ret == -1) {
|
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
2013-06-28 21:51:02 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
message_error(_("%s: poll() failed: %s"),
|
|
|
|
is_reading ? pair->src_name
|
|
|
|
: pair->dest_name,
|
|
|
|
strerror(errno));
|
2013-07-04 13:18:46 +02:00
|
|
|
return IO_WAIT_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-01-26 12:27:51 +01:00
|
|
|
if (ret == 0)
|
2013-07-04 13:18:46 +02:00
|
|
|
return IO_WAIT_TIMEOUT;
|
2013-06-28 21:51:02 +02:00
|
|
|
|
2013-06-28 22:48:05 +02:00
|
|
|
if (pfd[0].revents != 0)
|
2013-07-04 13:18:46 +02:00
|
|
|
return IO_WAIT_MORE;
|
2013-06-28 21:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
/// \brief Unlink a file
|
2007-12-08 23:42:33 +01:00
|
|
|
///
|
2008-11-19 19:46:52 +01:00
|
|
|
/// This tries to verify that the file being unlinked really is the file that
|
|
|
|
/// we want to unlink by verifying device and inode numbers. There's still
|
|
|
|
/// a small unavoidable race, but this is much better than nothing (the file
|
|
|
|
/// could have been moved/replaced even hours earlier).
|
2007-12-08 23:42:33 +01:00
|
|
|
static void
|
2008-11-19 19:46:52 +01:00
|
|
|
io_unlink(const char *name, const struct stat *known_st)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2009-09-22 13:03:02 +02:00
|
|
|
#if defined(TUKLIB_DOSLIKE)
|
2009-09-19 08:47:30 +02:00
|
|
|
// On DOS-like systems, st_ino is meaningless, so don't bother
|
|
|
|
// testing it. Just silence a compiler warning.
|
2009-06-27 09:02:24 +02:00
|
|
|
(void)known_st;
|
|
|
|
#else
|
2008-11-19 19:46:52 +01:00
|
|
|
struct stat new_st;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2010-02-01 10:44:45 +01:00
|
|
|
// If --force was used, use stat() instead of lstat(). This way
|
|
|
|
// (de)compressing symlinks works correctly. However, it also means
|
|
|
|
// that xz cannot detect if a regular file foo is renamed to bar
|
|
|
|
// and then a symlink foo -> bar is created. Because of stat()
|
|
|
|
// instead of lstat(), xz will think that foo hasn't been replaced
|
|
|
|
// with another file. Thus, xz will remove foo even though it no
|
|
|
|
// longer is the same file that xz used when it started compressing.
|
|
|
|
// Probably it's not too bad though, so this doesn't need a more
|
|
|
|
// complex fix.
|
|
|
|
const int stat_ret = opt_force
|
|
|
|
? stat(name, &new_st) : lstat(name, &new_st);
|
|
|
|
|
|
|
|
if (stat_ret
|
2009-09-22 13:03:02 +02:00
|
|
|
# ifdef __VMS
|
|
|
|
// st_ino is an array, and we don't want to
|
|
|
|
// compare st_dev at all.
|
2009-09-24 16:50:17 +02:00
|
|
|
|| memcmp(&new_st.st_ino, &known_st->st_ino,
|
2009-09-22 13:03:02 +02:00
|
|
|
sizeof(new_st.st_ino)) != 0
|
|
|
|
# else
|
|
|
|
// Typical POSIX-like system
|
2008-11-19 19:46:52 +01:00
|
|
|
|| new_st.st_dev != known_st->st_dev
|
2009-09-22 13:03:02 +02:00
|
|
|
|| new_st.st_ino != known_st->st_ino
|
|
|
|
# endif
|
|
|
|
)
|
2010-01-26 21:53:37 +01:00
|
|
|
// TRANSLATORS: When compression or decompression finishes,
|
|
|
|
// and xz is going to remove the source file, xz first checks
|
|
|
|
// if the source file still exists, and if it does, does its
|
|
|
|
// device and inode numbers match what xz saw when it opened
|
|
|
|
// the source file. If these checks fail, this message is
|
|
|
|
// shown, %s being the filename, and the file is not deleted.
|
|
|
|
// The check for device and inode numbers is there, because
|
|
|
|
// it is possible that the user has put a new file in place
|
|
|
|
// of the original file, and in that case it obviously
|
|
|
|
// shouldn't be removed.
|
|
|
|
message_error(_("%s: File seems to have been moved, "
|
|
|
|
"not removing"), name);
|
2009-02-05 08:12:57 +01:00
|
|
|
else
|
|
|
|
#endif
|
2007-12-08 23:42:33 +01:00
|
|
|
// There's a race condition between lstat() and unlink()
|
|
|
|
// but at least we have tried to avoid removing wrong file.
|
2008-11-19 19:46:52 +01:00
|
|
|
if (unlink(name))
|
|
|
|
message_error(_("%s: Cannot remove: %s"),
|
2007-12-08 23:42:33 +01:00
|
|
|
name, strerror(errno));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief Copies owner/group and permissions
|
|
|
|
///
|
|
|
|
/// \todo ACL and EA support
|
|
|
|
///
|
|
|
|
static void
|
|
|
|
io_copy_attrs(const file_pair *pair)
|
|
|
|
{
|
2009-02-05 08:12:57 +01:00
|
|
|
// Skip chown and chmod on Windows.
|
2009-09-19 08:47:30 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2007-12-08 23:42:33 +01:00
|
|
|
// This function is more tricky than you may think at first.
|
|
|
|
// Blindly copying permissions may permit users to access the
|
|
|
|
// destination file who didn't have permission to access the
|
|
|
|
// source file.
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Try changing the owner of the file. If we aren't root or the owner
|
|
|
|
// isn't already us, fchown() probably doesn't succeed. We warn
|
|
|
|
// about failing fchown() only if we are root.
|
2019-06-23 22:19:34 +02:00
|
|
|
if (fchown(pair->dest_fd, pair->src_st.st_uid, (gid_t)(-1))
|
|
|
|
&& warn_fchown)
|
2009-02-05 08:12:57 +01:00
|
|
|
message_warning(_("%s: Cannot set the file owner: %s"),
|
|
|
|
pair->dest_name, strerror(errno));
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
mode_t mode;
|
|
|
|
|
2019-06-23 22:19:34 +02:00
|
|
|
if (fchown(pair->dest_fd, (uid_t)(-1), pair->src_st.st_gid)) {
|
2008-11-19 19:46:52 +01:00
|
|
|
message_warning(_("%s: Cannot set the file group: %s"),
|
2007-12-08 23:42:33 +01:00
|
|
|
pair->dest_name, strerror(errno));
|
|
|
|
// We can still safely copy some additional permissions:
|
|
|
|
// `group' must be at least as strict as `other' and
|
|
|
|
// also vice versa.
|
|
|
|
//
|
|
|
|
// NOTE: After this, the owner of the source file may
|
|
|
|
// get additional permissions. This shouldn't be too bad,
|
|
|
|
// because the owner would have had permission to chmod
|
|
|
|
// the original file anyway.
|
|
|
|
mode = ((pair->src_st.st_mode & 0070) >> 3)
|
|
|
|
& (pair->src_st.st_mode & 0007);
|
|
|
|
mode = (pair->src_st.st_mode & 0700) | (mode << 3) | mode;
|
|
|
|
} else {
|
|
|
|
// Drop the setuid, setgid, and sticky bits.
|
|
|
|
mode = pair->src_st.st_mode & 0777;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fchmod(pair->dest_fd, mode))
|
2008-11-19 19:46:52 +01:00
|
|
|
message_warning(_("%s: Cannot set the file permissions: %s"),
|
2007-12-08 23:42:33 +01:00
|
|
|
pair->dest_name, strerror(errno));
|
2009-02-05 08:12:57 +01:00
|
|
|
#endif
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Copy the timestamps. We have several possible ways to do this, of
|
|
|
|
// which some are better in both security and precision.
|
|
|
|
//
|
|
|
|
// First, get the nanosecond part of the timestamps. As of writing,
|
|
|
|
// it's not standardized by POSIX, and there are several names for
|
|
|
|
// the same thing in struct stat.
|
|
|
|
long atime_nsec;
|
|
|
|
long mtime_nsec;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
# if defined(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
|
2008-11-19 19:46:52 +01:00
|
|
|
// GNU and Solaris
|
|
|
|
atime_nsec = pair->src_st.st_atim.tv_nsec;
|
|
|
|
mtime_nsec = pair->src_st.st_mtim.tv_nsec;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
# elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
|
2008-11-19 19:46:52 +01:00
|
|
|
// BSD
|
|
|
|
atime_nsec = pair->src_st.st_atimespec.tv_nsec;
|
|
|
|
mtime_nsec = pair->src_st.st_mtimespec.tv_nsec;
|
|
|
|
|
|
|
|
# elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
|
|
|
|
// GNU and BSD without extensions
|
|
|
|
atime_nsec = pair->src_st.st_atimensec;
|
|
|
|
mtime_nsec = pair->src_st.st_mtimensec;
|
|
|
|
|
|
|
|
# elif defined(HAVE_STRUCT_STAT_ST_UATIME)
|
|
|
|
// Tru64
|
|
|
|
atime_nsec = pair->src_st.st_uatime * 1000;
|
|
|
|
mtime_nsec = pair->src_st.st_umtime * 1000;
|
|
|
|
|
|
|
|
# elif defined(HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC)
|
|
|
|
// UnixWare
|
|
|
|
atime_nsec = pair->src_st.st_atim.st__tim.tv_nsec;
|
|
|
|
mtime_nsec = pair->src_st.st_mtim.st__tim.tv_nsec;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
# else
|
2008-11-19 19:46:52 +01:00
|
|
|
// Safe fallback
|
|
|
|
atime_nsec = 0;
|
|
|
|
mtime_nsec = 0;
|
2007-12-08 23:42:33 +01:00
|
|
|
# endif
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Construct a structure to hold the timestamps and call appropriate
|
|
|
|
// function to set the timestamps.
|
|
|
|
#if defined(HAVE_FUTIMENS)
|
|
|
|
// Use nanosecond precision.
|
|
|
|
struct timespec tv[2];
|
|
|
|
tv[0].tv_sec = pair->src_st.st_atime;
|
|
|
|
tv[0].tv_nsec = atime_nsec;
|
|
|
|
tv[1].tv_sec = pair->src_st.st_mtime;
|
|
|
|
tv[1].tv_nsec = mtime_nsec;
|
|
|
|
|
|
|
|
(void)futimens(pair->dest_fd, tv);
|
|
|
|
|
|
|
|
#elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMES)
|
|
|
|
// Use microsecond precision.
|
|
|
|
struct timeval tv[2];
|
|
|
|
tv[0].tv_sec = pair->src_st.st_atime;
|
|
|
|
tv[0].tv_usec = atime_nsec / 1000;
|
|
|
|
tv[1].tv_sec = pair->src_st.st_mtime;
|
|
|
|
tv[1].tv_usec = mtime_nsec / 1000;
|
|
|
|
|
|
|
|
# if defined(HAVE_FUTIMES)
|
2007-12-08 23:42:33 +01:00
|
|
|
(void)futimes(pair->dest_fd, tv);
|
2008-11-19 19:46:52 +01:00
|
|
|
# elif defined(HAVE_FUTIMESAT)
|
2007-12-08 23:42:33 +01:00
|
|
|
(void)futimesat(pair->dest_fd, NULL, tv);
|
2008-11-19 19:46:52 +01:00
|
|
|
# else
|
|
|
|
// Argh, no function to use a file descriptor to set the timestamp.
|
2009-02-03 09:41:11 +01:00
|
|
|
(void)utimes(pair->dest_name, tv);
|
2007-12-08 23:42:33 +01:00
|
|
|
# endif
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2016-06-30 19:27:36 +02:00
|
|
|
#elif defined(HAVE__FUTIME)
|
|
|
|
// Use one-second precision with Windows-specific _futime().
|
|
|
|
// We could use utime() too except that for some reason the
|
|
|
|
// timestamp will get reset at close(). With _futime() it works.
|
|
|
|
// This struct cannot be const as _futime() takes a non-const pointer.
|
|
|
|
struct _utimbuf buf = {
|
|
|
|
.actime = pair->src_st.st_atime,
|
|
|
|
.modtime = pair->src_st.st_mtime,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Avoid warnings.
|
|
|
|
(void)atime_nsec;
|
|
|
|
(void)mtime_nsec;
|
|
|
|
|
|
|
|
(void)_futime(pair->dest_fd, &buf);
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
#elif defined(HAVE_UTIME)
|
|
|
|
// Use one-second precision. utime() doesn't support using file
|
2009-02-03 11:15:17 +01:00
|
|
|
// descriptor either. Some systems have broken utime() prototype
|
|
|
|
// so don't make this const.
|
|
|
|
struct utimbuf buf = {
|
|
|
|
.actime = pair->src_st.st_atime,
|
|
|
|
.modtime = pair->src_st.st_mtime,
|
2008-11-19 19:46:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Avoid warnings.
|
|
|
|
(void)atime_nsec;
|
|
|
|
(void)mtime_nsec;
|
|
|
|
|
2009-02-03 09:41:11 +01:00
|
|
|
(void)utime(pair->dest_name, &buf);
|
2007-12-08 23:42:33 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// Opens the source file. Returns false on success, true on error.
|
|
|
|
static bool
|
2010-01-31 11:01:54 +01:00
|
|
|
io_open_src_real(file_pair *pair)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-11-19 19:46:52 +01:00
|
|
|
// There's nothing to open when reading from stdin.
|
|
|
|
if (pair->src_name == stdin_filename) {
|
|
|
|
pair->src_fd = STDIN_FILENO;
|
2009-09-19 08:47:30 +02:00
|
|
|
#ifdef TUKLIB_DOSLIKE
|
2009-02-05 08:12:57 +01:00
|
|
|
setmode(STDIN_FILENO, O_BINARY);
|
2013-06-28 21:51:02 +02:00
|
|
|
#else
|
2015-01-09 20:50:19 +01:00
|
|
|
// Try to set stdin to non-blocking mode. It won't work
|
2015-01-07 18:08:06 +01:00
|
|
|
// e.g. on OpenBSD if stdout is e.g. /dev/null. In such
|
2015-01-09 20:50:19 +01:00
|
|
|
// case we proceed as if stdin were non-blocking anyway
|
|
|
|
// (in case of /dev/null it will be in practice). The
|
|
|
|
// same applies to stdout in io_open_dest_real().
|
2013-06-28 21:51:02 +02:00
|
|
|
stdin_flags = fcntl(STDIN_FILENO, F_GETFL);
|
|
|
|
if (stdin_flags == -1) {
|
|
|
|
message_error(_("Error getting the file status flags "
|
|
|
|
"from standard input: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-07 18:08:06 +01:00
|
|
|
if ((stdin_flags & O_NONBLOCK) == 0
|
|
|
|
&& fcntl(STDIN_FILENO, F_SETFL,
|
|
|
|
stdin_flags | O_NONBLOCK) != -1)
|
2013-06-28 21:51:02 +02:00
|
|
|
restore_stdin_flags = true;
|
2011-04-05 14:27:26 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
// It will fail if stdin is a pipe and that's fine.
|
2017-03-30 21:01:54 +02:00
|
|
|
(void)posix_fadvise(STDIN_FILENO, 0, 0,
|
|
|
|
opt_mode == MODE_LIST
|
|
|
|
? POSIX_FADV_RANDOM
|
|
|
|
: POSIX_FADV_SEQUENTIAL);
|
2009-02-05 08:12:57 +01:00
|
|
|
#endif
|
2008-11-19 19:46:52 +01:00
|
|
|
return false;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2010-01-13 17:12:40 +01:00
|
|
|
// Symlinks are not followed unless writing to stdout or --force
|
|
|
|
// was used.
|
|
|
|
const bool follow_symlinks = opt_stdout || opt_force;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// We accept only regular files if we are writing the output
|
2010-01-13 17:12:40 +01:00
|
|
|
// to disk too. bzip2 allows overriding this with --force but
|
|
|
|
// gzip and xz don't.
|
|
|
|
const bool reg_files_only = !opt_stdout;
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
// Flags for open()
|
2009-02-05 08:12:57 +01:00
|
|
|
int flags = O_RDONLY | O_BINARY | O_NOCTTY;
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2013-06-28 21:51:02 +02:00
|
|
|
// Use non-blocking I/O:
|
|
|
|
// - It prevents blocking when opening FIFOs and some other
|
|
|
|
// special files, which is good if we want to accept only
|
|
|
|
// regular files.
|
|
|
|
// - It can help avoiding some race conditions with signal handling.
|
|
|
|
flags |= O_NONBLOCK;
|
2009-02-05 08:12:57 +01:00
|
|
|
#endif
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2009-02-05 08:12:57 +01:00
|
|
|
#if defined(O_NOFOLLOW)
|
2010-01-13 17:12:40 +01:00
|
|
|
if (!follow_symlinks)
|
2008-11-19 19:46:52 +01:00
|
|
|
flags |= O_NOFOLLOW;
|
2009-09-19 08:47:30 +02:00
|
|
|
#elif !defined(TUKLIB_DOSLIKE)
|
2008-11-19 19:46:52 +01:00
|
|
|
// Some POSIX-like systems lack O_NOFOLLOW (it's not required
|
|
|
|
// by POSIX). Check for symlinks with a separate lstat() on
|
|
|
|
// these systems.
|
2010-01-13 17:12:40 +01:00
|
|
|
if (!follow_symlinks) {
|
2008-11-19 19:46:52 +01:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(pair->src_name, &st)) {
|
|
|
|
message_error("%s: %s", pair->src_name,
|
|
|
|
strerror(errno));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else if (S_ISLNK(st.st_mode)) {
|
|
|
|
message_warning(_("%s: Is a symbolic link, "
|
|
|
|
"skipping"), pair->src_name);
|
|
|
|
return true;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-27 15:42:11 +01:00
|
|
|
#else
|
|
|
|
// Avoid warnings.
|
|
|
|
(void)follow_symlinks;
|
2008-11-19 19:46:52 +01:00
|
|
|
#endif
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2013-06-28 21:51:02 +02:00
|
|
|
// Try to open the file. Signals have been blocked so EINTR shouldn't
|
|
|
|
// be possible.
|
|
|
|
pair->src_fd = open(pair->src_name, flags);
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
if (pair->src_fd == -1) {
|
2013-06-28 21:51:02 +02:00
|
|
|
// Signals (that have a signal handler) have been blocked.
|
|
|
|
assert(errno != EINTR);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
#ifdef O_NOFOLLOW
|
2010-09-06 09:16:24 +02:00
|
|
|
// Give an understandable error message if the reason
|
2008-11-19 19:46:52 +01:00
|
|
|
// for failing was that the file was a symbolic link.
|
|
|
|
//
|
|
|
|
// Note that at least Linux, OpenBSD, Solaris, and Darwin
|
2010-09-06 09:16:24 +02:00
|
|
|
// use ELOOP to indicate that O_NOFOLLOW was the reason
|
2008-11-19 19:46:52 +01:00
|
|
|
// that open() failed. Because there may be
|
|
|
|
// directories in the pathname, ELOOP may occur also
|
|
|
|
// because of a symlink loop in the directory part.
|
2010-09-06 09:16:24 +02:00
|
|
|
// So ELOOP doesn't tell us what actually went wrong,
|
|
|
|
// and this stupidity went into POSIX-1.2008 too.
|
2008-11-19 19:46:52 +01:00
|
|
|
//
|
|
|
|
// FreeBSD associates EMLINK with O_NOFOLLOW and
|
|
|
|
// Tru64 uses ENOTSUP. We use these directly here
|
|
|
|
// and skip the lstat() call and the associated race.
|
|
|
|
// I want to hear if there are other kernels that
|
|
|
|
// fail with something else than ELOOP with O_NOFOLLOW.
|
|
|
|
bool was_symlink = false;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
# if defined(__FreeBSD__) || defined(__DragonFly__)
|
|
|
|
if (errno == EMLINK)
|
|
|
|
was_symlink = true;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
# elif defined(__digital__) && defined(__unix__)
|
|
|
|
if (errno == ENOTSUP)
|
|
|
|
was_symlink = true;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-20 21:59:10 +01:00
|
|
|
# elif defined(__NetBSD__)
|
|
|
|
if (errno == EFTYPE)
|
|
|
|
was_symlink = true;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
# else
|
2010-01-13 17:12:40 +01:00
|
|
|
if (errno == ELOOP && !follow_symlinks) {
|
2008-11-19 19:46:52 +01:00
|
|
|
const int saved_errno = errno;
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(pair->src_name, &st) == 0
|
|
|
|
&& S_ISLNK(st.st_mode))
|
|
|
|
was_symlink = true;
|
|
|
|
|
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
# endif
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (was_symlink)
|
|
|
|
message_warning(_("%s: Is a symbolic link, "
|
|
|
|
"skipping"), pair->src_name);
|
|
|
|
else
|
2007-12-08 23:42:33 +01:00
|
|
|
#endif
|
2008-11-19 19:46:52 +01:00
|
|
|
// Something else than O_NOFOLLOW failing
|
|
|
|
// (assuming that the race conditions didn't
|
|
|
|
// confuse us).
|
|
|
|
message_error("%s: %s", pair->src_name,
|
|
|
|
strerror(errno));
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
return true;
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Stat the source file. We need the result also when we copy
|
|
|
|
// the permissions, and when unlinking.
|
2011-04-10 11:47:47 +02:00
|
|
|
//
|
|
|
|
// NOTE: Use stat() instead of fstat() with DJGPP, because
|
|
|
|
// then we have a better chance to get st_ino value that can
|
|
|
|
// be used in io_open_dest_real() to prevent overwriting the
|
|
|
|
// source file.
|
|
|
|
#ifdef __DJGPP__
|
|
|
|
if (stat(pair->src_name, &pair->src_st))
|
|
|
|
goto error_msg;
|
|
|
|
#else
|
2008-11-19 19:46:52 +01:00
|
|
|
if (fstat(pair->src_fd, &pair->src_st))
|
|
|
|
goto error_msg;
|
2011-04-10 11:47:47 +02:00
|
|
|
#endif
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
if (S_ISDIR(pair->src_st.st_mode)) {
|
|
|
|
message_warning(_("%s: Is a directory, skipping"),
|
|
|
|
pair->src_name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-01-26 11:19:08 +01:00
|
|
|
if (reg_files_only && !S_ISREG(pair->src_st.st_mode)) {
|
|
|
|
message_warning(_("%s: Not a regular file, skipping"),
|
|
|
|
pair->src_name);
|
|
|
|
goto error;
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2011-01-26 11:19:08 +01:00
|
|
|
if (reg_files_only && !opt_force) {
|
2008-11-19 19:46:52 +01:00
|
|
|
if (pair->src_st.st_mode & (S_ISUID | S_ISGID)) {
|
|
|
|
// gzip rejects setuid and setgid files even
|
|
|
|
// when --force was used. bzip2 doesn't check
|
|
|
|
// for them, but calls fchown() after fchmod(),
|
|
|
|
// and many systems automatically drop setuid
|
|
|
|
// and setgid bits there.
|
|
|
|
//
|
|
|
|
// We accept setuid and setgid files if
|
|
|
|
// --force was used. We drop these bits
|
|
|
|
// explicitly in io_copy_attr().
|
|
|
|
message_warning(_("%s: File has setuid or "
|
|
|
|
"setgid bit set, skipping"),
|
2007-12-08 23:42:33 +01:00
|
|
|
pair->src_name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (pair->src_st.st_mode & S_ISVTX) {
|
|
|
|
message_warning(_("%s: File has sticky bit "
|
|
|
|
"set, skipping"),
|
|
|
|
pair->src_name);
|
|
|
|
goto error;
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (pair->src_st.st_nlink > 1) {
|
|
|
|
message_warning(_("%s: Input file has more "
|
|
|
|
"than one hard link, "
|
|
|
|
"skipping"), pair->src_name);
|
|
|
|
goto error;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
}
|
2013-06-28 21:51:02 +02:00
|
|
|
|
|
|
|
// If it is something else than a regular file, wait until
|
|
|
|
// there is input available. This way reading from FIFOs
|
|
|
|
// will work when open() is used with O_NONBLOCK.
|
|
|
|
if (!S_ISREG(pair->src_st.st_mode)) {
|
|
|
|
signals_unblock();
|
2013-07-04 13:18:46 +02:00
|
|
|
const io_wait_ret ret = io_wait(pair, -1, true);
|
2013-06-28 21:51:02 +02:00
|
|
|
signals_block();
|
|
|
|
|
2013-07-04 13:18:46 +02:00
|
|
|
if (ret != IO_WAIT_MORE)
|
2013-06-28 21:51:02 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2011-01-26 11:19:08 +01:00
|
|
|
#endif
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2011-04-05 14:27:26 +02:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
2013-06-28 13:55:37 +02:00
|
|
|
// It will fail with some special files like FIFOs but that is fine.
|
2017-03-30 21:01:54 +02:00
|
|
|
(void)posix_fadvise(pair->src_fd, 0, 0,
|
|
|
|
opt_mode == MODE_LIST
|
|
|
|
? POSIX_FADV_RANDOM
|
|
|
|
: POSIX_FADV_SEQUENTIAL);
|
2011-04-05 14:27:26 +02:00
|
|
|
#endif
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
return false;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
error_msg:
|
|
|
|
message_error("%s: %s", pair->src_name, strerror(errno));
|
2007-12-08 23:42:33 +01:00
|
|
|
error:
|
|
|
|
(void)close(pair->src_fd);
|
2008-11-19 19:46:52 +01:00
|
|
|
return true;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 11:01:54 +01:00
|
|
|
extern file_pair *
|
|
|
|
io_open_src(const char *src_name)
|
|
|
|
{
|
|
|
|
if (is_empty_filename(src_name))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Since we have only one file open at a time, we can use
|
|
|
|
// a statically allocated structure.
|
|
|
|
static file_pair pair;
|
|
|
|
|
|
|
|
pair = (file_pair){
|
|
|
|
.src_name = src_name,
|
|
|
|
.dest_name = NULL,
|
|
|
|
.src_fd = -1,
|
|
|
|
.dest_fd = -1,
|
|
|
|
.src_eof = false,
|
2020-01-26 13:13:42 +01:00
|
|
|
.src_has_seen_input = false,
|
2020-01-26 19:19:19 +01:00
|
|
|
.flush_needed = false,
|
2010-01-31 11:01:54 +01:00
|
|
|
.dest_try_sparse = false,
|
|
|
|
.dest_pending_sparse = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Block the signals, for which we have a custom signal handler, so
|
|
|
|
// that we don't need to worry about EINTR.
|
|
|
|
signals_block();
|
|
|
|
const bool error = io_open_src_real(&pair);
|
|
|
|
signals_unblock();
|
|
|
|
|
2015-03-31 21:19:34 +02:00
|
|
|
#ifdef ENABLE_SANDBOX
|
|
|
|
if (!error)
|
|
|
|
io_sandbox_enter(pair.src_fd);
|
|
|
|
#endif
|
|
|
|
|
2010-01-31 11:01:54 +01:00
|
|
|
return error ? NULL : &pair;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
/// \brief Closes source file of the file_pair structure
|
|
|
|
///
|
|
|
|
/// \param pair File whose src_fd should be closed
|
|
|
|
/// \param success If true, the file will be removed from the disk if
|
|
|
|
/// closing succeeds and --keep hasn't been used.
|
|
|
|
static void
|
|
|
|
io_close_src(file_pair *pair, bool success)
|
|
|
|
{
|
2013-09-17 10:55:38 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2013-06-28 21:51:02 +02:00
|
|
|
if (restore_stdin_flags) {
|
|
|
|
assert(pair->src_fd == STDIN_FILENO);
|
|
|
|
|
|
|
|
restore_stdin_flags = false;
|
|
|
|
|
|
|
|
if (fcntl(STDIN_FILENO, F_SETFL, stdin_flags) == -1)
|
|
|
|
message_error(_("Error restoring the status flags "
|
|
|
|
"to standard input: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2013-09-17 10:55:38 +02:00
|
|
|
#endif
|
2013-06-28 21:51:02 +02:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (pair->src_fd != STDIN_FILENO && pair->src_fd != -1) {
|
2015-11-02 14:19:10 +01:00
|
|
|
// Close the file before possibly unlinking it. On DOS-like
|
|
|
|
// systems this is always required since unlinking will fail
|
|
|
|
// if the file is open. On POSIX systems it usually works
|
|
|
|
// to unlink open files, but in some cases it doesn't and
|
|
|
|
// one gets EBUSY in errno.
|
|
|
|
//
|
|
|
|
// xz 5.2.2 and older unlinked the file before closing it
|
|
|
|
// (except on DOS-like systems). The old code didn't handle
|
|
|
|
// EBUSY and could fail e.g. on some CIFS shares. The
|
|
|
|
// advantage of unlinking before closing is negligible
|
|
|
|
// (avoids a race between close() and stat()/lstat() and
|
|
|
|
// unlink()), so let's keep this simple.
|
2009-02-05 08:12:57 +01:00
|
|
|
(void)close(pair->src_fd);
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (success && !opt_keep_original)
|
|
|
|
io_unlink(pair->src_name, &pair->src_st);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
static bool
|
2010-01-31 11:01:54 +01:00
|
|
|
io_open_dest_real(file_pair *pair)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
|
|
|
if (opt_stdout || pair->src_fd == STDIN_FILENO) {
|
|
|
|
// We don't modify or free() this.
|
|
|
|
pair->dest_name = (char *)"(stdout)";
|
|
|
|
pair->dest_fd = STDOUT_FILENO;
|
2009-09-19 08:47:30 +02:00
|
|
|
#ifdef TUKLIB_DOSLIKE
|
2009-02-05 08:12:57 +01:00
|
|
|
setmode(STDOUT_FILENO, O_BINARY);
|
2013-06-29 14:59:13 +02:00
|
|
|
#else
|
2015-01-09 20:50:19 +01:00
|
|
|
// Try to set O_NONBLOCK if it isn't already set.
|
|
|
|
// If it fails, we assume that stdout is non-blocking
|
|
|
|
// in practice. See the comments in io_open_src_real()
|
|
|
|
// for similar situation with stdin.
|
2013-06-29 14:59:13 +02:00
|
|
|
//
|
|
|
|
// NOTE: O_APPEND may be unset later in this function
|
|
|
|
// and it relies on stdout_flags being set here.
|
|
|
|
stdout_flags = fcntl(STDOUT_FILENO, F_GETFL);
|
|
|
|
if (stdout_flags == -1) {
|
|
|
|
message_error(_("Error getting the file status flags "
|
|
|
|
"from standard output: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-09 20:34:06 +01:00
|
|
|
if ((stdout_flags & O_NONBLOCK) == 0
|
|
|
|
&& fcntl(STDOUT_FILENO, F_SETFL,
|
|
|
|
stdout_flags | O_NONBLOCK) != -1)
|
|
|
|
restore_stdout_flags = true;
|
2009-02-05 08:12:57 +01:00
|
|
|
#endif
|
2009-11-25 10:19:20 +01:00
|
|
|
} else {
|
|
|
|
pair->dest_name = suffix_get_dest_name(pair->src_name);
|
|
|
|
if (pair->dest_name == NULL)
|
|
|
|
return true;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2011-04-10 11:47:47 +02:00
|
|
|
#ifdef __DJGPP__
|
|
|
|
struct stat st;
|
|
|
|
if (stat(pair->dest_name, &st) == 0) {
|
|
|
|
// Check that it isn't a special file like "prn".
|
|
|
|
if (st.st_dev == -1) {
|
|
|
|
message_error("%s: Refusing to write to "
|
|
|
|
"a DOS special file",
|
|
|
|
pair->dest_name);
|
2015-01-07 18:18:20 +01:00
|
|
|
free(pair->dest_name);
|
2011-04-10 11:47:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we aren't overwriting the source file.
|
|
|
|
if (st.st_dev == pair->src_st.st_dev
|
|
|
|
&& st.st_ino == pair->src_st.st_ino) {
|
|
|
|
message_error("%s: Output file is the same "
|
|
|
|
"as the input file",
|
|
|
|
pair->dest_name);
|
2015-01-07 18:18:20 +01:00
|
|
|
free(pair->dest_name);
|
2011-04-10 11:47:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-25 10:19:20 +01:00
|
|
|
// If --force was used, unlink the target file first.
|
|
|
|
if (opt_force && unlink(pair->dest_name) && errno != ENOENT) {
|
2009-11-28 16:45:22 +01:00
|
|
|
message_error(_("%s: Cannot remove: %s"),
|
2009-11-25 10:19:20 +01:00
|
|
|
pair->dest_name, strerror(errno));
|
|
|
|
free(pair->dest_name);
|
|
|
|
return true;
|
|
|
|
}
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2009-11-25 10:19:20 +01:00
|
|
|
// Open the file.
|
2013-06-29 14:59:13 +02:00
|
|
|
int flags = O_WRONLY | O_BINARY | O_NOCTTY
|
2009-11-25 10:19:20 +01:00
|
|
|
| O_CREAT | O_EXCL;
|
2013-06-29 14:59:13 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
#endif
|
2009-11-25 10:19:20 +01:00
|
|
|
const mode_t mode = S_IRUSR | S_IWUSR;
|
|
|
|
pair->dest_fd = open(pair->dest_name, flags, mode);
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2009-11-25 10:19:20 +01:00
|
|
|
if (pair->dest_fd == -1) {
|
2010-01-31 11:01:54 +01:00
|
|
|
message_error("%s: %s", pair->dest_name,
|
|
|
|
strerror(errno));
|
2009-11-25 10:19:20 +01:00
|
|
|
free(pair->dest_name);
|
|
|
|
return true;
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2011-04-09 14:24:59 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
|
|
|
// dest_st isn't used on DOS-like systems except as a dummy
|
|
|
|
// argument to io_unlink(), so don't fstat() on such systems.
|
2008-11-19 19:46:52 +01:00
|
|
|
if (fstat(pair->dest_fd, &pair->dest_st)) {
|
2011-04-09 14:24:59 +02:00
|
|
|
// If fstat() really fails, we have a safe fallback here.
|
|
|
|
# if defined(__VMS)
|
2009-09-22 13:03:02 +02:00
|
|
|
pair->dest_st.st_ino[0] = 0;
|
|
|
|
pair->dest_st.st_ino[1] = 0;
|
|
|
|
pair->dest_st.st_ino[2] = 0;
|
2011-04-09 14:24:59 +02:00
|
|
|
# else
|
2008-11-19 19:46:52 +01:00
|
|
|
pair->dest_st.st_dev = 0;
|
|
|
|
pair->dest_st.st_ino = 0;
|
2011-04-09 14:24:59 +02:00
|
|
|
# endif
|
2009-11-25 10:19:20 +01:00
|
|
|
} else if (try_sparse && opt_mode == MODE_DECOMPRESS) {
|
|
|
|
// When writing to standard output, we need to be extra
|
|
|
|
// careful:
|
|
|
|
// - It may be connected to something else than
|
|
|
|
// a regular file.
|
|
|
|
// - We aren't necessarily writing to a new empty file
|
|
|
|
// or to the end of an existing file.
|
|
|
|
// - O_APPEND may be active.
|
|
|
|
//
|
|
|
|
// TODO: I'm keeping this disabled for DOS-like systems
|
|
|
|
// for now. FAT doesn't support sparse files, but NTFS
|
|
|
|
// does, so maybe this should be enabled on Windows after
|
|
|
|
// some testing.
|
|
|
|
if (pair->dest_fd == STDOUT_FILENO) {
|
|
|
|
if (!S_ISREG(pair->dest_st.st_mode))
|
|
|
|
return false;
|
|
|
|
|
2013-06-28 16:36:47 +02:00
|
|
|
if (stdout_flags & O_APPEND) {
|
2009-11-25 10:19:20 +01:00
|
|
|
// Creating a sparse file is not possible
|
|
|
|
// when O_APPEND is active (it's used by
|
|
|
|
// shell's >> redirection). As I understand
|
|
|
|
// it, it is safe to temporarily disable
|
|
|
|
// O_APPEND in xz, because if someone
|
|
|
|
// happened to write to the same file at the
|
|
|
|
// same time, results would be bad anyway
|
|
|
|
// (users shouldn't assume that xz uses any
|
|
|
|
// specific block size when writing data).
|
|
|
|
//
|
|
|
|
// The write position may be something else
|
|
|
|
// than the end of the file, so we must fix
|
|
|
|
// it to start writing at the end of the file
|
|
|
|
// to imitate O_APPEND.
|
|
|
|
if (lseek(STDOUT_FILENO, 0, SEEK_END) == -1)
|
|
|
|
return false;
|
|
|
|
|
2015-01-09 20:34:06 +01:00
|
|
|
// Construct the new file status flags.
|
|
|
|
// If O_NONBLOCK was set earlier in this
|
|
|
|
// function, it must be kept here too.
|
|
|
|
int flags = stdout_flags & ~O_APPEND;
|
|
|
|
if (restore_stdout_flags)
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
|
|
|
|
// If this fcntl() fails, we continue but won't
|
2013-06-29 14:59:13 +02:00
|
|
|
// try to create sparse output. The original
|
|
|
|
// flags will still be restored if needed (to
|
|
|
|
// unset O_NONBLOCK) when the file is finished.
|
2015-01-09 20:34:06 +01:00
|
|
|
if (fcntl(STDOUT_FILENO, F_SETFL, flags) == -1)
|
2009-11-25 10:19:20 +01:00
|
|
|
return false;
|
|
|
|
|
2013-06-28 16:36:47 +02:00
|
|
|
// Disabling O_APPEND succeeded. Mark
|
|
|
|
// that the flags should be restored
|
2015-01-09 20:34:06 +01:00
|
|
|
// in io_close_dest(). (This may have already
|
|
|
|
// been set when enabling O_NONBLOCK.)
|
2013-06-28 16:36:47 +02:00
|
|
|
restore_stdout_flags = true;
|
2009-11-25 10:19:20 +01:00
|
|
|
|
|
|
|
} else if (lseek(STDOUT_FILENO, 0, SEEK_CUR)
|
|
|
|
!= pair->dest_st.st_size) {
|
|
|
|
// Writing won't start exactly at the end
|
|
|
|
// of the file. We cannot use sparse output,
|
|
|
|
// because it would probably corrupt the file.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pair->dest_try_sparse = true;
|
2008-11-19 19:46:52 +01:00
|
|
|
}
|
2011-04-09 14:24:59 +02:00
|
|
|
#endif
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
return false;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 11:01:54 +01:00
|
|
|
extern bool
|
|
|
|
io_open_dest(file_pair *pair)
|
|
|
|
{
|
|
|
|
signals_block();
|
|
|
|
const bool ret = io_open_dest_real(pair);
|
|
|
|
signals_unblock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
/// \brief Closes destination file of the file_pair structure
|
|
|
|
///
|
|
|
|
/// \param pair File whose dest_fd should be closed
|
|
|
|
/// \param success If false, the file will be removed from the disk.
|
|
|
|
///
|
|
|
|
/// \return Zero if closing succeeds. On error, -1 is returned and
|
|
|
|
/// error message printed.
|
2010-01-31 11:01:54 +01:00
|
|
|
static bool
|
2007-12-08 23:42:33 +01:00
|
|
|
io_close_dest(file_pair *pair, bool success)
|
|
|
|
{
|
2009-12-07 20:46:53 +01:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2009-11-25 10:19:20 +01:00
|
|
|
// If io_open_dest() has disabled O_APPEND, restore it here.
|
2013-06-28 16:36:47 +02:00
|
|
|
if (restore_stdout_flags) {
|
2009-11-25 10:19:20 +01:00
|
|
|
assert(pair->dest_fd == STDOUT_FILENO);
|
|
|
|
|
2013-06-28 16:36:47 +02:00
|
|
|
restore_stdout_flags = false;
|
2009-11-25 10:19:20 +01:00
|
|
|
|
2013-06-28 17:09:47 +02:00
|
|
|
if (fcntl(STDOUT_FILENO, F_SETFL, stdout_flags) == -1) {
|
2009-11-25 10:19:20 +01:00
|
|
|
message_error(_("Error restoring the O_APPEND flag "
|
|
|
|
"to standard output: %s"),
|
|
|
|
strerror(errno));
|
2010-01-31 11:01:54 +01:00
|
|
|
return true;
|
2009-11-25 10:19:20 +01:00
|
|
|
}
|
|
|
|
}
|
2009-12-07 20:46:53 +01:00
|
|
|
#endif
|
2009-11-25 10:19:20 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (pair->dest_fd == -1 || pair->dest_fd == STDOUT_FILENO)
|
2010-01-31 11:01:54 +01:00
|
|
|
return false;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
if (close(pair->dest_fd)) {
|
2008-11-19 19:46:52 +01:00
|
|
|
message_error(_("%s: Closing the file failed: %s"),
|
2007-12-08 23:42:33 +01:00
|
|
|
pair->dest_name, strerror(errno));
|
|
|
|
|
|
|
|
// Closing destination file failed, so we cannot trust its
|
|
|
|
// contents. Get rid of junk:
|
2008-11-19 19:46:52 +01:00
|
|
|
io_unlink(pair->dest_name, &pair->dest_st);
|
2007-12-08 23:42:33 +01:00
|
|
|
free(pair->dest_name);
|
2010-01-31 11:01:54 +01:00
|
|
|
return true;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the operation using this file wasn't successful, we git rid
|
|
|
|
// of the junk file.
|
|
|
|
if (!success)
|
2008-11-19 19:46:52 +01:00
|
|
|
io_unlink(pair->dest_name, &pair->dest_st);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
free(pair->dest_name);
|
|
|
|
|
2010-01-31 11:01:54 +01:00
|
|
|
return false;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern void
|
|
|
|
io_close(file_pair *pair, bool success)
|
|
|
|
{
|
2009-11-25 10:19:20 +01:00
|
|
|
// Take care of sparseness at the end of the output file.
|
|
|
|
if (success && pair->dest_try_sparse
|
|
|
|
&& pair->dest_pending_sparse > 0) {
|
|
|
|
// Seek forward one byte less than the size of the pending
|
|
|
|
// hole, then write one zero-byte. This way the file grows
|
|
|
|
// to its correct size. An alternative would be to use
|
|
|
|
// ftruncate() but that isn't portable enough (e.g. it
|
|
|
|
// doesn't work with FAT on Linux; FAT isn't that important
|
|
|
|
// since it doesn't support sparse files anyway, but we don't
|
|
|
|
// want to create corrupt files on it).
|
|
|
|
if (lseek(pair->dest_fd, pair->dest_pending_sparse - 1,
|
|
|
|
SEEK_CUR) == -1) {
|
|
|
|
message_error(_("%s: Seeking failed when trying "
|
|
|
|
"to create a sparse file: %s"),
|
|
|
|
pair->dest_name, strerror(errno));
|
|
|
|
success = false;
|
|
|
|
} else {
|
|
|
|
const uint8_t zero[1] = { '\0' };
|
|
|
|
if (io_write_buf(pair, zero, 1))
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
signals_block();
|
|
|
|
|
2010-01-31 11:01:54 +01:00
|
|
|
// Copy the file attributes. We need to skip this if destination
|
|
|
|
// file isn't open or it is standard output.
|
|
|
|
if (success && pair->dest_fd != -1 && pair->dest_fd != STDOUT_FILENO)
|
2007-12-08 23:42:33 +01:00
|
|
|
io_copy_attrs(pair);
|
|
|
|
|
|
|
|
// Close the destination first. If it fails, we must not remove
|
|
|
|
// the source file!
|
2008-11-19 19:46:52 +01:00
|
|
|
if (io_close_dest(pair, success))
|
|
|
|
success = false;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Close the source file, and unlink it if the operation using this
|
|
|
|
// file pair was successful and we haven't requested to keep the
|
|
|
|
// source file.
|
|
|
|
io_close_src(pair, success);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
signals_unblock();
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-01 11:24:23 +02:00
|
|
|
extern void
|
|
|
|
io_fix_src_pos(file_pair *pair, size_t rewind_size)
|
|
|
|
{
|
|
|
|
assert(rewind_size <= IO_BUFFER_SIZE);
|
|
|
|
|
|
|
|
if (rewind_size > 0) {
|
|
|
|
// This doesn't need to work on unseekable file descriptors,
|
|
|
|
// so just ignore possible errors.
|
|
|
|
(void)lseek(pair->src_fd, -(off_t)(rewind_size), SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
extern size_t
|
2020-01-26 12:47:31 +01:00
|
|
|
io_read(file_pair *pair, io_buf *buf, size_t size)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
|
|
|
// We use small buffers here.
|
|
|
|
assert(size < SSIZE_MAX);
|
|
|
|
|
2020-01-26 12:47:31 +01:00
|
|
|
size_t pos = 0;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2020-01-26 12:47:31 +01:00
|
|
|
while (pos < size) {
|
|
|
|
const ssize_t amount = read(
|
|
|
|
pair->src_fd, buf->u8 + pos, size - pos);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
if (amount == 0) {
|
|
|
|
pair->src_eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amount == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
if (user_abort)
|
|
|
|
return SIZE_MAX;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-28 21:51:02 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2016-06-16 21:46:02 +02:00
|
|
|
if (IS_EAGAIN_OR_EWOULDBLOCK(errno)) {
|
2020-01-26 13:13:42 +01:00
|
|
|
// Disable the flush-timeout if no input has
|
|
|
|
// been seen since the previous flush and thus
|
|
|
|
// there would be nothing to flush after the
|
|
|
|
// timeout expires (avoids busy waiting).
|
|
|
|
const int timeout = pair->src_has_seen_input
|
|
|
|
? mytime_get_flush_timeout()
|
|
|
|
: -1;
|
|
|
|
|
|
|
|
switch (io_wait(pair, timeout, true)) {
|
2013-07-04 13:18:46 +02:00
|
|
|
case IO_WAIT_MORE:
|
2013-06-28 21:51:02 +02:00
|
|
|
continue;
|
|
|
|
|
2013-07-04 13:18:46 +02:00
|
|
|
case IO_WAIT_ERROR:
|
|
|
|
return SIZE_MAX;
|
|
|
|
|
|
|
|
case IO_WAIT_TIMEOUT:
|
2020-01-26 19:19:19 +01:00
|
|
|
pair->flush_needed = true;
|
2020-01-26 12:47:31 +01:00
|
|
|
return pos;
|
2013-07-04 13:18:46 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
message_bug();
|
|
|
|
}
|
2013-06-28 21:51:02 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
message_error(_("%s: Read error: %s"),
|
2007-12-08 23:42:33 +01:00
|
|
|
pair->src_name, strerror(errno));
|
|
|
|
|
|
|
|
return SIZE_MAX;
|
|
|
|
}
|
|
|
|
|
2020-01-26 12:47:31 +01:00
|
|
|
pos += (size_t)(amount);
|
2020-01-26 19:53:25 +01:00
|
|
|
|
|
|
|
if (!pair->src_has_seen_input) {
|
|
|
|
pair->src_has_seen_input = true;
|
|
|
|
mytime_set_flush_time();
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2020-01-26 12:47:31 +01:00
|
|
|
return pos;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-24 21:46:11 +01:00
|
|
|
extern bool
|
2019-06-23 23:40:45 +02:00
|
|
|
io_seek_src(file_pair *pair, uint64_t pos)
|
2010-01-24 21:46:11 +01:00
|
|
|
{
|
2019-06-23 23:40:45 +02:00
|
|
|
// Caller must not attempt to seek past the end of the input file
|
|
|
|
// (seeking to 100 in a 100-byte file is seeking to the end of
|
|
|
|
// the file, not past the end of the file, and thus that is allowed).
|
|
|
|
//
|
|
|
|
// This also validates that pos can be safely cast to off_t.
|
|
|
|
if (pos > (uint64_t)(pair->src_st.st_size))
|
|
|
|
message_bug();
|
|
|
|
|
2019-06-24 00:24:17 +02:00
|
|
|
if (lseek(pair->src_fd, (off_t)(pos), SEEK_SET) == -1) {
|
2010-01-24 21:46:11 +01:00
|
|
|
message_error(_("%s: Error seeking the file: %s"),
|
|
|
|
pair->src_name, strerror(errno));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-05 17:47:22 +02:00
|
|
|
pair->src_eof = false;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern bool
|
2019-06-23 23:40:45 +02:00
|
|
|
io_pread(file_pair *pair, io_buf *buf, size_t size, uint64_t pos)
|
2017-04-05 17:47:22 +02:00
|
|
|
{
|
|
|
|
// Using lseek() and read() is more portable than pread() and
|
|
|
|
// for us it is as good as real pread().
|
|
|
|
if (io_seek_src(pair, pos))
|
|
|
|
return true;
|
|
|
|
|
2010-01-24 21:46:11 +01:00
|
|
|
const size_t amount = io_read(pair, buf, size);
|
|
|
|
if (amount == SIZE_MAX)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (amount != size) {
|
|
|
|
message_error(_("%s: Unexpected end of file"),
|
|
|
|
pair->src_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-25 10:19:20 +01:00
|
|
|
static bool
|
|
|
|
is_sparse(const io_buf *buf)
|
|
|
|
{
|
|
|
|
assert(IO_BUFFER_SIZE % sizeof(uint64_t) == 0);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(buf->u64); ++i)
|
|
|
|
if (buf->u64[i] != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
io_write_buf(file_pair *pair, const uint8_t *buf, size_t size)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
|
|
|
assert(size < SSIZE_MAX);
|
|
|
|
|
|
|
|
while (size > 0) {
|
|
|
|
const ssize_t amount = write(pair->dest_fd, buf, size);
|
|
|
|
if (amount == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
if (user_abort)
|
2013-06-28 22:56:17 +02:00
|
|
|
return true;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-29 14:59:13 +02:00
|
|
|
#ifndef TUKLIB_DOSLIKE
|
2016-06-16 21:46:02 +02:00
|
|
|
if (IS_EAGAIN_OR_EWOULDBLOCK(errno)) {
|
2013-07-04 13:18:46 +02:00
|
|
|
if (io_wait(pair, -1, false) == IO_WAIT_MORE)
|
2013-06-29 14:59:13 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-04 21:29:27 +02:00
|
|
|
// Handle broken pipe specially. gzip and bzip2
|
|
|
|
// don't print anything on SIGPIPE. In addition,
|
|
|
|
// gzip --quiet uses exit status 2 (warning) on
|
|
|
|
// broken pipe instead of whatever raise(SIGPIPE)
|
|
|
|
// would make it return. It is there to hide "Broken
|
|
|
|
// pipe" message on some old shells (probably old
|
|
|
|
// GNU bash).
|
|
|
|
//
|
|
|
|
// We don't do anything special with --quiet, which
|
2008-11-19 19:46:52 +01:00
|
|
|
// is what bzip2 does too. If we get SIGPIPE, we
|
|
|
|
// will handle it like other signals by setting
|
|
|
|
// user_abort, and get EPIPE here.
|
|
|
|
if (errno != EPIPE)
|
|
|
|
message_error(_("%s: Write error: %s"),
|
2007-12-08 23:42:33 +01:00
|
|
|
pair->dest_name, strerror(errno));
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
return true;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buf += (size_t)(amount);
|
|
|
|
size -= (size_t)(amount);
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
return false;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
2009-11-25 10:19:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
extern bool
|
|
|
|
io_write(file_pair *pair, const io_buf *buf, size_t size)
|
|
|
|
{
|
|
|
|
assert(size <= IO_BUFFER_SIZE);
|
|
|
|
|
|
|
|
if (pair->dest_try_sparse) {
|
|
|
|
// Check if the block is sparse (contains only zeros). If it
|
|
|
|
// sparse, we just store the amount and return. We will take
|
|
|
|
// care of actually skipping over the hole when we hit the
|
|
|
|
// next data block or close the file.
|
|
|
|
//
|
|
|
|
// Since io_close() requires that dest_pending_sparse > 0
|
|
|
|
// if the file ends with sparse block, we must also return
|
|
|
|
// if size == 0 to avoid doing the lseek().
|
|
|
|
if (size == IO_BUFFER_SIZE) {
|
2019-06-24 19:45:49 +02:00
|
|
|
// Even if the block was sparse, treat it as non-sparse
|
|
|
|
// if the pending sparse amount is large compared to
|
|
|
|
// the size of off_t. In practice this only matters
|
|
|
|
// on 32-bit systems where off_t isn't always 64 bits.
|
|
|
|
const off_t pending_max
|
|
|
|
= (off_t)(1) << (sizeof(off_t) * CHAR_BIT - 2);
|
|
|
|
if (is_sparse(buf) && pair->dest_pending_sparse
|
|
|
|
< pending_max) {
|
|
|
|
pair->dest_pending_sparse += (off_t)(size);
|
2009-11-25 10:19:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (size == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is not a sparse block. If we have a pending hole,
|
|
|
|
// skip it now.
|
|
|
|
if (pair->dest_pending_sparse > 0) {
|
|
|
|
if (lseek(pair->dest_fd, pair->dest_pending_sparse,
|
|
|
|
SEEK_CUR) == -1) {
|
|
|
|
message_error(_("%s: Seeking failed when "
|
|
|
|
"trying to create a sparse "
|
|
|
|
"file: %s"), pair->dest_name,
|
|
|
|
strerror(errno));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pair->dest_pending_sparse = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return io_write_buf(pair, buf->u8, size);
|
|
|
|
}
|