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