Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

files not working on extension

  • Loading branch information...
commit bd516aeac3b5090a6536e88ceab17cc5b8112b09 1 parent 0d48c76
@mettamara mettamara authored
Showing with 405 additions and 1 deletion.
  1. +54 −0 filesys/directory.cc
  2. +351 −1 filesys/openfile.cc
View
54 filesys/directory.cc
@@ -63,7 +63,21 @@ Directory::~Directory()
void
Directory::FetchFrom(OpenFile *file)
{
+#ifdef FILESYS
+ //Get tableSize
+ (void) file->ReadAt((char *)&tableSize, sizeof(int), 0);
+ DEBUG('f', "Tablesize :%d \n", tableSize);
+ DirectoryEntry* newTable = new DirectoryEntry[tableSize];
+
+ for (int i = 0; i < tableSize; i++)
+ newTable[i].inUse = FALSE;
+
+ delete table;
+ table = newTable;
+ (void) file->ReadAt((char *)table, tableSize * sizeof(DirectoryEntry), sizeof(int));
+#else
(void) file->ReadAt((char *)table, tableSize * sizeof(DirectoryEntry), 0);
+#endif
}
//----------------------------------------------------------------------
@@ -76,7 +90,15 @@ Directory::FetchFrom(OpenFile *file)
void
Directory::WriteBack(OpenFile *file)
{
+#ifdef FILESYS
+ DEBUG('f',"Writing a table of size: %d\n",tableSize);
+ (void) file->WriteAt((char *) &tableSize, sizeof(int), 0);
+ (void) file->WriteAt((char *)table, tableSize * sizeof(DirectoryEntry), sizeof(int));
+#else
(void) file->WriteAt((char *)table, tableSize * sizeof(DirectoryEntry), 0);
+
+
+#endif
}
//----------------------------------------------------------------------
@@ -139,6 +161,38 @@ Directory::Add(char *name, int newSector)
table[i].sector = newSector;
return TRUE;
}
+
+ //we had no space lets allocate more
+ int oldTableSize = tableSize;
+ tableSize = (int)(oldTableSize * 1.5);
+ DEBUG('f',"** Extending table size from %d to %d \n", oldTableSize,tableSize);
+ DirectoryEntry *newTable = new DirectoryEntry[tableSize];
+
+ for (int i = 0; i < tableSize; i++){
+ if (i < oldTableSize){
+ //copy the old entries
+ newTable[i].inUse = table[i].inUse;
+ newTable[i].sector = table[i].sector;
+ strncpy(newTable[i].name, table[i].name, FileNameMaxLen+1);
+ }
+ else{
+ //mark the new entries as not in use
+ newTable[i].inUse = FALSE;
+ }
+ }
+ //remove old and swap
+ delete [] table;
+ table = newTable;
+ //now add
+ if (!table[oldTableSize].inUse){
+ table[oldTableSize].inUse = TRUE;
+ strncpy(table[oldTableSize].name, name, FileNameMaxLen);
+ table[oldTableSize].sector = newSector;
+ return true;
+ }
+ //return this->Add(name,newSector);
+
+ //should never get here
return FALSE; // no space. Fix when we have extensible files.
}
View
352 filesys/openfile.cc
@@ -18,7 +18,7 @@
#ifdef HOST_SPARC
#include <strings.h>
#endif
-
+#define FreeMapSector 0
//----------------------------------------------------------------------
// OpenFile::OpenFile
// Open a Nachos file for reading and writing. Bring the file header
@@ -143,9 +143,358 @@ OpenFile::ReadAt(char *into, int numBytes, int position)
return numBytes;
}
+
+BitMap* getFreeMap(){
+ DEBUG('f', "opening free \n");
+ OpenFile* freeMapFile = new OpenFile(FreeMapSector);
+ BitMap* freeMap = new BitMap(NumSectors);
+ DEBUG('f', "Fetching free Map\n");
+ freeMap->FetchFrom(freeMapFile);
+ return freeMap;
+}
+
int
OpenFile::WriteAt(char *from, int numBytes, int position)
{
+
+#ifdef FILESYS
+ int fileLength = hdr->FileLength();
+ int i, firstSector, lastSector, numSectors;
+ bool firstAligned, lastAligned;
+ char *buf;
+ numSectors = fileLength/SectorSize;
+
+ int total_length = fileLength - position + numBytes;
+ 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('1', "Entering writeAt func...\n");
+
+ // CASE 1
+ if ((position + numBytes) <= fileLength) {
+ //DEBUG('1', "Case 1!-------------------------\n");
+ if ((position + numBytes) > fileLength)
+ numBytes = fileLength - position;
+
+ DEBUG('f', "Writing %d bytes at %d, from file of length %d.\n",
+ numBytes, position, fileLength);
+
+ firstSector = divRoundDown(position, SectorSize);
+ lastSector = divRoundDown(position + numBytes - 1, SectorSize);
+ numSectors = 1 + lastSector - firstSector;
+
+ buf = new char[numSectors * SectorSize];
+
+ 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))
+ ReadAt(&buf[(lastSector - firstSector) * SectorSize],
+ SectorSize, lastSector * SectorSize);
+
+ //DEBUG('1', "Midway through....\n");
+
+ // copy in the bytes we want to change
+ bcopy(from, &buf[position - (firstSector * SectorSize)], numBytes);
+
+ // write modified sectors back
+ for (i = firstSector; i <= lastSector; i++)
+ synchDisk->WriteSector(hdr->ByteToSector(i * SectorSize),
+ &buf[(i - firstSector) * SectorSize]);
+ delete [] buf;
+ //DEBUG('1', "Exiting case 1....\n");
+
+ //return numBytes;
+ }
+
+ // CASE 2
+ else if (position < fileLength) {
+ //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);
+
+ OpenFile* freeMapFile = new OpenFile(FreeMapSector);
+ BitMap* freeMap = new BitMap(NumSectors);
+
+ DEBUG('1', "Fetching free Map 88888888888888888888888888\n");
+ freeMap->FetchFrom(freeMapFile);
+ 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");
+ return 0;
+ }
+
+ freeMap->WriteBack(freeMapFile);
+
+ delete freeMapFile;
+ delete freeMap;
+
+ //WriteAt(from, numBytes, position);
+
+if ((position + numBytes) > fileLength) {
+ numBytes = fileLength - position;
+ }
+
+ DEBUG('f', "Writing %d bytes at %d, from file of length %d.\n",
+ numBytes, position, fileLength);
+
+ firstSector = divRoundDown(position, SectorSize);
+ lastSector = divRoundDown(position + numBytes - 1, SectorSize);
+ numSectors = 1 + lastSector - firstSector;
+
+ buf = new char[numSectors * SectorSize];
+
+ 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))
+ ReadAt(&buf[(lastSector - firstSector) * SectorSize],
+ SectorSize, lastSector * SectorSize);
+
+ // copy in the bytes we want to change
+ bcopy(from, &buf[position - (firstSector * SectorSize)], numBytes);
+
+
+ // write modified sectors back
+ for (i = firstSector; i <= lastSector; i++) {
+ DEBUG('1', "Writing modified sector %d at location given %d\n", i, hdr->ByteToSector(i * SectorSize));
+ synchDisk->WriteSector(hdr->ByteToSector(i * SectorSize),
+ &buf[(i - firstSector) * SectorSize]);
+ }
+ delete [] buf;
+ }
+
+ // CASE 3
+ else if (position >= fileLength) {
+ position = fileLength;
+
+ //DEBUG('1', "Case 3!-------------------------\n");
+
+ //int last_sector = hdr->ByteToSector(fileLength - 1);
+ int last_byte;
+
+ if (fileLength == 0) {
+ last_byte = 0;
+ }
+ else {
+ last_byte = fileLength - 1;
+ }
+
+ int offset = last_byte - divRoundDown(last_byte, SectorSize)*SectorSize;
+
+ int newSectors = divRoundUp(numBytes - (SectorSize - offset), SectorSize);
+
+ DEBUG('1', "Currently have %d sectors and want %d new sectors\n", numSectors, newSectors);
+
+ if ((numSectors == 0)&&(newSectors == 0)) {
+ newSectors++;
+ }
+
+
+ DEBUG('1', "OPENING free Map 88888888888888888888888888\n");
+ OpenFile* freeMapFile = new OpenFile(FreeMapSector);
+ DEBUG('1', "DONE OPENING free Map 88888888888888888888888888\n");
+ BitMap* freeMap = new BitMap(NumSectors);
+
+ DEBUG('1', "Fetching free Map 88888888888888888888888888\n");
+ freeMap->FetchFrom(freeMapFile);
+ 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");
+ return 0;
+ }
+
+ DEBUG('1', "STARTING FREE MAP WRITE BACK 9999999999999999999999999999999999999\n");;
+ freeMap->WriteBack(freeMapFile);
+ DEBUG('1', "DONE Writing Back free Map 9999999999999999999999999999999999999\n");
+
+ delete freeMapFile;
+ delete freeMap;
+
+ fileLength = hdr->FileLength();
+
+ //WriteAt(from, numBytes, position);
+
+ if ((position + numBytes) > fileLength) {
+ numBytes = fileLength - position;
+ }
+
+ DEBUG('f', "Writing %d bytes at %d, from file of length %d.\n",
+ numBytes, position, fileLength);
+
+ firstSector = divRoundDown(position, SectorSize);
+ lastSector = divRoundDown(position + numBytes - 1, SectorSize);
+ numSectors = 1 + lastSector - firstSector;
+
+ buf = new char[numSectors * SectorSize];
+
+ 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))
+ ReadAt(&buf[(lastSector - firstSector) * SectorSize],
+ SectorSize, lastSector * SectorSize);
+
+ // copy in the bytes we want to change
+ bcopy(from, &buf[position - (firstSector * SectorSize)], numBytes);
+
+ // write modified sectors back
+ for (i = firstSector; i <= lastSector; i++) {
+ DEBUG('1', "Writing modified sector %d at location given %d\n", i, hdr->ByteToSector(i * SectorSize));
+ synchDisk->WriteSector(hdr->ByteToSector(i * SectorSize),
+ &buf[(i - firstSector) * SectorSize]);
+ }
+ delete [] buf;
+ }
+ //DEBUG('1', "Exiting writeAt func...\n");
+
+ return numBytes;
+
+#elif defined FIXLESYS
+ int fileLength = hdr->FileLength();
+ int i, firstSector, lastSector, numSectors;
+ bool firstAligned, lastAligned;
+ char *buf;
+ if (numBytes <=0)
+ return 0;
+
+ if (position+numBytes <= fileLength){ //if in bounds
+
+ DEBUG('f', "Writing %d bytes at %d, from file of length %d.\n",
+ numBytes, position, fileLength);
+
+
+ //LEFTOFF finding where we can write in the sector block
+ //if not at start or crosses multiple sectors
+ firstSector = divRoundDown(position, SectorSize);
+ lastSector = divRoundDown(position + numBytes - 1, SectorSize);
+ numSectors = 1 + lastSector - firstSector;
+
+ buf = new char[numSectors * SectorSize];
+
+ 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))
+ ReadAt(&buf[(lastSector - firstSector) * SectorSize],
+ SectorSize, lastSector * SectorSize);
+
+ // copy in the bytes we want to change
+ bcopy(from, &buf[position - (firstSector * SectorSize)], numBytes);
+
+ // write modified sectors back
+ for (i = firstSector; i <= lastSector; i++)
+ synchDisk->WriteSector(hdr->ByteToSector(i * SectorSize),
+ &buf[(i - firstSector) * SectorSize]);
+ delete [] buf;
+ return numBytes;
+
+ }//if in bounds
+ else if( (position >= fileLength) ) { //Append end or past
+ //LEFTOFF
+ int start = fileLength; // we need to start adding sectors from end of the file
+ firstSector = divRoundDown(start, SectorSize);
+ lastSector = divRoundUp(position + numBytes - 1, SectorSize);
+ numSectors = 1 + lastSector - firstSector;
+ BitMap* freeMap = getFreeMap();
+ if (hdr->ExtendFile(freeMap, numSectors) == -1){
+ bool noSpace = false;
+ ASSERT(noSpace);
+ }
+
+ buf = new char[numSectors * SectorSize];
+
+ firstAligned = (position == (firstSector * SectorSize));
+ lastAligned = ((position + numBytes) == ((lastSector + 1) * SectorSize));
+
+ //TODO LEFTOF TEST
+
+ // 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))
+ ReadAt(&buf[(lastSector - firstSector) * SectorSize],
+ SectorSize, lastSector * SectorSize);
+
+ // copy in the bytes we want to change
+ bcopy(from, &buf[position - (firstSector * SectorSize)], numBytes);
+
+ // write modified sectors back
+ for (i = firstSector; i <= lastSector; i++)
+ synchDisk->WriteSector(hdr->ByteToSector(i * SectorSize),
+ &buf[(i - firstSector) * SectorSize]);
+ delete [] buf;
+ return numBytes;
+
+ }
+ else if(position+numBytes > fileLength){ //over write and extend
+ 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);
+ }
+
+ buf = new char[numSectors * SectorSize];
+
+ firstAligned = (position == (firstSector * SectorSize));
+ lastAligned = ((position + numBytes) == ((lastSector + 1) * SectorSize));
+
+ //TODO LEFTOF DO WE ZERO OUT FOR GAPS?
+
+ // 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))
+ ReadAt(&buf[(lastSector - firstSector) * SectorSize],
+ SectorSize, lastSector * SectorSize);
+
+ // copy in the bytes we want to change
+ bcopy(from, &buf[position - (firstSector * SectorSize)], numBytes);
+
+ // write modified sectors back
+ for (i = firstSector; i <= lastSector; i++)
+ synchDisk->WriteSector(hdr->ByteToSector(i * SectorSize),
+ &buf[(i - firstSector) * SectorSize]);
+ delete [] buf;
+ return numBytes;
+
+ }
+ else{
+ DEBUG('f', "UNKNOWN position condition reached\n");
+ bool howDidTheFileWriteGetHere= false;
+ ASSERT(howDidTheFileWriteGetHere);
+
+ }
+
+
+
+
+#else
+
int fileLength = hdr->FileLength();
int i, firstSector, lastSector, numSectors;
bool firstAligned, lastAligned;
@@ -186,6 +535,7 @@ OpenFile::WriteAt(char *from, int numBytes, int position)
&buf[(i - firstSector) * SectorSize]);
delete [] buf;
return numBytes;
+#endif
}
//----------------------------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.