获取文件属性

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

int stat(const char *pathname, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *pathname, struct stat *buf);

#include <fcntl.h>
#include <sys/stat.h>

int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags);<br />返回值:成功0,出错-1

stat结构:

struct stat {
  dev_t     st_dev;         /* ID of device containing file */
  ino_t     st_ino;         /* inode number */
  mode_t    st_mode;        /* protection */
  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;     /* blocksize 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. */
<br />  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
};

按实际用户ID和实际组ID进行访问权限测试

#include <unistd.h>

int access(const char *pathname, int mode);

#include <fcntl.h>
#include <unistd.h>

int faccessat(int dirfd, const char *pathname, int mode, int flags);<br />返回值:成功0,出错-1

为进程设置文件模式创建屏蔽字

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

mode_t umask(mode_t mask);
返回值:之前文件模式创建屏蔽字

更改现有文件的访问权限

#include <sys/stat.h>

int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);

#include <fcntl.h>
#inlcude <sys/stat.h>

int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);<br />返回值:成功0,出错-1

用于改变文件的用户ID和组ID。如果两个参数owner或group中的任意一个是-1,则对应ID不变

#include <unistd.h>

int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);

#include <fcntl.h>           /* Definition of AT_* constants */
#include <unistd.h>

int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags);<br />返回值:成功0,出错-1

截断文件

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

int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
返回值:成功0,出错-

创建一个指向现有文件的链接

#include <unistd.h>

int link(const char *oldpath, const char *newpath);

#include <fcntl.h>           /* Definition of AT_* constants */
#include <unistd.h>

int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags);
返回值:成功0,出错-

删除一个现有目录项

#include <unistd.h>

int unlink(const char *pathname);

#include <fcntl.h>
#include <unistd.h>

int unlinkat(int dirfd, const char *pathname, int flags);<br />返回值:成功0,出错-1

解除对一个文件或目录的链接

#include <stdio.h>

int remove(const char *pathname);
返回值:成功0,出错-

对文件或目录进行重命名

#include <stdio.h>

int rename(const char *oldpath, const char *newpath);

#include <fcntl.h>
#include <stdio.h>

int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
int renameat2(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, unsigned int flags);
返回值:成功0,出错-

创建一个符号链接

#include <unistd.h>

int symlink(const char *target, const char *linkpath);

#include <fcntl.h>
#include <unistd.h>

int symlinkat(const char *target, int newdirfd, const char *linkpath);
返回值:成功0,出错-

因为open函数跟随符号链接,所以需要一种方法打开链接本身,并读该链接中的名字。

#include <unistd.h>

ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);

#include <fcntl.h>           /* Definition of AT_* constants */
#include <unistd.h>

ssize_t readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz);
返回值:成功读取字节数,出错-

一个文件的访问和修改时间可以用以下几个函数更改。futimens和utimensat函数可以指定纳秒级精度的时间戳。

用到数据结构是与stat函数族相同的timespec结构。

#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>

int utimensat(int dirfd, const char *pathname, const struct timespec times[], int flags);

int futimens(int fd, const struct timespec times[]);
返回值:成功0,出错-

创建目录,删除目录

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

int mkdir(const char *pathname, mode_t mode);

#include <fcntl.h>           /* Definition of AT_* constants */
#include <sys/stat.h>

int mkdirat(int dirfd, const char *pathname, mode_t mode);<br />返回值:成功0,出错-1

删除一个空目录,空目录只包含.和..

#include <unistd.h>

int rmdir(const char *pathname);
返回值:成功0,出错-

读目录

#include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);
DIR *fdopendir(int fd);<br />返回值:成功指针,出错NULL
struct dirent *readdir(DIR *dirp);<br />返回值:成功指针,若在目录或出错,返回NULL
void rewinddir(DIR *dirp);
int closedir(DIR *dirp);<br />返回值:成功0,出错-1
long telldir(DIR *dirp);<br />返回值:dp关联的目录中的当前位置
void seekdir(DIR *dirp, long loc);

更改当前目录

#include <unistd.h>

int chdir(const char *path);
int fchdri(int fd);<br />返回值:成功0,出错-1

得到当前工作目录完整绝对路径名

#include <unistd.h>

char *getcwd(char *buf, size_t size);
返回值:成功返回buf,出错NULL