FileSystemEventStream.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // FileSystemEvent.swift
  3. // FileKit
  4. //
  5. // The MIT License (MIT)
  6. //
  7. // Copyright (c) 2015-2017 Nikolai Vazquez
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. //
  27. import Foundation
  28. #if os(OSX)
  29. /// A filesystem event stream.
  30. internal struct FileSystemEventStream: RawRepresentable {
  31. /// The raw FSEventStreamRef value of `self`.
  32. var rawValue: FSEventStreamRef
  33. /// Schedules the stream on the specified run loop.
  34. ///
  35. /// - Parameter runLoop: The run loop.
  36. /// - Parameter runLoopMode: The run loop mode.
  37. func scheduleWithRunLoop(_ runLoop: CFRunLoop, runLoopMode: CFRunLoopMode) {
  38. FSEventStreamScheduleWithRunLoop(rawValue, runLoop, runLoopMode.rawValue)
  39. }
  40. /// Invalidates the stream.
  41. func invalidate() {
  42. FSEventStreamInvalidate(rawValue)
  43. }
  44. /// Registers the stream.
  45. func start() {
  46. FSEventStreamStart(rawValue)
  47. }
  48. /// Unregisters the stream.
  49. func stop() {
  50. FSEventStreamStop(rawValue)
  51. }
  52. /// Removes the stream from the specified run loop.
  53. ///
  54. /// - Parameter runLoop: The run loop.
  55. /// - Parameter runLoopMode: The run loop mode.
  56. func unscheduleFromRunLoop(_ runLoop: CFRunLoop, runLoopMode: CFString) {
  57. FSEventStreamUnscheduleFromRunLoop(rawValue, runLoop, runLoopMode)
  58. }
  59. /// Schedules the stream on the specified dispatch queue
  60. ///
  61. /// - Parameter queue: The queue to be run within.
  62. func setDispatchQueue(_ queue: DispatchQueue) {
  63. FSEventStreamSetDispatchQueue(rawValue, queue)
  64. }
  65. /// Decrements the FSEventStreamRef's refcount.
  66. func release() {
  67. FSEventStreamRelease(rawValue)
  68. }
  69. /// Asks the FS Events service to flush out any events that have occurred
  70. /// but have not yet been delivered, due to the latency parameter that was
  71. /// supplied when the stream was created. This flushing occurs
  72. /// asynchronously.
  73. func flushAsync() {
  74. FSEventStreamFlushAsync(rawValue)
  75. }
  76. /// Asks the FS Events service to flush out any events that have occurred
  77. /// but have not yet been delivered, due to the latency parameter that was
  78. /// supplied when the stream was created. This flushing occurs
  79. /// synchronously.
  80. func flushSync() {
  81. FSEventStreamFlushSync(rawValue)
  82. }
  83. /// Prints a description of the stream to stderr.
  84. func show() {
  85. FSEventStreamShow(rawValue)
  86. }
  87. /// The dev_t for a device-relative stream, otherwise 0.
  88. ///
  89. /// - Returns: The dev_t for a device-relative stream or 0.
  90. func deviceBeingWatched() -> dev_t {
  91. return FSEventStreamGetDeviceBeingWatched(rawValue)
  92. }
  93. /// The sinceWhen attribute of the stream.
  94. var lastEventId: FSEventStreamEventId {
  95. return FSEventStreamGetLatestEventId(rawValue)
  96. }
  97. }
  98. #endif