Permalink
Browse files

extending working

  • Loading branch information...
1 parent f5123ec commit 29e146c96fea23ecd6191e0c33e91c622f57ece1 @mettamara mettamara committed May 30, 2011
Showing with 82 additions and 16 deletions.
  1. +12 −1 filesys/filehdr.cc
  2. +1 −0 filesys/filehdr.h
  3. +8 −3 filesys/filesys.cc
  4. +61 −12 filesys/openfile.cc
View
@@ -57,11 +57,13 @@ FileHeader::Allocate(BitMap *freeMap, int fileSize)
if (i < NumDirect){
dataSectors[i] = freeMap->Find();
sectorsToAllocate--;
+ DEBUG('f', "\nAllocating new direct dataSector[%d]:%d\n" , (i),dataSectors[i]);
+
}
else{
indirectPointers[i-NumDirect] = new IndirectPointerBlock();
indirectSector[i-NumDirect] = freeMap->Find();
- DEBUG('f', "\nAllocating new indirectPointerBlock[%d]:" , (i-NumDirect));
+ DEBUG('f', "\nAllocating new indirectPointerBlock[%d]:%d\n" , (i-NumDirect),indirectSector[i-NumDirect]);
int sectorsToAddToThisPage = sectorsToAllocate;
if (sectorsToAddToThisPage > MaxIndirectPointers)
sectorsToAddToThisPage = MaxIndirectPointers;
@@ -250,8 +252,13 @@ FileHeader::Print()
delete [] data;
}
+void FileHeader::setNumBytes(int newBytes){
+ numBytes= newBytes;
+}
+
bool FileHeader::ExtendFile(BitMap *freeMap, int sectorsToAllocate){
DEBUG('f',"Extending file by %d sectors\n",sectorsToAllocate);
+ sectorsToAllocate -= numSectors;
if (sectorsToAllocate <= 0)
return false;
if (freeMap->NumClear() < sectorsToAllocate)
@@ -261,7 +268,10 @@ bool FileHeader::ExtendFile(BitMap *freeMap, int sectorsToAllocate){
while(sectorsToAllocate > 0){
if (i < NumDirect){
dataSectors[i] = freeMap->Find();
+ DEBUG('f', "\nAllocating new direct DataSector[%d]:%d\n" , (i),dataSectors[i]);
+
sectorsToAllocate--;
+ numSectors++;
}
else{
indirectPointers[i-NumDirect] = new IndirectPointerBlock();
@@ -276,6 +286,7 @@ bool FileHeader::ExtendFile(BitMap *freeMap, int sectorsToAllocate){
int newSec = freeMap->Find();
indirectPointers[i-NumDirect]->PutSector(newSec);
sectorsToAllocate--;
+ numSectors++;
}
}
View
@@ -60,6 +60,7 @@ class FileHeader {
void Print(); // Print the contents of the file.
bool ExtendFile(BitMap *freeMap, int sectors); //extend the file by N sectors
+ void setNumBytes(int newBytes);
private:
int numBytes; // Number of bytes in the file
View
@@ -61,7 +61,7 @@
// supports extensible files, the directory size sets the maximum number
// of files that can be loaded onto the disk.
#define FreeMapFileSize (NumSectors / BitsInByte)
-#define NumDirEntries 10
+#define NumDirEntries 4
#define DirectoryFileSize (sizeof(DirectoryEntry) * NumDirEntries)
//----------------------------------------------------------------------
@@ -177,14 +177,14 @@ FileSystem::Create(char *name, int initialSize)
Directory *directory;
BitMap *freeMap;
FileHeader *hdr;
+ FileHeader *dirHdr = new FileHeader;
int sector;
bool success;
DEBUG('f', "Creating file %s, size %d\n", name, initialSize);
directory = new Directory(NumDirEntries);
directory->FetchFrom(directoryFile);
-
if (directory->Find(name) != -1)
success = FALSE; // file is already in directory
else {
@@ -197,20 +197,25 @@ FileSystem::Create(char *name, int initialSize)
success = FALSE; // no space in directory
else {
hdr = new FileHeader;
+ bzero(hdr,sizeof(FileHeader));
if (!hdr->Allocate(freeMap, initialSize))
success = FALSE; // no space on disk for data
else {
success = TRUE;
// everthing worked, flush all changes back to disk
+
+ freeMap->WriteBack(freeMapFile);
hdr->WriteBack(sector);
directory->WriteBack(directoryFile);
- freeMap->WriteBack(freeMapFile);
+
+ dirHdr->FetchFrom(DirectorySector);
}
delete hdr;
}
delete freeMap;
}
delete directory;
+ delete dirHdr;
return success;
}
View
@@ -32,6 +32,7 @@ OpenFile::OpenFile(int sector)
hdr = new FileHeader;
hdr->FetchFrom(sector);
seekPosition = 0;
+ hdrSector = sector;
}
//----------------------------------------------------------------------
@@ -122,8 +123,7 @@ OpenFile::ReadAt(char *into, int numBytes, int position)
if ((numBytes <= 0) || (position >= fileLength))
return 0; // check request
- if ((position + numBytes) > fileLength)
- numBytes = fileLength - position;
+// if ((position + numBytes) > fileLength) numBytes = fileLength - position;
DEBUG('x', "OpenFile::ReadAt Reading %d bytes at %d, from file of length %d.\n",
numBytes, position, fileLength);
@@ -159,25 +159,29 @@ BitMap* getFreeMap(){
int OpenFile::WriteAt(char *from, int numBytes, int position){
#ifdef FILESYS
int fileLength = hdr->FileLength();
+ if(numBytes+position > hdr->FileLength()){
+ //TODO?
+ //hdr->numBytes= numBytes+position;//optimistic
+ }
+ if (fileLength % SectorSize != 0){
+ int paddingInFile = SectorSize - (fileLength % SectorSize);
+ fileLength+=paddingInFile;
+ }
int i, firstSector, lastSector, numSectors;
bool firstAligned, lastAligned;
char *buf;
numSectors = fileLength/SectorSize;
- int total_length = fileLength - position + numBytes;
+ int total_length = position + numBytes;
+ if (total_length < fileLength) total_length =fileLength;
int num_sectors = divRoundUp(total_length, SectorSize);
int first_file_sector = hdr->ByteToSector(0); // Assume files start at beginning of sector
- if (first_file_sector + num_sectors > SectorSize*NumSectors) {
- return 0;
- }
+
DEBUG('f', "Entering writeAt func...\n");
// CASE 1
if ((position + numBytes) <= fileLength) {
- //DEBUG('1', "Case 1!-------------------------\n");
- if ((position + numBytes) > fileLength)
- numBytes = fileLength - position;
DEBUG('1', "Writing %d bytes at %d, from file of length %d.\n",
numBytes, position, fileLength);
@@ -215,8 +219,10 @@ int OpenFile::WriteAt(char *from, int numBytes, int position){
// CASE 2
else if (position < fileLength) {
+ printf("LEFTOFF do we need to wipe clean new sectors from extend *******************************\n");
//DEBUG('1', "Case 2!-------------------------\n");
//int last_sector = hdr->ByteToSector(fileLength - 1);
+ /*
int last_byte = position;
int offset = last_byte - divRoundDown(last_byte, SectorSize)*SectorSize;
int newSectors = divRoundUp(numBytes - (SectorSize - offset), SectorSize);
@@ -229,7 +235,7 @@ int OpenFile::WriteAt(char *from, int numBytes, int position){
DEBUG('1', "DONE Fetching free Map 88888888888888888888888888\n");
if (!hdr->ExtendFile(freeMap, newSectors)) {
- printf("Not enough free disk space to write to file. Ignoring write.\n");
+ printf("Unable to extend file by %d sectors. Ignoring write.\n",newSectors);
return 0;
}
@@ -274,6 +280,49 @@ int OpenFile::WriteAt(char *from, int numBytes, int position){
&buf[(i - firstSector) * SectorSize]);
}
delete [] buf;
+ */
+ int start = position; // we need to start adding sectors from end of the file
+ firstSector = divRoundDown(start, SectorSize);
+ lastSector = divRoundDown(position + numBytes - 1, SectorSize);
+ numSectors = 1 + lastSector - firstSector;
+ BitMap* freeMap = getFreeMap();
+ if (hdr->ExtendFile(freeMap, numSectors) == -1){
+ bool noSpace = false;
+ ASSERT(noSpace);
+ }
+
+ int bufSize = numSectors * SectorSize;
+ buf = new char[bufSize];
+ bzero(buf, bufSize);
+ firstAligned = (position == (firstSector * SectorSize));
+ lastAligned = ((position + numBytes) == ((lastSector + 1) * SectorSize));
+
+ // read in first and last sector, if they are to be partially modified
+ if (!firstAligned) {
+ ReadAt(buf, SectorSize, firstSector * SectorSize);
+ }
+ if (!lastAligned && ((firstSector != lastSector) || firstAligned)){
+ int bufStart = (lastSector - firstSector) * SectorSize;
+ int pos = lastSector * SectorSize;
+ ReadAt(&buf[bufStart], SectorSize, pos);
+ }
+
+
+ // copy in the bytes we want to change
+ int pos = position - (firstSector * SectorSize);
+
+ bcopy(from, &buf[pos], numBytes);
+
+ // write modified sectors back
+ for (i = firstSector; i <= lastSector; i++) {
+ int sectToWrite = hdr->ByteToSector(i * SectorSize);
+ int locInBuf = (i - firstSector) * SectorSize;
+ synchDisk->WriteSector(sectToWrite, &buf[locInBuf]);
+ }
+ delete [] buf;
+ hdr->setNumBytes(numBytes+position);
+ hdr->WriteBack(hdrSector);
+ return numBytes;
}
// CASE 3
@@ -367,7 +416,7 @@ int OpenFile::WriteAt(char *from, int numBytes, int position){
return numBytes;
-#elif defined FIXLESYS
+#elif defined FILESYSs
int fileLength = hdr->FileLength();
int i, firstSector, lastSector, numSectors;
bool firstAligned, lastAligned;
@@ -377,7 +426,7 @@ int OpenFile::WriteAt(char *from, int numBytes, int position){
if (position+numBytes <= fileLength){ //if in bounds
- DEBUG('f', "Writing %d bytes at %d, from file of length %d.\n",
+ DEBUG('f', "mWriting %d bytes at %d, from file of length %d.\n",
numBytes, position, fileLength);

0 comments on commit 29e146c

Please sign in to comment.