-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.c
372 lines (343 loc) · 10.8 KB
/
fs.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
Filename : fs.c
Author : Kyler Stigelman
Course : CSCI 380-01
Assignment : Filesystems
Description: A simple UNIX-like file system that resides on the disk.
The disk is 128KB and can support 16 files. A file is
8 blocks in size, and each block is 1KB.
*/
/*
*******************************************************************************
* INCLUDE DIRECTIVES
*******************************************************************************
*/
#include "fs.h"
// For open, close
#include <fcntl.h>
// For read, write, lseek
#include <unistd.h>
// For printf
#include <stdio.h>
// For exit
#include <stdlib.h>
// For strcmp, strtok
#include <string.h>
/*
*******************************************************************************
* CONSTANTS
*******************************************************************************
*/
#define FS_NUM_BLOCKS 128
#define FS_MAX_FILE_SIZE 8
#define FS_MAX_FILENAME 16
#define FS_MAX_FILES 16
#define FS_BLOCK_SIZE 1024
/*
*******************************************************************************
* STRUCTS
*******************************************************************************
*/
//Struct for the filesystem file.
struct fs_t
{
int fd;
};
//Struct containing the features of an inode.
typedef struct
{
char name[16];
int size;
int blockPointers[8];
int used;
} Inode;
// open the file with the above name
void
fs_open (struct fs_t *fs, char diskName[16])
{
// this file will act as the "disk" for your file system
fs->fd = open (diskName, O_RDWR, 0600);
if (fs->fd == -1) {
printf ("Error: Could not open disk %s", diskName);
exit (0);
}
}
// close and clean up all associated things
void
fs_close (struct fs_t *fs)
{
// this file will act as the "disk" for your file system
if (close (fs->fd) == 0) {
fs->fd = 0;
return;
}
printf ("Error: Failed to close file with descriptor %d\n", fs->fd);
exit (0);
}
// create a file with this name and this size
void
fs_create (struct fs_t *fs, char name[16], int size)
{
// high level pseudo code for creating a new file
// Step 1: check to see if we have sufficient free space on disk by
// reading in the free block list. To do this:
// - move the file pointer to the start of the disk file.
// - Read the first 128 bytes (the free/in-use block information)
// - Scan the list to make sure you have sufficient free blocks to
// allocate a new file of this size
if (size > FS_MAX_FILE_SIZE) {
printf ("Error: Requested file size is too big!");
return;
}
char block_buffer[FS_NUM_BLOCKS];
lseek (fs->fd, 0, SEEK_SET);
read (fs->fd, block_buffer, FS_NUM_BLOCKS);
int count = 0;
for (int i = 0; i < FS_NUM_BLOCKS; ++i) {
if (block_buffer[i] == 0)
++count;
if (count == size)
break;
}
if (count < size) {
printf ("Error: Not enough space on disk\n");
return;
}
// Step 2: we look for a free inode on disk
// - Read in a inode
// - check the "used" field to see if it is free
// - If not, repeat the above two steps until you find a free inode
// - Set the "used" field to 1
// - Copy the filename to the "name" field
// - Copy the file size (in units of blocks) to the "size" field
// Step 3: Allocate data blocks to the file
// - Scan the block list that you read in Step 1 for a free block
// - Once you find a free block, mark it as in-use (Set it to 1)
// - Set the blockPointer[i] field in the inode to this block number.
// - repeat until you allocated "size" blocks
// Step 4: Write out the inode and free block list to disk
// - Move the file pointer to the start of the file
// - Write out the 128 byte free block list
// - Move the file pointer to the position on disk where this inode was stored
// - Write out the inode
int nextIndex = 0;
for (int i = 0; i < FS_MAX_FILES; ++i) {
Inode inode;
readInode (fs->fd, &inode, i);
if (strncmp (name, inode.name, 16) == 0) {
printf ("Error: File already exists!");
return;
}
if (inode.used == 0) {
inode.used = 1;
strncpy (inode.name, name, FS_MAX_FILENAME);
inode.size = size;
for (int j = 0; j < FS_NUM_BLOCKS; ++j) {
if (block_buffer[j] == 0)
{
block_buffer[j] = 1;
inode.blockPointers[nextIndex] = j;
++nextIndex;
if (nextIndex == size)
break;
}
}
writeInode (fs->fd, &inode, i);
printInode (&inode);
break;
}
//If we get to this point, 16 files have already been made.
if (i == FS_MAX_FILES - 1) {
printf ("Error: Too many files already created!\n");
}
}
//Write free block list
lseek (fs->fd, 0, SEEK_SET);
write (fs->fd, block_buffer, FS_NUM_BLOCKS);
}
// Delete the file with this name
void
fs_delete (struct fs_t *fs, char name[16])
{
// Step 1: Locate the inode for this file
// - Move the file pointer to the 1st inode (129th byte)
// - Read in a inode
// - If the iinode is free, repeat above step.
// - If the iinode is in use, check if the "name" field in the
// inode matches the file we want to delete. IF not, read the next
// inode and repeat
Inode inode;
lseek (fs->fd, FS_NUM_BLOCKS, SEEK_SET);
for (int i = 0; i < FS_MAX_FILES; ++i)
{
readInode (fs->fd, &inode, i);
if (inode.used == 1 && strcmp (inode.name, name) == 0)
{
// Step 2: free blocks of the file being deleted
// Read in the 128 byte free block list (move file pointer to start
// of the disk and read in 128 bytes)
lseek (fs->fd, 0, SEEK_SET);
char block_list[FS_NUM_BLOCKS];
read (fs->fd, block_list, FS_NUM_BLOCKS);
// Free each block listed in the blockPointer fields
for (int j = 0; j < inode.size; ++j)
block_list[inode.blockPointers[j]] = 0;
// Step 3: mark inode as free
// Set the "used" field to 0.
inode.used = 0;
// Step 4: Write out the inode and free block list to disk
// Move the file pointer to the start of the file
// Write out the 128 byte free block list
lseek (fs->fd, 0, SEEK_SET);
write (fs->fd, block_list, FS_NUM_BLOCKS);
// Write out the inode to disk
writeInode (fs->fd, &inode, i);
return;
}
}
printf ("Error: File not found\n");
}
// List names of all files on disk
void
fs_ls (struct fs_t *fs)
{
// Step 1: read in each inode and print!
// Move file pointer to the position of the 1st inode (129th byte)
// for each inode:
// read it in
// if the inode is in-use
// print the "name" and "size" fields from the inode
// end for
lseek (fs->fd, FS_NUM_BLOCKS, SEEK_SET);
Inode inode;
for (int i = 0; i < FS_MAX_FILES; ++i) {
readInode (fs->fd, &inode, i);
if (inode.used == 1)
printf ("%16s %6dB\n", inode.name, inode.size * 1024);
}
}
// read this block from this file
void
fs_read (struct fs_t *fs, char name[16], int blockNum, char buf[1024])
{
// Step 1: locate the inode for this file
// - Move file pointer to the position of the 1st inode (129th byte)
// - Read in a inode
// - If the inode is in use, compare the "name" field with the above file
// - If the file names don't match, repeat
lseek (fs->fd, FS_NUM_BLOCKS, SEEK_SET);
Inode inode;
for (int i = 0; i < FS_MAX_FILES; ++i)
{
readInode (fs->fd, &inode, i);
if (inode.used == 1 && strcmp (inode.name, name) == 0)
{
// Step 2: Read in the specified block
// Check that blockNum < inode.size, else flag an error
if (blockNum >= inode.size) {
printf ("Error: Block index too large");
return;
}
// Get the disk address of the specified block
// move the file pointer to the block location
// Read in the block! => Read in 1024 bytes from this location into the buffer
// "buf"
lseek (fs->fd, FS_BLOCK_SIZE * inode.blockPointers[blockNum], SEEK_SET);
read (fs->fd, buf, FS_BLOCK_SIZE);
return;
}
}
printf ("Error: File not found\n");
}
// write this block to this file
void
fs_write (struct fs_t *fs, char name[16], int blockNum, char buf[1024])
{
// Step 1: locate the inode for this file
// Move file pointer to the position of the 1st inode (129th byte)
// Read in a inode
// If the inode is in use, compare the "name" field with the above file
// If the file names don't match, repeat
lseek (fs->fd, FS_NUM_BLOCKS, SEEK_SET);
Inode inode;
for (int i = 0; i < FS_MAX_FILES; ++i)
{
readInode (fs->fd, &inode, i);
if (inode.used == 1 && strcmp (inode.name, name) == 0)
{
// Step 2: Write to the specified block
// Check that blockNum < inode.size, else flag an error
if (blockNum >= inode.size) {
printf ("Error: Block index too large");
return;
}
// Get the disk address of the specified block
// move the file pointer to the block location
// Write the block! => Write 1024 bytes from the buffer "buf" to this location
lseek (fs->fd, FS_BLOCK_SIZE * inode.blockPointers[blockNum], SEEK_SET);
write (fs->fd, buf, FS_BLOCK_SIZE);
return;
}
}
printf ("Error: File not found\n");
}
// REPL entry point
void
fs_repl ()
{
char name[16];
fgets (name, sizeof (name), stdin);
strtok (name, "\n");
struct fs_t fs;
fs_open (&fs, name);
char command[32];
while (fgets (command, sizeof (command), stdin))
{
strtok (command, "\n");
int size;
char str[16];
if (sscanf (command, "C %15s %d", str, &size) == 2) {
printf ("Creating file: %15s %d\n", str, size);
fs_create (&fs, str, size);
}
else if (sscanf (command, "D %15s", str) == 1) {
printf ("Deleting file: %s\n", str);
fs_delete (&fs, str);
}
else if (strcmp (command, "L") == 0) {
printf ("Listing files:\n");
fs_ls (&fs);
}
else if (sscanf (command, "R %15s %d", str, &size) == 2) {
printf ("Reading from file: %s %d\n", str, size);
}
else if (sscanf (command, "W %7s %d", str, &size) == 2) {
printf ("Writing to file: %s %d\n", str, size);
}
else
printf ("Unknown command\n");
}
fs_close (&fs);
}
//Writes an inode to the file. lseek to the inode index, then write.
void
writeInode (int fd, Inode* inode, int inodeNum) {
lseek (fd, FS_NUM_BLOCKS + (inodeNum * sizeof (Inode)), SEEK_SET);
write (fd, inode, sizeof (Inode));
}
//Reads an inode from file. lseek to the inode index, then write.
void
readInode (int fd, Inode* inode, int inodeNum)
{
lseek (fd, FS_NUM_BLOCKS + (inodeNum * sizeof (Inode)), SEEK_SET);
read (fd, inode, sizeof (Inode));
}
//Print out the inode
void
printInode (const Inode* inode)
{
printf ("%15s: %d blocks, Block pointers: ", inode->name, inode->size);
for (int i = 0; i < FS_MAX_FILE_SIZE; ++i)
printf ("%d ", inode->blockPointers[i]);
printf ("Used: %d\n", inode->used);
}