Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Error while listing empty folder #10

Open
Ruko2010 opened this issue Jun 22, 2016 · 3 comments
Open

Error while listing empty folder #10

Ruko2010 opened this issue Jun 22, 2016 · 3 comments

Comments

@Ruko2010
Copy link

Hi,
i'm really new to swift (and iOS), and I tried your code. It works great except when I try to list an empty folder. Than I get
"fatal error: unexpectedly found nil while unwrapping an Optional value"

When there are files in it, everything work. I'm trying the basic list example:

_session.list(path) {
            (resources, error) -> Void in
            print("List directory with result:\n\(resources), error: \(error)\n\n")
        }
@alexviquez
Copy link

alexviquez commented Jun 29, 2016

Are you configure the complete session?, the steps to make it function is the next
first:
var session: Session!
var configuration = SessionConfiguration()
configuration.host = "ftp.myftpserver.com"
configuration.username = "myusername"
configuration.password = "mypassword"
session = Session(configuration: configuration)

and you are configure the server
the next step is listing the files in your ftp
session.list("/") {
(resources, error) -> Void in
println("List directory with result:\n\(resources), error: \(error)\n\n")
}

@Ruko2010
Copy link
Author

Ruko2010 commented Jun 29, 2016

Yes I think I have done everything correctly.

here is my complete code (I XXX out private details ;) ):

`
var _session: Session!
var configuration = SessionConfiguration()
configuration.host = "XXX"
configuration.encoding = NSUTF8StringEncoding
configuration.username = "XXX"
configuration.password = "XXX"
_session = Session(configuration: configuration)

    _session.list("/") {
        (resources, error) -> Void in
        print("List directory with result:\n\(resources), error: \(error)\n\n")
    }

`

As I said. when I try to list a directory that has files in it, everything works fine. But when the directory is empty, the following error occur:
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

this error is in line
let bytes = UnsafePointer<UInt8>(self.inputData!.bytes)
in class
"internal class ResourceListOperation: ReadStreamOperation"
in file
"ResourceListOperation.swift"

btw, i'm using xcode 7.3.1

@GhadaFullStack
Copy link

Hello,
I had the same issue due to listing an empty directory.
The solution is to modify the function streamEventEnd in ResourceListOperation class and add a test :
override func streamEventEnd(_ aStream: Stream) -> (Bool, NSError?) {
var offset = 0
if(inputData != nil){
let bytes = self.inputData!.bytes.bindMemory(to: UInt8.self, capacity: (self.inputData?.length)!)
let totalBytes = CFIndex(self.inputData!.length)
var parsedBytes = CFIndex(0)
let entity = UnsafeMutablePointer<Unmanaged?>.allocate(capacity: 1)
var resources = ResourceItem
repeat {
parsedBytes = CFFTPCreateParsedResourceListing(nil, bytes.advanced(by: offset), totalBytes - offset, entity)
if parsedBytes > 0 {
let value = entity.pointee?.takeUnretainedValue()
if let fptResource = value {
resources.append(self.mapFTPResources(fptResource))
}
offset += parsedBytes
}
} while parsedBytes > 0
self.resources = resources
entity.deinitialize()
}
return (true, nil)
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants