[nautilus] files-view: use GFile utilities



commit 672ccbc4dbde12b9a91a1adf6758550cd114aef2
Author: Ernestas Kulik <ernestask src gnome org>
Date:   Sun Aug 7 11:21:28 2016 +0300

    files-view: use GFile utilities
    
    set_up_scripts_directory_global() currently uses the POSIX file
    interface, which is inconsistent with the rest of the code. This commit
    rewrites the parts of the function to use the GFile API.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769602

 src/nautilus-files-view.c |   92 +++++++++++++++++++++++++++++---------------
 1 files changed, 60 insertions(+), 32 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index c0acbcb..e138e1f 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -2255,9 +2255,13 @@ show_hidden_files_changed_callback (gpointer callback_data)
 static gboolean
 set_up_scripts_directory_global (void)
 {
-        char *old_scripts_directory_path;
-        char *scripts_directory_path;
+        g_autofree gchar *old_scripts_directory_path = NULL;
+        g_autoptr (GFile) old_scripts_directory = NULL;
+        g_autofree gchar *scripts_directory_path = NULL;
+        g_autoptr (GFile) scripts_directory = NULL;
         const char *override;
+        GFileType file_type;
+        g_autoptr (GError) error = NULL;
 
         if (scripts_directory_uri != NULL) {
                 return TRUE;
@@ -2278,49 +2282,73 @@ set_up_scripts_directory_global (void)
                                                                NULL);
         }
 
-        if (g_file_test (old_scripts_directory_path, G_FILE_TEST_IS_DIR)
-            && !g_file_test (scripts_directory_path, G_FILE_TEST_EXISTS)) {
-                char *updated;
+        old_scripts_directory = g_file_new_for_path (old_scripts_directory_path);
+        scripts_directory = g_file_new_for_path (scripts_directory_path);
+
+        file_type = g_file_query_file_type (old_scripts_directory,
+                                            G_FILE_QUERY_INFO_NONE,
+                                            NULL);
+
+        if (file_type == G_FILE_TYPE_DIRECTORY &&
+            !g_file_query_exists (scripts_directory, NULL)) {
+                g_autoptr (GFile) updated;
                 const char *message;
 
                 /* test if we already attempted to migrate first */
-                updated = g_build_filename (old_scripts_directory_path, "DEPRECATED-DIRECTORY", NULL);
+                updated = g_file_get_child (old_scripts_directory, "DEPRECATED-DIRECTORY");
                 message = _("Nautilus 3.6 deprecated this directory and tried migrating "
                             "this configuration to ~/.local/share/nautilus");
-                if (!g_file_test (updated, G_FILE_TEST_EXISTS)) {
-                        char *parent_dir;
-
-                        parent_dir = g_path_get_dirname (scripts_directory_path);
-                        if (g_mkdir_with_parents (parent_dir, 0700) == 0) {
-                                int fd, res;
-
-                                /* rename() works fine if the destination directory is
-                                 * empty.
-                                 */
-                                res = g_rename (old_scripts_directory_path, scripts_directory_path);
-                                if (res == -1) {
-                                        fd = g_creat (updated, 0600);
-                                        if (fd != -1) {
-                                                write (fd, message, strlen (message));
-                                                close (fd);
-                                        }
+                if (!g_file_query_exists (updated, NULL)) {
+                        g_autoptr (GFile) parent = g_file_get_parent (scripts_directory);
+                        g_autoptr (GError) error = NULL;
+
+                        g_file_make_directory_with_parents (parent, NULL, &error);
+
+                        if (error == NULL ||
+                            g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
+                                g_clear_error (&error);
+
+                                g_file_set_attribute_uint32 (parent,
+                                                             G_FILE_ATTRIBUTE_UNIX_MODE,
+                                                             S_IRWXU,
+                                                             G_FILE_QUERY_INFO_NONE,
+                                                             NULL, NULL);
+
+                                g_file_move (old_scripts_directory,
+                                             scripts_directory,
+                                             G_FILE_COPY_NONE,
+                                             NULL, NULL, NULL,
+                                             &error);
+
+                                if (error == NULL) {
+                                        g_file_replace_contents (updated,
+                                                                 message, strlen (message),
+                                                                 NULL,
+                                                                 FALSE,
+                                                                 G_FILE_CREATE_PRIVATE,
+                                                                 NULL, NULL, NULL);
                                 }
                         }
-                        g_free (parent_dir);
-                }
 
-                g_free (updated);
+                        g_clear_error (&error);
+                }
         }
 
-        if (g_mkdir_with_parents (scripts_directory_path, 0700) == 0) {
-                scripts_directory_uri = g_filename_to_uri (scripts_directory_path, NULL, NULL);
+        g_file_make_directory_with_parents (scripts_directory, NULL, &error);
+
+        if (error == NULL ||
+            g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
+                g_file_set_attribute_uint32 (scripts_directory,
+                                             G_FILE_ATTRIBUTE_UNIX_MODE,
+                                             S_IRWXU,
+                                             G_FILE_QUERY_INFO_NONE,
+                                             NULL, NULL);
+
+                scripts_directory_uri = g_file_get_uri (scripts_directory);
                 scripts_directory_uri_length = strlen (scripts_directory_uri);
         }
 
-        g_free (scripts_directory_path);
-        g_free (old_scripts_directory_path);
-
-        return (scripts_directory_uri != NULL) ? TRUE : FALSE;
+        return scripts_directory_uri != NULL;
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]