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