[balsa] Replace readdir() by g_dir_read_name()



commit c66bb41f89b71e1d278c7d80f279b607cf1b4788
Author: Albrecht Dreß <albrecht dress arcor de>
Date:   Thu Dec 13 11:49:59 2018 -0500

    Replace readdir() by g_dir_read_name()
    
    * libbalsa/folder-scanners.c: replace opendir/readdir/closedir
      by g_dir_*, and refactor functions libbalsa_scanner_mdir()
      and libbalsa_scanner_local_dir_helper
    * libbalsa/mailbox_imap.c: replace opendir/readdir/closedir
      by g_dir_*
    
    Signed-off-by: Peter Bloomfield <PeterBloomfield bellsouth net>

 ChangeLog                  |  10 +++
 libbalsa/folder-scanners.c | 178 ++++++++++++++++++++++-----------------------
 libbalsa/mailbox_imap.c    |  17 +++--
 3 files changed, 105 insertions(+), 100 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 1a40d766d..d6bfd8aaa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2018-12-13  Albrecht Dreß  <albrecht dress arcor de>
+
+       Replace readdir() by g_dir_read_name()
+
+       * libbalsa/folder-scanners.c: replace opendir/readdir/closedir
+         by g_dir_*, and refactor functions libbalsa_scanner_mdir()
+         and libbalsa_scanner_local_dir_helper
+       * libbalsa/mailbox_imap.c: replace opendir/readdir/closedir
+         by g_dir_*
+
 2018-12-09  Albrecht Dreß  <albrecht dress arcor de>
 
        Clean up deadwood
diff --git a/libbalsa/folder-scanners.c b/libbalsa/folder-scanners.c
index 1a3dbe270..634af73ce 100644
--- a/libbalsa/folder-scanners.c
+++ b/libbalsa/folder-scanners.c
@@ -22,16 +22,7 @@
 #endif                          /* HAVE_CONFIG_H */
 #include "folder-scanners.h"
 
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <dirent.h>
-#include <pwd.h>
-#include <ctype.h>
-#include <gtk/gtk.h>
-#include <limits.h>
+#include <glib/gstdio.h>
 
 #include "libbalsa.h"
 #include "libimap.h"
@@ -57,53 +48,53 @@ libbalsa_scanner_mdir(gpointer rnode, const gchar * prefix,
                       LocalHandler folder_handler,
                       LocalHandler mailbox_handler, guint * depth)
 {
-    DIR *dpc;
-    struct dirent *de;
-    char filename[PATH_MAX];
-    struct stat st;
-    gpointer parent_node = NULL;
+    GDir *dpc;
+    GError *error = NULL;
+    const gchar *entry;
 
     if (!check_local_path(prefix, *depth)
         || !mark_local_path(rnode))
         return;
 
-    dpc = opendir(prefix);
-    if (!dpc)
+    dpc = g_dir_open(prefix, 0U, &error);
+    if (dpc == NULL) {
+       g_warning("error reading Maildir folder %s: %s", prefix, (error != NULL) ? error->message : 
"unknown");
+       g_clear_error(&error);
         return;
+    }
 
     /*
      * if we don't find any subdirectories inside, we'll go
      * and ignore this one too...
      */
-    while ((de = readdir(dpc)) != NULL) {
-        if (de->d_name[0] == '.')
-            continue;
-        snprintf(filename, PATH_MAX, "%s/%s", prefix, de->d_name);
-        /* ignore file if it can't be read. */
-        if (stat(filename, &st) == -1 || access(filename, R_OK) == -1)
-            continue;
-
-        if (S_ISDIR(st.st_mode)) {
-            /*
-             * if we think that this looks like a mailbox, include it as such.
-             * otherwise we'll lose the mail in this folder
-             */
-            GType foo = libbalsa_mailbox_type_from_path(filename);
-            if ((foo == LIBBALSA_TYPE_MAILBOX_MH) ||
-                (foo == LIBBALSA_TYPE_MAILBOX_MAILDIR)) {
-                parent_node =
-                   mailbox_handler(rnode, de->d_name, filename, foo);
-                ++*depth;
-                libbalsa_scanner_mdir(parent_node, filename,
-                                      check_local_path, mark_local_path,
-                                      folder_handler, mailbox_handler,
-                                      depth);
-                --*depth;
-            }
-        }
-        /* ignore regular files */
+    while ((entry = g_dir_read_name(dpc)) != NULL) {
+       if (entry[0] != '.') {
+               gchar *filename;
+
+               filename = g_build_filename(prefix, entry, NULL);
+
+               /* ignore regular file, or if it can't be read. */
+               if (g_file_test(filename, G_FILE_TEST_IS_DIR) && (g_access(filename, R_OK) == 0)) {
+                       /*
+                        * if we think that this looks like a mailbox, include it as such.
+                        * otherwise we'll lose the mail in this folder
+                        */
+                       GType foo = libbalsa_mailbox_type_from_path(filename);
+                       if ((foo == LIBBALSA_TYPE_MAILBOX_MH) ||
+                               (foo == LIBBALSA_TYPE_MAILBOX_MAILDIR)) {
+                               gpointer parent_node;
+
+                               parent_node = mailbox_handler(rnode, entry, filename, foo);
+                               ++*depth;
+                               libbalsa_scanner_mdir(parent_node, filename, check_local_path, 
mark_local_path, folder_handler,
+                                       mailbox_handler, depth);
+                               --*depth;
+                       }
+               }
+               g_free(filename);
+       }
     }
-    closedir(dpc);
+    g_dir_close(dpc);
 }
 
 static void
@@ -114,61 +105,64 @@ libbalsa_scanner_local_dir_helper(gpointer rnode, const gchar * prefix,
                                   LocalHandler mailbox_handler,
                                   guint * depth)
 {
-    DIR *dpc;
-    struct dirent *de;
-    char filename[PATH_MAX];
-    struct stat st;
-    GType mailbox_type;
-    gpointer current_node;
+    GDir *dpc;
+    GError *error = NULL;
+    const gchar *entry;
 
     if (!check_local_path(prefix, *depth)
         || !mark_local_path(rnode))
         return;
 
-    dpc = opendir(prefix);
-    if (!dpc)
+
+    dpc = g_dir_open(prefix, 0U, &error);
+    if (dpc == NULL) {
+       g_warning("error reading mail folder %s: %s", prefix, (error != NULL) ? error->message : "unknown");
+       g_clear_error(&error);
         return;
+    }
 
-    while ((de = readdir(dpc)) != NULL) {
-        if (de->d_name[0] == '.')
-            continue;
-        snprintf(filename, PATH_MAX, "%s/%s", prefix, de->d_name);
-
-        /* ignore file if it can't be read. */
-        if (stat(filename, &st) == -1 || access(filename, R_OK) == -1)
-            continue;
-
-        if (S_ISDIR(st.st_mode)) {
-           local_scanner_helper helper;
-            mailbox_type = libbalsa_mailbox_type_from_path(filename);
-
-            if ((mailbox_type == LIBBALSA_TYPE_MAILBOX_MH) ||
-                (mailbox_type == LIBBALSA_TYPE_MAILBOX_MAILDIR)) {
-                current_node =
-                    mailbox_handler(rnode, de->d_name, filename, mailbox_type);
-               helper = libbalsa_scanner_mdir;
-            } else {
-                gchar *name = g_path_get_basename(prefix);
-                current_node = folder_handler(rnode, name, filename, 0);
-                g_free(name);
-               helper = libbalsa_scanner_local_dir_helper;
-            }
-
-            ++*depth;
-            helper(current_node, filename, check_local_path,
-                   mark_local_path, folder_handler, mailbox_handler,
-                   depth);
-            --*depth;
-        } else {
-            mailbox_type = libbalsa_mailbox_type_from_path(filename);
-            if (mailbox_type != G_TYPE_OBJECT) {
-                mark_local_path(mailbox_handler
-                                (rnode, de->d_name, filename,
-                                 mailbox_type));
-            }
-        }
+    while ((entry = g_dir_read_name(dpc)) != NULL) {
+       if (entry[0] != '.') {
+               gchar *filename;
+
+               filename = g_build_filename(prefix, entry, NULL);
+
+               /* ignore file if it can't be read. */
+               if (g_access(filename, R_OK) == 0) {
+                   GType mailbox_type;
+
+                       if (g_file_test(filename, G_FILE_TEST_IS_DIR)) {
+                               local_scanner_helper helper;
+                           gpointer current_node;
+
+                               mailbox_type = libbalsa_mailbox_type_from_path(filename);
+
+                               if ((mailbox_type == LIBBALSA_TYPE_MAILBOX_MH) ||
+                                       (mailbox_type == LIBBALSA_TYPE_MAILBOX_MAILDIR)) {
+                                       current_node = mailbox_handler(rnode, entry, filename, mailbox_type);
+                                       helper = libbalsa_scanner_mdir;
+                               } else {
+                                       gchar *name = g_path_get_basename(prefix);
+
+                                       current_node = folder_handler(rnode, name, filename, 0);
+                                       g_free(name);
+                                       helper = libbalsa_scanner_local_dir_helper;
+                               }
+
+                               ++*depth;
+                               helper(current_node, filename, check_local_path, mark_local_path, 
folder_handler, mailbox_handler, depth);
+                               --*depth;
+                       } else {
+                               mailbox_type = libbalsa_mailbox_type_from_path(filename);
+                               if (mailbox_type != G_TYPE_OBJECT) {
+                                       mark_local_path(mailbox_handler(rnode, entry, filename, 
mailbox_type));
+                               }
+                       }
+               }
+               g_free(filename);
+       }
     }
-    closedir(dpc);
+    g_dir_close(dpc);
 }
 
 void
diff --git a/libbalsa/mailbox_imap.c b/libbalsa/mailbox_imap.c
index 128ebd357..d85e377c1 100644
--- a/libbalsa/mailbox_imap.c
+++ b/libbalsa/mailbox_imap.c
@@ -32,7 +32,6 @@
 
 
 #include <stdlib.h>
-#include <dirent.h>
 #include <string.h>
 
 /* for open() */
@@ -519,19 +518,21 @@ cmp_by_time (gconstpointer  a, gconstpointer  b)
 static void
 clean_dir(const char *dir_name, off_t cache_size)
 {
-    DIR* dir;
-    struct dirent* key;
+    GDir* dir;
+    const gchar *entry;
     GList *list, *lst;
     off_t sz;
-    dir = opendir(dir_name);
-    if(!dir)
+
+    dir = g_dir_open(dir_name, 0U, NULL);      /* do not notify the user about errors */
+    if (!dir)
         return;
 
     list = NULL;
-    while ( (key=readdir(dir)) != NULL) {
+    while ((entry = g_dir_read_name(dir)) != NULL) {
         struct stat st;
         struct file_info *fi;
-        gchar *fname = g_build_filename(dir_name, key->d_name, NULL);
+        gchar *fname = g_build_filename(dir_name, entry, NULL);
+
         if(stat(fname, &st) == -1 || !S_ISREG(st.st_mode)) {
            g_free(fname);
             continue;
@@ -542,7 +543,7 @@ clean_dir(const char *dir_name, off_t cache_size)
         fi->time = st.st_atime;
         list = g_list_prepend(list, fi);
     }
-    closedir(dir);
+    g_dir_close(dir);
 
     list = g_list_sort(list, cmp_by_time);
     sz = 0;


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