constantine-fry / rebekka

Rebekka - FTP/FTPS client in Swift.
BSD 2-Clause "Simplified" License
87 stars 56 forks source link

Error while listing empty folder #10

Open Ruko2010 opened 8 years ago

Ruko2010 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 7 years ago

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) }