Skip to content

Commit bc5717b

Browse files
author
itagaki.takahiro
committed
Add a portable implementation for IsDir().
dirent.d_type is not in POSIX and does not exist at least on Solaris. git-svn-id: http://pg-rman.googlecode.com/svn/trunk@23 182aca00-e38e-11de-a668-6fd11605f5ce
1 parent 8863b79 commit bc5717b

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

catalog.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
#include <dirent.h>
1313
#include <fcntl.h>
1414
#include <libgen.h>
15+
#include <sys/file.h>
1516
#include <sys/stat.h>
1617
#include <sys/types.h>
17-
#include <sys/file.h>
1818
#include <stdlib.h>
1919
#include <time.h>
2020
#include <unistd.h>
@@ -96,19 +96,18 @@ IsDir(const char *dirpath, const DIR *dir, const struct dirent *ent)
9696
#if defined(DT_DIR)
9797
return ent->d_type == DT_DIR;
9898
#elif defined(_finddata_t)
99+
/* Optimization for VC++ on Windows. */
99100
return (dir->dd_dta.attrib & FILE_ATTRIBUTE_DIRECTORY) != 0;
100-
#elif defined(WIN32)
101-
char path[MAXPGPATH];
102-
DWORD attr;
101+
#else
102+
/* Portable implementation because dirent.d_type is not in POSIX. */
103+
char path[MAXPGPATH];
104+
struct stat st;
103105

104-
/* dirent.d_type does not exists */
105106
strlcpy(path, dirpath, MAXPGPATH);
106107
strlcat(path, "/", MAXPGPATH);
107108
strlcat(path, ent->d_name, MAXPGPATH);
108-
attr = GetFileAttributes(path);
109-
return attr != (DWORD) -1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
110-
#else
111-
#error IsDir: this platform is not supported.
109+
110+
return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
112111
#endif
113112
}
114113

0 commit comments

Comments
 (0)