How to discover that a pendrive is in read-only mode?

萝らか妹 提交于 2019-12-24 03:06:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!