-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfstree.c
261 lines (210 loc) · 5.7 KB
/
fstree.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fstree.h"
#include "filesystem.h"
// Helper for delete_inode_list()
static void delete_inode(index_node* inode);
fs_entry* new_fs_entry(char* name, file_type type, user_id user,
permissions perms, unsigned int size_bytes) {
size_t nmlen; // Length of name
fs_entry* new_entry; // Return value
// Sanity check: name is short enough?
nmlen = strlen(name);
if (nmlen > MAX_NM_LEN - 1) {
// Too long.
return NULL;
}
// Allocate
new_entry = (fs_entry*)malloc(sizeof(fs_entry));
// Initialize
strcpy(new_entry->name, name);
new_entry->type = type;
new_entry->user = user;
new_entry->perms= perms;
new_entry->inode_head = new_entry->inode_tail = NULL;
if (type == DIRY) {
new_entry->size_bytes = 0;
new_entry->size_blocks = 0;
} else {
new_entry->size_bytes = size_bytes;
new_entry->size_blocks = size_bytes/BLOCK_SIZE;
// Account for possible partially occupied block
if (size_bytes % BLOCK_SIZE != 0) {
new_entry->size_blocks++;
}
}
return new_entry;
}
fs_node* new_fs_node(fs_entry* entry, fs_node* parent) {
fs_node* new_node; // Return value
// Allocate/initialize
new_node = (fs_node*)malloc(sizeof(fs_node));
new_node->entry = entry;
memset(new_node->children, 0, sizeof(new_node->children));
new_node->num_children = 0;
new_node->parent = parent;
// Check for special case: creating root node
if (parent == NULL) {
return new_node;
}
// Insert the new child
insert_child(new_node, parent);
return new_node;
}
void delete_fs_node(fs_node** nodepp) {
fs_node* nodeptr = *nodepp; // For readability
// XXX: Up to caller to free nodes in child list; easier this way since
// there's other cleanup that needs to be done when files are deleted
// Delete the index node list
delete_inode_list(nodeptr->entry);
// Delete the file entry
free(nodeptr->entry);
// Delete this
free(nodeptr);
// Invalidate the pointer to node
*nodepp = NULL;
}
fs_node* new_fs_tree() {
fs_entry* dir_entry; // The directory entry
printf("new_fs_tree(): Initializing filesystem tree...\n");
dir_entry = new_fs_entry(
"root",
DIRY,
ROOT_USER_ID,
RDWR,
0
);
return new_fs_node(dir_entry, NULL);
}
void insert_inode(unsigned int block_index, unsigned int offset,
fs_entry* entry) {
index_node* new_inode; // New inode
// Allocate/initialize new index node
new_inode = (index_node*)malloc(sizeof(index_node));
new_inode->index = block_index;
new_inode->offset = offset;
new_inode->next = NULL;
// Check for special case: first insertion, i.e. tail is NULL
if (entry->inode_tail == NULL) {
entry->inode_head = entry->inode_tail = new_inode;
} else {
entry->inode_tail->next = new_inode;
entry->inode_tail = new_inode;
}
}
index_node* remove_inode_tail(fs_entry* entry) {
index_node* node = entry->inode_head; // Temp node for traversing
index_node* old_tail = entry->inode_tail; // Return value
// Sanity check; for debuggning
if (entry == NULL || node == NULL || old_tail == NULL) {
return NULL;
}
// Special case: only one node
if (node->next == NULL) {
entry->inode_head = entry->inode_tail = NULL;
return old_tail;
}
// Traverse to 2nd to last node
while (node->next != entry->inode_tail) {
node++;
}
// Unlink the tail
entry->inode_tail = node;
node->next = NULL;
return old_tail;
}
int insert_child(fs_node* new_child, fs_node* dir) {
// Sanity check
if (new_child == NULL || dir == NULL) {
fprintf(stderr, "insert_child(): null ptr!!\n");
return -1;
}
// Link to parent; check for space first
if (dir->num_children == MAX_DIR_FILES) {
// No space
fprintf(stderr, "insert_child(): no space in parent's children list!");
return -1;
}
dir->children[dir->num_children] = new_child;
dir->num_children++;
return 0;
}
void unlink_child(fs_node* child) {
fs_node* parent = child->parent; // For convenience
unsigned int index = 0; // Index of child in parent's children
// Look through each of the children
for (unsigned int c = 0; c < parent->num_children; c++) {
if (parent->children[c] == child) {
// Found it; store the index
index = c;
break;
}
}
// Shift over the rest
for (unsigned int c = index; c < parent->num_children - 1; c++) {
parent->children[c] = parent->children[c + 1];
}
// Update count, parent
parent->num_children--;
child->parent = NULL;
}
void delete_inode_list(fs_entry* entry) {
// Check for empty list
if (entry->inode_head == NULL) {
return;
}
// Recursively delete nodes
delete_inode(entry->inode_head);
}
static void delete_inode(index_node* inode) {
if (inode->next != NULL) {
delete_inode(inode->next);
}
free(inode);
}
void dump_path(fs_node* node) {
// Print the parent before this
if (node->parent != NULL) {
dump_path(node->parent);
printf("/");
}
printf("%s", node->entry->name);
}
void dump_properties(fs_entry* entry) {
char* perms; // Permissions string for the entry
char* type; // Filetype string for the entry
// Get the permissions string
switch (entry->perms) {
case READ:
perms = "read";
break;
case RDWR:
perms = "rdwr";
break;
default:
perms = "syml";
}
// Get the filetype string
switch (entry->type) {
case TXT:
type = "txt";
break;
case EXE:
type = "exe";
break;
case IMG:
type = "img";
break;
case DOC:
type = "doc";
break;
case MOV:
type = "mov";
break;
default:
type = "dir";
}
printf("%10s%6s%5s%8u%8u\n", entry->name, perms, type, entry->user,
entry->size_bytes);
}