如何发现随身碟处于只读模式? [英] How to discover that a pendrive is in read-only mode?

查看:30
本文介绍了如何发现随身碟处于只读模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:我有一个在嵌入式 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.

这篇关于如何发现随身碟处于只读模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆