Unix文件系統(tǒng)和pwd命令實現(xiàn)詳解

2020-07-12 19:24:46 來源:互聯(lián)網(wǎng)作者:佚名 人氣: 次閱讀 0 條評論

小編要為大家?guī)淼氖荱nix文件系統(tǒng)和pwd命令實現(xiàn)詳解,希望對大家會有幫助,有需要的朋友一起去看看吧...

1. 對于用戶來講Unix系統(tǒng)硬盤上的文件組成一棵目錄樹。每個目錄能包含文件和其他子目錄。

目錄樹的深度幾乎沒有限制,當然如果你所創(chuàng)建的目錄樹太深,系統(tǒng)就會提醒超過范圍,并停止執(zhí)行,以下腳本經(jīng)測試有效

while true

do

mkdir deep_well

cd deep_well

done

我運行了幾秒后,中斷系統(tǒng)提示超過目錄樹范圍。

2. 一個磁盤可以劃分為N多扇區(qū),每個扇區(qū)有512字節(jié) 。扇區(qū)是磁盤上的基本存儲單元,我們可以將每個扇區(qū)進行編號,這樣磁盤就變?yōu)?/strong>

一系列編了號的塊的組合。

3. 磁盤塊上存儲文件時,按照一定的規(guī)律。

每個文件系統(tǒng)分為3部分:超級塊,i-節(jié)點表,數(shù)據(jù)區(qū) 。

超級塊 :存放文件系統(tǒng)本身的信息,比如記錄了每個區(qū)域的大小,或未被使用的磁盤塊的信息。(不同版本稍有差別)

i-節(jié)點表 :每個文件都有其屬性,大小,最近修改時間等等,這些被存儲在ino_t 的結(jié)構(gòu)體中,所有的i-節(jié)點都有一樣的大小,i-節(jié)點表就是這樣一些節(jié)點的列表。

(表中的每個i-節(jié)點都通過位置來標志,例如標志為2的i-節(jié)點位于文件系統(tǒng)i-節(jié)點表中的第3個位置 )

數(shù)據(jù)塊 :存放文件內(nèi)容,因為塊的大小一定,所以有時一個文件會分布在多個磁盤上。

4. 創(chuàng)建一個文件的4個步驟:

存儲屬性:內(nèi)核先找到一個空的i-節(jié)點,把文件的屬性信息填入其中;

存儲數(shù)據(jù):從磁盤上找出空閑塊,把文件數(shù)據(jù)復(fù)制進去;

記錄分配情況:內(nèi)核在i-節(jié)點的磁盤分布區(qū)記錄了剛剛的磁盤編號

添加文件名到目錄:將(i-節(jié)點號,文件名)添加到目錄。

5. cat,more等一些命令的實現(xiàn)思想:

cat name

在目錄中尋找文件名,

定位到相應(yīng)文件名的i-節(jié)點號;

根據(jù)i-節(jié)點號里面獲得文件屬性,查看權(quán)限,若權(quán)限不夠則open()函數(shù)返回1,打開失敗,停止;

根據(jù)i-節(jié)點里面磁盤位置訪問文件位置的數(shù)據(jù)塊

一遍遍調(diào)用read讀取數(shù)據(jù)(可以存放到緩沖區(qū))

6. 大文件的存儲

如果一個文件需要14個編號的磁盤塊來存儲,但是i-節(jié)點值包含13個項的分配鏈表,這時候,我們可以將前10個放到i-節(jié)點里,其他4個放到一個數(shù)據(jù)塊里面,在i-節(jié)點的第

11位寫上指向存那4個編號的塊。則我們實際用了10+4+1個數(shù)據(jù)塊,那個多出來的叫:間接塊 。

同理,間接塊飽和時,我們可以設(shè)置二級間接塊,,,

7. 文件在目錄中的含義

目錄包含(i-節(jié)點號,文件名)的入口,即目錄包含的是文件的引用,每個應(yīng)用稱為鏈接。

8. 目錄包含子目錄的含義

目錄包含指向子目錄i-節(jié)點的鏈接。

9. 目錄有個父目錄的含義:

目錄包含..的鏈接,即指向父目錄。

10. 文件沒有名字只有i-節(jié)點號,但是鏈接可以有名字 ,一個文件可以有多個鏈接(他們的名字也可以不同,但是他們指向一個文件,對他們的操作就是對源文件的操作)

11. Unix系統(tǒng)可以包含多個文件系統(tǒng),每個文件系統(tǒng)都是一棵獨立的樹,都有根目錄,但是系統(tǒng)可以將他們整合成一棵大樹,即一個樹的根裝載到另一個數(shù)的某個節(jié)點上。mount

12 符號鏈接通過文件名引用文件,可以跨越文件系統(tǒng),也可以指向目錄。相當于windows中快捷方式。

硬鏈接是將目錄鏈接到樹的指針,同時也是將文件名和文件本身鏈接起來的指針。通過對i-節(jié)點號引用文件。

13 .與目錄樹相關(guān)的命令和系統(tǒng)調(diào)用

命令 mkdir

實現(xiàn) 頭文件 #include <sys/stat.h> #include <sys/types.h>

函數(shù)原型 int res=mkdir (char *path,mode_t mode);

命令 rmdir 刪除一個目錄,這個目錄必須是空的

實現(xiàn) 頭文件#include <unistd.h>

函數(shù)原型int res=rmdir (const char* path);

命令 rm 減少相應(yīng)i-節(jié)點連接數(shù),若此時節(jié)點書減為0,就釋放數(shù)據(jù)塊和節(jié)點。不能用來刪除目錄

實現(xiàn) 頭文件#include <unistd.h>

函數(shù)原型int res=unlink (const char *path);

命令 ln 不能用來生成目錄的鏈接。

實現(xiàn) 頭文件#include <unistd.h>

函數(shù)原型 int res=link (const char *old,const char *new);

命令 mv 刪除原來的目錄,復(fù)制到新的里面

實現(xiàn) 頭文件#include <unistd.h>

函數(shù)原型int res=rename (const char* from,const char *to);

原理:復(fù)制鏈接到新的名字/位置再刪除原來的鏈接

if(link("x","z")!=-1)

unlink("x");

命令 cd 對進程有影響,對目錄本身沒有影響

實現(xiàn) 頭文件 #include <unistd.h>

函數(shù)原型 int res=chdir (const char *path);

14. pwd 命令的實現(xiàn)

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <dirent.h>

#include <string.h>

#include <unistd.h>

ino_t get_inode(char *);//get the inode number

void printpathto(ino_t);

void inum_to_name(ino_t,char *,int);//get the node name by its inode number

int main()

{

printpathto(get_inode("."));

putchar('\n');

return 0;

}

void printpathto(ino_t this_inode)

{

ino_t my_inode;

char its_name[BUFSIZ];

if(get_inode("..")!=this_inode)

{

chdir(".."); //up one dir

inum_to_name(this_inode,its_name,BUFSIZ); //get its name

my_inode=get_inode(".");

printpathto(my_inode); //itorater

printf("/%s",its_name);

}

}

void inum_to_name(ino_t inode_to_find,char *namebuf,int buflen)

{

DIR *dir_ptr; //the directory

struct dirent *direntp; //each entry

dir_ptr=opendir(".");

if(dir_ptr==NULL)

{

perror(".");

return;

}

while((direntp=readdir(dir_ptr))!=NULL)

{

if(direntp->d_ino==inode_to_find)

{

strncpy(namebuf,direntp->d_name,buflen);

namebuf[buflen-1]='\0';

closedir(dir_ptr);

return;

}

}

fprintf(stderr,"error looking for inum %d\n",(int)inode_to_find);

return;

}

ino_t get_inode(char *fname)

{

struct stat info;

if(stat(fname,&info)==-1)

{

fprintf(stderr,"Can not stat");

perror(fname);

return 1;

}

return info.st_ino;

}

運行結(jié)果:

caoli@caoli-laptop:~/workspace/test$ ./pwd1

/home/caoli/workspace/test

caoli@caoli-laptop:~/workspace/test$

    無相關(guān)信息