-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplefs.h
More file actions
146 lines (116 loc) · 5.15 KB
/
simplefs.h
File metadata and controls
146 lines (116 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
#include "bitmap.h"
#include "disk_driver.h"
/*these are structures stored on disk*/
// header, occupies the first portion of each block in the disk
// represents a chained list of blocks
typedef struct {
int previous_block; // chained list (previous block)
int next_block; // chained list (next_block)
int block_in_file; // position in the file, if 0 we have a file control block
} BlockHeader;
// this is in the first block of a chain, after the header
typedef struct {
int directory_block; // first block of the parent directory
int block_in_disk; // repeated position of the block on the disk
char name[128];
int size_in_bytes;
int size_in_blocks;
int written_bytes; // used for files
int is_dir; // 0 for file, 1 for dir
} FileControlBlock;
// this is the first physical block of a file
// it has a header
// an FCB storing file infos
// and can contain some data
/******************* stuff on disk BEGIN *******************/
typedef struct {
BlockHeader header;
FileControlBlock fcb;
char data[BLOCK_SIZE-sizeof(FileControlBlock) - sizeof(BlockHeader)] ;
} FirstFileBlock;
// this is one of the next physical blocks of a file
typedef struct {
BlockHeader header;
char data[BLOCK_SIZE-sizeof(BlockHeader)];
} FileBlock;
// this is the first physical block of a directory
typedef struct {
BlockHeader header;
FileControlBlock fcb;
int num_entries;
int file_blocks[ (BLOCK_SIZE
-sizeof(BlockHeader)
-sizeof(FileControlBlock)
-sizeof(int))/sizeof(int) ];
} FirstDirectoryBlock;
// this is remainder block of a directory
typedef struct {
BlockHeader header;
int file_blocks[ (BLOCK_SIZE-sizeof(BlockHeader))/sizeof(int) ];
} DirectoryBlock;
/******************* stuff on disk END *******************/
typedef struct {
DiskDriver* disk;
// add more fields if needed
} SimpleFS;
// this is a file handle, used to refer to open files
typedef struct {
SimpleFS* sfs; // pointer to memory file system structure
FirstFileBlock* fcb; // pointer to the first block of the file(read it)
FirstDirectoryBlock* directory; // pointer to the directory where the file is stored
BlockHeader* current_block; // current block in the file
int pos_in_file; // position of the cursor
} FileHandle;
typedef struct {
SimpleFS* sfs; // pointer to memory file system structure
FirstDirectoryBlock* dcb; // pointer to the first block of the directory(read it)
FirstDirectoryBlock* directory; // pointer to the parent directory (null if top level)
BlockHeader* current_block; // current block in the directory
int pos_in_dir; // absolute position of the cursor in the directory
int pos_in_block; // relative position of the cursor in the block
} DirectoryHandle;
// initializes a file system on an already made disk
// returns a handle to the top level directory stored in the first block
DirectoryHandle* SimpleFS_init(SimpleFS* fs, DiskDriver* disk);
// creates the inital structures, the top level directory
// has name "/" and its control block is in the first position
// it also clears the bitmap of occupied blocks on the disk
// the current_directory_block is cached in the SimpleFS struct
// and set to the top level directory
void SimpleFS_format(SimpleFS* fs);
// creates an empty file in the directory d
// returns null on error (file existing, no free blocks)
// an empty file consists only of a block of type FirstBlock
FileHandle* SimpleFS_createFile(DirectoryHandle* d, const char* filename);
// reads in the (preallocated) blocks array, the name of all files in a directory
// in is_file we store 0 if is file, 1 if is dir
int SimpleFS_readDir(char** names, int* is_file, DirectoryHandle* d);
// opens a file in the directory d. The file should be exisiting
FileHandle* SimpleFS_openFile(DirectoryHandle* d, const char* filename);
// closes a file handle (destroyes it)
int SimpleFS_close(FileHandle* f);
// writes in the file, at current position for size bytes stored in data
// overwriting and allocating new space if necessary
// returns the number of bytes written
int SimpleFS_write(FileHandle* f, void* data, int size);
// writes in the file, at current position size bytes stored in data
// overwriting and allocating new space if necessary
// returns the number of bytes read
int SimpleFS_read(FileHandle* f, void* data, int size);
// returns the number of bytes read (moving the current pointer to pos)
// returns pos on success
// -1 on error (file too short)
int SimpleFS_seek(FileHandle* f, int pos);
// seeks for a directory in d. If dirname is equal to ".." it goes one level up
// 0 on success, negative value on error
// it does side effect on the provided handle
int SimpleFS_changeDir(DirectoryHandle* d, char* dirname);
// creates a new directory in the current one (stored in fs->current_directory_block)
// 0 on success
// -1 on error
int SimpleFS_mkDir(DirectoryHandle* d, char* dirname);
// removes the file in the current directory
// returns -1 on failure 0 on success
// if a directory, it removes recursively all contained files
int SimpleFS_remove(DirectoryHandle* dir, char* filename);