如何发现随身碟处于只读模式?

编程入门 行业动态 更新时间:2024-10-27 22:33:38
本文介绍了如何发现随身碟处于只读模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

如何发现随身碟处于只读模式?

本文发布于:2023-06-04 22:09:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/506074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   发现

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!