-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutilities.c
More file actions
440 lines (386 loc) · 13.8 KB
/
utilities.c
File metadata and controls
440 lines (386 loc) · 13.8 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <time.h>
#include <assert.h>
#include "ext2.h"
#include "utilities.h"
// get superblock, group descriptor
struct ext2_super_block *getSuperblock() {
return (struct ext2_super_block *)(disk+EXT2_BLOCK_SIZE);
}
struct ext2_group_desc *getGroupDesc() {
return (struct ext2_group_desc *)(disk+2*EXT2_BLOCK_SIZE);
}
// bitmaps, bit
char unsigned *getBitmap(int bitmapNum) {
struct ext2_group_desc *gd = getGroupDesc();
if (bitmapNum == INODE_BITMAP)
return (char unsigned *)(disk+gd->bg_inode_bitmap*EXT2_BLOCK_SIZE);
else if (bitmapNum == BLOCK_BITMAP)
return (char unsigned *)(disk+gd->bg_block_bitmap*EXT2_BLOCK_SIZE);
return NULL;
}
int getBit(char unsigned *bitmap, int index) {
return (bitmap[index/8]>>index%8)&1;
}
int getFirstEmptyBitIndex(int bitmapNum) {
int index, maxLength;
unsigned char *bitmap;
if (bitmapNum == INODE_BITMAP) {
index = EXT2_GOOD_OLD_FIRST_INO;
maxLength = getSuperblock()->s_inodes_count;
bitmap = getBitmap(INODE_BITMAP);
} else {
index = 0;
maxLength = getSuperblock()->s_blocks_count;
bitmap = getBitmap(BLOCK_BITMAP);
}
while (index < maxLength) {
if (getBit(bitmap, index) == 0) {
return index;
}
index++;
}
return -1;
}
void changeBitmap(char unsigned *bitmap, int idx, char mode) {
int turn_on = 00000001;
int turn_off = 00000001;
turn_on = turn_on << idx%8;
turn_off = ~(turn_off << idx%8);
if (mode == 'a'){
// turning on the bit
bitmap[idx/8] = bitmap[idx/8] | turn_on;
}else{
// turning off the bit
bitmap[idx/8] = bitmap[idx/8] & turn_off;
}
}
// inode
struct ext2_inode *getInodeTable() {
struct ext2_group_desc *gd = getGroupDesc();
return (struct ext2_inode *)(disk+gd->bg_inode_table*EXT2_BLOCK_SIZE);
}
struct ext2_inode *getInode(int inodeNum) {
struct ext2_inode *inodeTable = getInodeTable();
return &inodeTable[inodeNum-1];
}
/**
* return new intialized inode number
*/
int initInode(unsigned short mode) {
// find the first free inode
int index = getFirstEmptyBitIndex(INODE_BITMAP);
// change its bitmap and update field in gd
char unsigned *bitmap = getBitmap(INODE_BITMAP);
changeBitmap(bitmap, index, 'a');
getGroupDesc()->bg_free_inodes_count--;
// initialize inode attribute
struct ext2_inode *inode_table = getInodeTable();
inode_table[index].i_mode = mode;
inode_table[index].i_size = 1024;
inode_table[index].i_links_count = 1;
inode_table[index].i_blocks = 2;
for(int i=0; i<15; i++) {
inode_table[index].i_block[i] = 0;
}
// set creation time for this inode
inode_table[index].i_ctime = time(NULL);
return index+1;
}
void deleteInode(int inodeNum) {
char unsigned *inode_bitmap = getBitmap(INODE_BITMAP);
char unsigned *block_bitmap = getBitmap(BLOCK_BITMAP);
// delete inode
changeBitmap(inode_bitmap, inodeNum-1, 'd');
getGroupDesc()->bg_free_inodes_count++;
struct ext2_inode *inode_table = getInodeTable();
struct ext2_inode *target = &inode_table[inodeNum-1];
target->i_dtime = time(NULL);
// delete block
int i;
for(i = 0; i<12;i++) {
if (target->i_block[i] != 0) {
changeBitmap(block_bitmap, target->i_block[i]-1, 'd');
getGroupDesc()->bg_free_blocks_count++;
}
}
// delete single indirect
int bp = target->i_block[12];
if (bp != 0)
{
// delete blocks in single
unsigned int *single = (unsigned int*)getBlock(bp);
for (int i=0; i<EXT2_BLOCK_SIZE/4; i++) {
if (single[i] != 0) {
changeBitmap(block_bitmap, single[i]-1, 'd');
getGroupDesc()->bg_free_blocks_count++;
}
}
// delte single itself
changeBitmap(block_bitmap, target->i_block[12]-1, 'd');
getGroupDesc()->bg_free_blocks_count++;
}
}
// block
char unsigned *getBlock(int blockNum) {
// block index start at 1, so block Number == block Index
// since "block[0]" is allocated for superblock
return (char unsigned*)(disk+blockNum*EXT2_BLOCK_SIZE);
}
/**
* return new allocated block number
*/
int allocateNewBlock() {
int index = getFirstEmptyBitIndex(BLOCK_BITMAP);
changeBitmap(getBitmap(BLOCK_BITMAP), index, 'a');
getGroupDesc()->bg_free_blocks_count--;
return index+1;
}
// dir_entry
int searchFileInDir(struct ext2_inode *parentInode, char *childFileName) {
/**
* return inode num if childFile is found, o/w 0
*/
struct ext2_dir_entry_2 *cur_dir_entry = NULL;
struct ext2_dir_entry_2 *pre_dir_entry = getPreDirent(parentInode, childFileName);
if (strcmp(childFileName, ".") == 0) {
return ((struct ext2_dir_entry_2 *)getBlock(parentInode->i_block[0]))->inode;
}
else if (pre_dir_entry != NULL) {
cur_dir_entry = (void *)pre_dir_entry + pre_dir_entry->rec_len;
return cur_dir_entry->inode;
}
return 0;
}
int calculateActuralSize(struct ext2_dir_entry_2 *dirent) {
return ((sizeof(struct ext2_dir_entry_2)+(dirent->name_len+4))/4)*4;
}
struct ext2_dir_entry_2 *allocateNewDirent(struct ext2_inode *parent_inode, int size) {
struct ext2_dir_entry_2 *new_dir_entry = NULL;
// search in all used direct block
for(int i = 0; i<12;i++) {
if (parent_inode->i_block[i] != 0) {
new_dir_entry = allocateDirentHelper(parent_inode->i_block[i], size);
if (new_dir_entry!=NULL)
return new_dir_entry;
}
}
// if we cannot find a space, try to allocate a new block
int newBlockNum = 0;
for(int i = 0; i<12;i++) {
if (parent_inode->i_block[i] != 0) continue;
newBlockNum = allocateNewBlock();
parent_inode->i_block[i] = newBlockNum;
// increse parentdir size
parent_inode->i_blocks+=(EXT2_BLOCK_SIZE+511)/512;
parent_inode->i_size+=EXT2_BLOCK_SIZE;
// insert dummy head
new_dir_entry = (struct ext2_dir_entry_2 *)getBlock(newBlockNum);
new_dir_entry->file_type = EXT2_FT_UNKNOWN;
new_dir_entry->inode = 0;
new_dir_entry->name_len = 0;
new_dir_entry->rec_len = ((sizeof(struct ext2_dir_entry_2)+3)/4)*4;
// return new setted dir_entry
new_dir_entry = (void *)new_dir_entry + new_dir_entry->rec_len;
new_dir_entry->rec_len=EXT2_BLOCK_SIZE-((sizeof(struct ext2_dir_entry_2)+3)/4)*4;
return new_dir_entry;
}
return NULL;
}
struct ext2_dir_entry_2 *allocateDirentHelper(int blockNum, int size) {
/*
* Helper function for initDirent
*/
struct ext2_dir_entry_2 *dir_entry, *new_dir_entry;
int total_rec_len, actural_len, residue_len;
dir_entry = (struct ext2_dir_entry_2 *)getBlock(blockNum);
total_rec_len = 0;
while (total_rec_len < EXT2_BLOCK_SIZE) {
// calculate residue
actural_len = calculateActuralSize(dir_entry);
residue_len = dir_entry->rec_len - actural_len;
if (residue_len >= size) {
new_dir_entry = (void *)dir_entry+actural_len;
dir_entry->rec_len=actural_len;
new_dir_entry->rec_len=residue_len;
return new_dir_entry;
}
total_rec_len = total_rec_len + dir_entry->rec_len;
dir_entry = (void *) dir_entry + dir_entry->rec_len;
}
return NULL;
}
struct ext2_dir_entry_2 *initNewDirent(struct ext2_inode *parentInode, int childInodeNum, int type, char *fileName) {
int name_len, size;
struct ext2_dir_entry_2 *newDirent;
// calculate actual size required for new dir_entry
name_len = strlen(fileName);
size = sizeof(struct ext2_dir_entry_2) + ((name_len+4)/4)*4;
// allocate new dir_entry in parent directory
newDirent = allocateNewDirent(parentInode, size);
// initialize new dir_entry
newDirent->inode = childInodeNum;
newDirent->file_type = type;
newDirent->name_len = (unsigned char) name_len;
strcpy(newDirent->name, fileName);
return newDirent;
}
unsigned int *initSingleIndirect(int blockNum) {
unsigned int *singleIndirect = (unsigned int *)getBlock(blockNum);
for (int i=0; i<(EXT2_BLOCK_SIZE/4); i++) {
singleIndirect[i] = 0;
}
return singleIndirect;
}
// path handling
int getInodeFromPath(char *path) {
/*
* return: inodeNum of the path, 0 if path is not found or invalid
*/
struct ext2_inode *inodeTable = getInodeTable();
int inode_num = EXT2_ROOT_INO;
struct ext2_inode *cur_inode = &inodeTable[inode_num-1];
char *next_file;
int endWithDir = path[strlen(path)-1] == '/'; // is path endwith '/' ?
if (path[0] != '/') {
// invalid path: absolute path startwith
return 0;
}
// travel through path
next_file = strtok(path, "/");
while(next_file != NULL) {
// cannot have a non-directory type file in the middle of path
if (!(cur_inode->i_mode & EXT2_S_IFDIR)) {
// Invalid path; non-dir type file inside path
return 0;
}
// get next inode from current directory
inode_num = searchFileInDir(cur_inode, next_file);
// update inode to next file with next_file
if (inode_num != 0) {
cur_inode = &inodeTable[inode_num-1];
} else {
// invalid path: file name not found
return 0;
}
next_file = strtok(NULL, "/");
}
if (!(cur_inode->i_mode & EXT2_S_IFDIR) && endWithDir) {
// invalid path: path endwith '/' but have a non-dir type file at the end
return 0;
}
return inode_num;
}
void getFileNameFromPath(char *fileName, char *path) {
/*
* return the last section of path into fileName
*/
char *curr = strtok(path, "/");
while (curr != NULL) {
strcpy(fileName, curr);
curr = strtok(NULL, "/");
}
}
void getParentDirPath(char *path) {
/*
* modify input path
*/
assert (strcmp(path, "/")!=0);
int len = strlen(path);
if (path[len-1]=='/')
path[len-1] = '\0';
char *target = strrchr(path, '/');
*(target+1) = '\0';
}
struct ext2_dir_entry_2 *getPreDirent(struct ext2_inode *parentInode, char *childFileName) {
struct ext2_dir_entry_2 * pre_dir_entry;
struct ext2_dir_entry_2 * cur_dir_entry;
int total_rec_len;
// search in direct block
for (int i=0; i<12; i++) {
if (parentInode->i_block[i] == 0) {
continue;
} else {
pre_dir_entry = (struct ext2_dir_entry_2 *)getBlock(parentInode->i_block[i]);
total_rec_len = pre_dir_entry->rec_len;
cur_dir_entry = (void *) pre_dir_entry + pre_dir_entry->rec_len;
}
// for each dir entry in the block
while (total_rec_len < EXT2_BLOCK_SIZE) {
if(strcmp(cur_dir_entry->name, childFileName)==0) {
return pre_dir_entry;
}
total_rec_len = total_rec_len + cur_dir_entry->rec_len;
pre_dir_entry = cur_dir_entry;
cur_dir_entry = (void *) cur_dir_entry + cur_dir_entry->rec_len;
}
}
return NULL;
}
void rm(struct ext2_inode *parentInode, char *childFileName) {
struct ext2_dir_entry_2 *pre_dir_entry = NULL;
struct ext2_dir_entry_2 *cur_dir_entry = NULL;
struct ext2_dir_entry_2 *child_dir_entry = NULL;
struct ext2_inode *childInode = NULL;
int total_rec_len;
// delete childFile from parentDir and get childInode
pre_dir_entry = getPreDirent(parentInode, childFileName);
cur_dir_entry = (void *)pre_dir_entry + pre_dir_entry->rec_len;
childInode = getInode(cur_dir_entry->inode);
// base case1, if childInode is Symbolic link
if (cur_dir_entry->file_type == EXT2_FT_SYMLINK)
{
if (childInode->i_size > 60) {
changeBitmap(getBitmap(BLOCK_BITMAP), childInode->i_block[0]-1, 'd');
getGroupDesc()->bg_free_blocks_count++;
}
changeBitmap(getBitmap(INODE_BITMAP), cur_dir_entry->inode-1, 'd');
getGroupDesc()->bg_free_inodes_count++;
}
// base case2, if childInode is a file
else if (cur_dir_entry->file_type == EXT2_FT_REG_FILE)
{
// if link count == 0 remove inode
if (childInode->i_links_count == 1) {
deleteInode(cur_dir_entry->inode);
} else {
childInode->i_links_count--;
}
}
// recursive case, if childInode is a dir
else if (cur_dir_entry->file_type == EXT2_FT_DIR)
{
// reduce link count for self and parent (. and ..)
childInode->i_links_count--;
parentInode->i_links_count--;
// for each file name (other than . and ..) in child dir, call recursion
for (int i=0; i<12; i++) {
if (childInode->i_block[i] == 0)
continue;
child_dir_entry = (struct ext2_dir_entry_2 *)getBlock(childInode->i_block[i]);
total_rec_len = 0;
// for each dir entry in the block
while (total_rec_len < EXT2_BLOCK_SIZE) {
if (child_dir_entry->name_len != 0 &&
strcmp(child_dir_entry->name, ".")!=0 &&
strcmp(child_dir_entry->name, "..")!=0) {
rm(childInode, child_dir_entry->name);
}
total_rec_len = total_rec_len + child_dir_entry->rec_len;
child_dir_entry = (void *) child_dir_entry + child_dir_entry->rec_len;
}
}
// if child link count == 0 remove inode
if (childInode->i_links_count == 1) {
deleteInode(cur_dir_entry->inode);
getGroupDesc()->bg_used_dirs_count--;
}
}
pre_dir_entry->rec_len += cur_dir_entry->rec_len;
}