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

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