獲取文件屬性node
#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);
返回值:成功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. */
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);
返回值:成功0,出錯-1
爲進程設置文件模式建立屏蔽字測試
#include <sys/types.h> #include <sys/stat.h> mode_t umask(mode_t mask); 返回值:以前文件模式建立屏蔽字
更改現有文件的訪問權限ui
#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);
返回值:成功0,出錯-1
用於改變文件的用戶ID和組ID。若是兩個參數owner或group中的任意一個是-1,則對應ID不變this
#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);
返回值:成功0,出錯-1
截斷文件spa
#include <unistd.h> #include <sys/types.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length); 返回值:成功0,出錯-1
建立一個指向現有文件的連接指針
#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,出錯-1
刪除一個現有目錄項code
#include <unistd.h> int unlink(const char *pathname); #include <fcntl.h> #include <unistd.h> int unlinkat(int dirfd, const char *pathname, int flags);
返回值:成功0,出錯-1
解除對一個文件或目錄的連接blog
#include <stdio.h> int remove(const char *pathname); 返回值:成功0,出錯-1
對文件或目錄進行重命名
#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,出錯-1
建立一個符號連接
#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,出錯-1
由於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); 返回值:成功讀取字節數,出錯-1
一個文件的訪問和修改時間能夠用如下幾個函數更改。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[2], int flags); int futimens(int fd, const struct timespec times[2]); 返回值:成功0,出錯-1
建立目錄,刪除目錄
#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);
返回值:成功0,出錯-1
刪除一個空目錄,空目錄只包含.和..
#include <unistd.h> int rmdir(const char *pathname); 返回值:成功0,出錯-1
讀目錄
#include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); DIR *fdopendir(int fd);
返回值:成功指針,出錯NULL struct dirent *readdir(DIR *dirp);
返回值:成功指針,若在目錄或出錯,返回NULL void rewinddir(DIR *dirp); int closedir(DIR *dirp);
返回值:成功0,出錯-1 long telldir(DIR *dirp);
返回值:dp關聯的目錄中的當前位置 void seekdir(DIR *dirp, long loc);
dirent結構
struct dirent { ino_t d_ino; /* inode number */ off_t d_off; /* not an offset; see NOTES */ unsigned short d_reclen; /* length of this record */ unsigned char d_type; /* type of file; not supported by all filesystem types */ char d_name[256]; /* filename */ };
更改當前目錄
#include <unistd.h> int chdir(const char *path); int fchdri(int fd);
返回值:成功0,出錯-1
獲得當前工做目錄完整絕對路徑名
#include <unistd.h> char *getcwd(char *buf, size_t size); 返回值:成功返回buf,出錯NULL