Class: Snapsync::PartitionsMonitor
- Inherits:
-
Object
- Object
- Snapsync::PartitionsMonitor
- Defined in:
- lib/snapsync/partitions_monitor.rb
Instance Attribute Summary collapse
-
#dirty ⇒ Object
readonly
Returns the value of attribute dirty.
-
#known_partitions ⇒ Object
readonly
Returns the value of attribute known_partitions.
-
#monitored_partitions ⇒ Object
readonly
Returns the value of attribute monitored_partitions.
-
#udisk ⇒ Object
readonly
Returns the value of attribute udisk.
Instance Method Summary collapse
- #dirty! ⇒ Object
- #dirty? ⇒ Boolean
-
#each_partition_with_filesystem {|the, the| ... } ⇒ Object
Yields the udev objects representing block devices that support an underlying filesystem.
- #emit_added(uuid, fs) ⇒ Object
- #emit_removed(uuid) ⇒ Object
-
#initialize ⇒ PartitionsMonitor
constructor
A new instance of PartitionsMonitor.
- #monitor_for(partition_uuid) ⇒ Object
- #partition_of(dir) ⇒ Object
- #partition_uuid_for_dir(dir) ⇒ Object
- #poll ⇒ Object
Constructor Details
#initialize ⇒ PartitionsMonitor
Returns a new instance of PartitionsMonitor.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/snapsync/partitions_monitor.rb', line 10 def initialize dbus = DBus::SystemBus.instance @udisk = dbus.service('org.freedesktop.UDisks2') udisk.introspect @dirty = Concurrent::AtomicBoolean.new(false) # udisk.on_signal('InterfacesAdded') do # dirty! # end # udisk.on_signal('InterfacesRemoved') do # dirty! # end @monitored_partitions = Set.new @known_partitions = Hash.new end |
Instance Attribute Details
#dirty ⇒ Object (readonly)
Returns the value of attribute dirty.
5 6 7 |
# File 'lib/snapsync/partitions_monitor.rb', line 5 def dirty @dirty end |
#known_partitions ⇒ Object (readonly)
Returns the value of attribute known_partitions.
8 9 10 |
# File 'lib/snapsync/partitions_monitor.rb', line 8 def known_partitions @known_partitions end |
#monitored_partitions ⇒ Object (readonly)
Returns the value of attribute monitored_partitions.
7 8 9 |
# File 'lib/snapsync/partitions_monitor.rb', line 7 def monitored_partitions @monitored_partitions end |
#udisk ⇒ Object (readonly)
Returns the value of attribute udisk.
3 4 5 |
# File 'lib/snapsync/partitions_monitor.rb', line 3 def udisk @udisk end |
Instance Method Details
#dirty! ⇒ Object
53 54 55 |
# File 'lib/snapsync/partitions_monitor.rb', line 53 def dirty! dirty.set end |
#dirty? ⇒ Boolean
57 58 59 |
# File 'lib/snapsync/partitions_monitor.rb', line 57 def dirty? dirty.set? end |
#each_partition_with_filesystem {|the, the| ... } ⇒ Object
Yields the udev objects representing block devices that support an underlying filesystem
117 118 119 120 121 122 123 124 125 |
# File 'lib/snapsync/partitions_monitor.rb', line 117 def each_partition_with_filesystem return enum_for(__method__) if !block_given? udisk.root['org']['freedesktop']['UDisks2']['block_devices'].each do |device_name, _| dev = udisk.object("/org/freedesktop/UDisks2/block_devices/#{device_name}") if dev['org.freedesktop.UDisks2.Partition'] && dev['org.freedesktop.UDisks2.Filesystem'] yield(device_name, dev) end end end |
#emit_added(uuid, fs) ⇒ Object
106 107 |
# File 'lib/snapsync/partitions_monitor.rb', line 106 def emit_added(uuid, fs) end |
#emit_removed(uuid) ⇒ Object
109 110 |
# File 'lib/snapsync/partitions_monitor.rb', line 109 def emit_removed(uuid) end |
#monitor_for(partition_uuid) ⇒ Object
27 28 29 |
# File 'lib/snapsync/partitions_monitor.rb', line 27 def monitor_for(partition_uuid) monitored_partitions << partition_uuid.to_str end |
#partition_of(dir) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/snapsync/partitions_monitor.rb', line 31 def partition_of(dir) rel = Pathname.new("") while !dir.mountpoint? rel = dir.basename + rel dir = dir.dirname end each_partition_with_filesystem do |name, dev| partition = dev['org.freedesktop.UDisks2.Block'] uuid = partition['IdUUID'] fs = dev['org.freedesktop.UDisks2.Filesystem'] mount_points = fs['MountPoints'].map do |str| str[0..-2].pack("U*") end if mount_points.include?(dir.to_s) return uuid, rel end end nil end |
#partition_uuid_for_dir(dir) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/snapsync/partitions_monitor.rb', line 61 def partition_uuid_for_dir(dir) dir = dir. # Find the dir's mountpoint while !dir.mountpoint? dir = dir.parent end dir = dir.to_s each_partition_with_filesystem.find do |name, dev| fs = dev['org.freedesktop.UDisks2.Filesystem'] mp = fs['MountPoints'] if !mp.empty? binding.pry end # .map { |str| Pathname.new(str) } mp.include?(dir) end end |
#poll ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/snapsync/partitions_monitor.rb', line 80 def poll udisk.introspect dirty.make_false all = Hash.new each_partition_with_filesystem do |name, dev| partition = dev['org.freedesktop.UDisks2.Block'] uuid = partition['IdUUID'] if monitored_partitions.include?(uuid) all[uuid] = dev['org.freedesktop.UDisks2.Filesystem'] end end added = Hash.new (all.keys - known_partitions.keys).each do |uuid| fs = added[uuid] = all[uuid] emit_added(uuid, fs) end removed = (known_partitions.keys - all.keys) removed.each { |uuid| emit_removed(uuid) } @known_partitions = all return added, removed end |