Blame support/support_copy_file_range.c

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