From ec7c5eca2a8c4d3416c0c6b3f9db3ba95fb40094 Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Tue, 1 Jan 2013 17:55:01 +0100 Subject: [PATCH] DIRECTORY Iterating: fix typo found by Rachid; use lstat instead of stat (grml misread manpage) --- citadel/modules/bio/serv_bio.c | 60 ++++++++++-- citadel/modules/network/serv_netspool.c | 116 +++++++++++++++++++----- webcit/static.c | 12 ++- webcit/subst.c | 13 ++- 4 files changed, 159 insertions(+), 42 deletions(-) diff --git a/citadel/modules/bio/serv_bio.c b/citadel/modules/bio/serv_bio.c index 56e0d4824..041485d93 100644 --- a/citadel/modules/bio/serv_bio.c +++ b/citadel/modules/bio/serv_bio.c @@ -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); diff --git a/citadel/modules/network/serv_netspool.c b/citadel/modules/network/serv_netspool.c index e1c75315d..1da98f807 100644 --- a/citadel/modules/network/serv_netspool.c +++ b/citadel/modules/network/serv_netspool.c @@ -83,6 +83,15 @@ #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; diff --git a/webcit/static.c b/webcit/static.c index ab7ce69e0..9a91e2676 100644 --- a/webcit/static.c +++ b/webcit/static.c @@ -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); } } diff --git a/webcit/subst.c b/webcit/subst.c index 130b1a12a..b119ce18c 100644 --- a/webcit/subst.c +++ b/webcit/subst.c @@ -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: -- 2.30.2