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

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