本期分享的主要是关于获取文件属性的一些函数接口,由于这些接口涉及的结构体较多,所以不像前面几期分享的函数接口显得容易记忆,因此希望大家在遇到稍微复杂的函数接口时要有耐心,仔细分析,认清每个函数参数的具体意义和内涵:
文件属性的获取:
1.stat
int stat(const char *pathname, struct stat *statbuf);
功能: 获得文件的属性
参数:
pathname:文件路径
statbuf:存放文件属性空间首地址
返回值:
成功返回0
失败返回-1
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
nlink_t st_nlink; /* Number of hard links */
uid_t st_uid; /* User ID of owner */
gid_t st_gid; /* Group ID of owner */
dev_t st_rdev; /* Device ID (if special file) */
off_t st_size; /* Total size, in bytes */
blksize_t st_blksize; /* Block size for filesystem I/O */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
/* Since Linux 2.6, the kernel supports nanosecond
precision for the following timestamp fields.
For the details before Linux 2.6, see NOTES. */
struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification */
struct timespec st_ctim; /* Time of last status change */
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
stat是得到链接文件指向的文件的属性
lstat是得到链接文件本身
2.getpwuid
struct passwd *getpwuid(uid_t uid);
功能:
通过用户UID获得用户信息
返回值:
成功返回用户信息结构体指针
失败返回NULL
struct passwd {
char *pw_name; /* username */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
3.getgrgid
struct group *getgrgid(gid_t gid);
功能:获得组信息
参数:
gid:组ID号
返回值:
成功返回组结构体空间首地址
失败返回NULL
4.软连接、硬链接:
软连接(符号链接)
ln -s 要连接向的文件 软链接文件
靠文件名链接
硬链接(通过几个名字可以同时访问一个空间)
ln 要连接向的文件 硬链接文件
通过inode节点链接
inode节点是一个标号,可以通过过这个节点找到文件的位置;
本期的分享就到这里结束啦,这里基本上列举的是关于获取文件属性的函数接口,小伙伴们可以点赞评论私信交流哦!