当前位置: 技术问答>linux和unix
请问Linux如何区分目录和文件?
来源: 互联网 发布时间:2017-02-01
本文导语: Linux中目录和文件都是用inode结构表示的? struct inode结构中有区分的么? 和struct dentry目录项结构有关系么? 附上struct inode结构前几个变量: struct inode { struct hlist_node i_hash; struct list_head i_list; /* backing dev IO l...
Linux中目录和文件都是用inode结构表示的?
struct inode结构中有区分的么?
和struct dentry目录项结构有关系么?
附上struct inode结构前几个变量:
struct inode {
struct hlist_node i_hash;
struct list_head i_list; /* backing dev IO 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;
struct inode结构中有区分的么?
和struct dentry目录项结构有关系么?
附上struct inode结构前几个变量:
struct inode {
struct hlist_node i_hash;
struct list_head i_list; /* backing dev IO 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;
|
通过struct stat.st_mode 来区分的吧
有一个宏,可以判断是否目录
S_ISDIR(st.st_mode)
有一个宏,可以判断是否目录
S_ISDIR(st.st_mode)
|
这个结构体中的i_mode中有此节点所对应的结构是文件还是目录。
内核中用宏区分如下:
#define S_IFMT 00170000
#define S_IFSOCK 0140000
#define S_IFLNK 0120000
#define S_IFREG 0100000//常规文件
#define S_IFBLK 0060000
#define S_IFDIR 0040000//目录
#define S_IFCHR 0020000
#define S_IFIFO 0010000
#define S_ISUID 0004000
#define S_ISGID 0002000
#define S_ISVTX 0001000
#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
内核中用宏区分如下:
#define S_IFMT 00170000
#define S_IFSOCK 0140000
#define S_IFLNK 0120000
#define S_IFREG 0100000//常规文件
#define S_IFBLK 0060000
#define S_IFDIR 0040000//目录
#define S_IFCHR 0020000
#define S_IFIFO 0010000
#define S_ISUID 0004000
#define S_ISGID 0002000
#define S_ISVTX 0001000
#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)