xfs文件系统d_type=0的问题

在使用readdir函数的时候,遇到一个问题–不管是文件还是目录d_type的值都是0(DT_UNKNOWN)

`#include <stdio.h>
#include <dirent.h>
#include <string.h>

int main()
{
DIR *dirp;
struct dirent *dp;
dirp = opendir(“gg/”);

while ( (dp = readdir(dirp) ) != NULL) {
    if (strcmp(dp->d_name, "..") == 0 || strcmp(dp->d_name, ".") == 0) {
        continue;
    }   
    printf("dp->d_name %s \n ", dp->d_name);
    printf("dp->d_type: %d\n", dp->d_type);
}   

}`
查看了一下man手册readdir函数中对dirent 结构体的定义,和对d_type中的描述中指出d_type不被所有的文件系统支持,对不支持的文件系统d_type会返回DT_UNKNOWN,我使用的文件系统是xfs。所以d_type返回0好像是正常的。
struct dirent {
ino_t d_ino; /* Inode number /
off_t d_off; /
Not an offset; see below /
unsigned short d_reclen; /
Length of this record /
unsigned char d_type; /
Type of file; not supported
by all filesystem types /
char d_name[256]; /
Null-terminated filename */
};
Currently, only some filesystems (among them: Btrfs, ext2,
ext3, and ext4) have full support for returning the file
type in d_type. All applications must properly handle a
return of DT_UNKNOWN.
但是我在另一台机器的xfs文件系统的目录下运行同样的代码却可以得到正确的d_type类型。
在这里插入图片描述
google了一下,结果是这样的:
As the readdir(3) man page points out, not all filesystems return real info in the d_type field (typically because it would take an extra disk seek to read the inode, as is the case for XFS if you didn’t use mkfs.xfs -n ftype=1 (implied by -m crc=1 which is not yet the default). Filesystems that always set DT_UNKNOWN are common in real life, and not something that you can ignore. XFS is not the only example.
说是ftype是原因,用xfs_info命令查了两台机器运行代码的目录,果然一个ftype是0,一个是1,ftype是1的机器可以正确输出d_type的值

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值