2. 函數stat、fstat、fstatat、lstat
struct stat {
mode_t st_mode; /* file type & mode (permissions), suid, sgid */
ino_t st_ino; /* i-node number (serial number) */
dev_t st_dev; /* device number (file system) */
dev_t st_rdev; /* device number for special files */
nlink_t st_nlink; /* number of links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
off_t st_size; /* size in bytes, for regular files */
struct timespec st_atim; /* time of last access */
struct timespec st_mtim; /* time of last modification */
struct timespec st_ctim; /* time of last file status change */
blksize_t st_blksize; /* best I/O block size */
blkcnt_t st_blocks; /* number of disk blocks allocated , 512B */
};
struct timespec {
time_t tv_sec;
long tv_nsec;
}
3. 文件類型
#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict buf );
int fstat(int fd, struct stat *buf );
int lstat(const char *restrict pathname, struct stat *restrict buf ); // 返回該符號鏈接本身的有關信息
int fstatat(int fd, const char *restrict pathname, struct stat *restrict buf, int flag);
All four return: 0 if OK, −1 on error
符號鏈接 l
文件類型宏:參數為stat結構中的st_mode成員(文件模式字)
ipC類型宏:參數為stat結構的指針
每個進程關聯的用戶ID和組ID
,包括實際用戶ID、有效用戶ID、保存的設置用戶ID。實際用戶ID指的是執行該程序的用戶的ID。注意區別于文件的所有者
。
每個文件有一個所有者和組所有者,由stat結構中的st_uid, st_gid指定。
所有文件類型都有訪問權限
進程每次打開、創建或刪除一個文件時,內核就進行文件訪問權限測試,這種測試可能涉及文件的所有者(st_uid和st_gid)、進程的有效ID(有效用戶ID和有效組ID)、進程的附屬組ID。兩個所有者ID是文件的性質,而兩個有效ID和附屬組ID則是進程的性質。
6. 新文件和新目錄的所有權
- 若進程的有效用戶ID是0(超級用戶),則運行訪問
- 若進程的有效用戶ID等于文件的所有者ID(即進程擁有此文件),那么如果所有者適當的訪問權限位被設置,則運行訪問;否則拒絕訪問。適當的訪問權限位指的是:若進程為讀而打開該文件,則用戶讀位應為1;若進程為寫而打開該文件,則用戶寫位應為1;若進程將執行該文件,則用戶執行位應為1。
- 若進程的有效組ID或進程的附屬組ID之一等于文件的組ID,那么如果組適當的訪問權限位被設置,則允許訪問;否則拒絕訪問。
- 若其他用戶適當的訪問權限位被設置,則允許訪問;否則拒絕訪問。按順序執行這4步。一旦前面的被拒絕了,即使后面的組、其他用戶擁有相應權限也白搭。
linux下如果新目錄的父目錄的SUID被設置,則選擇2
當open函數打開一個文件時,內核以進程的有效用戶ID和有效組ID為基礎執行其訪問權限測試。有時,進程希望以進程的實際用戶ID和實際組ID為基礎來執行其訪問權限測試。這使用以下兩個函數:
#include <unistd.h>
int access(const char *pathname, int mode);
int faccessat(int fd, const char *pathname, int mode, int flag);
Both return: 0 if OK, −1 on error
測試文件是否存在,mode為F_OK;測試讀/寫/執行權限,mode為R_OK、W_OK、X_OK的按位與
8. 函數umask
#include <sys/stat.h>
mode_t umask(mode_t cmask);
Returns: PRevious file mode creation mask
#include <sys/stat.h>
新聞熱點
疑難解答