[gnome-commander] Helper method for checking if directory is existing



commit 56621c8d0e5777d0e5eb6843f7884eebddbba1ad
Author: Uwe Scholz <u scholz83 gmx de>
Date:   Sun Mar 3 21:24:08 2019 +0100

    Helper method for checking if directory is existing

 src/utils.cc | 48 +++++++++++++++++++++++++++++++++++++++++-------
 src/utils.h  |  1 +
 2 files changed, 42 insertions(+), 7 deletions(-)
---
diff --git a/src/utils.cc b/src/utils.cc
index 61017444..135e6140 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -926,8 +926,12 @@ GList *file_list_to_info_list (GList *files)
     return infos;
 }
 
-
-gboolean create_dir_if_needed (const gchar *dpath)
+/**
+ * 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)
 {
     g_return_val_if_fail (dpath, FALSE);
 
@@ -936,6 +940,33 @@ gboolean create_dir_if_needed (const gchar *dpath)
     if (!dir)
     {
         if (errno == ENOENT)
+        {
+            return 0;
+        }
+        else
+            g_warning (_("Couldn’t read from the directory %s: %s"), dpath, strerror (errno));
+
+        return -1;
+    }
+
+    closedir (dir);
+    return 1;
+}
+
+
+gboolean create_dir_if_needed (const gchar *dpath)
+{
+    g_return_val_if_fail (dpath, FALSE);
+
+    auto dir_exists = is_dir_existing(dpath);
+
+    switch (dir_exists)
+    {
+        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
@@ -943,15 +974,18 @@ gboolean create_dir_if_needed (const gchar *dpath)
                 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;
         }
-        else
-            g_warning (_("Couldn’t read from the directory %s: %s"), dpath, strerror (errno));
-
-        return FALSE;
     }
 
-    closedir (dir);
+    // never reached
     return TRUE;
 }
 
diff --git a/src/utils.h b/src/utils.h
index 32f0f7ee..394d95d0 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -216,6 +216,7 @@ GdkColor *gdk_color_new (gushort r, gushort g, gushort b);
 GList *file_list_to_uri_list (GList *files);
 GList *file_list_to_info_list (GList *files);
 
+int is_dir_existing(const gchar *dpath);
 gboolean create_dir_if_needed (const gchar *dpath);
 void fix_uri (GnomeVFSURI *uri);
 


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