-
Notifications
You must be signed in to change notification settings - Fork 2
/
Superblock.java
146 lines (120 loc) · 4.49 KB
/
Superblock.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
import java.nio.*;
/**
*This class reads the data of an ext2 filesystem's SuperBlock
*@author Petros Soutzis, 2017-19
*/
public class Superblock {
//the magic number which should always be 0xef53 for an ext2 filesystem
private short sMagic;
//Total number of inodes in filesystem
private int inodeCount;
//Total number of blocks in filesystem
private int blockCount;
//Number of blocks per Group
private int blocksPerGroup;
//Number of inodes per Group
private int inodesPerGroup;
//Size of each inode in bytes
private int sInodeSize;
//Volume label (disk name)
private String volumeName;
//ByteBuffer, where the bytes to be readBytes, will be parsed to
private ByteBuffer buffer;
/**
*Constructor of the Superblock class
*@param bytes is the byte array that contains the Superblock's data
*/
public Superblock(byte[] bytes) {
buffer = ByteBuffer.wrap(bytes);
buffer.order(ByteOrder.LITTLE_ENDIAN);
read(); //init
}
/**
*This method will readBytes all the data that is contained in the Superblock,
*from the buffer in which the byte array was parsed
*/
private void read() {
sMagic = buffer.getShort(Constants.S_MAGIC_OFFSET);
inodeCount = buffer.getInt(Constants.S_INODE_COUNT_OFFSET);
blockCount = buffer.getInt(Constants.S_BLOCK_COUNT_OFFSET);
blocksPerGroup = buffer.getInt(Constants.S_BLOCKS_PER_GROUP_OFFSET);
inodesPerGroup = buffer.getInt(Constants.S_INODES_PER_GROUP_OFFSET);
sInodeSize = buffer.getInt(Constants.S_INODE_SIZE_OFFSET);
//Get the Volume name
byte[] char_bytes = new byte[Constants.S_FILESYSTEM_NAME_LENGTH];
for(int i = 0; i< Constants.S_FILESYSTEM_NAME_LENGTH; i++) {
//Get the characters 1 by 1
char_bytes[i]=buffer.get(Constants.S_FILESYSTEM_NAME_OFFSET + i);
}
//converting the bytes that contain the name to a String
volumeName = new String(char_bytes);
}
/**
*@return The total size of each Inode
*/
int getInodeSize() {
return sInodeSize;
}
/**
*@return The total number of inodes in filesystem
*/
int getInodeCount() {
return inodeCount;
}
/**
*@return The total number of inodes in every block group
*/
int getInodesPerGroup() {
return inodesPerGroup;
}
/**
*@return The total number of blocks in filesystem
*/
int getBlockCount() {
return blockCount;
}
/**
*@return The total number of blocks in every block group
*/
int getBlocksPerGroup() {
return blocksPerGroup;
}
/**
*@return The disk's name
*/
String getVolumeName() {
return volumeName;
}
String shortToHex(Short s){
String transformed = Integer.toHexString(s-0xffff0000); //remove first 16 bits, to get the value of short 's'
return "0x" + transformed;
}
/**
*@param num_of_blocks the total number of blocks in the filesystem
*@param blocks_per_group the total number of blocks in every block group
*@return The total number of block groups that the volume has
*/
int getBlockGroupCount(int num_of_blocks, int blocks_per_group) {
//Get the total number of blocks, divided with the number of blocks per group
int count = num_of_blocks/blocks_per_group;
//If the remainder of the above division is not 0, then add "1" to the count.
if((num_of_blocks % blocks_per_group) != 0)
count++;
return count;
}
void printGenericData(int inodeSize, int blockGroupCount){
boolean correctMagicNum = Constants.MAGIC_NUM_VALUE == sMagic;
System.out.println("VOLUME NAME: " + volumeName);
System.out.print("Magic number is: " + shortToHex(sMagic));
if(correctMagicNum)
System.out.println(" -> Magic number is OK.");
else
System.out.println(" -> Magic number is INVALID, possible corruption of files.");
System.out.println("Total number of inodes is: " + inodeCount);
System.out.println("Total number of inodes per group is: " + inodesPerGroup);
System.out.println("Total size of inodes is: " + inodeSize);
System.out.println("Total number of blocks is: " + blockCount);
System.out.println("Total number of blocks per group is: " + blocksPerGroup);
System.out.println("Total number of block groups is: " + blockGroupCount);
}
}