forked from AmhCS/SysII-FS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSmakefile.java
189 lines (159 loc) · 3.69 KB
/
FSmakefile.java
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
package external_tools;
import java.io.*;
public class FSmakefile {
private String newFile;
private RandomAccessFile file;
private long directoryBlockPos = 4096;
private long freePosition = 12;
public void start(String newFile, String filename) {
try {
file = new RandomAccessFile(filename,"rw");
if( fileExists(newFile) ) {
System.out.println("File " + newFile + " already exists.");
}
else {
makeFile(newFile);
}
}
catch ( IOException e ) {
System.out.print("IO Exception - BlockDevice file not found.");
}
}
private boolean fileExists(String newFile) throws IOException {
//A byte equal to null is 0x00000000
long currPos = directoryBlockPos;
while ( currPos <= 8191 ) {
file.seek(currPos);
byte[] bytes = new byte[12];
char[] chars = new char[12];
int c = 0;
while ( c < 12 ) {
bytes[c] = (byte)file.read();
chars[c] = (char)bytes[c];
if ( (int)chars[c] == 0 ) {
break;
}
c++;
}
String s = "";
c = 0;
while ( c < 12 ) {
if ( (int)chars[c] == 0 ) {
break;
}
s += chars[c];
c++;
}
if ( s.equals(newFile) ) {
return true;
}
currPos += 16;
}
return false;
}
private long firstFreeEntry() throws IOException {
//A byte equal to null is 0x00000000
long currPos = directoryBlockPos;
while ( currPos <= 8191 ) {
file.seek(currPos);
byte b = (byte)file.read();
if ( b == 0 ) {
break;
}
currPos += 16;
}
if ( currPos >= 8191 ) {
throw new RuntimeException("Directory block full.");
}
return currPos;
}
private int getFreeBlock() throws IOException {
file.seek(freePosition);
return file.readInt();
}
private void incrementFreeBlock() throws IOException {
file.seek(freePosition);
int free = file.readInt();
free++;
file.seek(freePosition);
file.writeInt(free);
}
private void makeFile(String newFile) throws IOException {
long currPos = firstFreeEntry();
int freeBlock = getFreeBlock();
incrementFreeBlock();
char[] chars = newFile.toCharArray();
byte[] bytes = new byte[chars.length];
int c = 0;
for ( byte b : bytes ) {
bytes[c] = (byte)chars[c];
c++;
}
//Write new entry as 16 bytes in the directory block
file.seek(currPos);
file.write(bytes);
currPos += 12;
file.seek(currPos);
file.writeInt(freeBlock);
//Seek to the first free block, where you will write the inode for the file
long inodePos = 8192;
c = freeBlock;
while ( c > 2 ) {
inodePos += 4096;
c--;
}
file.seek(inodePos);
//Create inode
RandomAccessFile input = new RandomAccessFile(newFile,"r");
int byteLength = (int)input.length();
file.writeInt(byteLength);
currPos = inodePos + 4;
byte[] nfb = new byte[byteLength];
byte b;
//Read and write the immediate data
c = 0;
while ( c < byteLength ) {
nfb[c] = (byte)input.read();
c++;
}
file.seek(currPos);
int nfbCounter = 0;
for ( c = 0; c < byteLength; c++ ) {
file.writeByte((int)nfb[c]);
nfbCounter++;
if ( c > 2044 ) {
break;
}
}
currPos += 2044;
//Write the direct indeces
if ( byteLength <= 2044 ) {
return;
}
while ( nfbCounter < nfb.length - 1 ) {
freeBlock = getFreeBlock();
incrementFreeBlock();
file.seek(currPos);
file.writeInt(freeBlock);
currPos += 4;
//Iterate to block
c = freeBlock;
int indicePos = 8192;
while ( c > 2 ) {
indicePos += 4096;
c--;
}
//Write 4096 bytes to block
file.seek(indicePos);
c = 0;
while ( c < 4096 ) {
if ( nfbCounter > nfb.length - 1 ) {
break;
}
file.writeByte((int)nfb[nfbCounter]);
c++;
nfbCounter++;
}
}
}
}