Skip to content

[POSIX] readdir_r() deprectated, replace with readdir() #2637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CoreFoundation/Base.subproj/CFFileUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc,
}
}

struct dirent buffer;
struct dirent *dp;
int err;

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

while((0 == readdir_r(dirp, &buffer, &dp)) && dp) {
while((dp = readdir(dirp))) {
CFURLRef fileURL;
unsigned namelen = strlen(dp->d_name);

Expand Down
20 changes: 10 additions & 10 deletions Foundation/FileManager+POSIX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,22 +373,22 @@ extension FileManager {
}
defer { closedir(dir) }

var entry = dirent()
var result: UnsafeMutablePointer<dirent>? = nil

while readdir_r(dir, &entry, &result) == 0 {
guard result != nil else {
return
}
let length = Int(_direntNameLength(&entry))
let entryName = withUnsafePointer(to: &entry.d_name) { (ptr) -> String in
// readdir returns NULL on EOF and error so set errno to 0 to check for errors
errno = 0
while let entry = readdir(dir) {
let length = Int(_direntNameLength(entry))
let entryName = withUnsafePointer(to: entry.pointee.d_name) { (ptr) -> String in
let namePtr = UnsafeRawPointer(ptr).assumingMemoryBound(to: CChar.self)
return string(withFileSystemRepresentation: namePtr, length: length)
}
if entryName != "." && entryName != ".." {
let entryType = Int32(entry.d_type)
let entryType = Int32(entry.pointee.d_type)
try closure(entryName, entryType)
}
errno = 0
}
guard errno == 0 else {
throw _NSErrorWithErrno(errno, reading: true, path: path)
}
}
}
Expand Down