日期:2014-05-16  浏览次数:20665 次

unix是如何从i节点得到目录信息的
unix是如何从i节点得到目录信息的,包括目录名和目录包含的文件之类的内容。
“由这个索引节点得到目录内容的存放位置”这样一句话就带过了……

i节点的结构体里面没有找到指向目录表的编号,难道i节点的i就指向dir[i]?

------解决方案--------------------
Linux内核中inode结构是通过i_dentry链表成员指向目录项对象的。
dentry结构在内核中表示目录项。
可以通过inode_operations结构中的create函数建立指向dentry结构对象的连接,lookup函数可以查找inode中的目录项。等等。
------解决方案--------------------
i节点结构中有个i_dentry这个成员,这个成员是一个链表,对应着一群目录项。
比如硬连接的情况,一个i节点可能对应多个目录项。
------解决方案--------------------
这些数据结构linux内核源代码都有,
~/include/linux/fs.h中定义的inode结构:
struct inode {
struct hlist_node i_hash;
struct list_head i_list;
struct list_head i_sb_list;
struct list_head i_dentry; //这是一个dentry结构的链表
unsigned long i_ino;
atomic_t i_count;
umode_t i_mode;
unsigned int i_nlink;
uid_t i_uid;
gid_t i_gid;
dev_t i_rdev;
loff_t i_size;
struct timespec i_atime;
struct timespec i_mtime;
struct timespec i_ctime;
unsigned int i_blkbits;
unsigned long i_blksize;
unsigned long i_version;
unsigned long i_blocks;
unsigned short i_bytes;
spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
struct semaphore i_sem;
struct rw_semaphore i_alloc_sem;
struct inode_operations *i_op;
struct file_operations *i_fop; /* former -> i_op-> default_file_ops */
struct super_block *i_sb;
struct file_lock *i_flock;
struct address_space *i_mapping;
struct address_space i_data;
#ifdef CONFIG_QUOTA
struct dquot *i_dquot[MAXQUOTAS];
#endif
/* These three should probably be a union */
struct list_head i_devices;
struct pipe_inode_info *i_pipe;
struct block_device *i_bdev;
struct cdev *i_cdev;
int i_cindex;

__u32 i_generation;

#ifdef CONFIG_DNOTIFY
unsigned long i_dnotify_mask; /* Directory notify events */
struct dnotify_struct *i_dnotify; /* for directory notifications */
#endif

#ifdef CONFIG_INOTIFY
struct list_head inotify_watches; /* watches on this inode */
struct semaphore inotify_sem; /* protects the watches list */
#endif

unsigned long i_state;
unsigned long dirtied_when; /* jiffies of first dirtying */

unsigned int i_flags;

atomic_t i_writecount;
void *i_security;
union {
void *generic_ip;
} u;
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t i_size_seqcount;
#endif
};

结构dentry定义在~/include/linux/dcache.h中,其中的d_iname成员就是文件名
比如
inode i;
.... //对i进行初始化
char *ch=(struct dentry)list_entry(&i-> i_dentry,struct inode,i_dentry)-> d_iname;
就可以访问i节点i的文件名了。