【nachos】nachos学习笔记(四) 文件系统扩展

一、分析

   分析fstest.cc可知,在Append函数中通过调用openfile的Write方法对文件进行拓展,而Write是调用WriteAt方法,当该方法发现要写入的数据大于文件长度时会对多出的部分进行舍去,因此,需要修改WriteAt方法,使其检测到写入数据溢出时,可以增加文件的大小。但是文件的大小在创建时已经固定了,因此需要给文件头类filehdr添加一个方法用来扩展文件大小。当文件修改完成后,Append调用openfile的WriteBack方法将更改过的文件头写回虚拟磁盘。该方法可以通过调用filehdr的WriteBack方法实现,但是openfile类并未记录自己所打开的文件头的磁道信息,因此需要给该类添加一个成员sector来记录所打开的文件头的磁道号。

  综上分析,需要改动的文件fileopen.cc fileopen.h 和 filehdr.cc filehdr.h四个文件。同时对fstest中部分注释的代码取消注释。fileopen类需要添加一个sector成员,并在其构造函数中初始化,还要添加一个WriteBack()方法。Filehdr类需要添加一个ExtendFileSize方法来更改文件大小。

二、编码和实现

Filehdr类中的ExtenFileSize方法:

bool

FileHeader::ExtendFileSize(int filesize)

{

    if(filesize < numBytes)return false;

    if(filesize == numBytes)return true;

   

   

    int newnumSectors  = divRoundUp(filesize, SectorSize);

    if(newnumSectors == numSectors)

    {

        numBytes = filesize;

        return true;

    }

    int ExtendSectors = newnumSectors - numSectors;

 

    OpenFile *bitMapHeader = new OpenFile(0);

    BitMap *bitMap = new BitMap(NumSectors);

    bitMap->FetchFrom(bitMapHeader);

    if (bitMap->NumClear() < ExtendSectors || newnumSectors > NumDirect)

       return FALSE;            // not enough space

 

    for (int i = 0; i < ExtendSectors; i++)

       dataSectors[i+numSectors] = bitMap->Find();

    bitmap->WriteBack(bitMapHeader);

 

 

 

numBytes = filesize;

numSectors = newnumSectors;

return true;

}

openfile中的WriteBack方法

void

OpenFile::WriteBack()

{

    hdr->WriteBack(sector);

}

Openfile新增成员sector和初始化:

Openfile中WriteAt方法的部分修改:

3、编译和测试

a.先使用-f命令创建DiSK,然后用-cp test/small small 将small复制进DISK中,再使用-ap test/small small 将small中的内容添加到small末尾,结果如下:

b. nachos -cp test/empty empty

nachos -ap test/medium empty

 

猜你喜欢

转载自blog.csdn.net/darord/article/details/84324879
今日推荐