我的问题如下:我有一个在嵌入式 Linux 系统中运行的 C++ 软件,该软件具有将一些数据导出到随身碟的功能,现在我的陷阱来了,一些用户试图在一个旧的随身碟中使用在读/写和只读模式之间切换的键.现在,我需要知道如何检查设备是否处于只读模式,并在我的应用程序中向用户显示一些反馈.挂载设备前是否有系统调用检查只读状态?
解决方案通常的处理方式是尝试打开一个文件进行写入,然后检查是否errno == EACCES.
但是,如果您必须事先检查,那就是
int on_readonly_fs(char const *path){结构 statvfs fsinfo;while (statvfs(path, &fsinfo)) == -1)如果(错误号!= EINTR)返回-1;返回 fsinfo.f_flag &ST_RDONLY;}但这仅在安装设备后有效.
My question is the following: I have a software in C++ running in a embedded Linux system, the software has a feature to export some data to a pendrive, now comes my pitfall, some users tried to use a old pendrive in a key to change between read/write and read-only mode. Now, I need to know how to check if the device is in read-only mode the show some feedback to the user in my application. Is there a system call to check the read-only status before mount the device?
解决方案The usual way to handle this is to try to open a file for writing, then check whether errno == EACCES.
However, if you must check beforehand, that's
int on_readonly_fs(char const *path) { struct statvfs fsinfo; while (statvfs(path, &fsinfo)) == -1) if (errno != EINTR) return -1; return fsinfo.f_flag & ST_RDONLY; }But this only works after mounting the device.
更多推荐
如何发现随身碟处于只读模式?
发布评论