2016-08-30 21:44:58 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
2016-09-04 00:13:52 +03:00
|
|
|
import Cocoa
|
2016-08-30 21:44:58 +03:00
|
|
|
import RxSwift
|
|
|
|
import EonilFileSystemEvents
|
|
|
|
|
|
|
|
func == (left: Token, right: Token) -> Bool {
|
|
|
|
return left === right
|
|
|
|
}
|
|
|
|
|
|
|
|
class Token: Equatable {}
|
|
|
|
|
|
|
|
class FileItemService {
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate(set) var ignorePatterns: Set<FileItemIgnorePattern> = [] {
|
2016-08-30 21:44:58 +03:00
|
|
|
didSet {
|
|
|
|
self.ignoreToken = Token()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used to cache fnmatch calls in `FileItem`.
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate var ignoreToken = Token()
|
2016-08-30 21:44:58 +03:00
|
|
|
|
|
|
|
/// When at least this much of non-directory and visible files are scanned, they are emitted.
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate let emitChunkSize = 1000
|
2016-08-30 21:44:58 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate let scanDispatchQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated)
|
|
|
|
fileprivate let monitorDispatchQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated)
|
2016-08-30 21:44:58 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate let root = FileItem(URL(fileURLWithPath: "/", isDirectory: true))
|
2016-08-30 21:44:58 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate let fileSystemEventsLatency = Double(2)
|
|
|
|
fileprivate var monitors = [URL: FileSystemEventMonitor]()
|
|
|
|
fileprivate var monitorCounter = [URL: Int]()
|
2016-09-04 00:13:52 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate let workspace = NSWorkspace.shared()
|
|
|
|
fileprivate let iconsCache = NSCache<NSURL, NSImage>()
|
2016-09-06 19:45:15 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate var spinLock = OS_SPINLOCK_INIT
|
2016-09-04 00:13:52 +03:00
|
|
|
|
|
|
|
init() {
|
|
|
|
self.iconsCache.countLimit = 2000
|
|
|
|
self.iconsCache.name = "icon-cache"
|
|
|
|
}
|
2016-08-30 21:44:58 +03:00
|
|
|
|
|
|
|
func set(ignorePatterns patterns: Set<FileItemIgnorePattern>) {
|
|
|
|
self.ignorePatterns = patterns
|
|
|
|
}
|
2016-09-04 00:13:52 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func icon(forUrl url: URL) -> NSImage? {
|
|
|
|
let path = url.path
|
|
|
|
let icon = workspace.icon(forFile: path)
|
2016-09-04 00:13:52 +03:00
|
|
|
icon.size = CGSize(width: 16, height: 16)
|
2016-09-25 18:50:33 +03:00
|
|
|
self.iconsCache.setObject(icon, forKey: url as NSURL)
|
2016-09-04 00:13:52 +03:00
|
|
|
|
|
|
|
return icon
|
|
|
|
}
|
2016-08-30 21:44:58 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func monitor(url: URL) {
|
|
|
|
let path = url.path
|
2016-08-30 21:44:58 +03:00
|
|
|
|
|
|
|
// FIXME: Handle EonilFileSystemEventFlag.RootChanged, ie watchRoot: true
|
|
|
|
let monitor = FileSystemEventMonitor(pathsToWatch: [path],
|
|
|
|
latency: self.fileSystemEventsLatency,
|
|
|
|
watchRoot: false,
|
|
|
|
queue: self.monitorDispatchQueue)
|
|
|
|
{ [unowned self] events in
|
2016-09-25 18:50:33 +03:00
|
|
|
let urls = events.map { URL(fileURLWithPath: $0.path) }
|
2016-08-30 21:44:58 +03:00
|
|
|
let parent = FileUtils.commonParent(ofUrls: urls)
|
|
|
|
self.fileItem(forUrl: parent)?.needsScanChildren = true
|
|
|
|
}
|
|
|
|
|
|
|
|
self.monitors[url] = monitor
|
2016-09-07 21:12:18 +03:00
|
|
|
if let counter = self.monitorCounter[url] {
|
|
|
|
self.monitorCounter[url] = counter + 1
|
|
|
|
} else {
|
|
|
|
self.monitorCounter[url] = 1
|
|
|
|
}
|
2016-08-30 21:44:58 +03:00
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func unmonitor(url: URL) {
|
2016-09-07 21:12:18 +03:00
|
|
|
guard let counter = self.monitorCounter[url] else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let newCounter = counter - 1
|
|
|
|
if newCounter > 0 {
|
|
|
|
self.monitorCounter[url] = newCounter
|
|
|
|
} else {
|
2016-09-25 18:50:33 +03:00
|
|
|
self.monitorCounter.removeValue(forKey: url)
|
|
|
|
self.monitors.removeValue(forKey: url)
|
2016-09-07 21:12:18 +03:00
|
|
|
|
|
|
|
// TODO Remove cached items more aggressively?
|
|
|
|
let hasRelations = self.monitors.keys.reduce(false) { (result, monitoredUrl) in
|
|
|
|
return result ? result : monitoredUrl.parent(ofUrl: url) || url.parent(ofUrl: monitoredUrl)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasRelations {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.parentFileItem(ofUrl: url).removeChild(withUrl: url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func parentFileItem(ofUrl url: URL) -> FileItem {
|
|
|
|
return self.fileItem(forPathComponents: Array(url.pathComponents.dropLast()))!
|
2016-08-30 21:44:58 +03:00
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
func flatFileItems(ofUrl url: URL) -> Observable<[FileItem]> {
|
|
|
|
guard url.isFileURL else {
|
2016-08-30 21:44:58 +03:00
|
|
|
return Observable.empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
guard FileUtils.fileExistsAtUrl(url) else {
|
|
|
|
return Observable.empty()
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
let pathComponents = url.pathComponents
|
2016-08-30 21:44:58 +03:00
|
|
|
return Observable.create { [unowned self] observer in
|
2016-09-25 19:10:07 +03:00
|
|
|
let cancel = Disposables.create {
|
2016-08-30 21:44:58 +03:00
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
self.scanDispatchQueue.async { [unowned self] in
|
2016-08-30 21:44:58 +03:00
|
|
|
guard let targetItem = self.fileItem(forPathComponents: pathComponents) else {
|
|
|
|
observer.onCompleted()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var flatNewFileItems: [FileItem] = []
|
|
|
|
|
|
|
|
var dirStack: [FileItem] = [targetItem]
|
|
|
|
while let curItem = dirStack.popLast() {
|
2016-09-25 19:10:07 +03:00
|
|
|
if cancel.isDisposed {
|
2016-08-30 21:44:58 +03:00
|
|
|
observer.onCompleted()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !curItem.childrenScanned || curItem.needsScanChildren {
|
|
|
|
self.scanChildren(curItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
curItem.children
|
|
|
|
.filter { item in
|
2016-09-11 15:37:04 +03:00
|
|
|
if item.hidden || item.package {
|
2016-08-30 21:44:58 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// This item already has been fnmatch'ed, thus return the cached value.
|
|
|
|
if item.ignoreToken == self.ignoreToken {
|
|
|
|
return !item.ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
item.ignoreToken = self.ignoreToken
|
|
|
|
item.ignore = false
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
let path = item.url.path
|
2016-08-30 21:44:58 +03:00
|
|
|
for pattern in self.ignorePatterns {
|
|
|
|
// We don't use `String.FnMatchOption.leadingDir` (`FNM_LEADING_DIR`) for directories since we do not
|
|
|
|
// scan ignored directories at all when filtering. For example "*/.git" would create a `FileItem`
|
|
|
|
// for `/some/path/.git`, but not scan its children when we filter.
|
|
|
|
if pattern.match(absolutePath: path) {
|
|
|
|
item.ignore = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
.forEach { $0.dir ? dirStack.append($0) : flatNewFileItems.append($0) }
|
|
|
|
|
|
|
|
if flatNewFileItems.count >= self.emitChunkSize {
|
|
|
|
observer.onNext(flatNewFileItems)
|
|
|
|
flatNewFileItems = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 19:10:07 +03:00
|
|
|
if !cancel.isDisposed {
|
2016-08-30 21:44:58 +03:00
|
|
|
observer.onNext(flatNewFileItems)
|
|
|
|
observer.onCompleted()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cancel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func fileItem(forUrl url: URL) -> FileItem? {
|
|
|
|
let pathComponents = url.pathComponents
|
2016-08-30 21:44:58 +03:00
|
|
|
return self.fileItem(forPathComponents: pathComponents)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the `FileItem` corresponding to the `pathComponents` parameter. This is like mkdir -p, i.e. it
|
|
|
|
/// instantiates the intermediate `FileItem`s.
|
|
|
|
///
|
|
|
|
/// - returns: `FileItem` corresponding to `pathComponents`. `nil` if the file does not exist.
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func fileItem(forPathComponents pathComponents: [String]) -> FileItem? {
|
2016-08-30 21:44:58 +03:00
|
|
|
let result = pathComponents.dropFirst().reduce(self.root) { (resultItem, childName) -> FileItem? in
|
|
|
|
guard let parent = resultItem else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.child(withName: childName, ofParent: parent, create: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Even when the result is nil it does not mean that there's no child with the given name. It could well be that
|
|
|
|
/// it's not been scanned yet. However, if `create` parameter was true and `nil` is returned, the requested
|
|
|
|
/// child does not exist.
|
|
|
|
///
|
|
|
|
/// - parameters:
|
|
|
|
/// - name: name of the child to get.
|
|
|
|
/// - parent: parent of the child.
|
|
|
|
/// - create: whether to create the child `FileItem` if it's not scanned yet.
|
|
|
|
/// - returns: child `FileItem` or nil.
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func child(withName name: String, ofParent parent: FileItem, create: Bool = false) -> FileItem? {
|
2016-08-30 21:44:58 +03:00
|
|
|
let filteredChildren = parent.children.filter { $0.url.lastPathComponent == name }
|
|
|
|
|
|
|
|
if filteredChildren.isEmpty && create {
|
2016-09-25 18:50:33 +03:00
|
|
|
let childUrl = parent.url.appendingPathComponent(name)
|
2016-08-30 21:44:58 +03:00
|
|
|
|
|
|
|
guard FileUtils.fileExistsAtUrl(childUrl) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let child = FileItem(childUrl)
|
2016-09-06 19:45:15 +03:00
|
|
|
self.syncAddChildren { parent.children.append(child) }
|
|
|
|
|
2016-08-30 21:44:58 +03:00
|
|
|
return child
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredChildren.first
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func scanChildren(_ item: FileItem) {
|
2016-09-06 19:45:15 +03:00
|
|
|
let children = FileUtils.directDescendants(item.url).map(FileItem.init)
|
|
|
|
self.syncAddChildren { item.children = children }
|
|
|
|
|
2016-08-30 21:44:58 +03:00
|
|
|
item.childrenScanned = true
|
|
|
|
item.needsScanChildren = false
|
|
|
|
}
|
2016-09-06 19:45:15 +03:00
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
fileprivate func syncAddChildren(_ fn: () -> Void) {
|
2016-09-06 19:45:15 +03:00
|
|
|
OSSpinLockLock(&self.spinLock)
|
|
|
|
fn()
|
|
|
|
OSSpinLockUnlock(&self.spinLock)
|
|
|
|
}
|
2016-08-30 21:44:58 +03:00
|
|
|
}
|