目录Inode与常规文件Inode

时间:2012-03-31 21:52:40

标签: linux kernel inode

我可以查看linux内核的inode结构中的哪些值来判断我当前正在查看的inode是用于目录还是常规文件?

我已经搜索了inode,但似乎无法找到一个看起来像我有我想要的成员,因为我使用的是较旧的内核版本,我将在下面发布结构(2.6.24)

struct inode 
  {
     struct hlist_node  i_hash;
     struct list_head   i_list;
     struct list_head   i_sb_list;
     struct list_head   i_dentry;
     unsigned long      i_ino;
     atomic_t           i_count;
     unsigned int       i_nlink;
     uid_t              i_uid;
     gid_t              i_gid;
     dev_t              i_rdev;
     unsigned long      i_version;
     loff_t             i_size;
     struct timespec    i_atime;
     struct timespec    i_mtime;
     struct timespec    i_ctime;
     unsigned int       i_blkbits;
     blkcnt_t           i_blocks;
     unsigned short     i_bytes;
     umode_t            i_mode;
     spinlock_t         i_lock;
     struct mutex       i_mutex;
     struct rw_semaphore              i_alloc_sem;
     const struct inode_operations   *i_op;
     const struct file_operations    *i_fop;
     struct super_block              *i_sb;
     struct file_lock                *i_flock;
     struct address_space            *i_mapping;
     struct address_space             i_data;
     struct list_head                 i_devices;

     union {
               struct pipe_inode_info *i_pipe;
               struct block_device    *i_bdev;
               struct cdev            *i_cdev;
            };

     int            i_cindex;
     __u32          i_generation;
     unsigned long  i_state;
     unsigned long  dirtied_when;   
     unsigned int   i_flags;
     atomic_t       i_writecount;
     void           *i_private;
   };

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

我相信这是inode的模式......

umode_t            i_mode;

要访问该字段,请参阅 stat(2)手册页:

   The following POSIX macros are defined to check the file type using the st_mode field:

       S_ISREG(m)  is it a regular file?

       S_ISDIR(m)  directory?

       S_ISCHR(m)  character device?

       S_ISBLK(m)  block device?

       S_ISFIFO(m) FIFO (named pipe)?

       S_ISLNK(m)  symbolic link? (Not in POSIX.1-1996.)

       S_ISSOCK(m) socket? (Not in POSIX.1-1996.)

以下是Linux driver for minix FS的一些示例代码:

434 void minix_set_inode(struct inode *inode, dev_t rdev)
435 {
436         if (S_ISREG(inode->i_mode)) {
437                 inode->i_op = &minix_file_inode_operations;
438                 inode->i_fop = &minix_file_operations;
439                 inode->i_mapping->a_ops = &minix_aops;
440         } else if (S_ISDIR(inode->i_mode)) {
441                 inode->i_op = &minix_dir_inode_operations;
442                 inode->i_fop = &minix_dir_operations;
443                 inode->i_mapping->a_ops = &minix_aops;
444         } else if (S_ISLNK(inode->i_mode)) {
445                 inode->i_op = &minix_symlink_inode_operations;
446                 inode->i_mapping->a_ops = &minix_aops;
447         } else
448                 init_special_inode(inode, inode->i_mode, rdev);
449 }
450