tac0S
Template Affectional Command Operating System
openfile.h
Go to the documentation of this file.
1 
22 #ifndef OPENFILE_H
23 #define OPENFILE_H
24 
25 #include "copyright.h"
26 #include "utility.h"
27 
28 #ifdef FILESYS_STUB // Temporarily implement calls to
29 // Nachos file system as calls to UNIX!
30 // See definitions listed under #else
31 class OpenFileTable {
32 public:
33 OpenFileTable(int f) { file = f; currentOffset = 0; } // open the file
34 ~OpenFileTable() { Close(file); } // close the file
35 
36 int ReadAt(char *into, int numBytes, int position) {
37 Lseek(file, position, 0);
38 return ReadPartial(file, into, numBytes);
39 }
40 int WriteAt(const char *from, int numBytes, int position) {
41 Lseek(file, position, 0);
42 WriteFile(file, from, numBytes);
43 return numBytes;
44 }
45 int Read(char *into, int numBytes) {
46 int numRead = ReadAt(into, numBytes, currentOffset);
47 currentOffset += numRead;
48 return numRead;
49 }
50 int Write(const char *from, int numBytes) {
51 int numWritten = WriteAt(from, numBytes, currentOffset);
52 currentOffset += numWritten;
53 return numWritten;
54 }
55 
56 int Length() { Lseek(file, 0, 2); return Tell(file); }
57 
58 private:
59 int file;
60 int currentOffset;
61 };
62 
63 #else // FILESYS
64 
65 class FileHeader;
66 
71 typedef struct tuple {
72  int seekPosition;
73  unsigned int tid;
74  struct tuple *next;
75 } tuple_t;
76 
77 class OpenFile {
78 public:
79  OpenFile(int sector); // Open a file whose header is located
80  // at "sector" on the disk
81  ~OpenFile(); // Close the file
82 
83  void Seek(int position, unsigned int tid = 0); // Set the position from which to
84  // start reading/writing -- UNIX lseek
85 
86  int Read(char *into, int numBytes, unsigned int tid = 0); // Read/write bytes from the file,
87  // starting at the implicit position.
88  // Return the # actually read/written,
89  // and increment position in file.
90  int Write(const char *from, int numBytes, unsigned int tid = 0);
91 
92  int ReadAt(char *into, int numBytes, int position);
93 
94  // Read/write bytes from the file,
95  // bypassing the implicit position.
96  int WriteAt(const char *from, int numBytes, int position);
97 
98  int Length(); // Return the number of bytes in the
99  // file (this interface is simpler
100  // than the UNIX idiom -- lseek to
101  // end of file, tell, lseek back
102 
103  int get_sector();
104 
105  void add_seek(unsigned int tid);
106 
107  bool remove_seek(unsigned int tid);
108 
109  bool isOpenByOthers();
110 
111  bool isdir();
112 
113 
114 private:
115  void set_seek_position(unsigned int tid, int seekPosition);
116  int get_seek_position(unsigned int tid);
117 
118  FileHeader *hdr; // Header for this file
119  FileHeader* hdrTofree;
120  tuple_t *seek_tid_list;
121 
122 };
123 
124 #endif // FILESYS
125 
126 #endif // OPENFILE_H
int Tell(int fd)
Tell Report the current location within an open file.
Definition: sysdep.cc:231
void WriteFile(int fd, const char *buffer, int nBytes)
WriteFile Write characters to an open file. Abort if write fails.
Definition: sysdep.cc:207
void Read(int fd, char *buffer, int nBytes)
Read Read characters from an open file. Abort if read fails.
Definition: sysdep.cc:182
void Close(int fd)
Close Close a file. Abort on error.
Definition: sysdep.cc:247
tuple Structure that allow multi-seek on one Openfile
Definition: openfile.h:71
Definition: openfile.h:77
Miscellaneous useful definitions, including debugging routines.
int ReadPartial(int fd, char *buffer, int nBytes)
ReadPartial Read characters from an open file, returning as many as are available.
Definition: sysdep.cc:195
The following class defines the Nachos "file header" (in UNIX terms, the "i-node"), describing where on disk to find all of the data in the file. The file header is organized as a simple table of pointers to data blocks.
Definition: filehdr.h:45
struct tuple tuple_t
tuple Structure that allow multi-seek on one Openfile
void Lseek(int fd, int offset, int whence)
Lseek Change the location within an open file. Abort on error.
Definition: sysdep.cc:219