|
Packit Service |
6b2d43 |
/* Simplified copy_file_range with cross-device copy.
|
|
Packit Service |
6b2d43 |
Copyright (C) 2018 Free Software Foundation, Inc.
|
|
Packit Service |
6b2d43 |
This file is part of the GNU C Library.
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
6b2d43 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
6b2d43 |
License as published by the Free Software Foundation; either
|
|
Packit Service |
6b2d43 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
6b2d43 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
6b2d43 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
6b2d43 |
Lesser General Public License for more details.
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
6b2d43 |
License along with the GNU C Library; if not, see
|
|
Packit Service |
6b2d43 |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
#include <errno.h>
|
|
Packit Service |
6b2d43 |
#include <fcntl.h>
|
|
Packit Service |
6b2d43 |
#include <inttypes.h>
|
|
Packit Service |
6b2d43 |
#include <limits.h>
|
|
Packit Service |
6b2d43 |
#include <sys/stat.h>
|
|
Packit Service |
6b2d43 |
#include <sys/types.h>
|
|
Packit Service |
6b2d43 |
#include <unistd.h>
|
|
Packit Service |
6b2d43 |
#include <support/support.h>
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
ssize_t
|
|
Packit Service |
6b2d43 |
support_copy_file_range (int infd, __off64_t *pinoff,
|
|
Packit Service |
6b2d43 |
int outfd, __off64_t *poutoff,
|
|
Packit Service |
6b2d43 |
size_t length, unsigned int flags)
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
if (flags != 0)
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
errno = EINVAL;
|
|
Packit Service |
6b2d43 |
return -1;
|
|
Packit Service |
6b2d43 |
}
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
struct stat64 instat;
|
|
Packit Service |
6b2d43 |
struct stat64 outstat;
|
|
Packit Service |
6b2d43 |
if (fstat64 (infd, &instat) != 0 || fstat64 (outfd, &outstat) != 0)
|
|
Packit Service |
6b2d43 |
return -1;
|
|
Packit Service |
6b2d43 |
if (S_ISDIR (instat.st_mode) || S_ISDIR (outstat.st_mode))
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
errno = EISDIR;
|
|
Packit Service |
6b2d43 |
return -1;
|
|
Packit Service |
6b2d43 |
}
|
|
Packit Service |
6b2d43 |
if (!S_ISREG (instat.st_mode) || !S_ISREG (outstat.st_mode))
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
/* We need a regular input file so that the we can seek
|
|
Packit Service |
6b2d43 |
backwards in case of a write failure. */
|
|
Packit Service |
6b2d43 |
errno = EINVAL;
|
|
Packit Service |
6b2d43 |
return -1;
|
|
Packit Service |
6b2d43 |
}
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
/* The output descriptor must not have O_APPEND set. */
|
|
Packit Service |
6b2d43 |
if (fcntl (outfd, F_GETFL) & O_APPEND)
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
errno = EBADF;
|
|
Packit Service |
6b2d43 |
return -1;
|
|
Packit Service |
6b2d43 |
}
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
/* Avoid an overflow in the result. */
|
|
Packit Service |
6b2d43 |
if (length > SSIZE_MAX)
|
|
Packit Service |
6b2d43 |
length = SSIZE_MAX;
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
/* Main copying loop. The buffer size is arbitrary and is a
|
|
Packit Service |
6b2d43 |
trade-off between stack size consumption, cache usage, and
|
|
Packit Service |
6b2d43 |
amortization of system call overhead. */
|
|
Packit Service |
6b2d43 |
size_t copied = 0;
|
|
Packit Service |
6b2d43 |
char buf[8192];
|
|
Packit Service |
6b2d43 |
while (length > 0)
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
size_t to_read = length;
|
|
Packit Service |
6b2d43 |
if (to_read > sizeof (buf))
|
|
Packit Service |
6b2d43 |
to_read = sizeof (buf);
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
/* Fill the buffer. */
|
|
Packit Service |
6b2d43 |
ssize_t read_count;
|
|
Packit Service |
6b2d43 |
if (pinoff == NULL)
|
|
Packit Service |
6b2d43 |
read_count = read (infd, buf, to_read);
|
|
Packit Service |
6b2d43 |
else
|
|
Packit Service |
6b2d43 |
read_count = pread64 (infd, buf, to_read, *pinoff);
|
|
Packit Service |
6b2d43 |
if (read_count == 0)
|
|
Packit Service |
6b2d43 |
/* End of file reached prematurely. */
|
|
Packit Service |
6b2d43 |
return copied;
|
|
Packit Service |
6b2d43 |
if (read_count < 0)
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
if (copied > 0)
|
|
Packit Service |
6b2d43 |
/* Report the number of bytes copied so far. */
|
|
Packit Service |
6b2d43 |
return copied;
|
|
Packit Service |
6b2d43 |
return -1;
|
|
Packit Service |
6b2d43 |
}
|
|
Packit Service |
6b2d43 |
if (pinoff != NULL)
|
|
Packit Service |
6b2d43 |
*pinoff += read_count;
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
/* Write the buffer part which was read to the destination. */
|
|
Packit Service |
6b2d43 |
char *end = buf + read_count;
|
|
Packit Service |
6b2d43 |
for (char *p = buf; p < end; )
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
ssize_t write_count;
|
|
Packit Service |
6b2d43 |
if (poutoff == NULL)
|
|
Packit Service |
6b2d43 |
write_count = write (outfd, p, end - p);
|
|
Packit Service |
6b2d43 |
else
|
|
Packit Service |
6b2d43 |
write_count = pwrite64 (outfd, p, end - p, *poutoff);
|
|
Packit Service |
6b2d43 |
if (write_count < 0)
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
/* Adjust the input read position to match what we have
|
|
Packit Service |
6b2d43 |
written, so that the caller can pick up after the
|
|
Packit Service |
6b2d43 |
error. */
|
|
Packit Service |
6b2d43 |
size_t written = p - buf;
|
|
Packit Service |
6b2d43 |
/* NB: This needs to be signed so that we can form the
|
|
Packit Service |
6b2d43 |
negative value below. */
|
|
Packit Service |
6b2d43 |
ssize_t overread = read_count - written;
|
|
Packit Service |
6b2d43 |
if (pinoff == NULL)
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
if (overread > 0)
|
|
Packit Service |
6b2d43 |
{
|
|
Packit Service |
6b2d43 |
/* We are on an error recovery path, so we
|
|
Packit Service |
6b2d43 |
cannot deal with failure here. */
|
|
Packit Service |
6b2d43 |
int save_errno = errno;
|
|
Packit Service |
6b2d43 |
(void) lseek64 (infd, -overread, SEEK_CUR);
|
|
Packit Service |
6b2d43 |
errno = save_errno;
|
|
Packit Service |
6b2d43 |
}
|
|
Packit Service |
6b2d43 |
}
|
|
Packit Service |
6b2d43 |
else /* pinoff != NULL */
|
|
Packit Service |
6b2d43 |
*pinoff -= overread;
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
if (copied + written > 0)
|
|
Packit Service |
6b2d43 |
/* Report the number of bytes copied so far. */
|
|
Packit Service |
6b2d43 |
return copied + written;
|
|
Packit Service |
6b2d43 |
return -1;
|
|
Packit Service |
6b2d43 |
}
|
|
Packit Service |
6b2d43 |
p += write_count;
|
|
Packit Service |
6b2d43 |
if (poutoff != NULL)
|
|
Packit Service |
6b2d43 |
*poutoff += write_count;
|
|
Packit Service |
6b2d43 |
} /* Write loop. */
|
|
Packit Service |
6b2d43 |
|
|
Packit Service |
6b2d43 |
copied += read_count;
|
|
Packit Service |
6b2d43 |
length -= read_count;
|
|
Packit Service |
6b2d43 |
}
|
|
Packit Service |
6b2d43 |
return copied;
|
|
Packit Service |
6b2d43 |
}
|