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

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