PageRenderTime 25ms CodeModel.GetById 36ms RepoModel.GetById 11ms app.codeStats 0ms

/lib/vendor/linux/lib/rb-inotify/event.rb

http://github.com/guard/guard
Ruby | 139 lines | 41 code | 12 blank | 86 comment | 4 complexity | 7fd59382fafe2607c4411f9be1e8f1db MD5 | raw file
  1. module INotify
  2. # An event caused by a change on the filesystem.
  3. # Each {Watcher} can fire many events,
  4. # which are passed to that watcher's callback.
  5. class Event
  6. # A list of other events that are related to this one.
  7. # Currently, this is only used for files that are moved within the same directory:
  8. # the `:moved_from` and the `:moved_to` events will be related.
  9. #
  10. # @return [Array<Event>]
  11. attr_reader :related
  12. # The name of the file that the event occurred on.
  13. # This is only set for events that occur on files in directories;
  14. # otherwise, it's `""`.
  15. # Similarly, if the event is being fired for the directory itself
  16. # the name will be `""`
  17. #
  18. # This pathname is relative to the enclosing directory.
  19. # For the absolute pathname, use \{#absolute\_name}.
  20. # Note that when the `:recursive` flag is passed to {Notifier#watch},
  21. # events in nested subdirectories will still have a `#name` field
  22. # relative to their immediately enclosing directory.
  23. # For example, an event on the file `"foo/bar/baz"`
  24. # will have name `"baz"`.
  25. #
  26. # @return [String]
  27. attr_reader :name
  28. # The {Notifier} that fired this event.
  29. #
  30. # @return [Notifier]
  31. attr_reader :notifier
  32. # An integer specifying that this event is related to some other event,
  33. # which will have the same cookie.
  34. #
  35. # Currently, this is only used for files that are moved within the same directory.
  36. # Both the `:moved_from` and the `:moved_to` events will have the same cookie.
  37. #
  38. # @private
  39. # @return [Fixnum]
  40. attr_reader :cookie
  41. # The {Watcher#id id} of the {Watcher} that fired this event.
  42. #
  43. # @private
  44. # @return [Fixnum]
  45. attr_reader :watcher_id
  46. # Returns the {Watcher} that fired this event.
  47. #
  48. # @return [Watcher]
  49. def watcher
  50. @watcher ||= @notifier.watchers[@watcher_id]
  51. end
  52. # The absolute path of the file that the event occurred on.
  53. #
  54. # This is actually only as absolute as the path passed to the {Watcher}
  55. # that created this event.
  56. # However, it is relative to the working directory,
  57. # assuming that hasn't changed since the watcher started.
  58. #
  59. # @return [String]
  60. def absolute_name
  61. return watcher.path if name.empty?
  62. return File.join(watcher.path, name)
  63. end
  64. # Returns the flags that describe this event.
  65. # This is generally similar to the input to {Notifier#watch},
  66. # except that it won't contain options flags nor `:all_events`,
  67. # and it may contain one or more of the following flags:
  68. #
  69. # `:unmount`
  70. # : The filesystem containing the watched file or directory was unmounted.
  71. #
  72. # `:ignored`
  73. # : The \{#watcher watcher} was closed, or the watched file or directory was deleted.
  74. #
  75. # `:isdir`
  76. # : The subject of this event is a directory.
  77. #
  78. # @return [Array<Symbol>]
  79. def flags
  80. @flags ||= Native::Flags.from_mask(@native[:mask])
  81. end
  82. # Constructs an {Event} object from a string of binary data,
  83. # and destructively modifies the string to get rid of the initial segment
  84. # used to construct the Event.
  85. #
  86. # @private
  87. # @param data [String] The string to be modified
  88. # @param notifier [Notifier] The {Notifier} that fired the event
  89. # @return [Event, nil] The event, or `nil` if the string is empty
  90. def self.consume(data, notifier)
  91. return nil if data.empty?
  92. ev = new(data, notifier)
  93. data.replace data[ev.size..-1]
  94. ev
  95. end
  96. # Creates an event from a string of binary data.
  97. # Differs from {Event.consume} in that it doesn't modify the string.
  98. #
  99. # @private
  100. # @param data [String] The data string
  101. # @param notifier [Notifier] The {Notifier} that fired the event
  102. def initialize(data, notifier)
  103. ptr = FFI::MemoryPointer.from_string(data)
  104. @native = Native::Event.new(ptr)
  105. @related = []
  106. @cookie = @native[:cookie]
  107. @name = data[@native.size, @native[:len]].gsub(/\0+$/, '')
  108. @notifier = notifier
  109. @watcher_id = @native[:wd]
  110. raise Exception.new("inotify event queue has overflowed.") if @native[:mask] & Native::Flags::IN_Q_OVERFLOW != 0
  111. end
  112. # Calls the callback of the watcher that fired this event,
  113. # passing in the event itself.
  114. #
  115. # @private
  116. def callback!
  117. watcher.callback!(self)
  118. end
  119. # Returns the size of this event object in bytes,
  120. # including the \{#name} string.
  121. #
  122. # @return [Fixnum]
  123. def size
  124. @native.size + @native[:len]
  125. end
  126. end
  127. end