如何在Linux上用Ruby实现实时USB存储检测?
How to achieve realtime usb storage detection with Ruby on Linux?
我认为 ruby-dbus 可能是正确的解决方案,但我似乎找不到任何有关如何实时检测的示例,每当存储设备连接到 Linux 机器时.谁有这样的例子?
您似乎可以采取多种不同的方法。
Here's a Ruby implementation I found 使用 Ruby FFI 链接到 libusb
。
这样做的好处是 libusb 支持热插拔通知,所以这可能是满足您需要的东西:
http://libusb.sourceforge.net/api-1.0/hotplug.html
Device hotplug support
Support for device hotplugging can be used, if LIBUSB.has_capability?(:CAP_HAS_HOTPLUG)
returns true. This requires libusb-1.0.16 or newer on Linux or MacOS. Windows support is still on the way.
A hotplug event handler can be registered with {LIBUSB::Context#on_hotplug_event
}. You then need to call {LIBUSB::Context#handle_events
} in order to receive any events. This can be done as blocking calls (possibly in it's own thread) or by using {LIBUSB::Context#pollfds
} to detect any events to handle.
我认为 ruby-dbus 可能是正确的解决方案,但我似乎找不到任何有关如何实时检测的示例,每当存储设备连接到 Linux 机器时.谁有这样的例子?
您似乎可以采取多种不同的方法。
Here's a Ruby implementation I found 使用 Ruby FFI 链接到 libusb
。
这样做的好处是 libusb 支持热插拔通知,所以这可能是满足您需要的东西:
http://libusb.sourceforge.net/api-1.0/hotplug.html
Device hotplug support
Support for device hotplugging can be used, if
LIBUSB.has_capability?(:CAP_HAS_HOTPLUG)
returns true. This requires libusb-1.0.16 or newer on Linux or MacOS. Windows support is still on the way.A hotplug event handler can be registered with {
LIBUSB::Context#on_hotplug_event
}. You then need to call {LIBUSB::Context#handle_events
} in order to receive any events. This can be done as blocking calls (possibly in it's own thread) or by using {LIBUSB::Context#pollfds
} to detect any events to handle.