Blame support/support_copy_file.c

Packit Service 23f266
/* Copy a file from one path to another.
Packit Service 23f266
   Copyright (C) 2020 Free Software Foundation, Inc.
Packit Service 23f266
   This file is part of the GNU C Library.
Packit Service 23f266
Packit Service 23f266
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 23f266
   modify it under the terms of the GNU Lesser General Public
Packit Service 23f266
   License as published by the Free Software Foundation; either
Packit Service 23f266
   version 2.1 of the License, or (at your option) any later version.
Packit Service 23f266
Packit Service 23f266
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 23f266
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 23f266
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 23f266
   Lesser General Public License for more details.
Packit Service 23f266
Packit Service 23f266
   You should have received a copy of the GNU Lesser General Public
Packit Service 23f266
   License along with the GNU C Library; if not, see
Packit Service 23f266
   <https://www.gnu.org/licenses/>.  */
Packit Service 23f266
Packit Service 23f266
#include <fcntl.h>
Packit Service 23f266
#include <support/check.h>
Packit Service 23f266
#include <support/support.h>
Packit Service 23f266
#include <support/xunistd.h>
Packit Service 23f266
Packit Service 23f266
void
Packit Service 23f266
support_copy_file (const char *from, const char *to)
Packit Service 23f266
{
Packit Service 23f266
  struct stat64 st;
Packit Service 23f266
  xstat (from, &st);
Packit Service 23f266
  int fd_from = xopen (from, O_RDONLY, 0);
Packit Service 23f266
  mode_t mode = st.st_mode & 0777;
Packit Service 23f266
  int fd_to = xopen (to, O_WRONLY | O_TRUNC | O_CREAT, mode);
Packit Service 23f266
  ssize_t ret = support_copy_file_range (fd_from, NULL, fd_to, NULL,
Packit Service 23f266
                                         st.st_size, 0);
Packit Service 23f266
  if (ret < 0)
Packit Service 23f266
    FAIL_EXIT1 ("copying from \"%s\" to \"%s\": %m", from, to);
Packit Service 23f266
  if (ret != st.st_size)
Packit Service 23f266
    FAIL_EXIT1 ("copying from \"%s\" to \"%s\": only %zd of %llu bytes copied",
Packit Service 23f266
                from, to, ret, (unsigned long long int) st.st_size);
Packit Service 23f266
  if (fchmod (fd_to, mode) < 0)
Packit Service 23f266
    FAIL_EXIT1 ("fchmod on %s to 0%o: %m", to, mode);
Packit Service 23f266
  xclose (fd_to);
Packit Service 23f266
  xclose (fd_from);
Packit Service 23f266
}