Skip to content

Implement contentsOfDirectoryAtPath #134

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
Dec 17, 2015
Merged

Implement contentsOfDirectoryAtPath #134

merged 1 commit into from
Dec 17, 2015

Conversation

euantorano
Copy link
Contributor

Implementing NSFileManager::contentsOfDirectoryAtPath() and NSFileManager::subpathsOfDirectoryAtPath().

Error messages aren't currently an exact match for the standard Foundation library, which throws:

Fatal error: Error raised at top level: Error Domain=NSCocoaErrorDomain Code=260 "The folder “notexist” doesn’t exist." UserInfo={NSFilePath=/Users/euan/Desktop/notexist, NSUserStringVariant=(
    Folder
), NSUnderlyingError=0x1007019f0 {Error Domain=NSOSStatusErrorDomain Code=-43 "fnfErr: File not found"}}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/ErrorType.swift, line 56
(lldb)

Whereas this commit throws:

fatal error: Error raised at top level: Error Domain=NSCocoaErrorDomain Code=260 "The file notexist couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/euan/Desktop/notexist}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/ErrorType.swift, line 56

However, I lack the knowledge to know what I'm missing with the NSError.

Otherwise, the test cases pass and this should hopefully work.

@parkera
Copy link
Contributor

parkera commented Dec 14, 2015

Overall this looks pretty good, with the minor comment on ._, which is worth testing.

Can you rebase this on top of master and into a single commit?

@euantorano
Copy link
Contributor Author

Not a problem, I'll do so tonight once I've tested the ._ issue.

@euantorano
Copy link
Contributor Author

@parkera Rebased and squashed, hopefully that's all fixed! Your comments about the ._ seem to have been lost, but basically the docs here seem to be wrong. The comments state that This method also does not return URLs for the current directory (“.”), parent directory (“..”), or resource forks (files that begin with “._”) when in actual fact files starting with ._ are indeed returned. This PR copies that behaviour, but removes that comment.


defer {
closedir(dir)
dir.destroy()
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not certain this is the correct usage here; destroy is usually reserved from the initialized from swift scope buffers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@phausler That may be true, but I wasn't sure. It doesn't cause any errors, but if it's not needed then I can remove it.

On 17 Dec 2015, at 17:30, Philippe Hausler [email protected] wrote:

In Foundation/NSFileManager.swift:

 public func subpathsOfDirectoryAtPath(path: String) throws -> [String] {
  •    NSUnimplemented()
    
  •    var contents : [String] = [String]()
    
  •    let dir = opendir(path)
    
  •    if dir == nil {
    
  •        throw NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.FileReadNoSuchFileError.rawValue, userInfo: [NSFilePathErrorKey: path])
    
  •    }
    
  •    defer {
    
  •        closedir(dir)
    
  •        dir.destroy()
    
    I am not certain this is the correct usage here; destroy is usually reserved from the initialized from swift scope buffers.


Reply to this email directly or view it on GitHub.

Copy link
Contributor

Choose a reason for hiding this comment

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

After looking into it - it seems to be almost a no-op in this case

TIL...
Interesting - did some digging around in the standard lib:
It claims

  /// Destroy the object the pointer points to.
  ///
  /// - Precondition: The memory is initialized.
  ///
  /// - Postcondition: The value has been destroyed and the memory must
  ///   be initialized before being used again.

and then promptly calls

Builtin.destroy(Memory.self, _rawValue)

Which in turn ends up dealing with witness tables and seems to be primarily for object based buffers. So it would probably only matter if this was happening on UnsafeMutablePointer of CF types which are translated to swift retain/released objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. Sounds like I can just remove it then without any downsides. I'll update this PR now.

@euantorano
Copy link
Contributor Author

Fixed the concerns regarding .destroy(). Any other possible blockers?

@phausler
Copy link
Contributor

looks pretty reasonable overall to me; just need to run tests before merging

let int32Ptr = unsafeBitCast(ptr, UnsafePointer<UInt8>.self)
return Int32(int32Ptr.memory)
}) {
if entryType == DT_DIR {
Copy link
Contributor

Choose a reason for hiding this comment

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

on ubuntu I get this error:

Foundation/NSFileManager.swift:280:38: error: binary operator '==' cannot be applied to operands of type 'Int32' and 'Int'
                        if entryType == DT_DIR {
                           ~~~~~~~~~ ^  ~~~~~~
Foundation/NSFileManager.swift:280:38: note: overloads for '==' exist with these partially matching parameter lists: (Int32, Int32), (Int, Int)
                        if entryType == DT_DIR {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great. In OS X DT_DIR is an Int32, but obviously it's an Int on Linux. I'll add a conditional.

Implementing NSFileManager::contentsOfDirectoryAtPath and NSFileManager::subpathsOfDirectoryAtPath.
@euantorano
Copy link
Contributor Author

Fixed, hopefully. I haven't tested on Linux, I'm setting up a VM to do so now.

@phausler
Copy link
Contributor

yep, seems to be good now.

phausler added a commit that referenced this pull request Dec 17, 2015
…sOfDirectoryAtPath

Implement contentsOfDirectoryAtPath
@phausler phausler merged commit 176e8c5 into swiftlang:master Dec 17, 2015
@euantorano
Copy link
Contributor Author

👍

@euantorano euantorano deleted the feature/NSFileManager_contentsOfDirectoryAtPath branch December 17, 2015 19:17
atrick pushed a commit to atrick/swift-corelibs-foundation that referenced this pull request Jan 12, 2021
[test] Add a test for SwiftPM integration
kateinoigakukun pushed a commit to kateinoigakukun/swift-corelibs-foundation that referenced this pull request Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants