檔案和目錄一

2021-09-26 01:37:33 字數 1809 閱讀 1152

stat函式

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)

這幾個函式用來獲取檔案後者目錄的基本資訊,第乙個函式有兩個引數,第乙個表示檔案或目錄的名字,第二個為stat型別的指標。第二個函式第乙個引數為檔案描述符,第二個引數為stat型別的指標。第三個函式的引數和第乙個一樣,但是和第乙個函式的功能不同,當檔案或目錄是乙個鏈結檔案時第乙個函式得到資訊是被鏈結的檔案,第三個是這個鏈結的檔案的資訊。

stat型別的介紹

struct stat

int i;

char *p;

struct stat buf;

for(i=1;i測試:

root@ubuntu:/home/sun/project/dirent# ./stat2 stat1 stat2 / /usr

regular

regular

directory

directory

檔案建立遮蔽字

mode_t umask(mode_t cmask)

這個函式的作用是更改檔案建立遮蔽字,當我們建立乙個檔案時,建立檔案許可權的結果可能和我們編寫的不一樣,這是因為檔案建立遮蔽字的存在,檔案建立遮蔽字的作用便是將某個許可權設定的不起作用。

下面我們使用乙個例子來進行說明。

首先我先觀察我們的檔案建立遮蔽字
root@ubuntu:/home/sun/project/dirent# umask

0022

可以看到我的檔案建立遮蔽字是0022,後面兩個2表示遮蔽組和其他的寫許可權

我們編寫乙個程式

#include"head.h"

#define rwxall (s_irusr|s_iwusr|s_ixusr|s_irgrp|s_iwgrp|s_ixgrp|s_iroth|s_iwoth|s_ixoth)

int main()

結果:-rwxr-xr-x 1 root root 0 7月 19 17:56 bb1.txt

-rwxrwxrwx 1 root root 0 7月 19 17:56 bb2.txt

可以看到我們建立的乙個777的許可權,可以看到我們建立的第乙個檔案的組合其他使用者的寫許可權並沒有被設定,這邊是檔案建立遮蔽字起的作用,遮蔽了組和其他使用者的寫許可權。

我們建立的第二個檔案的許可權是777這是因為我們使用了umask(0),關閉了檔案建立遮蔽字。

更改檔案的訪問許可權

int chmod(const char *pathname,mode_t mode)

int fchmod(int fd,mode_t mode)

成功返回0,失敗返回-1

這兩個函式的第乙個引數表示檔名和檔案開啟的描述符。

下面是第二個引數mode

s_isuid 執行時設定使用者id

s_isgid 執行時設定組id

s_irwxu 使用者讀寫執行

s_irusr

s_iwusr

s_ixusr

s_irwxg 組讀寫執行

s_irgrp

s_iwgrp

s_ixgrp

s_irwxo 其他讀寫執行

s_iroth

s_iwoth

s_ixoth

檔案和目錄

include include include include 判斷目錄或檔案是否存在 bool isfiledirexist string path while bsearchfinished tmppath fd.cfilename tmpdestpath destdir if tmpdestp...

檔案和目錄

1 與乙個程序相關聯的id 有6個或者更多。實際 有效 儲存的設定。設定使用者id 和設定組id 當執行乙個程式檔案時,程序的有效使用者id通常就是實際的使用者id,有效組id通常就是實際的組id。但是,可以在在檔案模式字 st mode 中設定乙個特殊的標誌,其含義是 當執行此檔案時,將程序的有效...

檔案和目錄

學習筆記,小白可以相互學習,大佬看到能告訴咱理解不對的地方就好了。函式 1.stat 獲取檔案 目錄屬性資訊 include include include int stat const char path,struct stat buf 主要掌握這乙個,函式返回乙個與此命名有關的資訊結構 int ...