Skip to content

Commit cf9616b

Browse files
authored
Merge pull request #2637 from spevans/pr_readdir
2 parents 1b12594 + 88f194e commit cf9616b

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

CoreFoundation/Base.subproj/CFFileUtilities.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
372372
}
373373
}
374374

375-
struct dirent buffer;
376375
struct dirent *dp;
377376
int err;
378377

@@ -389,7 +388,7 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
389388
}
390389
files = CFArrayCreateMutable(alloc, 0, & kCFTypeArrayCallBacks);
391390

392-
while((0 == readdir_r(dirp, &buffer, &dp)) && dp) {
391+
while((dp = readdir(dirp))) {
393392
CFURLRef fileURL;
394393
unsigned namelen = strlen(dp->d_name);
395394

Foundation/FileManager+POSIX.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,22 +373,22 @@ extension FileManager {
373373
}
374374
defer { closedir(dir) }
375375

376-
var entry = dirent()
377-
var result: UnsafeMutablePointer<dirent>? = nil
378-
379-
while readdir_r(dir, &entry, &result) == 0 {
380-
guard result != nil else {
381-
return
382-
}
383-
let length = Int(_direntNameLength(&entry))
384-
let entryName = withUnsafePointer(to: &entry.d_name) { (ptr) -> String in
376+
// readdir returns NULL on EOF and error so set errno to 0 to check for errors
377+
errno = 0
378+
while let entry = readdir(dir) {
379+
let length = Int(_direntNameLength(entry))
380+
let entryName = withUnsafePointer(to: entry.pointee.d_name) { (ptr) -> String in
385381
let namePtr = UnsafeRawPointer(ptr).assumingMemoryBound(to: CChar.self)
386382
return string(withFileSystemRepresentation: namePtr, length: length)
387383
}
388384
if entryName != "." && entryName != ".." {
389-
let entryType = Int32(entry.d_type)
385+
let entryType = Int32(entry.pointee.d_type)
390386
try closure(entryName, entryType)
391387
}
388+
errno = 0
389+
}
390+
guard errno == 0 else {
391+
throw _NSErrorWithErrno(errno, reading: true, path: path)
392392
}
393393
}
394394
}

0 commit comments

Comments
 (0)