446cf2
commit 603ae243f6fe03208a3bb92adecf72403367bd95
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Thu Nov 26 16:59:44 2020 +0100
446cf2
446cf2
    support: Add support_copy_file
446cf2
    
446cf2
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
446cf2
446cf2
diff --git a/support/Makefile b/support/Makefile
446cf2
index 895b83a426369b0c..35b21b19a248ba7f 100644
446cf2
--- a/support/Makefile
446cf2
+++ b/support/Makefile
446cf2
@@ -46,6 +46,7 @@ libsupport-routines = \
446cf2
   support_capture_subprocess \
446cf2
   support_capture_subprocess_check \
446cf2
   support_chroot \
446cf2
+  support_copy_file \
446cf2
   support_copy_file_range \
446cf2
   support_descriptor_supports_holes \
446cf2
   support_descriptors \
446cf2
diff --git a/support/support.h b/support/support.h
446cf2
index 3af87f85fe1b762d..6f7f804847f67600 100644
446cf2
--- a/support/support.h
446cf2
+++ b/support/support.h
446cf2
@@ -115,6 +115,11 @@ extern const char support_install_rootsbindir[];
446cf2
 /* Corresponds to the install's compiled locale directory.  */
446cf2
 extern const char support_complocaledir_prefix[];
446cf2
 
446cf2
+/* Copies the file at the path FROM to TO.  If TO does not exist, it
446cf2
+   is created.  If TO is a regular file, it is truncated before
446cf2
+   copying.  The file mode is copied, but the permissions are not.  */
446cf2
+extern void support_copy_file (const char *from, const char *to);
446cf2
+
446cf2
 extern ssize_t support_copy_file_range (int, off64_t *, int, off64_t *,
446cf2
 					size_t, unsigned int);
446cf2
 
446cf2
diff --git a/support/support_copy_file.c b/support/support_copy_file.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..c93e1e58c81b749d
446cf2
--- /dev/null
446cf2
+++ b/support/support_copy_file.c
446cf2
@@ -0,0 +1,43 @@
446cf2
+/* Copy a file from one path to another.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <fcntl.h>
446cf2
+#include <support/check.h>
446cf2
+#include <support/support.h>
446cf2
+#include <support/xunistd.h>
446cf2
+
446cf2
+void
446cf2
+support_copy_file (const char *from, const char *to)
446cf2
+{
446cf2
+  struct stat64 st;
446cf2
+  xstat (from, &st);
446cf2
+  int fd_from = xopen (from, O_RDONLY, 0);
446cf2
+  mode_t mode = st.st_mode & 0777;
446cf2
+  int fd_to = xopen (to, O_WRONLY | O_TRUNC | O_CREAT, mode);
446cf2
+  ssize_t ret = support_copy_file_range (fd_from, NULL, fd_to, NULL,
446cf2
+                                         st.st_size, 0);
446cf2
+  if (ret < 0)
446cf2
+    FAIL_EXIT1 ("copying from \"%s\" to \"%s\": %m", from, to);
446cf2
+  if (ret != st.st_size)
446cf2
+    FAIL_EXIT1 ("copying from \"%s\" to \"%s\": only %zd of %llu bytes copied",
446cf2
+                from, to, ret, (unsigned long long int) st.st_size);
446cf2
+  if (fchmod (fd_to, mode) < 0)
446cf2
+    FAIL_EXIT1 ("fchmod on %s to 0%o: %m", to, mode);
446cf2
+  xclose (fd_to);
446cf2
+  xclose (fd_from);
446cf2
+}