Linux文件管理參考

在這裏插入圖片描述

本文參考《APUE》
爲我的團隊成員們作準備,詳情見:https://blog.csdn.net/qq_43762191/article/details/106697094
CSDN搜“看,未來”:https://blog.csdn.net/qq_43762191

本文代碼皆爲使用示例。

stat

先來看是三個函數:stat、fstat、lstat

 #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);
//返回值:若成功,返回0;若出錯,返回-1

這三個函數幹嘛用呢?獲取文件信息。那我稍微解釋一下吧,這個不是重點。

stat函數返回與pathname有關的信息結構。stat稱爲跟蹤鏈接。
fstat函數獲得已在描述符fd上打開的文件的有關信息。
lstat函數與stat函數類似,但當命令文件是一個符號鏈接時,lstat返回該符號鏈接的有關信息,而不是由該符號鏈接引用的文件的信息。lstat稱爲不跟蹤符號鏈接。

這三個函數有一個相同類型的參數struct stat *類型的buf,buf是一個指針。

文件類型

頭文件:#include<sys/stat.h>
在這裏插入圖片描述
看個實驗測試,書裏代碼太長我就不搬運了:

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

using namespace _GNU_SOURCE

在這裏插入圖片描述

拿去運行一下呢:

在這裏插入圖片描述

用戶訪問權限

每個文件有9個訪問權限位,可以分成三類。

在這裏插入圖片描述chmod(1)命令用於修改這9個權限位。

access函數

access函數用於測試用戶對於文件的權限:
在這裏插入圖片描述

#include <unistd.h>

int access(const char *pathname, int mode);
//返回值:若成功,返回0,若出錯,返回-1

使用示例:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char* argv[])
{
    if(argc < 2){
        printf("./aut filename\n");
        exit(1);
    }

    if(access(argv[1], F_OK) < 0){
        perror("access");
        exit(1);
    }
    else{
        printf("%s : file exist\n", argv[1]);
    }

    if(access(argv[1], R_OK) < 0){
        perror("access error");
    }
    else{
        printf("read access OK\n");
    }
    
    if(open(argv[1], O_RDONLY) < 0){
        perror("open error");
        exit(1);
    }
    else{
        printf("open read OK\n");
    }
    return 0;
}

chmod、fchmod

這倆函數,用於修改現有文件權限。

#include <sys/stat.h>

int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
//返回值:若成功,返回0,若出錯,返回-1

mode參數除了之前的9個,還有6個如下:
在這裏插入圖片描述
使用示例:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main()
{
        struct stat buf;

        if(stat("foo", &buf) < 0){
                perror("stat err");
                exit(1);
        }
        if(chmod("foo", (buf.st_mode & ~S_IXGRP) | S_ISGID) < 0){
                perror("chmod foo err");
                exit(1);
        }
        return 0;
}

link / unlink

使用link函數創建一個指向現有文件的鏈接:

link

#include <unistd.h>

int link(const char *oldpath, const char *newpath);
//返回值:若成功,返回0, 若出錯,返回-1
  • 這個函數創建一個新目錄項newpath,它引用現有的文件,如果newpath已經存在,則返回出錯。只創建newpath最後一個分量,路徑中的其他部分應當已經存在。
  • 不允許對目錄創建硬鏈接。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
        if(link("file", "file.link") < 0){
                perror("link err");
                exit(1);
        }
}

unlink

爲了刪除一個現有的目錄項:

#include <unistd.h>

int unlink(const char *pathname);
//返回值:若成功,返回0,若出錯,返回-1
  • unlink刪除目錄項,並由pathname所引用文件的鏈接數減1
  • 如果要解除對文件鏈接,必須對包含該目錄項的目錄具有寫和執行權限。
  • 只有當鏈接計數達到0時,該文件的內容纔可被刪除,如果內核首先檢查是否有進程打開了該文件, 在檢查連接數,二者計數都爲0時,那麼才刪除文件的內容。
int main()
{
        int fd;
        if((fd = open("file.link", O_RDONLY)) < 0){//打開file.link文件
                perror("open file.link err");
                exit(1);
        }
        printf("open file.link\n");
        if(unlink("file.link") < 0){//解除file.link
                perror("unlink file.link err");
                exit(1);
        }
        if(unlink("file") < 0){//解除file
                perror("unlink file.link err");
                exit(1);
        }
        sleep(10);

        printf("done\n");
        close(fd);
}

mkdir / rmdir

mkdir

mkdir函數可以創建新的目錄,其中“.”和“…”自動創建。
所指定的文件訪問權限mode由進程的文件模式創建屏蔽字修改。
目錄至少要設置一個執行權限位。

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

int mkdir(const char *pathname, mode_t mode);
//返回值:若成功,返回0, 若出錯返回-1

rmdir

rmdir函數刪除一個空目錄。空目錄只有“.”和“…”這兩項目錄
若調用這兩個函數時目錄鏈接計數爲0,並且也沒有其他進程打開此目錄,則釋放由此目錄佔用的空間。

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

int mkdir(const char *pathname, mode_t mode);
//返回值:若成功,返回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);
//返回值:與drip關聯的目錄中的當前位置

void seekdir(DIR *dirp, long loc);

一般由opendir和fopendir返回指向DIR結構的指針由另外5個函數調用。

大概先來這些。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章