有没有类似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内核版本
struct inode
field i_mode
是一个位域,可以使用标准S_ISDIR
,S_ISREG
,S_ISLNK
等宏检查:
/*
* 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);
}
请注意,您需要非常小心地遍历此类内核结构。如果你没有采取正确的锁,他们可以从检查它们的线程下面更换。