Skip to content

isDeletableFile Implementation + Tests #1670

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 3 commits into from
Sep 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: 34 additions & 2 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,41 @@ open class FileManager : NSObject {
access($0, X_OK) == 0
})
}


/**
- parameters:
- path: The path to the file we are trying to determine is deletable.

- returns: `true` if the file is deletable, `false` otherwise.
*/
open func isDeletableFile(atPath path: String) -> Bool {
NSUnimplemented()
// Get the parent directory of supplied path
let parent = path._nsObject.deletingLastPathComponent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty close to the Darwin implementation! That implementation also special-cases the empty string to mean the current directory, but it's not behavior that's documented, so I don't think we need to port it.


return _fileSystemRepresentation(withPath: parent, andPath: path, { parentFsRep, fsRep in
// Check the parent directory is writeable, else return false.
guard access(parentFsRep, W_OK) == 0 else {
return false
}

// Stat the parent directory, if that fails, return false.
guard let parentS = try? _lstatFile(atPath: path, withFileSystemRepresentation: parentFsRep) else {
return false
}

// Check if the parent is 'sticky' if it exists.
if (parentS.st_mode & S_ISVTX) == S_ISVTX {
guard let s = try? _lstatFile(atPath: path, withFileSystemRepresentation: fsRep) else {
return false
}

// If the current user owns the file, return true.
return s.st_uid == getuid()
}

// Return true as the best guess.
return true
})
}

private func _compareFiles(withFileSystemRepresentation file1Rep: UnsafePointer<Int8>, andFileSystemRepresentation file2Rep: UnsafePointer<Int8>, size: Int64, bufSize: Int) -> Bool {
Expand Down
89 changes: 89 additions & 0 deletions TestFoundation/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class TestFileManager : XCTestCase {
("test_moveFile", test_moveFile),
("test_fileSystemRepresentation", test_fileSystemRepresentation),
("test_fileExists", test_fileExists),
("test_isReadableFile", test_isReadableFile),
("test_isWritableFile", test_isWritableFile),
("test_isExecutableFile", test_isExecutableFile),
("test_isDeletableFile", test_isDeletableFile),
("test_fileAttributes", test_fileAttributes),
("test_fileSystemAttributes", test_fileSystemAttributes),
("test_setFileAttributes", test_setFileAttributes),
Expand Down Expand Up @@ -202,6 +206,91 @@ class TestFileManager : XCTestCase {
ignoreError { try fm.removeItem(atPath: tmpDir.path) }
}

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

do {
// create test file
XCTAssertTrue(fm.createFile(atPath: path, contents: Data()))

// test unReadable if file has no permissions
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0000))], ofItemAtPath: path)
XCTAssertFalse(fm.isReadableFile(atPath: path))

// test readable if file has read permissions
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0400))], ofItemAtPath: path)
XCTAssertTrue(fm.isReadableFile(atPath: path))
} catch let e {
XCTFail("\(e)")
}
}

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

do {
// create test file
XCTAssertTrue(fm.createFile(atPath: path, contents: Data()))

// test unWritable if file has no permissions
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0000))], ofItemAtPath: path)
XCTAssertFalse(fm.isWritableFile(atPath: path))

// test writable if file has write permissions
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0200))], ofItemAtPath: path)
XCTAssertTrue(fm.isWritableFile(atPath: path))
} catch let e {
XCTFail("\(e)")
}
}

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

do {
// create test file
XCTAssertTrue(fm.createFile(atPath: path, contents: Data()))

// test unExecutable if file has no permissions
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0000))], ofItemAtPath: path)
XCTAssertFalse(fm.isExecutableFile(atPath: path))

// test executable if file has execute permissions
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0100))], ofItemAtPath: path)
XCTAssertTrue(fm.isExecutableFile(atPath: path))
} catch let e {
XCTFail("\(e)")
}
}

func test_isDeletableFile() {
let fm = FileManager.default

do {
let dir_path = NSTemporaryDirectory() + "/test_isDeletableFile_dir/"
let file_path = dir_path + "test_isDeletableFile\(NSUUID().uuidString)"
// create test directory
try fm.createDirectory(atPath: dir_path, withIntermediateDirectories: true)
// create test file
XCTAssertTrue(fm.createFile(atPath: file_path, contents: Data()))

// test undeletable if parent directory has no permissions
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0000))], ofItemAtPath: dir_path)
XCTAssertFalse(fm.isDeletableFile(atPath: file_path))

// test deletable if parent directory has all necessary permissions
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0755))], ofItemAtPath: dir_path)
XCTAssertTrue(fm.isDeletableFile(atPath: file_path))
}
catch { XCTFail("\(error)") }

// test against known undeletable file
XCTAssertFalse(fm.isDeletableFile(atPath: "/dev/null"))
}

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