Skip to content
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

isDeletableFile Implementation + Tests #1670

Merged
merged 3 commits into from Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 34 additions & 2 deletions Foundation/FileManager.swift
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
60 changes: 60 additions & 0 deletions TestFoundation/TestFileManager.swift
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,62 @@ class TestFileManager : XCTestCase {
ignoreError { try fm.removeItem(atPath: tmpDir.path) }
}

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

XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))

do {
let attrs = try fm.attributesOfItem(atPath: path)
let permissions = attrs[FileAttributeKey.posixPermissions] as! UInt16
let fileIsReadableFile = (permissions & S_IRUSR == S_IRUSR)
let fmIsReadableFile = fm.isReadableFile(atPath: path)
XCTAssertTrue(fileIsReadableFile == fmIsReadableFile)
} catch let e {
XCTFail("\(e)")
}
}

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

XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))

do {
let attrs = try fm.attributesOfItem(atPath: path)
let permissions = attrs[FileAttributeKey.posixPermissions] as! UInt16
let fileIsReadableFile = (permissions & S_IWUSR == S_IWUSR)
let fmIsReadableFile = fm.isReadableFile(atPath: path)
XCTAssertTrue(fileIsReadableFile == fmIsReadableFile)
} catch let e {
XCTFail("\(e)")
}
}

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

XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))

do {
let attrs = try fm.attributesOfItem(atPath: path)
let permissions = attrs[FileAttributeKey.posixPermissions] as! UInt16
let fileIsReadableFile = (permissions & S_IXUSR == S_IXUSR)
let fmIsReadableFile = fm.isReadableFile(atPath: path)
XCTAssertTrue(fileIsReadableFile == fmIsReadableFile)
} catch let e {
rauhul marked this conversation as resolved.
Show resolved Hide resolved
XCTFail("\(e)")
}
}

func test_isDeletableFile() {
// TODO: Implement test
// how to test?
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could create a file and check its is deletable. Also check a file that should fail eg /dev/null and also check / to validate the parent checking code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to confirm, we would expect the created file to be deletable, correct?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, thats correct. You should be able to create a file that fails by putting it into a subdirectory with no permissions:

eg

mkdir dir1
touch dir1/file1
chmod 000 dir1

isDeletableFile("dir1/file1") -> false

chmod 755 dir1

isDeletableFile("dir1/file1") -> true


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