Main Page   Compound List   File List   Compound Members   File Members  

openfile.h

Go to the documentation of this file.
00001 // openfile.h 
00002 //      Data structures for opening, closing, reading and writing to 
00003 //      individual files.  The operations supported are similar to
00004 //      the UNIX ones -- type 'man open' to the UNIX prompt.
00005 //
00006 //      There are two implementations.  One is a "STUB" that directly
00007 //      turns the file operations into the underlying UNIX operations.
00008 //      (cf. comment in filesys.h).
00009 //
00010 //      The other is the "real" implementation, that turns these
00011 //      operations into read and write disk sector requests. 
00012 //      In this baseline implementation of the file system, we don't 
00013 //      worry about concurrent accesses to the file system
00014 //      by different threads -- this is part of the assignment.
00015 //
00016 // Copyright (c) 1992-1993 The Regents of the University of California.
00017 // All rights reserved.  See copyright.h for copyright notice and limitation 
00018 // of liability and disclaimer of warranty provisions.
00019 
00020 #ifndef OPENFILE_H
00021 #define OPENFILE_H
00022 
00023 #include "copyright.h"
00024 #include "utility.h"
00025 
00026 #ifdef FILESYS_STUB                     // Temporarily implement calls to 
00027                                         // Nachos file system as calls to UNIX!
00028                                         // See definitions listed under #else
00029 class OpenFile {
00030   public:
00031     OpenFile(int f) { file = f; currentOffset = 0; }    // open the file
00032     ~OpenFile() { Close(file); }                        // close the file
00033 
00034     int ReadAt(char *into, int numBytes, int position) { 
00035                 Lseek(file, position, 0); 
00036                 return ReadPartial(file, into, numBytes); 
00037                 }       
00038     int WriteAt(char *from, int numBytes, int position) { 
00039                 Lseek(file, position, 0); 
00040                 WriteFile(file, from, numBytes); 
00041                 return numBytes;
00042                 }       
00043     int Read(char *into, int numBytes) {
00044                 int numRead = ReadAt(into, numBytes, currentOffset); 
00045                 currentOffset += numRead;
00046                 return numRead;
00047                 }
00048     int Write(char *from, int numBytes) {
00049                 int numWritten = WriteAt(from, numBytes, currentOffset); 
00050                 currentOffset += numWritten;
00051                 return numWritten;
00052                 }
00053 
00054     int Length() { Lseek(file, 0, 2); return Tell(file); }
00055     
00056   private:
00057     int file;
00058     int currentOffset;
00059 };
00060 
00061 #else // FILESYS
00062 class FileHeader;
00063 
00064 class OpenFile {
00065   public:
00066     OpenFile(int sector);               // Open a file whose header is located
00067                                         // at "sector" on the disk
00068     ~OpenFile();                        // Close the file
00069 
00070     void Seek(int position);            // Set the position from which to 
00071                                         // start reading/writing -- UNIX lseek
00072 
00073     int Read(char *into, int numBytes); // Read/write bytes from the file,
00074                                         // starting at the implicit position.
00075                                         // Return the # actually read/written,
00076                                         // and increment position in file.
00077     int Write(char *from, int numBytes);
00078 
00079     int ReadAt(char *into, int numBytes, int position);
00080                                         // Read/write bytes from the file,
00081                                         // bypassing the implicit position.
00082     int WriteAt(char *from, int numBytes, int position);
00083 
00084     int Length();                       // Return the number of bytes in the
00085                                         // file (this interface is simpler 
00086                                         // than the UNIX idiom -- lseek to 
00087                                         // end of file, tell, lseek back 
00088     
00089   private:
00090     FileHeader *hdr;                    // Header for this file 
00091     int seekPosition;                   // Current position within the file
00092 };
00093 
00094 #endif // FILESYS
00095 
00096 #endif // OPENFILE_H

Generated on Mon Feb 10 09:54:46 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