Question

What values in the inode struct of the linux kernel can I look at to tell if the inode I am currently looking at is for a directory or a regular file?

I have searched through the inode but can't really seem to find a member to looks like it has what I want, since I am using an older kernel version, I will post the struct below (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;
   };

Any help would be appreciated.

Was it helpful?

Solution

I believe it's the mode of the inode...

umode_t            i_mode;

To access the field see stat(2) man page:

   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.)

Here is some example code from the 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 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top