SwiftFoundation FileManager.isDeletableFile Implementation

Currently FileManager.isDeletableFile is marked as NSUnimplemented and I was wondering if the correct implementation is as simple as the methods above it with the flag _DELETE_OK. Namely the methods: isReadableFile, isWritableFile, and isExecutableFile; the implementations of which are included below.

open func isReadableFile(atPath path: String) -> Bool {
    return _fileSystemRepresentation(withPath: path, {
        access($0, R_OK) == 0
    })
}

open func isWritableFile(atPath path: String) -> Bool {
    return _fileSystemRepresentation(withPath: path, {
        access($0, W_OK) == 0
    })
}

open func isExecutableFile(atPath path: String) -> Bool {
    return _fileSystemRepresentation(withPath: path, {
        access($0, X_OK) == 0
    })
}

Additionally, I did not see any test code for these functions and I was wondering if there were tests in the Darwin Foundation that I could draw from.

Any help would be greatly appreciated.

@millenomi @Tony_Parker

Yup, that tracks.

isDeletableFile is a little more tricky, but these should be enough. Do you want to PR this implementation and tests?

1 Like

@millenomi Oh, I think you misunderstood my question, those 3 methods are already implemented. I was wondering if isDeletableFile would be the same with _DELETE_OK flag, but it seems not.

Also the three methods above don't appear to be used in any tests or in any method anywhere for that matter and I was wondering if there are tests in the Darwin Implementation I could use to base new tests off of.

Ah, my bad.

_DELETE_OK, from what I see, is a Darwin constant — we need an implementation that will work across platforms. The way Foundation does things on Darwin does not use _DELETE_OK in that way, and I'd prefer to calque that in Swift carefully to preserve behavior bug-for-bug.

Is it appropriate to ask how the method works on Darwin, so I can copy it as best as possible?

Sure. Here is a rundown of the implementation:

  1. Get the parent directory of supplied path
  2. If the parent directory is not writeable return false
  3. stat the parent directory, and if that fails, return false
  4. Check if the parent is 'sticky' (S_ISVTX on Darwin) and if it exists. If so, and if the current user (getuid) owns the file, return true.
  5. Return true as the best guess after all of that
2 Likes

Super long delay on this (sorry), but the implementation/pr is here.