SimpleFS
A Simple File Sytem implementation including Disk and Shell Layers
fs.h
Go to the documentation of this file.
1 
116 #pragma once
117 
118 #include "sfs/disk.h"
119 #include <cstring>
120 #include <vector>
121 #include <stdint.h>
122 #include <vector>
123 
124 using namespace std;
125 
131 class FileSystem {
132 public:
133  const static uint32_t MAGIC_NUMBER = 0xf0f03410; // Magic number helps in checking Validity of the FileSystem on disk @hideinitializer
134  const static uint32_t INODES_PER_BLOCK = 128; // Number of Inodes which can be contained in a block of (4kb) @hideinitializer
135  const static uint32_t POINTERS_PER_INODE = 5; // Number of Direct block pointers in an Inode Block @hideinitializer
136  const static uint32_t POINTERS_PER_BLOCK = 1024; // Number of block pointers in one Indirect pointer @hideinitializer
137  const static uint32_t NAMESIZE = 16; // Max Name size for files/directories @hideinitializer
138  const static uint32_t ENTRIES_PER_DIR = 7; // Number of Files/Directory entries within a Directory @hideinitializer
139  const static uint32_t DIR_PER_BLOCK = 8; // Number of Directories per 4KB block @hideinitializer
140 
141 private:
148  struct SuperBlock {
149  uint32_t MagicNumber;
150  uint32_t Blocks;
151  uint32_t InodeBlocks;
152  uint32_t DirBlocks;
153  uint32_t Inodes;
154  uint32_t Protected;
155  char PasswordHash[257];
156  };
157 
163  struct Dirent {
164  uint8_t type;
165  uint8_t valid;
166  uint32_t inum;
167  char Name[NAMESIZE];
168  };
169 
177  struct Directory {
178  uint16_t Valid;
179  uint32_t inum;
180  char Name[NAMESIZE];
181  Dirent Table[ENTRIES_PER_DIR];
182  };
183 
190  struct Inode {
191  uint32_t Valid;
192  uint32_t Size;
193  uint32_t Direct[FileSystem::POINTERS_PER_INODE];
194  uint32_t Indirect;
195  };
196 
202  union Block {
203  struct SuperBlock Super;
204  struct Inode Inodes[FileSystem::INODES_PER_BLOCK];
205  uint32_t Pointers[FileSystem::POINTERS_PER_BLOCK];
206  char Data[Disk::BLOCK_SIZE];
207  struct Directory Directories[FileSystem::DIR_PER_BLOCK];
208  };
209 
210  // Internal member variables
211  Disk* fs_disk;
212  vector<bool> free_blocks;
213  vector<int> inode_counter;
214  vector<uint32_t> dir_counter;
215  struct SuperBlock MetaData; // Caches the SuperBlock to save a disk-read @hideinitializer
216  bool mounted; // Boolean to check if the disk is mounted and saved @hideinitializer
217 
218  // Layer 1 Core Functions
223  ssize_t create();
224 
230  bool remove(size_t inumber);
231 
237  ssize_t stat(size_t inumber);
238 
247  ssize_t read(size_t inumber, char *data, int length, size_t offset);
248 
257  ssize_t write(size_t inumber, char *data, int length, size_t offset);
258 
259  // Helper functions for Layer 1
266  bool load_inode(size_t inumber, Inode *node);
267 
272  ssize_t allocate_free_block();
273 
283  void read_helper(uint32_t blocknum, int offset, int *length, char **data, char **ptr);
284 
292  ssize_t write_ret(size_t inumber, Inode* node, int ret);
293 
303  void read_buffer(int offset, int *read, int length, char *data, uint32_t blocknum);
304 
315  bool check_allocation(Inode *node, int read, int orig_offset, uint32_t &blocknum, bool write_indirect, Block indirect);
316 
321  uint32_t allocate_block();
322 
323 
325  Directory curr_dir;
326 
336  Directory add_dir_entry(Directory dir, uint32_t inum, uint32_t type, char name[]);
337 
343  void write_dir_back(struct Directory dir);
344 
352  int dir_lookup(Directory dir, char name[]);
353 
360  Directory read_dir_from_offset(uint32_t offset);
361 
369  Directory rmdir_helper(Directory parent, char name[]);
370 
378  Directory rm_helper(Directory parent, char name[]);
379 
380 public:
381 
387  static void debug(Disk *disk);
388 
394  static bool format(Disk *disk);
395 
396 
402  bool mount(Disk *disk);
403 
404 
405  // Security Functions
406 
414  bool set_password();
415 
423  bool change_password();
424 
431  bool remove_password();
432 
433  // Directories and Files
434 
445  bool touch(char name[]);
446 
455  bool mkdir(char name[]);
456 
465  bool rmdir(char name[]);
466 
474  bool cd(char name[]);
475 
482  bool ls();
483 
491  bool rm(char name[]);
492 
502  bool copyout(char name[],const char *path);
503 
515  bool copyin(const char *path, char name[]);
516 
526  bool ls_dir(char name[]);
527 
532  void exit();
533 
542  void stat();
543 };
544 
FileSystem::Directory::inum
uint32_t inum
Definition: fs.h:179
FileSystem::SuperBlock::Inodes
uint32_t Inodes
Definition: fs.h:153
FileSystem::SuperBlock::Blocks
uint32_t Blocks
Definition: fs.h:150
FileSystem::Dirent::inum
uint32_t inum
Definition: fs.h:166
FileSystem::Dirent::valid
uint8_t valid
Definition: fs.h:165
FileSystem::Inode::Size
uint32_t Size
Definition: fs.h:192
FileSystem::Inode
Inode Structure Corresponds to a file stored on the disk. Contains the list of raw data blocks used t...
Definition: fs.h:190
FileSystem::Block
Block Union Corresponds to one block of disk of size Disk::BLOCKSIZE. Can be used as a SuperBlock,...
Definition: fs.h:202
FileSystem::Dirent
Directory Entry. Contains necessary fields to locate the file and directory Consumes 64 KB per object...
Definition: fs.h:163
disk.h
Contains the implementation of disk layer to support File System.
FileSystem
FileSytem Class. Contains fs layer to access and store disk blocks. Used by sfssh (shell) to provide ...
Definition: fs.h:131
FileSystem::SuperBlock
SuperBlock structure. It is the first block in any disk. It's main function is to help validating the...
Definition: fs.h:148
FileSystem::SuperBlock::DirBlocks
uint32_t DirBlocks
Definition: fs.h:152
Disk
Disk class Implements Disk abstraction that enables emulation of a disk image. Used by file system to...
Definition: disk.h:18
FileSystem::SuperBlock::InodeBlocks
uint32_t InodeBlocks
Definition: fs.h:151
FileSystem::Directory
Directory Structure. Contains a table of directory entries for storing hierarchy. Also contains field...
Definition: fs.h:177
FileSystem::Inode::Indirect
uint32_t Indirect
Definition: fs.h:194
FileSystem::SuperBlock::Protected
uint32_t Protected
Definition: fs.h:154