UNIX環境編程學習筆記(9)——檔案I/O之檔案存取權限的屏蔽和更改

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   ar   strong   for   

lienhua34
2014-09-10

1 檔案存取權限

在檔案存取權限和進程存取控制中,我們已經講述過檔案存取權限位,為了方便,我們重新列在下面,

 

表 1: 檔案的 9 個存取權限位
 st_mode 屏蔽  意義
 S_IRUSR  使用者 -讀
 S_IWUSR  使用者 -寫
 S_IXUSR  使用者 -執行
 S_IRGRP   組 -讀
 S_IWGRP  組 -寫
 S_IXGRP  組 -執行
 S_IROTH  其他 -讀
 S_IWOTH  其他 -寫
 S_IXOTH  其他 -執行

2 新檔案存取權限的屏蔽

每個進程都有一個單獨的跟進程相關聯的檔案模式建立屏蔽字(file creation mask). 每個進程的檔案模式建立屏蔽字跟該進程的父進程沒有關係,不繼承於父進程的檔案模式建立屏蔽字,本進程檔案模式建立屏蔽字變更對父進程的檔案模式進程也不會產生任何影響。

在進程建立一個新檔案時,會通過檔案模式建立屏蔽字和建立新檔案時指定的檔案存取權限位一起決定新檔案最終的檔案存取權限。對於任何在檔案模式建立屏蔽字中為 1 的位,在檔案 mode 中的相對應位則一定被關閉。

umask 函數為進程設定檔案模式建立屏蔽字,並返回以前的值。

#include <sys/stat.h>

mode_t umask(mode_t cmask);

傳回值:以前的檔案模式建立屏蔽字。

其中,參數 cmask 是由表 1 中列出的 9 個常量中的若干個按位或構成的。下面程式建立兩個檔案,建立第一個檔案 foo 時將進程的檔案模式建立屏蔽字清空,而建立第二個檔案之前,則屏蔽了所有組和其他使用者的讀寫權限。

#include <stdlib.h>#include <stdio.h>#include <fcntl.h>#include <sys/stat.h>#define RWRWRW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)intmain(void){    umask(0);    if (creat("foo", RWRWRW) < 0) {    printf("creat error for foo");        exit(-1);    }    umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);    if (creat("bar", RWRWRW) < 0) {        printf("creat error for bar");        exit(-1);    }    exit(0);}

編譯該程式,組建檔案 umaskdemo,然後運行之,

lienhua34:demo$ gcc -o umaskdemo umaskdemo.clienhua34:demo$ ./umaskdemolienhua34:demo$ ls -l bar foo-rw------- 1 lienhua34 lienhua34 0 9月 3 23:51 bar-rw-rw-rw- 1 lienhua34 lienhua34 0 9月 3 23:51 foo
3 檔案存取權限的更改

UNIX 提供了兩個函數 chmod 和 fchmod 實現對現有檔案存取權限的更改。

#include <sys/stat.h>

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

int fchmod(int filedes, mode_t mode);

兩個函數傳回值:若成功則返回0,若出錯則返回-1。

chmod 函數在指定的檔案上進行操作,而 fchmod 函數則對已開啟的檔案進行操作。

要改變一個檔案的存取權限,進程的有效使用者識別碼 必須等於檔案的所有者 ID,或者該進程必須具有超級使用者權限。

參數 mode 是表 2 中所示常量的某種按位或運算構成的。

表 2: chmod 函數的 mode 常量
mode 說明
S_ISUID 執行時設定使用者識別碼
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 其他 -執行

 

執行個體:

下面程式開啟了檔案 foo 的設定組 ID 位、關閉了組執行位。而對於檔案 bar 則強制設定為某個指定的存取權限。

#include <stdlib.h>#include <stdio.h>#include <sys/stat.h>intmain(void){    struct stat statbuf;    if (stat("foo", &statbuf) < 0) {        printf("stat error for foo");        exit(-1);    }    if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0) {        printf("chmod error for foo");        exit(-1);    }    if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {        printf("chmod error for bar");        exit(-1);    }    exit(0);}

編譯該程式檔案,組建檔案 chmoddemo,然後運行該檔案,

lienhua34:demo$ gcc -o chmoddemo chmoddemo.clienhua34:demo$ ls -l foo bar-rw------- 1 lienhua34 lienhua34 0 9月 3 23:51 bar-rw-rw-r-- 1 lienhua34 lienhua34 0 9月 3 23:51 foo-rw-r--r-- 1 lienhua34 lienhua34 0 9月 3 23:51 bar-rw-rwSr-- 1 lienhua34 lienhua34 0 9月 3 23:51 foolienhua34:demo$ ./chmoddemolienhua34:demo$ ls -l foo bar

(done)

UNIX環境編程學習筆記(9)——檔案I/O之檔案存取權限的屏蔽和更改

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.