Skip to content

Commit 5e5532c

Browse files
committed
Fix _CFIterateDirectory behavior on some old file systems
1 parent 4438c6e commit 5e5532c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

CoreFoundation/Base.subproj/CFFileUtilities.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,23 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla
10751075
while ((dent = readdir(dirp))) {
10761076
#if DEPLOYMENT_TARGET_LINUX
10771077
CFIndex nameLen = strlen(dent->d_name);
1078+
if (dent->d_type == DT_UNKNOWN) {
1079+
// on some old file systems readdir may always fill d_type as DT_UNKNOWN (0), double check with stat
1080+
struct stat statBuf;
1081+
char pathToStat[sizeof(dent->d_name)];
1082+
strncpy(pathToStat, directoryPathBuf, sizeof(pathToStat));
1083+
strlcat(pathToStat, "/", sizeof(pathToStat));
1084+
strlcat(pathToStat, dent->d_name, sizeof(pathToStat));
1085+
if (stat(pathToStat, &statBuf) == 0) {
1086+
if (S_ISDIR(statBuf.st_mode)) {
1087+
dent->d_type = DT_DIR;
1088+
} else if (S_ISREG(statBuf.st_mode)) {
1089+
dent->d_type = DT_REG;
1090+
} else if (S_ISLNK(statBuf.st_mode)) {
1091+
dent->d_type = DT_LNK;
1092+
}
1093+
}
1094+
}
10781095
#else
10791096
CFIndex nameLen = dent->d_namlen;
10801097
#endif

0 commit comments

Comments
 (0)