[gnome-commander] Migrate directory check and creation functions to GIO, used already in commit b6e4aefe



commit c7abcae1c886d0616c5f29fb49b994b39b9163e1
Author: Uwe Scholz <u scholz83 gmx de>
Date:   Sat Oct 9 11:18:31 2021 +0200

    Migrate directory check and creation functions to GIO, used already in commit b6e4aefe

 src/plugin_manager.cc | 32 +++++++----------------
 src/utils.cc          | 72 +++++++++++++++------------------------------------
 src/utils.h           |  4 +--
 3 files changed, 32 insertions(+), 76 deletions(-)
---
diff --git a/src/plugin_manager.cc b/src/plugin_manager.cc
index d70947f1..7077bce9 100644
--- a/src/plugin_manager.cc
+++ b/src/plugin_manager.cc
@@ -190,10 +190,13 @@ void plugin_manager_init ()
 
     // find user plugins
     gchar *user_dir = get_plugin_config_location();
-    if (create_dir_if_needed (user_dir))
+    if (!is_dir_existing(user_dir))
     {
-        scan_plugins_in_dir (user_dir);
+        create_dir (user_dir);
     }
+
+    scan_plugins_in_dir (user_dir);
+
     g_free (user_dir);
 
     // find system plugins
@@ -441,29 +444,12 @@ gchar* get_plugin_config_location()
     string userPluginConfigDir = get_package_config_dir();
     userPluginConfigDir += (char*) "/plugins";
 
-    auto dir_exists = is_dir_existing(userPluginConfigDir.c_str());
-
-    switch (dir_exists)
+    if (!is_dir_existing(userPluginConfigDir.c_str()))
     {
-        case 0:
-        {
-            if (create_dir_if_needed (userPluginConfigDir.c_str()))
-            {
-                returnString = g_strdup(userPluginConfigDir.c_str());
-            }
-            break;
-        }
-        case 1:
-        {
-            returnString = g_strdup(userPluginConfigDir.c_str());
-            break;
-        }
-        case -1:
-        default:
-        {
-            break;
-        }
+        create_dir (userPluginConfigDir.c_str());
     }
 
+    returnString = g_strdup(userPluginConfigDir.c_str());
+
     return returnString;
 }
diff --git a/src/utils.cc b/src/utils.cc
index b66ac583..f68f65b2 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -789,71 +789,41 @@ GList *gnome_cmd_file_list_to_gfile_list (GList *files)
 }
 
 
-/**
- * returns  1 if dir is existing,
- * returns  0 if dir is not existing,
- * returns -1 if dir is not readable
- */
-int is_dir_existing(const gchar *dpath)
+gboolean is_dir_existing(const gchar *dpath)
 {
     g_return_val_if_fail (dpath, FALSE);
+    auto gFile = g_file_new_for_path(dpath);
 
-    DIR *dir = opendir (dpath);
+    auto returnValue = false;
 
-    if (!dir)
+    if (g_file_query_exists(gFile, nullptr))
     {
-        if (errno == ENOENT)
-        {
-            return 0;
-        }
-        else
-            g_warning (_("Couldn’t read from the directory %s: %s"), dpath, strerror (errno));
-
-        return -1;
+        returnValue = true;
     }
-
-    closedir (dir);
-    return 1;
+    g_object_unref(gFile);
+    return returnValue;
 }
 
 
-gboolean create_dir_if_needed (const gchar *dpath)
+gboolean create_dir (const gchar *dpath)
 {
     g_return_val_if_fail (dpath, FALSE);
 
-    auto dir_exists = is_dir_existing(dpath);
-
-    switch (dir_exists)
+    g_print (_("Creating directory %s… "), dpath);
+    GError *error = nullptr;
+    auto gFile = g_file_new_for_path(dpath);
+    if (g_file_make_directory (gFile, nullptr, &error))
     {
-        case -1:
-        {
-            return FALSE;
-        }
-        case 0:
-        {
-            g_print (_("Creating directory %s… "), dpath);
-            if (mkdir (dpath, S_IRUSR|S_IWUSR|S_IXUSR) == 0)
-            {
-                return TRUE;
-            }
-            else
-            {
-                gchar *msg = g_strdup_printf (_("Failed to create the directory %s"), dpath);
-                perror (msg);
-                g_free (msg);
-                return FALSE;
-            }
-            break;
-        }
-        case 1:
-        default:
-        {
-            return TRUE;
-        }
+        g_object_unref(gFile);
+        return TRUE;
+    }
+    else
+    {
+        g_object_unref(gFile);
+        g_warning (_("Failed to create the directory %s: %s"), dpath, error->message);
+        g_error_free (error);
+        return FALSE;
     }
-
-    // never reached
-    return TRUE;
 }
 
 
diff --git a/src/utils.h b/src/utils.h
index d731cf80..a35bf425 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -223,8 +223,8 @@ gchar *unix_to_unc (const gchar *path);
 GdkColor *gdk_color_new (gushort r, gushort g, gushort b);
 GList *gnome_cmd_file_list_to_gfile_list (GList *files);
 
-int is_dir_existing(const gchar *dpath);
-gboolean create_dir_if_needed (const gchar *dpath);
+gboolean is_dir_existing(const gchar *dpath);
+gboolean create_dir (const gchar *dpath);
 
 inline gboolean uri_is_valid (const gchar *uri)
 {


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