-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FolderMonitor for launch directories
- Loading branch information
1 parent
2712da7
commit 1cee8a4
Showing
5 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// FolderMonitor.swift | ||
// Brewed | ||
// | ||
// Created by Rick Kerkhof on 13/03/2021. | ||
// | ||
// Code found from https://swiftrocks.com/dispatchsource-detecting-changes-in-files-and-folders-in-swift | ||
// and altered. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol FolderMonitorDelegate: AnyObject { | ||
func folderEvent(url: URL, event: DispatchSource.FileSystemEvent, additions: [URL]) | ||
} | ||
|
||
protocol FilesystemMonitor {} | ||
|
||
final class FolderMonitor: FilesystemMonitor { | ||
let url: URL | ||
|
||
let fileHandle: Int32 | ||
let source: DispatchSourceFileSystemObject | ||
|
||
weak var delegate: FolderMonitorDelegate? | ||
|
||
init(url: URL) throws { | ||
self.url = url | ||
fileHandle = open(url.path, O_EVTONLY) | ||
|
||
source = DispatchSource.makeFileSystemObjectSource( | ||
fileDescriptor: fileHandle, | ||
eventMask: .write, | ||
queue: DispatchQueue.main | ||
) | ||
|
||
var contents = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil) | ||
|
||
source.setEventHandler { | ||
let event = self.source.data | ||
let oldContents = contents | ||
contents = try! FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil) | ||
self.delegate?.folderEvent( | ||
url: url, | ||
event: event, | ||
additions: contents.filter { !oldContents.contains($0) } | ||
) | ||
} | ||
|
||
source.setCancelHandler { | ||
close(self.fileHandle) | ||
} | ||
|
||
source.resume() | ||
} | ||
|
||
deinit { | ||
source.cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters