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