-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.c
More file actions
185 lines (166 loc) · 5.46 KB
/
FileSystem.c
File metadata and controls
185 lines (166 loc) · 5.46 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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#define BLOCK_SIZE 1024
#define MAX_FILE_SIZE 4194304 // 4GB of file size
#define FREE_ARRAY_SIZE 248 // free and inode array size
#define INODE_SIZE 64
/*************** super block structure**********************/
typedef struct {
unsigned int isize; // 4 byte
unsigned int fsize;
unsigned int nfree;
unsigned short free[FREE_ARRAY_SIZE];
unsigned int ninode;
unsigned short inode[FREE_ARRAY_SIZE];
unsigned short flock;
unsigned short ilock;
unsigned short fmod;
unsigned int time[2];
} superBlockType;
/****************inode structure ************************/
typedef struct {
unsigned short flags; // 2 bytes
char nlinks; // 1 byte
char uid;
char gid;
unsigned int size; // 32bits 2^32 = 4GB filesize
unsigned short addr[22]; // to make total size = 64 byte inode size
unsigned int actime;
unsigned int modtime;
} Inode;
typedef struct {
unsigned short inode;
char filename[14];
} directoryEntry;
superBlockType superBlock;
int fileDescriptor, currentINodeNumber, totalINodesCount;
char currentWorkingDirectory[100];
char fileSystemPath[100];
// function declarations for the main method to use
void writeBufferToBlock(int blockNumber, void* buffer, int numberOfBytes);
void writeToBlockWithOffset(int blockNumber, int offset, void* buffer, int numberOfBytes);
void readFromBlockWithOffset(int blockNumber, int offset, void* buffer, int numberOfBytes);
void addAFreeBlock(int blockNumber);
void getAFreeBlock();
void addAFreeInode(int iNumber);
Inode getInode(int INumber);
int getAFreeInode();
void writeTheInode(int INumber, Inode inode);
void initializeRootDirectory();
void ls();
void makeDirectory(char* directoryName);
int findLastIndex(char str[100], char ch); // String size (and currentWorkingDirectory size is hard-coded to 100 for now)
void sliceString(const char * str, char * buffer, size_t startPosition, size_t endPosition);
void changeDirectory(char* directoryName);
void copyIn(char* sourceFilePath, char* fileName);
void copyOut(char* destinationFilePath, char* fileName);
void removeFile(char* fileName); // Implements the rm command
void removeDirectory(char* directoryName);
void openFileSystem(const char *fileName);
void initfs(char* filePath, int totalNumberOfBlocks, int totalNumberOfINodes);
void quit();
Inode getInode(int INumber) {
Inode inode;
lseek(fileDescriptor, 2 * BLOCK_SIZE + INumber * INODE_SIZE, SEEK_SET);
read(fileDescriptor, &inode, INODE_SIZE);
return inode;
}
void writeBufferToBlock(int blockNumber, void* buffer, int numberOfBytes)
{
lseek(fileDescriptor, BLOCK_SIZE * blockNumber, SEEK_SET);
write(fileDescriptor, buffer, numberOfBytes);
}
void writeToBlockWithOffset(int blockNumber, int offset, void* buffer, int numberOfBytes)
{
lseek(fileDescriptor, (BLOCK_SIZE * blockNumber) + offset, SEEK_SET);
write(fileDescriptor, buffer, numberOfBytes);
}
void readFromBlockWithOffset(int blockNumber, int offset, void* buffer, int numberOfBytes)
{
lseek(fileDescriptor, (BLOCK_SIZE * blockNumber) + offset, SEEK_SET);
read(fileDescriptor, buffer, numberOfBytes);
}
void quit() {
close(fileDescriptor);
exit(0);
}
int main(int argc, char *argv[])
{
char c;
printf("\n Clearing screen \n");
system("clear");
unsigned int block_number =0, inode_number=0;
char *fs_path;
char *arg1, *arg2;
char *my_argv, cmd[256];
while(1)
{
printf("\n%s@%s>>>",fileSystemPath,currentWorkingDirectory);
scanf(" %[^\n]s", cmd);
my_argv = strtok(cmd," ");
if(strcmp(my_argv, "initfs")==0)
{
fs_path = strtok(NULL, " ");
arg1 = strtok(NULL, " ");
arg2 = strtok(NULL, " ");
if(access(fs_path, X_OK) != -1)
{
printf("filesystem already exists. \n");
printf("same file system will be used\n");
}
else
{
if (!arg1 || !arg2)
printf(" insufficient arguments to proceed\n");
else
{
block_number = atoi(arg1);
inode_number = atoi(arg2);
initfs(fs_path,block_number, inode_number);
}
}
my_argv = NULL;
}
else if(strcmp(my_argv, "q")==0){
quit();
}
else if(strcmp(my_argv, "ls")==0){
ls();
}
else if(strcmp(my_argv, "mkdir")==0){
arg1 = strtok(NULL, " ");
makeDirectory(arg1);
}
else if(strcmp(my_argv, "cd")==0){
arg1 = strtok(NULL, " ");
changedir(arg1);
}
else if(strcmp(my_argv, "cpin")==0){
arg1 = strtok(NULL, " ");
arg2 = strtok(NULL, " ");
copyIn(arg1,arg2);
}
else if(strcmp(my_argv, "cpout")==0){
arg1 = strtok(NULL, " ");
arg2 = strtok(NULL, " ");
copyOut(arg1,arg2);
}
else if(strcmp(my_argv, "rm")==0){
arg1 = strtok(NULL, " ");
rm(arg1);
}
else if(strcmp(my_argv, "currentWorkingDirectory")==0){
printf("%s\n",currentWorkingDirectory);
}
}
}