dataSectors[i]=freeMap->Find();
returntrue;
}
printf("Thefileheaderseefilesize%d\n",FileLength());
}
3,
在openfile.cc文件中,OpenFile类中加入方法,用于追加文件的AppFileSize(intnumByte)。
这个方法首先从文件系统中获取空闲的扇区的位试图文件,构造BitMap对象,
传给FileHeader类的对象(也就是这个OpenFile的文件头),
执行AppSectors(BitMap*freeMap,intfileSize)方法,扩大文件的长度。
bool
OpenFile:
:
AppFileSize(intnumBytes)
{
//fetchthebitmap
OpenFile*freeMapFile=newOpenFile(FreeMapSector);
BitMap*freeMap=newBitMap(NumSectors);
freeMap->FetchFrom(freeMapFile);
//askfornewspace
if(!
(hdr->AppSectors(freeMap,numBytes)))returnfalse;
printf("openFileseefilesize%d\n",Length());
//writebackthebitmap
freeMap->WriteBack(freeMapFile);
deletefreeMapFile;
deletefreeMap;
returntrue;
}
4,
在fstest.cc文件中的修改Append方法,也就是在Nachos运行的时候执行命令,
例如:
Nachos–xap../test/smallsmall
这个方法是Nachos系统中本来就提供好的一个方法,只需要在局部修改一下语句,
就可以正确的运行。
void
Append(char*from,char*to,inthalf)
{
/*fileLength是计算出来的Linux文件中的文件的大小
fileLengthBefore是指追加之前的Nachos系统的文件的大小
start是文件开始追加的位置
*/
FILE*fp;
OpenFile*openFile;
intamountRead,fileLength;
char*buffer;
//startpositionforappending
intstart;
//OpenUNIXfile
if((fp=fopen(from,"r"))==NULL){
printf("Copy:
couldn'topeninputfile%s\n",from);
return;
}
//FigureoutlengthofUNIXfile
fseek(fp,0,2);
fileLength=ftell(fp);
fseek(fp,0,0);
if(fileLength==0)
{
printf("Append:
nothingtoappendfromfile%s\n",from);
return;
}
if((openFile=fileSystem->Open(to))==NULL)
{
//file"to"doesnotexits,thencreateone
if(!
fileSystem->Create(to,0))
{
printf("Append:
couldn'tcreatethefile%stoappend\n",to);
fclose(fp);
return;
}
openFile=fileSystem->Open(to);
}
intfileLengthBefore=openFile->Length();
/*执行追加,并判断要追加文件的大小是否超过最大长度*/
if(!
(openFile->AppFileSize(fileLength)))
{
printf("Theappendingfileistoobig!
\nAppendingfilefailed\n");
//toolongfiletoappend
return;
}
ASSERT(openFile!
=NULL);
//appendfromposition"start"
start=openFile->Length()-fileLengthBefore;
printf("valueofstart%d\n",start);
if(half)start=start/2;//如果是从文件的中部追加
openFile->Seek(start);
//AppendthedatainTransferSizechunks
buffer=newchar[TransferSize];
while((amountRead=fread(buffer,sizeof(char),TransferSize,fp))>0)
{
intresult;
printf("startvalue:
%d,amountRead%d,",start,amountRead);
result=openFile->WriteAt(buffer,amountRead,start);
printf("resultofwrite:
%d\n",result);
start+=amountRead;
}
delete[]buffer;
//Writetheinodebacktothedisk,becausewehavechangedit
//需要把该文件的文件头写回磁盘
openFile->WriteBack(to);
printf("inodeshavebeenwrittenback\n");
//ClosetheUNIXandtheNachosfiles
deleteopenFile;
fclose(fp);
}
5,
运行结果:
首先执行./nachos–f,格式化Nachos磁盘
lu@ubuntu:
~/csc2404/nachos-3.4/code/appfilesys$./nachos-f
Nothreadsreadyorrunnable,andnopendinginterrupts.
Assumingtheprogramcompleted.
Machinehalting!
Ticks:
total82600,idle82350,system250,user0
DiskI/O:
reads3,writes5
ConsoleI/O:
reads0,writes0
Paging:
faults0
NetworkI/O:
packetsreceived0,sent0
Cleaningup...
然后执行追加文件的操作:
./nachos-aptest/smallsmall
lu@ubuntu:
~/csc2404/nachos-3.4/code/appfilesys$./nachos-aptest/smallsmall
openFileseefilesize38
valueofstart38
startvalue:
38,amountRead10,resultofwrite:
0
startvalue:
48,amountRead10,resultofwrite:
0
startvalue:
58,amountRead10,resultofwrite:
0
startvalue:
68,amountRead