三个区域
- 超级快:文件系统本身的结构信息(每个区域大小,空白磁盘块)
- i-节点表:i-节点(节点文件属性),以i-节点组成的列表(数组)
- 数据区:存储数据
目录文件
目录也是一种文件,保存了(i-节点号,文件名)的特殊文件。
当前目录“.”对应的i-节点号是2342,意味着当前目录文件的属性在2342号i-节点。
i-节点与大文件(多级)
关键系统调用
// 改变进程的当前目录,进到父目录
chdir("..");
ino_t get_inode(char *fname)
{
//文件名得到对应的i_node
struct stat info;
if(stat(fname,&info)==-1)
{
printf("Cannot stat");
perror(fname);
exit(1);
}
return info.st_ino;
}
以pwd命令为例
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
ino_t get_inode(char*);
void printpathto(ino_t);
void inum_to_name(ino_t,char*,int);
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("..");
//在父目录里得到当前目录名字
inum_to_name(this_inode,its_name,BUFSIZ);
//得到父目录i_node_num
my_inode=get_inode(".");
//递归
printpathto(my_inode);
printf("/%s",its_name);
}
}
void inum_to_name(ino_t inode_to_find,char * namebuf,int buflen)
{
//当前“.”目录的i_num如果等于inode_to_find,得到文件名
DIR *dir_ptr;
struct dirent* direntp;
dir_ptr=opendir(".");
if(dir_ptr==NULL)
{
perror(".");
exit(0);
}
while ((direntp=readdir(dir_ptr))!=NULL)
{ //readdir next
//如果此节点i_num==inode_to_find
if(direntp->d_ino==inode_to_find)
{
strncpy(namebuf,direntp->d_name,buflen);
namebuf[buflen-1]='\0';
closedir(dir_ptr);
return;
}
}
printf("error looking for inum %ld\n",inode_to_find);
exit(1);
}
ino_t get_inode(char *fname)
{
//文件名得到对应的i_node
struct stat info;
if(stat(fname,&info)==-1)
{
printf("Cannot stat");
perror(fname);
exit(1);
}
return info.st_ino;
}