-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFSformat.java
58 lines (46 loc) · 1.34 KB
/
FSformat.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
package external_tools;
import java.io.*;
public class FSformat {
private RandomAccessFile file;
private int magic = 0x1234ABCD;
private long magicPosition = 0;
private long currPositon = 0;
private int directoryBlock = 1;
private long directoryPosition = 8;
private int firstFreeBlock = 2;
private long freePosition = 12;
public void start(String filename) {
try {
file = new RandomAccessFile(filename,"rw");
if ( testFormated() ) {
System.out.println("Already formated.");
}
else {
format();
}
}
catch ( IOException e ) {
System.out.print("IO Exception - BlockDevice file not found.");
}
}
private boolean testFormated() throws IOException {
file.seek(magicPosition);
int key = file.readInt(); //read and writes advances file pos pointer
if ( key == magic ) {
return true;
}
else {
return false;
}
}
private void format() throws IOException {
//Write magic number as the first 32bit word on the super block
file.seek(magicPosition);
file.writeInt(magic);
//Set directory block number as third 32bit word
file.seek(directoryPosition);
file.writeInt(directoryBlock);
//Set block number of start of free block chain as fourth 32bit word, which is after the directory block number in the super block
file.writeInt(firstFreeBlock);
}
}