Blame support/support_copy_file.c

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