Blame SOURCES/0126-lib-introduce-a-new-function-copy_file_ext.patch

4b6aa8
From e705c7ff8b6907422753b44ad2bd9d8293578098 Mon Sep 17 00:00:00 2001
4b6aa8
From: Jakub Filak <jfilak@redhat.com>
4b6aa8
Date: Wed, 15 Apr 2015 15:17:47 +0200
4b6aa8
Subject: [LIBREPORT PATCH] lib: introduce a new function copy_file_ext
4b6aa8
4b6aa8
The new function allows to specify UID, GID and open() flags for both
4b6aa8
source and destination files.
4b6aa8
4b6aa8
This function is need to avoid race conditions and symbolic link issues.
4b6aa8
4b6aa8
Related: #1211835
4b6aa8
4b6aa8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
4b6aa8
---
4b6aa8
 src/include/internal_libreport.h |  2 ++
4b6aa8
 src/lib/copyfd.c                 | 21 ++++++++++++++++++---
4b6aa8
 2 files changed, 20 insertions(+), 3 deletions(-)
4b6aa8
4b6aa8
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
4b6aa8
index 967324b..4c5c72a 100644
4b6aa8
--- a/src/include/internal_libreport.h
4b6aa8
+++ b/src/include/internal_libreport.h
4b6aa8
@@ -153,6 +153,8 @@ off_t copyfd_eof(int src_fd, int dst_fd, int flags);
4b6aa8
 off_t copyfd_size(int src_fd, int dst_fd, off_t size, int flags);
4b6aa8
 #define copyfd_exact_size libreport_copyfd_exact_size
4b6aa8
 void copyfd_exact_size(int src_fd, int dst_fd, off_t size);
4b6aa8
+#define copy_file_ext libreport_copy_file_ext
4b6aa8
+off_t copy_file_ext(const char *src_name, const char *dst_name, int mode, uid_t uid, gid_t gid, int src_flags, int dst_flags);
4b6aa8
 #define copy_file libreport_copy_file
4b6aa8
 off_t copy_file(const char *src_name, const char *dst_name, int mode);
4b6aa8
 #define copy_file_recursive libreport_copy_file_recursive
4b6aa8
diff --git a/src/lib/copyfd.c b/src/lib/copyfd.c
4b6aa8
index e9f429d..64fece7 100644
4b6aa8
--- a/src/lib/copyfd.c
4b6aa8
+++ b/src/lib/copyfd.c
4b6aa8
@@ -149,16 +149,16 @@ off_t copyfd_eof(int fd1, int fd2, int flags)
4b6aa8
 	return full_fd_action(fd1, fd2, 0, flags);
4b6aa8
 }
4b6aa8
 
4b6aa8
-off_t copy_file(const char *src_name, const char *dst_name, int mode)
4b6aa8
+off_t copy_file_ext(const char *src_name, const char *dst_name, int mode, uid_t uid, gid_t gid, int src_flags, int dst_flags)
4b6aa8
 {
4b6aa8
     off_t r;
4b6aa8
-    int src = open(src_name, O_RDONLY);
4b6aa8
+    int src = open(src_name, src_flags);
4b6aa8
     if (src < 0)
4b6aa8
     {
4b6aa8
         perror_msg("Can't open '%s'", src_name);
4b6aa8
         return -1;
4b6aa8
     }
4b6aa8
-    int dst = open(dst_name, O_WRONLY | O_TRUNC | O_CREAT, mode);
4b6aa8
+    int dst = open(dst_name, dst_flags, mode);
4b6aa8
     if (dst < 0)
4b6aa8
     {
4b6aa8
         close(src);
4b6aa8
@@ -167,6 +167,21 @@ off_t copy_file(const char *src_name, const char *dst_name, int mode)
4b6aa8
     }
4b6aa8
     r = copyfd_eof(src, dst, /*flags:*/ 0);
4b6aa8
     close(src);
4b6aa8
+    if (uid != (uid_t)-1L)
4b6aa8
+    {
4b6aa8
+        if (fchown(dst, uid, gid) == -1)
4b6aa8
+        {
4b6aa8
+            perror_msg("Can't change '%s' ownership to %lu:%lu", dst_name, (long)uid, (long)gid);
4b6aa8
+            close(dst);
4b6aa8
+            unlink(dst_name);
4b6aa8
+            return -1;
4b6aa8
+        }
4b6aa8
+    }
4b6aa8
     close(dst);
4b6aa8
     return r;
4b6aa8
 }
4b6aa8
+
4b6aa8
+off_t copy_file(const char *src_name, const char *dst_name, int mode)
4b6aa8
+{
4b6aa8
+    return copy_file_ext(src_name, dst_name, mode, -1, -1, O_RDONLY, O_WRONLY | O_TRUNC | O_CREAT);
4b6aa8
+}
4b6aa8
-- 
4b6aa8
1.8.3.1
4b6aa8