简体   繁体   English

USB设备/ dev / sdb的opendir

[英]opendir for usb device /dev/sdb

I am trying to implement in c program a way to detect if usb is connected or not. 我试图在C程序中实现一种检测USB是否已连接的方法。 I noticed that when the usb is connected, then the following command from shell will result as following: 我注意到,当连接USB时,来自shell的以下命令将导致如下结果:

root:~# ls /dev/sdb
/dev/sdb

If usb is disconnected then I get 如果USB断开连接,我会得到

root:~# ls /dev/sdb
ls: /dev/sdb: No such file or directory

I therefore assumed that best way to detect usb connected from c program shall be by doing opendir("/dev/sdb"), but the open call is always failed. 因此,我认为检测从c程序连接的USB的最佳方法应该是执行opendir(“ / dev / sdb”),但是open调用总是失败。

Do you have any idea what's best methd to do this detection ? 您是否知道进行此检测的最佳方法是什么?

The main goal, is knowing in run-time program where the udev mounted the harddisk, and where the usb flash drive (if plugged in). 主要目标是在运行时程序中知道udev在何处安装硬盘,以及USB闪存驱动器(如果已插入)。 Thanks, Ran 谢谢,冉

A) Why your opendir call failed? A)为什么您的opendir调用失败? It's easy to understand. 很容易理解。 Because /dev/sdb is not a directory. 因为/dev/sdb不是目录。 For me it's hard to understand what you expect, when you try to open device as a directory. 对我来说,当您尝试将设备作为目录打开时,很难理解您的期望。 If you want to get file list from you usb drive, you need to mount the partition ( /dev/sdb1 , /dev/sdb2 , etc. not the /dev/sdb ) to some mount point ( directory ). 如果你想从您的USB驱动器获取文件列表,你需要mount的分区( /dev/sdb1/dev/sdb2等没有/dev/sdb )一些mount pointdirectory )。 And if you mount it successfully, then you can open the directory with your call. 如果成功挂载,则可以通过呼叫打开目录。 Many linux distributions mount usb drive automatically. 许多Linux发行版会自动安装USB驱动器。 You can look to your distribution documentation to get the information about automatical mounting of the usb drives. 您可以查看发行文档以获取有关USB驱动器自动安装的信息。

B) I think it's easy to understand why detecting the usb device this way is bad idea. B)我认为很容易理解为什么用这种方法检测USB设备是个坏主意。

=> Different computers have different amount of drives. =>不同的计算机具有不同数量的驱动器。 So on many computers /dev/sdb - is a hard drive. 因此,在许多计算机上, /dev/sdb是硬盘驱动器。

=> Asuming you know that there is only one hard drive, the disk naming still can change after computer reboot, so it's possible that after reboot the usb drive will become sda and hard drive will become sdb (but usually this doesn't happen). =>假设您知道只有一个硬盘驱动器,则在计算机重新启动后,磁盘命名仍然可以更改,因此,重新启动后,usb驱动器将变为sda ,硬盘驱动器将变为sdb (但通常不会发生) 。

=> Generally, it's not possible to predict a new letter ( sdb or sdc or sde ). =>通常,无法预测新字母( sdbsdcsde )。

=> You can't access to the usb devices, that are not usb drives. =>您无法访问不是USB驱动器的USB设备。

=> There are tonns of other problems with such solution. =>这种解决方案还有其他许多问题。

C) I didn't understand your goal completely, but if you want your program to be make some action if the usb device is plugged in, that the best solution you can do is read about udev and the event system of your distribution. C)我不完全了解您的目标,但是如果您希望在插入USB设备后让程序采取一些措施,那么您可以采取的最佳解决方案是阅读udev和发行版的事件系统。 If you want to make with your usb device some low level operations, you can read about libusb . 如果要使用USB设备进行一些低级操作,可以阅读libusb You can get the general information about usb devices with lsusb command which is usually a part of a distributions. 您可以使用lsusb命令获取有关USB设备的一般信息,该命令通常是发行版的一部分。 You can google for some other infermer commands. 您可以在Google上搜索其他一些推理程序命令。

The device /dev/sdb is not a directory, it's just a device, so you should use a system call like stat that will tell you whether a file exists instead of trying to calling opendir on it. 设备/dev/sdb不是目录,而只是设备,因此您应该使用stat类的系统调用来告诉您文件是否存在,而不是尝试在文件上调用opendir
Please note that /dev/sdb could be any kind of hard drive depending on the setup of your specific system; 请注意, /dev/sdb可以是任何类型的硬盘驱动器,具体取决于您特定系统的设置。 it is not necessarily a USB drive. 它不一定是USB驱动器。

Alternatively, you could execute the lsusb utility and parse its output. 或者,您可以执行lsusb实用程序并解析其输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM