Skip to content

FileManager: Fix fileExists(atPath:isDirectory:) with dangling symlink #1514

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
Apr 14, 2018
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
36 changes: 15 additions & 21 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -593,32 +593,26 @@ open class FileManager : NSObject {

open func fileExists(atPath path: String, isDirectory: UnsafeMutablePointer<ObjCBool>?) -> Bool {
var s = stat()
if lstat(path, &s) >= 0 {
if let isDirectory = isDirectory {
if (s.st_mode & S_IFMT) == S_IFLNK {
if stat(path, &s) >= 0 {
isDirectory.pointee = ObjCBool((s.st_mode & S_IFMT) == S_IFDIR)
} else {
return false
}
} else {
let isDir = (s.st_mode & S_IFMT) == S_IFDIR
isDirectory.pointee = ObjCBool(isDir)
}
}
guard lstat(path, &s) >= 0 else {
return false
}

if (s.st_mode & S_IFMT) == S_IFLNK {
// don't chase the link for this magic case -- we might be /Net/foo
// which is a symlink to /private/Net/foo which is not yet mounted...
if (s.st_mode & S_IFMT) == S_IFLNK {
if (s.st_mode & S_ISVTX) == S_ISVTX {
return true
}
// chase the link; too bad if it is a slink to /Net/foo
stat(path, &s)
if isDirectory == nil && (s.st_mode & S_ISVTX) == S_ISVTX {
return true
}
// chase the link; too bad if it is a slink to /Net/foo
guard stat(path, &s) >= 0 else {
return false
}
} else {
return false
}

if let isDirectory = isDirectory {
isDirectory.pointee = ObjCBool((s.st_mode & S_IFMT) == S_IFDIR)
}

return true
}

Expand Down
49 changes: 48 additions & 1 deletion TestFoundation/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TestFileManager : XCTestCase {
("test_createFile", test_createFile ),
("test_moveFile", test_moveFile),
("test_fileSystemRepresentation", test_fileSystemRepresentation),
("test_fileExists", test_fileExists),
("test_fileAttributes", test_fileAttributes),
("test_fileSystemAttributes", test_fileSystemAttributes),
("test_setFileAttributes", test_setFileAttributes),
Expand Down Expand Up @@ -161,7 +162,53 @@ class TestFileManager : XCTestCase {
result.deallocate()
#endif
}


func test_fileExists() {
let fm = FileManager.default
let tmpDir = fm.temporaryDirectory.appendingPathComponent("testFileExistsDir")
let testFile = tmpDir.appendingPathComponent("testFile")
let goodSymLink = tmpDir.appendingPathComponent("goodSymLink")
let badSymLink = tmpDir.appendingPathComponent("badSymLink")
let dirSymLink = tmpDir.appendingPathComponent("dirSymlink")

ignoreError { try fm.removeItem(atPath: tmpDir.path) }

do {
try fm.createDirectory(atPath: tmpDir.path, withIntermediateDirectories: false, attributes: nil)
XCTAssertTrue(fm.createFile(atPath: testFile.path, contents: Data()))
try fm.createSymbolicLink(atPath: goodSymLink.path, withDestinationPath: testFile.path)
try fm.createSymbolicLink(atPath: badSymLink.path, withDestinationPath: "no_such_file")
try fm.createSymbolicLink(atPath: dirSymLink.path, withDestinationPath: "..")

var isDirFlag: ObjCBool = false
XCTAssertTrue(fm.fileExists(atPath: tmpDir.path))
XCTAssertTrue(fm.fileExists(atPath: tmpDir.path, isDirectory: &isDirFlag))
XCTAssertTrue(isDirFlag.boolValue)

isDirFlag = true
XCTAssertTrue(fm.fileExists(atPath: testFile.path))
XCTAssertTrue(fm.fileExists(atPath: testFile.path, isDirectory: &isDirFlag))
XCTAssertFalse(isDirFlag.boolValue)

isDirFlag = true
XCTAssertTrue(fm.fileExists(atPath: goodSymLink.path))
XCTAssertTrue(fm.fileExists(atPath: goodSymLink.path, isDirectory: &isDirFlag))
XCTAssertFalse(isDirFlag.boolValue)

isDirFlag = true
XCTAssertFalse(fm.fileExists(atPath: badSymLink.path))
XCTAssertFalse(fm.fileExists(atPath: badSymLink.path, isDirectory: &isDirFlag))

isDirFlag = false
XCTAssertTrue(fm.fileExists(atPath: dirSymLink.path))
XCTAssertTrue(fm.fileExists(atPath: dirSymLink.path, isDirectory: &isDirFlag))
XCTAssertTrue(isDirFlag.boolValue)
} catch {
XCTFail(String(describing: error))
}
ignoreError { try fm.removeItem(atPath: tmpDir.path) }
}

func test_fileAttributes() {
let fm = FileManager.default
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)"
Expand Down