hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame io/copy_file_range-compat.c

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