您的位置:首页 > 运维架构 > Linux

Linux文件系统及属性

2016-03-30 08:44 316 查看
Linux文件系统及属性

宗旨:技术的学习是有限的,分享的精神是无限的。


一、Linux系统下文件类型及属性

1、inode结构

/*索引节点对象由inode结构体表示,定义文件在linux/fs.h中*/
struct inode
{
struct hlist_node       i_hash;              /* 哈希表 */
struct list_head         i_list;              /* 索引节点链表 */
struct list_head         i_dentry;            /* 目录项链表 */
unsigned long            i_ino;               /* 节点号 */
atomic_t                     i_count;             /* 引用记数 */
umode_t                     i_mode;              /* 访问权限控制 */
unsigned int               i_nlink;             /* 硬链接数 */
uid_t                           i_uid;               /* 使用者id */
gid_t                           i_gid;               /* 使用者id组 */
kdev_t                        i_rdev;              /* 实设备标识符 */
loff_t                          i_size;              /* 以字节为单位的文件大小 */
struct timespec         i_atime;             /* 最后访问时间 */
struct timespec         i_mtime;             /* 最后修改(modify)时间 */
struct timespec         i_ctime;             /* 最后改变(change)时间 */
unsigned int               i_blkbits;           /* 以位为单位的块大小 */
unsigned long            i_blksize;           /* 以字节为单位的块大小 */
unsigned long            i_version;           /* 版本号 */
unsigned long            i_blocks;            /* 文件的块数 */
unsigned short          i_bytes;             /* 使用的字节数 */
spinlock_t              i_lock;              /* 自旋锁 */
struct rw_semaphore     i_alloc_sem;         /* 索引节点信号量 */
struct inode_operations *i_op;               /* 索引节点操作表 */
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 dquot            *i_dquot[MAXQUOTAS]; /* 节点的磁盘限额 */
struct list_head        i_devices;           /* 块设备链表 */
struct pipe_inode_info  *i_pipe;             /* 管道信息 */
struct block_device     *i_bdev;             /* 块设备驱动 */
unsigned long           i_dnotify_mask;      /* 目录通知掩码 */
struct dnotify_struct   *i_dnotify;          /* 目录通知 */
unsigned long           i_state;             /* 状态标志 */
unsigned long           dirtied_when;        /* 首次修改时间 */
unsigned int            i_flags;             /* 文件系统标志 */
unsigned char           i_sock;              /* 可能是个套接字吧 */
atomic_t                i_writecount;        /* 写者记数 */
void                    *i_security;         /* 安全模块 */
__u32                   i_generation;        /* 索引节点版本号 */
union
{
void            *generic_ip;         /* 文件特殊信息 */
} u;
};<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>


2、Linux文件类型(/usr/include/bits/stat.h)

-:表示常规文件; ls –l /etc/exports

d:目录文件            ls –ld /etc/rc.d

c:字符设备文件    ls –l /dev/null

b:块设备文件        ls –l /dev/sda1

l:符号链接       

s:套接字文件

p:管道文件  【mknod pipe p     ls–l pipe】

 

二、获取文件属性函数

1、stat

       ——读取任意类型文件属性

(1)函数原型

#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>

int stat(const char *filename, struct stat buf);

struct stat
{
dev_t     st_dev;         /* 设备号 */
ino_t     st_ino;          /* inode值 */
mode_t    st_mode;   /* 文件类型及权限 */
nlink_t   st_nlink;      /* 硬连接数 */
uid_t     st_uid;          /* 用户ID */
gid_t     st_gid;          /* 用户组ID */
dev_t     st_rdev;       /* 设备号 */
off_t     st_size;          /* 文件大小 */
blksize_t st_blksize;  /* 数据块大小 */
blkcnt_t  st_blocks;   /* 数据块数量 */
time_t    st_atime;    /* 最后一次访问的时间 */
time_t    st_mtime;   /* 最后一次修改的时间 */
time_t    st_ctime;    /* 最后一次改变属性时间 */
};


(2)函数参数

        filename:欲读取状态的文件(路径字符串)

        buf:文件属性临时存放位置

(3)返回值

        执行成功,将在第二个参数中存储该文件的基本信息,并返回0;出错返回-1

 

2、fstat

       ——读取已打开的文件的状态

(1) 函数原型

int fstat(int fd, structstat *buf);


(2)函数参数

        fd:如果第一个参数是符号链接文件,其读取的属性是源文件的属性,因此要获取链接文件的属性,需要调用lstat函数。

        int lstat(const char *filename,struct stat *buf);

(3)返回值

        执行成功,将在第二个参数中存储该文件的基本信息,并返回0;出错返回-1

 

三、用户名/组名与uid/gid的转换

       函数stat()返回的某文件属性中,其拥有者所在的组以UID和GID值存在,在输出某文件用户属性时有必要将其转换为特定用户名和组名。

getpwuid()/getpwnam()

——通过用户UID或用户名查看某特定用户的基本信息

(1) 函数原型 —— 都是从/etc/passwd下读取该用户的基本信息

struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);

struct passwd
{
char *pw_name; // 用户名
char *pw_paawd; // 密码
uid_t pw_uid;      //uid
gid_t pw_gid;      // gid
char *pw_gecos; // 注释
char *pw_dir;      // 主目录
char *pw_shell;  // 默认shell
};


(2)函数参数

        uid:用户uid

(3)返回值

        成功返回struct passwd的结构体 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息