-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInode.java
More file actions
252 lines (211 loc) · 6.25 KB
/
Copy pathInode.java
File metadata and controls
252 lines (211 loc) · 6.25 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
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
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
// TODO:
// add dates so it can be traced when the file was created, deleted and modified ;
/**
* Each object in the filesystem is represented by an inode.
*/
public class Inode {
/**
* Indicate the start point by which all other fields can be retrieved
*/
private int startingPoint;
/**
* Indicates the permission
*/
private int fileMode; // fileMode == permission
/**
* Indicates the user id of the user accessing this file
*/
private short userId;
/**
* Indicates the file size
*/
private int fileSize;
/**
* Indicates the time when the file was last accessed
*/
private int lastAccessTime;
/**
* Indicates the time when the file was created
*/
private int creationTime;
/**
* Indicates the time when the file was last modified
*/
private int lastModifiedTime;
/**
* Indicates when the when the file was deleted
*/
private int deletedTime;
/**
* Indicates the group id of the user accessing this file
*/
private short groupId;
/**
* Hard links allow you to have multiple "file names" that point to the same inode.
* numHardLinks specifies the number of hard links of the current file
*/
private short numHardLinks;
/**
* Contains file's data
*/
private int dataBlockPointers[];
/**
* Pointer to an indirect block which contains pointers to the next set of blocks
*/
private int indirectPointer;
/**
* Poitner to a doubly-linked block which contains pointers to indirect blocks
*/
private int doubleIndirectPointer;
/**
* Pointer to a trebly-indirect block which contains pointers to doubly-indirect blocks
*/
private int tripleIndirectPointer;
private boolean isFile;
private Ext2File file;
private ByteBuffer byteBuffer;
public Inode(int startingPoint, Ext2File file) {
this.startingPoint = startingPoint;
this.file = file;
process();
}
private void process() {
byteBuffer = ByteBuffer.wrap(file.read(startingPoint, Constants.INODE_TABLE_SIZE)).order(ByteOrder.LITTLE_ENDIAN);
fileMode = byteBuffer.getInt(Constants.FILE_MODE_OFFSET);
userId = byteBuffer.getShort(Constants.USER_ID_OFFSET);
fileSize = byteBuffer.getInt(Constants.FILE_SIZE_OFFSET);
lastAccessTime = byteBuffer.getInt(Constants.LAST_ACCESS_TIME_OFFSET);
creationTime = byteBuffer.getInt(Constants.CREATION_TIME_OFFSET);
lastModifiedTime = byteBuffer.getInt(Constants.LAST_MODIFIED_TIME_OFFSET);
deletedTime = byteBuffer.getInt(Constants.DELETED_TIME_OFFSET);
groupId = byteBuffer.getShort(Constants.GROUP_ID_OFFSET);
numHardLinks = byteBuffer.getShort(Constants.NUM_HARD_LINKS_OFFSET);
indirectPointer = byteBuffer.getInt(Constants.INDIRECT_POINTER_OFFSET);
doubleIndirectPointer = byteBuffer.getInt(Constants.DOUBLE_INDIRECT_POINTER_OFFSET);
tripleIndirectPointer = byteBuffer.getInt(Constants.TRIPLE_INDIRECT_POITNER_OFFSET);
dataBlockPointers = new int[12];
// Populate data block pointers
for (int i = 0; i < 12; i++) {
byteBuffer = ByteBuffer.wrap(file.read(startingPoint + Constants.DATA_BLOCKS_POINTER + (i * 4), 4)).order(ByteOrder.LITTLE_ENDIAN);
dataBlockPointers[i] = byteBuffer.getInt();
}
}
@Deprecated
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
/* File Type */
// d
if ((fileMode & 0x8000) == 0x8000) {
stringBuilder.append("-"); // file
isFile = true;
}
else if ((fileMode & 0x4000) == 0x4000) {
stringBuilder.append("d"); // directory
isFile = false;
}
/* User Permissions */
// drwx
if ((fileMode & 0x0100) == 0x0100)
stringBuilder.append("r"); // read
else
stringBuilder.append("-"); // non-read
if ((fileMode & 0x0080) == 0x0080)
stringBuilder.append("w"); // write
else
stringBuilder.append("-"); // non-write
if ((fileMode & 0x0040) == 0x0040)
stringBuilder.append("x"); // execute permission
else
stringBuilder.append("-"); // non-execution permission
/* Group Permissions */
// drwxr-x
if((fileMode & 0x0020) == 0x0020)
stringBuilder.append("r");
else
stringBuilder.append("-");
if((fileMode & 0x0010) == 0x0010)
stringBuilder.append("w");
else
stringBuilder.append("-");
if((fileMode & 0x0008) == 0x0008)
stringBuilder.append("x");
else
stringBuilder.append("-");
/* Other Permissions */
// drwxr-xr-x |STOP HERE|
if((fileMode & 0x0004) == 0x0004)
stringBuilder.append("r");
else
stringBuilder.append("-");
if((fileMode & 0x0002) == 0x0002)
stringBuilder.append("w");
else
stringBuilder.append("-");
if((fileMode & 0x0001) == 0x0001)
stringBuilder.append("x ");
else
stringBuilder.append("- ");
/* Hard Links */
stringBuilder.append(numHardLinks + " ");
/* User ID */
stringBuilder.append(userId + " ");
/* Group ID */
stringBuilder.append(groupId + " ");
/* File Size */
stringBuilder.append(fileSize + " ");
/* Dates */
Date lastModifiedInDateFormat = new Date((long) lastModifiedTime * 1000);
stringBuilder.append(lastModifiedInDateFormat.toString());
return stringBuilder.toString();
}
public int getStartingPoint() {
return this.startingPoint;
}
public int getFileMode() {
return this.fileMode;
}
public int getUserId() {
return this.userId;
}
public int getFileSize() {
return this.fileSize;
}
public int getLastAccessTime() {
return this.lastAccessTime;
}
public int getCreationTime() {
return this.creationTime;
}
public int getLastModifiedTime() {
return this.lastModifiedTime;
}
public int getDeletedTime() {
return this.deletedTime;
}
public int getGroupId() {
return this.groupId;
}
public int getNumHardLinks() {
return this.numHardLinks;
}
public int[] getDataBlockPointers() {
return this.dataBlockPointers;
}
public int getIndirectPointer() {
return this.indirectPointer;
}
public int getDoubleIndirectPointer() {
return this.doubleIndirectPointer;
}
public int getTripleIndirectPointer() {
return this.tripleIndirectPointer;
}
public boolean isFile() {
return isFile;
}
}