diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2014-07-07 02:45:30 -0700 |
---|---|---|
committer | David Adam <zanchey@ucc.gu.uwa.edu.au> | 2014-12-21 23:38:02 +0800 |
commit | b9db5553430d8355bab48275727f685c7c42fa66 (patch) | |
tree | 3ce7304c18eb199d7b586ec802fb6e8823466346 | |
parent | 9b43e6fa8b9ab11826370ca6e4c6fa2aac3b486a (diff) |
Support for dirents without d_type (e.g. Solaris)
-rw-r--r-- | wutil.cpp | 24 |
1 files changed, 19 insertions, 5 deletions
@@ -77,14 +77,32 @@ bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, std::wstring &ou if (out_is_dir) { /* The caller cares if this is a directory, so check */ - bool is_dir; + bool is_dir = false; + + /* We may be able to skip stat, if the readdir can tell us the file type directly */ + bool check_with_stat = true; +#if defined(_DIRENT_HAVE_D_TYPE) || __APPLE__ if (d->d_type == DT_DIR) { + /* Known directory */ is_dir = true; + check_with_stat = false; } else if (d->d_type == DT_LNK || d->d_type == DT_UNKNOWN) { /* We want to treat symlinks to directories as directories. Use stat to resolve it. */ + check_with_stat = true; + } + else + { + /* Regular file */ + is_dir = false; + check_with_stat = false; + } +#endif + if (check_with_stat) + { + /* We couldn't determine the file type from the dirent; check by stat'ing it */ cstring fullpath = wcs2string(dir_path); fullpath.push_back('/'); fullpath.append(d->d_name); @@ -98,10 +116,6 @@ bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, std::wstring &ou is_dir = !!(S_ISDIR(buf.st_mode)); } } - else - { - is_dir = false; - } *out_is_dir = is_dir; } return true; |