如何使用access / stat来测试mountpoint是否正常?

时间:2017-03-15 08:12:38

标签: c linux access mount ext4

我将磁盘挂载到挂载点,然后在没有卸载的情况下拔出磁盘。 我想使用access(mountpoint,R_OK | W_OK)来发现磁盘坏了,但是失败了......

这是我的测试代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>

int main(void)
{
    char *dir = "/sdf";
    int ret;
    struct stat s;

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

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

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

    printf("stat INFO: %s can be read and write ret=%d\n", dir, ret);
    return 0;
}

结果如下:

[test]# ./access 
access INFO: /sdf can be read and write ret=0
stat INFO: /sdf can be read and write ret=0

我使用ls列出mountpoint:

[test]# ls /sdf
ls: reading directory /sdf: Input/output error

我不知道为什么会这样......

任何人都可以帮助我吗?

我使用ext4文件系统

[test]# mount |grep sdf
/root/loopfile.img on /sdf type ext4 (ro,relatime)
/dev/sdf on /sdf type ext4 (rw,noatime,data=ordered)

0 个答案:

没有答案