是否存在诸如struct dirent* -> d_type
之类的包含DT_REG
,DT_DIR
,DT_SOCK
等内容的内核结构,例如struct file
?纵观其领域,我找不到任何目的。
也许有人知道readdir
如何确定d_type
?我在这里https://github.com/lattera/glibc/blob/master/dirent/readdir.c处查看其实现,我不明白这里的情况。
Ubuntu18.04,4.15.0-45内核版本
答案 0 :(得分:4)
struct inode
field i_mode
是一个位域,可以使用标准的S_ISDIR
,S_ISREG
,S_ISLNK
等宏进行检查:< / p>
/*
* Keep mostly read-only and often accessed (especially for
* the RCU path lookup and 'stat' data) fields at the beginning
* of the 'struct inode'
*/
struct inode {
umode_t i_mode;
unsigned short i_opflags;
kuid_t i_uid;
kgid_t i_gid;
.
.
.
An example of its use in ext4 kernel code:
/*
* Test whether an inode is a fast symlink.
* A fast symlink has its symlink data stored in ext4_inode_info->i_data.
*/
int ext4_inode_is_fast_symlink(struct inode *inode)
{
if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
int ea_blocks = EXT4_I(inode)->i_file_acl ?
EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
if (ext4_has_inline_data(inode))
return 0;
return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
}
return S_ISLNK(inode->i_mode) && inode->i_size &&
(inode->i_size < EXT4_N_BLOCKS * 4);
}
请注意,在遍历此类内核结构时必须格外小心。如果您未使用适当的锁,则可以从检查它们的线程下面将其更改。