1.6.3 Linux 文件属性函数
Linux
中一切皆文件,你同样可以用下面这些函数操作目录等特殊文件。
前言
关于文件操作函数这一块主要用英文文档的形势书写,因为凉皮在写文档的时候发现Markdown
用起来太繁琐了。那么关于函数
这一块主要也是读文档就能解决的了。
关于文档可以使用Linux
命令man 2/3 xxx
查看,在后面也写了查看每个文档的命令。
返回文件属性
-
stat
、lstat
-
常用属性:
st_mode
:文件属性编码。
st_size
:文件大小Bytes
。st_atime
:访问时间。st_mtime
:修改时间。st_ctime
:修改时间,指属性修改。
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
// get file status
// pathname:
// the path of target file
// statbuf:
// the container to restore data
// 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 */
//
// 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
// #define st_mtime st_mtim.tv_sec
// #define st_ctime st_ctim.tv_sec
// };
// return value:
// return 0 for success, -1 for error
int stat(const char *pathname, struct stat *statbuf);
// similar to stat
int lstat(const char *pathname, struct stat *statbuf);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("stat");
// About more
// $ man 2 stat
lstat
与stat
的区别:stat
在访问软链接时,返回软链接属性;lstat
在访问软链接时,返回指向文件属性。
$ ln -s src.txt link.lnk // 建立软链接 link.lnk -> src.txt
判断文件访问
access
#include <unistd.h>
// check permissions to the file for current process
// pathname:
// the file path
// mode:
// optional and limitless:
// F_OK for file exists,
// R_OK for read permission for process,
// W_OK for write permission for process,
// X_OK for execute permission for process
// return value:
// return 0 for all permission is available, -1 for other case and error
int access(const char *pathname, int mode);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("access");
// About more
// $ man 2 access
修改文件属性
chmod
#include <sys/stat.h>
// change mode of file
// pathname:
// the file path
// mode:
// three digits of oct-based number
// UMASK work here
// return value:
// return 0 for success, -1 for error
int chmod(const char *pathname, mode_t mode);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
void perror("chmod");
// About more
// $ man 2 chmod
修改文件所有
chown
#include <unistd.h>
// change owner of file
// pathname:
// the file path
// owner:
// the UID of owner
// group:
// the GID of group
// return 0 for success, -1 for error
int chown(const char *pathname, uid_t owner, gid_t group);
// if return value is -1, you can then call to print the error message
#include <stdio.h>
void perror("chown");
// About more
// $ man 2 chown