Main Page   Compound List   File List   Compound Members   File Members  

FileSystem Class Reference

#include <filesys.h>

List of all members.

Public Methods

 FileSystem (bool format)
bool Create (char *name, int initialSize)
OpenFileOpen (char *name)
bool Remove (char *name)
void List ()
void Print ()

Private Attributes

OpenFilefreeMapFile
OpenFiledirectoryFile


Constructor & Destructor Documentation

FileSystem::FileSystem bool    format
 

Definition at line 80 of file filesys.cc.

References FileHeader::Allocate, ASSERT, DEBUG, DebugIsEnabled, directoryFile, DirectoryFileSize, DirectorySector, freeMapFile, FreeMapFileSize, FreeMapSector, BitMap::Mark, NumDirEntries, NumSectors, Directory::Print, BitMap::Print, Directory::WriteBack, BitMap::WriteBack, and FileHeader::WriteBack.

00081 { 
00082     DEBUG('f', "Initializing the file system.\n");
00083     if (format) {
00084         BitMap *freeMap = new BitMap(NumSectors);
00085         Directory *directory = new Directory(NumDirEntries);
00086         FileHeader *mapHdr = new FileHeader;
00087         FileHeader *dirHdr = new FileHeader;
00088 
00089         DEBUG('f', "Formatting the file system.\n");
00090 
00091     // First, allocate space for FileHeaders for the directory and bitmap
00092     // (make sure no one else grabs these!)
00093         freeMap->Mark(FreeMapSector);       
00094         freeMap->Mark(DirectorySector);
00095 
00096     // Second, allocate space for the data blocks containing the contents
00097     // of the directory and bitmap files.  There better be enough space!
00098 
00099         ASSERT(mapHdr->Allocate(freeMap, FreeMapFileSize));
00100         ASSERT(dirHdr->Allocate(freeMap, DirectoryFileSize));
00101 
00102     // Flush the bitmap and directory FileHeaders back to disk
00103     // We need to do this before we can "Open" the file, since open
00104     // reads the file header off of disk (and currently the disk has garbage
00105     // on it!).
00106 
00107         DEBUG('f', "Writing headers back to disk.\n");
00108         mapHdr->WriteBack(FreeMapSector);    
00109         dirHdr->WriteBack(DirectorySector);
00110 
00111     // OK to open the bitmap and directory files now
00112     // The file system operations assume these two files are left open
00113     // while Nachos is running.
00114 
00115         freeMapFile = new OpenFile(FreeMapSector);
00116         directoryFile = new OpenFile(DirectorySector);
00117      
00118     // Once we have the files "open", we can write the initial version
00119     // of each file back to disk.  The directory at this point is completely
00120     // empty; but the bitmap has been changed to reflect the fact that
00121     // sectors on the disk have been allocated for the file headers and
00122     // to hold the file data for the directory and bitmap.
00123 
00124         DEBUG('f', "Writing bitmap and directory back to disk.\n");
00125         freeMap->WriteBack(freeMapFile);         // flush changes to disk
00126         directory->WriteBack(directoryFile);
00127 
00128         if (DebugIsEnabled('f')) {
00129             freeMap->Print();
00130             directory->Print();
00131 
00132         delete freeMap; 
00133         delete directory; 
00134         delete mapHdr; 
00135         delete dirHdr;
00136         }
00137     } else {
00138     // if we are not formatting the disk, just open the files representing
00139     // the bitmap and directory; these are left open while Nachos is running
00140         freeMapFile = new OpenFile(FreeMapSector);
00141         directoryFile = new OpenFile(DirectorySector);
00142     }
00143 }


Member Function Documentation

bool FileSystem::Create char *    name,
int    initialSize
 

Definition at line 175 of file filesys.cc.

References Directory::Add, FileHeader::Allocate, DEBUG, directoryFile, FALSE, BitMap::FetchFrom, Directory::FetchFrom, BitMap::Find, Directory::Find, freeMapFile, NumDirEntries, NumSectors, TRUE, BitMap::WriteBack, Directory::WriteBack, and FileHeader::WriteBack.

00176 {
00177     Directory *directory;
00178     BitMap *freeMap;
00179     FileHeader *hdr;
00180     int sector;
00181     bool success;
00182 
00183     DEBUG('f', "Creating file %s, size %d\n", name, initialSize);
00184 
00185     directory = new Directory(NumDirEntries);
00186     directory->FetchFrom(directoryFile);
00187 
00188     if (directory->Find(name) != -1)
00189       success = FALSE;                  // file is already in directory
00190     else {      
00191         freeMap = new BitMap(NumSectors);
00192         freeMap->FetchFrom(freeMapFile);
00193         sector = freeMap->Find();       // find a sector to hold the file header
00194         if (sector == -1)               
00195             success = FALSE;            // no free block for file header 
00196         else if (!directory->Add(name, sector))
00197             success = FALSE;    // no space in directory
00198         else {
00199             hdr = new FileHeader;
00200             if (!hdr->Allocate(freeMap, initialSize))
00201                 success = FALSE;        // no space on disk for data
00202             else {      
00203                 success = TRUE;
00204                 // everthing worked, flush all changes back to disk
00205                 hdr->WriteBack(sector);                 
00206                 directory->WriteBack(directoryFile);
00207                 freeMap->WriteBack(freeMapFile);
00208             }
00209             delete hdr;
00210         }
00211         delete freeMap;
00212     }
00213     delete directory;
00214     return success;
00215 }

void FileSystem::List  
 

Definition at line 296 of file filesys.cc.

References directoryFile, Directory::FetchFrom, Directory::List, and NumDirEntries.

00297 {
00298     Directory *directory = new Directory(NumDirEntries);
00299 
00300     directory->FetchFrom(directoryFile);
00301     directory->List();
00302     delete directory;
00303 }

OpenFile * FileSystem::Open char *    name
 

Definition at line 228 of file filesys.cc.

References DEBUG, directoryFile, Directory::FetchFrom, Directory::Find, NULL, and NumDirEntries.

00229 { 
00230     Directory *directory = new Directory(NumDirEntries);
00231     OpenFile *openFile = NULL;
00232     int sector;
00233 
00234     DEBUG('f', "Opening file %s\n", name);
00235     directory->FetchFrom(directoryFile);
00236     sector = directory->Find(name); 
00237     if (sector >= 0)            
00238         openFile = new OpenFile(sector);        // name was found in directory 
00239     delete directory;
00240     return openFile;                            // return NULL if not found
00241 }

void FileSystem::Print  
 

Definition at line 316 of file filesys.cc.

References directoryFile, DirectorySector, Directory::FetchFrom, BitMap::FetchFrom, FileHeader::FetchFrom, freeMapFile, FreeMapSector, NumDirEntries, NumSectors, Directory::Print, BitMap::Print, and FileHeader::Print.

00317 {
00318     FileHeader *bitHdr = new FileHeader;
00319     FileHeader *dirHdr = new FileHeader;
00320     BitMap *freeMap = new BitMap(NumSectors);
00321     Directory *directory = new Directory(NumDirEntries);
00322 
00323     printf("Bit map file header:\n");
00324     bitHdr->FetchFrom(FreeMapSector);
00325     bitHdr->Print();
00326 
00327     printf("Directory file header:\n");
00328     dirHdr->FetchFrom(DirectorySector);
00329     dirHdr->Print();
00330 
00331     freeMap->FetchFrom(freeMapFile);
00332     freeMap->Print();
00333 
00334     directory->FetchFrom(directoryFile);
00335     directory->Print();
00336 
00337     delete bitHdr;
00338     delete dirHdr;
00339     delete freeMap;
00340     delete directory;
00341 } 

bool FileSystem::Remove char *    name
 

Definition at line 258 of file filesys.cc.

References BitMap::Clear, FileHeader::Deallocate, directoryFile, FALSE, BitMap::FetchFrom, FileHeader::FetchFrom, Directory::FetchFrom, Directory::Find, freeMapFile, NumDirEntries, NumSectors, Directory::Remove, TRUE, Directory::WriteBack, and BitMap::WriteBack.

00259 { 
00260     Directory *directory;
00261     BitMap *freeMap;
00262     FileHeader *fileHdr;
00263     int sector;
00264     
00265     directory = new Directory(NumDirEntries);
00266     directory->FetchFrom(directoryFile);
00267     sector = directory->Find(name);
00268     if (sector == -1) {
00269        delete directory;
00270        return FALSE;                     // file not found 
00271     }
00272     fileHdr = new FileHeader;
00273     fileHdr->FetchFrom(sector);
00274 
00275     freeMap = new BitMap(NumSectors);
00276     freeMap->FetchFrom(freeMapFile);
00277 
00278     fileHdr->Deallocate(freeMap);               // remove data blocks
00279     freeMap->Clear(sector);                     // remove header block
00280     directory->Remove(name);
00281 
00282     freeMap->WriteBack(freeMapFile);            // flush to disk
00283     directory->WriteBack(directoryFile);        // flush to disk
00284     delete fileHdr;
00285     delete directory;
00286     delete freeMap;
00287     return TRUE;
00288 } 


Member Data Documentation

OpenFile* FileSystem::directoryFile [private]
 

Definition at line 91 of file filesys.h.

Referenced by Create, FileSystem, List, Open, Print, and Remove.

OpenFile* FileSystem::freeMapFile [private]
 

Definition at line 89 of file filesys.h.

Referenced by Create, FileSystem, Print, and Remove.


The documentation for this class was generated from the following files:
Generated on Mon Feb 10 09:54:56 2003 for nachos by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees