问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/12484364/how-to-discover-that-a-pendrive-is-in-read-only-mode