DIRECTORY Iterating: fix typo found by Rachid; use lstat instead of stat (grml misrea...
authorWilfried Goesgens <dothebart@citadel.org>
Tue, 1 Jan 2013 16:55:01 +0000 (17:55 +0100)
committerWilfried Goesgens <dothebart@citadel.org>
Tue, 1 Jan 2013 16:55:01 +0000 (17:55 +0100)
citadel/modules/bio/serv_bio.c
citadel/modules/network/serv_netspool.c
webcit/static.c
webcit/subst.c

index 56e0d482488f23c28f8e5d3467cbb49f33de952f..041485d933f0afb5767d73399cd3a34e340b6908 100644 (file)
@@ -92,6 +92,8 @@ void cmd_lbio(char *cmdbuf)
        int dont_resolve_uids;
        size_t d_namelen;
        struct ctdluser usbuf;
+       int d_type = 0;
+
 
        d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
        if (d == NULL) {
@@ -110,22 +112,60 @@ void cmd_lbio(char *cmdbuf)
        while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
               (filedir_entry != NULL))
        {
-#ifdef _DIRENT_HAVE_D_NAMELEN
+#ifdef _DIRENT_HAVE_D_NAMLEN
                d_namelen = filedir_entry->d_namelen;
+
 #else
                d_namelen = strlen(filedir_entry->d_name);
 #endif
-               if (((d_namelen == 1) && (filedir_entry->d_name[0] == '.')) || 
-                   ((d_namelen == 2) && (filedir_entry->d_name[0] == '.') && (filedir_entry->d_name[1] == '.')))
+
+#ifdef _DIRENT_HAVE_D_TYPE
+               d_type = filedir_entry->d_type;
+#else
+
+#ifndef DT_UNKNOWN
+#define DT_UNKNOWN     0
+#define DT_DIR         4
+#define DT_REG         8
+#define DT_LNK         10
+
+#define IFTODT(mode)   (((mode) & 0170000) >> 12)
+#define DTTOIF(dirtype)        ((dirtype) << 12)
+#endif
+               d_type = DT_UNKNOWN;
+#endif
+               if ((d_namelen == 1) && 
+                   (filedir_entry->d_name[0] == '.'))
+                       continue;
+
+               if ((d_namelen == 2) && 
+                   (filedir_entry->d_name[0] == '.') &&
+                   (filedir_entry->d_name[1] == '.'))
                        continue;
-                   
-               if (dont_resolve_uids) {
-                       filedir_entry->d_name[d_namelen++] = '\n';
-                       filedir_entry->d_name[d_namelen] = '\0';
-                       client_write(filedir_entry->d_name, d_namelen);
+
+               if (d_type == DT_UNKNOWN) {
+                       struct stat s;
+                       char path[PATH_MAX];
+                       snprintf(path, PATH_MAX, "%s/%s", 
+                                ctdl_bio_dir, filedir_entry->d_name);
+                       if (lstat(path, &s) == 0) {
+                               d_type = IFTODT(s.st_mode);
+                       }
+               }
+               switch (d_type)
+               {
+               case DT_DIR:
+                       break;
+               case DT_LNK:
+               case DT_REG:
+                       if (dont_resolve_uids) {
+                               filedir_entry->d_name[d_namelen++] = '\n';
+                               filedir_entry->d_name[d_namelen] = '\0';
+                               client_write(filedir_entry->d_name, d_namelen);
+                       }
+                       else if (CtdlGetUserByNumber(&usbuf,atol(filedir_entry->d_name))==0)
+                               cprintf("%s\n", usbuf.fullname);
                }
-               else if (CtdlGetUserByNumber(&usbuf,atol(filedir_entry->d_name))==0)
-                       cprintf("%s\n", usbuf.fullname);
        }
        free(d);
        closedir(filedir);
index e1c75315d7f89d4b28cfd4db40e9f0ab8ef2861c..1da98f8071412c5570a0aded7cf13fcdf0124168 100644 (file)
 #include "netmail.h"
 
 
+#ifndef DT_UNKNOWN
+#define DT_UNKNOWN     0
+#define DT_DIR         4
+#define DT_REG         8
+#define DT_LNK         10
+
+#define IFTODT(mode)   (((mode) & 0170000) >> 12)
+#define DTTOIF(dirtype)        ((dirtype) << 12)
+#endif
 
 
 void ParseLastSent(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg)
@@ -513,9 +522,12 @@ void network_do_spoolin(HashList *working_ignetcfg, HashList *the_netmap, int *n
        struct CitContext *CCC = CC;
        DIR *dp;
        struct dirent *d;
+       struct dirent *filedir_entry;
        struct stat statbuf;
        char filename[PATH_MAX];
        static time_t last_spoolin_mtime = 0L;
+       int d_type = 0;
+        int d_namelen;
 
        /*
         * Check the spoolin directory's modification time.  If it hasn't
@@ -535,8 +547,60 @@ void network_do_spoolin(HashList *working_ignetcfg, HashList *the_netmap, int *n
        dp = opendir(ctdl_netin_dir);
        if (dp == NULL) return;
 
-       while (d = readdir(dp), d != NULL) {
-               if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
+       d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
+       if (d == NULL) {
+               closedir(dp);
+               return;
+       }
+
+       while ((readdir_r(dp, d, &filedir_entry) == 0) &&
+              (filedir_entry != NULL))
+       {
+#ifdef _DIRENT_HAVE_D_NAMLEN
+               d_namelen = filedir_entry->d_namelen;
+
+#else
+               d_namelen = strlen(filedir_entry->d_name);
+#endif
+
+#ifdef _DIRENT_HAVE_D_TYPE
+               d_type = filedir_entry->d_type;
+#else
+               d_type = DT_UNKNOWN;
+#endif
+               if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
+                       continue; /* Ignore backup files... */
+
+               if ((d_namelen == 1) && 
+                   (filedir_entry->d_name[0] == '.'))
+                       continue;
+
+               if ((d_namelen == 2) && 
+                   (filedir_entry->d_name[0] == '.') &&
+                   (filedir_entry->d_name[1] == '.'))
+                       continue;
+
+               if (d_type == DT_UNKNOWN) {
+                       struct stat s;
+                       char path[PATH_MAX];
+
+                       snprintf(path,
+                                PATH_MAX,
+                                "%s/%s", 
+                                ctdl_netin_dir,
+                                filedir_entry->d_name);
+
+                       if (lstat(path, &s) == 0) {
+                               d_type = IFTODT(s.st_mode);
+                       }
+               }
+
+               switch (d_type)
+               {
+               case DT_DIR:
+                       break;
+               case DT_LNK: /* TODO: check whether its a file or a directory */
+               case DT_REG:
                        snprintf(filename, 
                                sizeof filename,
                                "%s/%s",
@@ -551,6 +615,7 @@ void network_do_spoolin(HashList *working_ignetcfg, HashList *the_netmap, int *n
        }
 
        closedir(dp);
+       free(d);
 }
 
 /*
@@ -574,6 +639,8 @@ void network_consolidate_spoolout(HashList *working_ignetcfg, HashList *the_netm
        int i;
        struct stat statbuf;
        int nFailed = 0;
+       int d_type = 0;
+
 
        /* Step 1: consolidate files in the outbound queue into one file per neighbor node */
        d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
@@ -593,21 +660,21 @@ void network_consolidate_spoolout(HashList *working_ignetcfg, HashList *the_netm
        while ((readdir_r(dp, d, &filedir_entry) == 0) &&
               (filedir_entry != NULL))
        {
-#ifdef _DIRENT_HAVE_D_NAMELEN
+#ifdef _DIRENT_HAVE_D_NAMLEN
                d_namelen = filedir_entry->d_namelen;
-#else
-
-#ifndef DT_UNKNOWN
-#define DT_UNKNOWN     0
-#define DT_DIR         4
-#define DT_REG         8
-#define DT_LNK         10
 
-#define IFTODT(mode)   (((mode) & 0170000) >> 12)
-#define DTTOIF(dirtype)        ((dirtype) << 12)
-#endif
+#else
                d_namelen = strlen(filedir_entry->d_name);
 #endif
+
+#ifdef _DIRENT_HAVE_D_TYPE
+               d_type = filedir_entry->d_type;
+#else
+               d_type = DT_UNKNOWN;
+#endif
+               if (d_type == DT_DIR)
+                       continue;
+
                if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
                        continue; /* Ignore backup files... */
 
@@ -742,22 +809,21 @@ void network_consolidate_spoolout(HashList *working_ignetcfg, HashList *the_netm
        while ((readdir_r(dp, d, &filedir_entry) == 0) &&
               (filedir_entry != NULL))
        {
-#ifdef _DIRENT_HAVE_D_NAMELEN
+#ifdef _DIRENT_HAVE_D_NAMLEN
                d_namelen = filedir_entry->d_namelen;
-               d_type = filedir_entry->d_type;
-#else
-
-#ifndef DT_UNKNOWN
-#define DT_UNKNOWN     0
-#define DT_DIR         4
-#define DT_REG         8
-#define DT_LNK         10
 
-#define IFTODT(mode)   (((mode) & 0170000) >> 12)
-#define DTTOIF(dirtype)        ((dirtype) << 12)
-#endif
+#else
                d_namelen = strlen(filedir_entry->d_name);
 #endif
+
+#ifdef _DIRENT_HAVE_D_TYPE
+               d_type = filedir_entry->d_type;
+#else
+               d_type = DT_UNKNOWN;
+#endif
+               if (d_type == DT_DIR)
+                       continue;
+
                if ((d_namelen == 1) && 
                    (filedir_entry->d_name[0] == '.'))
                        continue;
index ab7ce69e0989c2788e94f8ed028efc04f106d06b..9a91e267649c7a21a9b7358b608495a3781dbe06 100644 (file)
@@ -170,6 +170,7 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
 
        d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
        if (d == NULL) {
+               closedir(filedir);
                return 0;
        }
 
@@ -181,8 +182,14 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
        while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
               (filedir_entry != NULL))
        {
-#ifdef _DIRENT_HAVE_D_NAMELEN
+#ifdef _DIRENT_HAVE_D_NAMLEN
                d_namelen = filedir_entry->d_namelen;
+
+#else
+               d_namelen = strlen(filedir_entry->d_name);
+#endif
+
+#ifdef _DIRENT_HAVE_D_TYPE
                d_type = filedir_entry->d_type;
 #else
 
@@ -195,7 +202,6 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
 #define DTTOIF(dirtype)        ((dirtype) << 12)
 #endif
-               d_namelen = strlen(filedir_entry->d_name);
                d_type = DT_UNKNOWN;
 #endif
                if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
@@ -215,7 +221,7 @@ int LoadStaticDir(const char *DirName, HashList *DirList, const char *RelDir)
                        char path[PATH_MAX];
                        snprintf(path, PATH_MAX, "%s/%s", 
                                DirName, filedir_entry->d_name);
-                       if (stat(path, &s) == 0) {
+                       if (lstat(path, &s) == 0) {
                                d_type = IFTODT(s.st_mode);
                        }
                }
index 130b1a12ac61bd7e2811d485d7775c11527f4803..b119ce18ca645bebdaebda59d7b4be67a5f98966 100644 (file)
@@ -1525,8 +1525,14 @@ int LoadTemplateDir(const StrBuf *DirName, HashList *big, const StrBuf *BaseKey)
        {
                char *MinorPtr;
 
-#ifdef _DIRENT_HAVE_D_NAMELEN
+#ifdef _DIRENT_HAVE_D_NAMLEN
                d_namelen = filedir_entry->d_namelen;
+
+#else
+               d_namelen = strlen(filedir_entry->d_name);
+#endif
+
+#ifdef _DIRENT_HAVE_D_TYPE
                d_type = filedir_entry->d_type;
 #else
 
@@ -1539,7 +1545,6 @@ int LoadTemplateDir(const StrBuf *DirName, HashList *big, const StrBuf *BaseKey)
 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
 #define DTTOIF(dirtype)        ((dirtype) << 12)
 #endif
-               d_namelen = strlen(filedir_entry->d_name);
                d_type = DT_UNKNOWN;
 #endif
                d_without_ext = d_namelen;
@@ -1561,7 +1566,7 @@ int LoadTemplateDir(const StrBuf *DirName, HashList *big, const StrBuf *BaseKey)
                        char path[PATH_MAX];
                        snprintf(path, PATH_MAX, "%s/%s", 
                                 ChrPtr(DirName), filedir_entry->d_name);
-                       if (stat(path, &s) == 0) {
+                       if (lstat(path, &s) == 0) {
                                d_type = IFTODT(s.st_mode);
                        }
                }
@@ -1589,7 +1594,7 @@ int LoadTemplateDir(const StrBuf *DirName, HashList *big, const StrBuf *BaseKey)
                        LoadTemplateDir(SubDirectory, big, SubKey);
 
                        break;
-               case DT_LNK: /* TODO: check whether its a file or a directory */
+               case DT_LNK: 
                case DT_REG: