Blame support/support_copy_file_range.c

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