天天看點

檔案管理--檔案的硬連結

1.建立一個硬連結

#include<unistd.h>

定義函數 int link (const char * oldpath,const char * newpath);

函數說明 link()以參數newpath指定的名稱來建立一個新的連接配接(硬連接配接)到參數oldpath所指定的已存在檔案。如果參數newpath指定的名稱為一已存在的檔案則不會建立連接配接。

傳回值 成功則傳回0,失敗傳回-1,錯誤原因存于errno。

附加說明 link()所建立的硬連接配接無法跨越不同檔案系統,如果需要請改用symlink()。

錯誤代碼

EXDEV 參數oldpath與newpath不是建立在同一檔案系統。

EPERM 參數oldpath與newpath所指的檔案系統不支援硬連接配接

EROFS 檔案存在于隻讀檔案系統内

EFAULT 參數oldpath或newpath 指針超出可存取記憶體空間。

ENAMETOLLONG 參數oldpath或newpath太長

ENOMEM 核心記憶體不足

EEXIST 參數newpath所指的檔案名已存在。

EMLINK 參數oldpath所指的檔案已達最大連接配接數目。

ELOOP 參數pathname有過多符号連接配接問題

ENOSPC 檔案系統的剩餘空間不足。

EIO I/O 存取錯誤。

#include<unistd.h>

定義函數 int symlink( const char * oldpath,const char * newpath);

函數說明 symlink()以參數newpath指定的名稱來建立一個新的連接配接(符号連接配接)到參數oldpath所指定的已存在檔案。參數oldpath指定的檔案不一定要存在,如果參數newpath指定的名稱為一已存在的檔案則不會建立連接配接。

傳回值 成功則傳回0,失敗傳回-1,錯誤原因存于errno。

錯誤代碼 EPERM 參數oldpath與newpath所指的檔案系統不支援符号連接配接

EROFS 欲測試寫入權限的檔案存在于隻讀檔案系統内

EFAULT 參數oldpath或newpath指針超出可存取記憶體空間。

ENAMETOOLONG 參數oldpath或newpath太長

ENOMEM 核心記憶體不足

EEXIST 參數newpath所指的檔案名已存在。

EMLINK 參數oldpath所指的檔案已達到最大連接配接數目

ELOOP 參數pathname有過多符号連接配接問題

ENOSPC 檔案系統的剩餘空間不足

EIO I/O 存取錯誤

2.删除一個硬連結

#include<unistd.h>

定義函數 int unlink(const char * pathname);

函數說明 unlink()會删除參數pathname指定的檔案。如果該檔案名為最後連接配接點,但有其他程序打開了此檔案,則在所有關于此檔案的檔案描述詞皆關閉後才會删除。如果參數pathname為一符号連接配接,則此連接配接會被删除。

傳回值 成功則傳回0,失敗傳回-1,錯誤原因存于errno

錯誤代碼 EROFS 檔案存在于隻讀檔案系統内

EFAULT 參數pathname指針超出可存取記憶體空間

ENAMETOOLONG 參數pathname太長

ENOMEM 核心記憶體不足

ELOOP 參數pathname 有過多符号連接配接問題

EIO I/O 存取錯誤

3.範例

//hardlink.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <string.h>

#define MAX 1024

int main(void)

{

    int fd;

    struct stat statbuf;

    char buf[MAX];

    int n;

    if(stat("test.txt", &statbuf) == -1){

        perror("fail to get status");

        exit(1);   

    }

    printf("test.txt, the number of links is : %d/n", statbuf.st_nlink);

    if(link("test.txt", "test2.txt") == -1){

        perror("fail to link");

        exit(1);   

    }

    if(stat("test.txt", &statbuf) == -1){

        perror("fail to get status");

        exit(1);   

    }

    printf("test.txt, the number of links is : %d/n", statbuf.st_nlink);

    if(stat("test2.txt", &statbuf) == -1){

        perror("fail to get status");

        exit(1);   

    }

    printf("test2.txt, the number of links is : %d/n", statbuf.st_nlink);

    printf("/n");

    if((fd = open("test.txt", O_RDWR)) == -1){

        perror("fail to open");

        exit(1);

    }

    strcpy(buf, "hello world");

    if((n = write(fd, buf, strlen(buf))) == -1){

        perror("fail to write");

        exit(1);   

    }

    close(fd);

    if((fd = open("test2.txt", O_RDWR)) == -1){

        perror("fail to open");

        exit(1);

    }

    if((n = read(fd, buf, n)) == -1){

        perror("fail to read");

        exit(1);   

    }

    buf[n] = '/0';

    printf("content of test2.txt is : %s/n", buf);

    close(fd);

    if(unlink("test2.txt") == -1){

        perror("fail to unlink");

        exit(1);   

    }

    if(stat("test.txt", &statbuf) == -1){

        perror("fail to get status");

        exit(1);   

    }

    printf("test.txt, the number of links is : %d/n", statbuf.st_nlink);

    if((fd = open("test.txt", O_RDWR)) == -1){

        perror("fail to open");

        exit(1);

    }

    if(unlink("test.txt") == -1){

        perror("fail to unlink");

        exit(1);   

    }

    if(fstat(fd, &statbuf) == -1){

        perror("fail to get status");

        exit(1);   

    }

    printf("test.txt, the number of links is : %d/n", statbuf.st_nlink);

    printf("/n");

    if((n = read(fd, buf, n)) == -1){

        perror("fail to read");

        exit(1);   

    }

    buf[n] = '/0';

    printf("content of test.txt is : %s/n", buf);

    close(fd);

    return 0;   

}

該例子同時說明,即使一個檔案的連結數已經為0,但是隻要該檔案打開,程式仍可以引用該檔案。包括對檔案的讀和寫,但此時的讀寫操作實際上是對記憶體中緩沖區進行操作的。當檔案被關閉時,記憶體中的緩沖區消失,檔案也消失。