From 38fb3fc89ae207a7aa26fe1704992ebb3b225168 Mon Sep 17 00:00:00 2001 From: Amir Abbas Date: Thu, 26 Jul 2018 13:18:49 +0430 Subject: [PATCH] Fix remote session crash, minor fixes --- Sources/FileObject.swift | 5 ++++- Sources/FileProvider.swift | 2 +- Sources/LocalHelper.swift | 5 ++++- Sources/RemoteSession.swift | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Sources/FileObject.swift b/Sources/FileObject.swift index d5d0b09..a242ba7 100644 --- a/Sources/FileObject.swift +++ b/Sources/FileObject.swift @@ -34,7 +34,10 @@ open class FileObject: NSObject { if let url = allValues[.fileURLKey] as? URL { return url } else { - let path = self.path.addingPercentEncoding(withAllowedCharacters: .filePathAllowed) ?? self.path + var path = self.path.addingPercentEncoding(withAllowedCharacters: .filePathAllowed) ?? self.path + if path.hasPrefix("/") { + path.remove(at: path.startIndex) + } return URL(string: path) ?? URL(string: "/")! } } diff --git a/Sources/FileProvider.swift b/Sources/FileProvider.swift index e94453c..442de83 100644 --- a/Sources/FileProvider.swift +++ b/Sources/FileProvider.swift @@ -786,7 +786,7 @@ public extension FileProviderBasic { public func url(of path: String) -> URL { var rpath: String = path rpath = rpath.addingPercentEncoding(withAllowedCharacters: .filePathAllowed) ?? rpath - if let baseURL = baseURL { + if let baseURL = baseURL?.absoluteURL { if rpath.hasPrefix("/") { rpath.remove(at: rpath.startIndex) } diff --git a/Sources/LocalHelper.swift b/Sources/LocalHelper.swift index d389442..4388d45 100644 --- a/Sources/LocalHelper.swift +++ b/Sources/LocalHelper.swift @@ -126,7 +126,10 @@ public final class LocalFileMonitor { /// Creates a folder monitor object with monitoring enabled. public init?(url: URL, handler: @escaping ()->Void) { self.url = url - descriptor = open((url.absoluteURL as NSURL).fileSystemRepresentation, O_EVTONLY) + descriptor = url.absoluteURL.withUnsafeFileSystemRepresentation { rep in + guard let rep = rep else { return -1 } + return open(rep, O_EVTONLY) + } guard descriptor >= 0 else { return nil } let event: DispatchSource.FileSystemEvent = url.fileIsDirectory ? [.write] : .all source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: event, queue: qq) diff --git a/Sources/RemoteSession.swift b/Sources/RemoteSession.swift index df61dfd..e0af6e2 100755 --- a/Sources/RemoteSession.swift +++ b/Sources/RemoteSession.swift @@ -62,7 +62,9 @@ final public class SessionDelegate: NSObject, URLSessionDataDelegate, URLSession } public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { - if let progress = context?.load(as: Progress.self), let newVal = change?[.newKey] as? Int64 { + if let progress = context?.assumingMemoryBound(to: Progress.self).pointee, + progress.responds(to: #selector(Progress.becomeCurrent(withPendingUnitCount:))), + let newVal = change?[.newKey] as? Int64 { switch keyPath ?? "" { case #keyPath(URLSessionTask.countOfBytesReceived): progress.completedUnitCount = newVal