From 5e5532c65a2d20e871a02917705052bf00aecbbd Mon Sep 17 00:00:00 2001 From: JT Xue Date: Thu, 4 Oct 2018 08:43:21 -0700 Subject: [PATCH] Fix _CFIterateDirectory behavior on some old file systems --- CoreFoundation/Base.subproj/CFFileUtilities.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CoreFoundation/Base.subproj/CFFileUtilities.c b/CoreFoundation/Base.subproj/CFFileUtilities.c index 984cf1f122..05400878c5 100644 --- a/CoreFoundation/Base.subproj/CFFileUtilities.c +++ b/CoreFoundation/Base.subproj/CFFileUtilities.c @@ -1075,6 +1075,23 @@ CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean appendSla while ((dent = readdir(dirp))) { #if DEPLOYMENT_TARGET_LINUX CFIndex nameLen = strlen(dent->d_name); + if (dent->d_type == DT_UNKNOWN) { + // on some old file systems readdir may always fill d_type as DT_UNKNOWN (0), double check with stat + struct stat statBuf; + char pathToStat[sizeof(dent->d_name)]; + strncpy(pathToStat, directoryPathBuf, sizeof(pathToStat)); + strlcat(pathToStat, "/", sizeof(pathToStat)); + strlcat(pathToStat, dent->d_name, sizeof(pathToStat)); + if (stat(pathToStat, &statBuf) == 0) { + if (S_ISDIR(statBuf.st_mode)) { + dent->d_type = DT_DIR; + } else if (S_ISREG(statBuf.st_mode)) { + dent->d_type = DT_REG; + } else if (S_ISLNK(statBuf.st_mode)) { + dent->d_type = DT_LNK; + } + } + } #else CFIndex nameLen = dent->d_namlen; #endif