免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1511 | 回复: 2

[C] 判断目录是否已经不能使用 [复制链接]

论坛徽章:
1
NBA常规赛纪念章
日期:2015-05-04 22:32:03
发表于 2017-03-15 15:27 |显示全部楼层
将磁盘挂载到某个目录上,然后磁盘损坏了,如何通过代码判断磁盘是否损坏。
我是通过access函数去判断的,但是access返回的是目录可读可写,代码如下:
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/stat.h>

  4. int main(void)
  5. {
  6.         char *dir = "/mnt";
  7.         int ret;
  8.         struct stat s;

  9.         ret = access(dir, R_OK | W_OK);
  10.         if (ret < 0) {
  11.                 printf("access ERROR: %s can not be read or write ret=%d\n", dir, ret);
  12.                 return 0;
  13.         }

  14.         printf("access INFO: %s can be read and write ret=%d\n", dir, ret);

  15.         ret = stat(dir, &s);
  16.         if (ret < 0) {
  17.                 printf("stat ERROR: %s can not be read or write ret=%d\n", dir, ret);
  18.                 return 0;
  19.         }

  20.         printf("stat INFO: %s can be read and write ret=%d\n", dir, ret);
  21.         return 0;
  22. }
复制代码


而我执行ls,却提示错误
  1. [test]# ./access
  2. access INFO: /mnt can be read and write ret=0
  3. stat INFO: /mnt can be read and write ret=0
  4. [test]# ls /sdf
  5. ls: reading directory /mnt: Input/output error
复制代码


各位大神知道是什么原因吗?

论坛徽章:
0
发表于 2017-04-06 12:42 |显示全部楼层
用opendir试试

论坛徽章:
0
发表于 2017-04-07 17:21 |显示全部楼层
建议使用strace.

strace ls linux-stable/


stat("linux-stable/", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
openat(AT_FDCWD, "linux-stable/", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
getdents(3, /* 39 entries */, 3276    = 1216
getdents(3, /* 0 entries */, 3276     = 0
close(3)                                = 0

在我的机器上使用的getdents。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP