tac0S
Template Affectional Command Operating System
filesys.h
Go to the documentation of this file.
1 
36 #ifndef FS_H
37 #define FS_H
38 
39 #include "copyright.h"
40 #include "openfile.h"
41 #include "filehdr.h"
42 
43 #define MAX_OPEN_FILE 50
44 #define ROOT_DIRECTORY_FILE 0
45 #define CURRENT_DIRECTORY_FILE 1
46 
47 #ifdef FILESYS_STUB // Temporarily implement file system calls as
48 // calls to UNIX, until the real file system
49 // implementation is available
50 
51 
52 class FileSystem {
53 public:
54  FileSystem(bool format) {}
55 
56  bool Create(const char *name, int initialSize) {
57  int fileDescriptor = OpenForWrite(name);
58 
59  if (fileDescriptor == -1) return FALSE;
60  Close(fileDescriptor);
61  return TRUE;
62  }
63 
64  OpenFileTable* Open(char *name) {
65  int fileDescriptor = OpenForReadWrite(name, FALSE);
66 
67  if (fileDescriptor == -1) return NULL;
68  return new OpenFileTable(fileDescriptor);
69  }
70 
71  bool Remove(char *name) { return Unlink(name) == 0; }
72 };
73 
74 #else // FILESYS
75 
76 typedef struct path_parse {
77  char** pathSplit;
78  int size;
79 } path_parse_t;
80 
81 typedef struct file_table {
82  unsigned int tid;
83  char *path;
84  OpenFile** OpenFileTable;
85  struct file_table *next;
86 } file_table_t;
87 
88 typedef struct global_file_table {
89  OpenFile *openFile;
90  struct global_file_table *next;
92 
93 class FileSystem {
94 public:
95  FileSystem(bool format); // Initialize the file system.
96  // Must be called *after* "synchDisk"
97  // has been initialized.
98  // If "format", there is nothing on
99  // the disk, so initialize the directory
100  // and the bitmap of free blocks.
101 
102  ~FileSystem();
103 
104  bool Create(const char *name, int initialSize, File_type type = f);
105 
106  bool MkDir(const char *directory_name, unsigned int tid = 0); // Create a folder
107 
108  bool CdDir(const char *directory_name, unsigned int tid = 0); // Change the current folder
109 
110  bool RmDir(const char *directory_name, unsigned int tid = 0);// Remove a folder
111 
112  path_parse_t* CdFromPathName(const char *path_name, unsigned int tid = 0, int truncate = 0);
113 
114  OpenFile* OpenFromPathName(const char* path_name, unsigned int tid = 0);
115 
116  bool MkdirFromPathName(const char* path_name, unsigned int tid = 0);
117 
118  bool RmdirFromPathName(const char* path_name, unsigned int tid = 0);
119 
120  OpenFile *Open(const char *name, unsigned int tid = 0); // Open a file (UNIX open)
121 
122  bool Remove(const char *name, unsigned int tid = 0); // Delete a file (UNIX unlink)
123 
124  void List(); // List all the files in the file system
125 
126  void Print(); // List all the files and their contents
127 
128  void registerOpenFileTable(int* table, unsigned int tid);
129 
130  bool unregisterOpenFileTable(unsigned int tid);
131 
133  int UserOpen(const char *name, unsigned int tid);
134 
135  int UserRead(int fileDescriptor, char *into, int numBytes, unsigned int tid);
136 
137  int UserWrite(int fileDescriptor, char *from, int numBytes, unsigned int tid);
138 
139  void UserSetSeek(int fileDescriptor, int position, unsigned int tid);
140 
141  int UserCloseFile(int fileDescriptor, int* threadTableFileDescriptor, unsigned int tid);
142 
143  private:
144 
145  bool add_to_openFile_table(OpenFile *openFile, OpenFile **table = NULL);
146 
147  OpenFile *get_open_file_by_sector(int sector);
148 
149  int close_file(OpenFile* openFile, OpenFile** threadFileTable, unsigned int tid);
150 
151  int removeFiletoGlobalTable(OpenFile* openFile);
152 
153  OpenFile** get_thread_file_table(unsigned int tid);
154 
155  file_table_t* get_thread_file_table_t(unsigned int tid);
156 
157  void addFiletoGlobalTable(OpenFile *openFile);
158 
159  void init_ThreadsFilesTable();
160 
161  int getFileDescriptor(OpenFile* openFile,unsigned int tid);
162 
163  void initFileDesciptortable(int* table);
164 
165  void initOpenFileTable(OpenFile** table);
166 
167  path_parse_t* parse(char *path_name);
168 
169  global_file_table_t *GlobalOpenFileTable;
170  OpenFile *freeMapFile; // Bit map of free disk blocks, represented as a file
171  file_table_t *ThreadsFilesTable;
172 
173 };
174 
175 #endif // FILESYS
176 
177 #endif // FS_H
OpenFile * OpenFromPathName(const char *path_name, unsigned int tid=0)
FileSystem::OpenFromPathName.
Definition: filesys.cc:346
path_parse_t * CdFromPathName(const char *path_name, unsigned int tid=0, int truncate=0)
FileSystem::CdFromPathName can jump into the directory with a path.
Definition: filesys.cc:296
bool MkdirFromPathName(const char *path_name, unsigned int tid=0)
FileSystem::MkdirFromPathName.
Definition: filesys.cc:396
int UserRead(int fileDescriptor, char *into, int numBytes, unsigned int tid)
allow user to read into file
Definition: filesys.cc:721
bool Create(const char *name, int initialSize, File_type type=f)
FileSystem::Create Create a file in the Nachos file system (similar to UNIX create). Since we can't increase the size of files dynamically, we have to give Create the initial size of the file.
Definition: filesys.cc:206
bool Remove(const char *name, unsigned int tid=0)
FileSystem::Remove Delete a file from the file system. This requires: Remove it from the directory De...
Definition: filesys.cc:853
int OpenForReadWrite(const char *name, bool crashOnError)
OpenForReadWrite Open a file for reading or writing.
Definition: sysdep.cc:168
Definition: filesys.h:93
Data structures for opening, closing, reading and writing.
int OpenForWrite(const char *name)
OpenForWrite Open a file for writing. Create it if it doesn't exist; truncate it if it does already e...
Definition: sysdep.cc:151
void UserSetSeek(int fileDescriptor, int position, unsigned int tid)
User method that allow user to move the seek into a file.
Definition: filesys.cc:739
bool RmDir(const char *directory_name, unsigned int tid=0)
FileSystem::RmDir Remove the folder given.
Definition: filesys.cc:560
File_type
enum that define if the FileHeader define a file or a directory
Definition: filehdr.h:25
void Close(int fd)
Close Close a file. Abort on error.
Definition: sysdep.cc:247
bool MkDir(const char *directory_name, unsigned int tid=0)
FileSystem::MkDir Create a new folder in the Nachos file system.
Definition: filesys.cc:502
Definition: openfile.h:77
bool CdDir(const char *directory_name, unsigned int tid=0)
FileSystem::CdDir Come in folder given.
Definition: filesys.cc:477
void Print()
FileSystem::Print Print everything about the file system: the contents of the bitmap the contents of ...
Definition: filesys.cc:904
OpenFile * Open(const char *name, unsigned int tid=0)
FileSystem::Open Open a file for reading and writing. To open a file: Find the location of the file's...
Definition: filesys.cc:768
Definition: filesys.h:76
int UserWrite(int fileDescriptor, char *from, int numBytes, unsigned int tid)
Allow user to write into a file.
Definition: filesys.cc:731
bool unregisterOpenFileTable(unsigned int tid)
When thread die need to call this method.
Definition: filesys.cc:964
void registerOpenFileTable(int *table, unsigned int tid)
Allow a thread to use filesystem by register his tid.
Definition: filesys.cc:945
Definition: filesys.h:81
Definition: filesys.h:88
FileSystem(bool format)
FileSystem::FileSystem Initialize the file system. If format = TRUE, the disk has nothing on it...
Definition: filesys.cc:82
bool RmdirFromPathName(const char *path_name, unsigned int tid=0)
Definition: filesys.cc:435
int UserOpen(const char *name, unsigned int tid)
allow a user to open a file
Definition: filesys.cc:711
void List()
FileSystem::List List all the files in the file system directory.
Definition: filesys.cc:888
~FileSystem()
~FileSystem() delete all file system
Definition: filesys.cc:986
Data structures for managing a disk file header.
bool Unlink(const char *name)
Unlink Delete a file.
Definition: sysdep.cc:259