6b172a
From 08f5ab3c3a1877e4a8965a9075bd7675f64eae53 Mon Sep 17 00:00:00 2001
6b172a
From: Philip Withnall <withnall@endlessm.com>
6b172a
Date: Fri, 27 Sep 2019 14:46:18 +0100
6b172a
Subject: [PATCH 1/2] gfile: Factor out flags when copying files
6b172a
6b172a
This introduces no functional changes; just reduces duplication in the
6b172a
code a little.
6b172a
6b172a
Signed-off-by: Philip Withnall <withnall@endlessm.com>
6b172a
---
6b172a
 gio/gfile.c | 15 +++++++++------
6b172a
 1 file changed, 9 insertions(+), 6 deletions(-)
6b172a
6b172a
diff --git a/gio/gfile.c b/gio/gfile.c
6b172a
index 29ebaaa62..a617b4cc8 100644
6b172a
--- a/gio/gfile.c
6b172a
+++ b/gio/gfile.c
6b172a
@@ -3184,6 +3184,7 @@ file_copy_fallback (GFile                  *source,
6b172a
   const char *target;
6b172a
   char *attrs_to_read;
6b172a
   gboolean do_set_attributes = FALSE;
6b172a
+  GFileCreateFlags create_flags;
6b172a
 
6b172a
   /* need to know the file type */
6b172a
   info = g_file_query_info (source,
6b172a
@@ -3274,18 +3275,21 @@ file_copy_fallback (GFile                  *source,
6b172a
    * If a future API like g_file_replace_with_info() is added, switch
6b172a
    * this code to use that.
6b172a
    */
6b172a
+  create_flags = G_FILE_CREATE_PRIVATE;
6b172a
+  if (flags & G_FILE_COPY_OVERWRITE)
6b172a
+    create_flags |= G_FILE_CREATE_REPLACE_DESTINATION;
6b172a
+
6b172a
   if (G_IS_LOCAL_FILE (destination))
6b172a
     {
6b172a
       if (flags & G_FILE_COPY_OVERWRITE)
6b172a
         out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
6b172a
                                                                    FALSE, NULL,
6b172a
                                                                    flags & G_FILE_COPY_BACKUP,
6b172a
-                                                                   G_FILE_CREATE_REPLACE_DESTINATION |
6b172a
-                                                                   G_FILE_CREATE_PRIVATE, info,
6b172a
+                                                                   create_flags, info,
6b172a
                                                                    cancellable, error);
6b172a
       else
6b172a
         out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
6b172a
-                                                                  FALSE, G_FILE_CREATE_PRIVATE, info,
6b172a
+                                                                  FALSE, create_flags, info,
6b172a
                                                                   cancellable, error);
6b172a
     }
6b172a
   else if (flags & G_FILE_COPY_OVERWRITE)
6b172a
@@ -3293,13 +3297,12 @@ file_copy_fallback (GFile                  *source,
6b172a
       out = (GOutputStream *)g_file_replace (destination,
6b172a
                                              NULL,
6b172a
                                              flags & G_FILE_COPY_BACKUP,
6b172a
-                                             G_FILE_CREATE_REPLACE_DESTINATION |
6b172a
-                                             G_FILE_CREATE_PRIVATE,
6b172a
+                                             create_flags,
6b172a
                                              cancellable, error);
6b172a
     }
6b172a
   else
6b172a
     {
6b172a
-      out = (GOutputStream *)g_file_create (destination, G_FILE_CREATE_PRIVATE, cancellable, error);
6b172a
+      out = (GOutputStream *)g_file_create (destination, create_flags, cancellable, error);
6b172a
     }
6b172a
 
6b172a
   if (!out)
6b172a
-- 
6b172a
2.37.3
6b172a
6b172a
From b37d628c01da0bd61348b3ac73b7a436af008d8d Mon Sep 17 00:00:00 2001
6b172a
From: Philip Withnall <withnall@endlessm.com>
6b172a
Date: Fri, 27 Sep 2019 15:02:32 +0100
6b172a
Subject: [PATCH 2/2] =?UTF-8?q?gfile:=20Don=E2=80=99t=20copy=20files=20as?=
6b172a
 =?UTF-8?q?=20private=20if=20using=20default=20permissions?=
6b172a
MIME-Version: 1.0
6b172a
Content-Type: text/plain; charset=UTF-8
6b172a
Content-Transfer-Encoding: 8bit
6b172a
6b172a
If a copy operation is started with `G_FILE_COPY_TARGET_DEFAULT_PERMS`,
6b172a
don’t create the destination file as private. Instead, create it with
6b172a
the process’ current umask (i.e. ‘default permissions’).
6b172a
6b172a
This is a partial re-work of commit d8f8f4d637ce43f8699ba94c9b, with
6b172a
input from Ondrej Holy.
6b172a
6b172a
Signed-off-by: Philip Withnall <withnall@endlessm.com>
6b172a
6b172a
Fixes: #174
6b172a
---
6b172a
 gio/gfile.c | 22 +++++++++++++++++++---
6b172a
 1 file changed, 19 insertions(+), 3 deletions(-)
6b172a
6b172a
diff --git a/gio/gfile.c b/gio/gfile.c
6b172a
index a617b4cc8..447da3cfb 100644
6b172a
--- a/gio/gfile.c
6b172a
+++ b/gio/gfile.c
6b172a
@@ -3274,8 +3274,22 @@ file_copy_fallback (GFile                  *source,
6b172a
    *
6b172a
    * If a future API like g_file_replace_with_info() is added, switch
6b172a
    * this code to use that.
6b172a
+   *
6b172a
+   * Use %G_FILE_CREATE_PRIVATE unless
6b172a
+   *  - we were told to create the file with default permissions (i.e. the
6b172a
+   *    process’ umask),
6b172a
+   *  - or if the source file is on a file system which doesn’t support
6b172a
+   *    `unix::mode` (in which case it probably also makes sense to create the
6b172a
+   *    destination with default permissions because the source cannot be
6b172a
+   *    private),
6b172a
+   *  - or if the destination file is a `GLocalFile`, in which case we can
6b172a
+   *    directly open() it with the permissions from the source file.
6b172a
    */
6b172a
-  create_flags = G_FILE_CREATE_PRIVATE;
6b172a
+  create_flags = G_FILE_CREATE_NONE;
6b172a
+  if (!(flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) &&
6b172a
+      g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE) &&
6b172a
+      !G_IS_LOCAL_FILE (destination))
6b172a
+    create_flags |= G_FILE_CREATE_PRIVATE;
6b172a
   if (flags & G_FILE_COPY_OVERWRITE)
6b172a
     create_flags |= G_FILE_CREATE_REPLACE_DESTINATION;
6b172a
 
6b172a
@@ -3285,11 +3299,13 @@ file_copy_fallback (GFile                  *source,
6b172a
         out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
6b172a
                                                                    FALSE, NULL,
6b172a
                                                                    flags & G_FILE_COPY_BACKUP,
6b172a
-                                                                   create_flags, info,
6b172a
+                                                                   create_flags,
6b172a
+                                                                   (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
6b172a
                                                                    cancellable, error);
6b172a
       else
6b172a
         out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
6b172a
-                                                                  FALSE, create_flags, info,
6b172a
+                                                                  FALSE, create_flags,
6b172a
+                                                                  (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
6b172a
                                                                   cancellable, error);
6b172a
     }
6b172a
   else if (flags & G_FILE_COPY_OVERWRITE)
6b172a
-- 
6b172a
2.37.3
6b172a