-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.c
executable file
·305 lines (230 loc) · 8.26 KB
/
state.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
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "state.h"
#include "../er/error.h"
#include "../thr/threads.h"
#include "../tecnicofs-api-constants.h"
inode_t inode_table[INODE_TABLE_SIZE];
void lockInumberRead(int inumber){
lockReadRW(&inode_table[inumber].lockP);
}
void lockInumberWrite(int inumber){
lockWriteRW(&inode_table[inumber].lockP);
}
void unlockInumberRW(int inumber){
unlockRW(&inode_table[inumber].lockP);
}
void tryInumberRead(int inumber){
tryLockRead(&inode_table[inumber].lockP);
}
void tryInumberWrite(int inumber){
tryLockWrite(&inode_table[inumber].lockP);
}
pthread_rwlock_t* getLockInumber(int inumber){
return &inode_table[inumber].lockP;
}
/*
* Sleeps for synchronization testing.
*/
void insert_delay(int cycles) {
for (int i = 0; i < cycles; i++) {}
}
/*
* Initializes the i-nodes table.
*/
void inode_table_init() {
for (int i = 0; i < INODE_TABLE_SIZE; i++) {
inode_table[i].nodeType = T_NONE;
inode_table[i].data.dirEntries = NULL;
inode_table[i].data.fileContents = NULL;
initLockRW(&inode_table[i].lockP);
}
}
/*
* Releases the allocated memory for the i-nodes tables.
*/
void inode_table_destroy() {
for (int i = 0; i < INODE_TABLE_SIZE; i++) {
if (inode_table[i].nodeType != T_NONE) {
/* as data is an union, the same pointer is used for both dirEntries and fileContents */
/* just release one of them */
if (inode_table[i].data.dirEntries)
free(inode_table[i].data.dirEntries);
destroyRW(&inode_table[i].lockP);
}
}
}
/*
* Creates a new i-node in the table with the given information.
* Input:
* - nType: the type of the node (file or directory)
* Returns:
* inumber: identifier of the new i-node, if successfully created
* FAIL: if an error occurs
*/
int inode_create(type nType) {
/* Used for testing synchronization speedup */
insert_delay(DELAY);
for (int inumber = 0; inumber < INODE_TABLE_SIZE; inumber++) {
if(tryLockRead(&inode_table[inumber].lockP)!=0){
continue;
}
if (inode_table[inumber].nodeType == T_NONE){
unlockRW(&inode_table[inumber].lockP);
lockWriteRW(&inode_table[inumber].lockP);
if (inode_table[inumber].nodeType == T_NONE) {
inode_table[inumber].nodeType = nType;
if (nType == T_DIRECTORY) {
/* Initializes entry table */
inode_table[inumber].data.dirEntries = malloc(sizeof(DirEntry) * MAX_DIR_ENTRIES);
for (int i = 0; i < MAX_DIR_ENTRIES; i++) {
inode_table[inumber].data.dirEntries[i].inumber = FREE_INODE;
}
}
else {
inode_table[inumber].data.fileContents = NULL;
}
}
unlockRW(&inode_table[inumber].lockP);
return inumber;
}
unlockRW(&inode_table[inumber].lockP);
}
return FAIL;
}
/*
* Deletes the i-node.
* Input:
* - inumber: identifier of the i-node
* Returns: SUCCESS or FAIL
*/
int inode_delete(int inumber) {
/* Used for testing synchronization speedup */
insert_delay(DELAY);
if ((inumber < 0) || (inumber > INODE_TABLE_SIZE) || (inode_table[inumber].nodeType == T_NONE)) {
printf("inode_delete: invalid inumber\n");
return FAIL;
}
inode_table[inumber].nodeType = T_NONE;
/* see inode_table_destroy function */
if (inode_table[inumber].data.dirEntries)
free(inode_table[inumber].data.dirEntries);
destroyRW(&inode_table[inumber].lockP);
return SUCCESS;
}
/*
* Copies the contents of the i-node into the arguments.
* Only the fields referenced by non-null arguments are copied.
* Input:
* - inumber: identifier of the i-node
* - nType: pointer to type
* - data: pointer to data
* Returns: SUCCESS or FAIL
*/
int inode_get(int inumber, type *nType, union Data *data) {
/* Used for testing synchronization speedup */
insert_delay(DELAY);
if ((inumber < 0) || (inumber > INODE_TABLE_SIZE) || (inode_table[inumber].nodeType == T_NONE)) {
printf("inode_get: invalid inumber %d\n", inumber);
return FAIL;
}
if (nType)
*nType = inode_table[inumber].nodeType;
if (data)
*data = inode_table[inumber].data;
return SUCCESS;
}
/*
* Resets an entry for a directory.
* Input:
* - inumber: identifier of the i-node
* - sub_inumber: identifier of the sub i-node entry
* Returns: SUCCESS or FAIL
*/
int dir_reset_entry(int inumber, int sub_inumber) {
/* Used for testing synchronization speedup */
insert_delay(DELAY);
if ((inumber < 0) || (inumber > INODE_TABLE_SIZE) || (inode_table[inumber].nodeType == T_NONE)) {
printf("inode_reset_entry: invalid inumber\n");
return FAIL;
}
if (inode_table[inumber].nodeType != T_DIRECTORY) {
printf("inode_reset_entry: can only reset entry to directories\n");
return FAIL;
}
if ((sub_inumber < FREE_INODE) || (sub_inumber > INODE_TABLE_SIZE) || (inode_table[sub_inumber].nodeType == T_NONE)) {
printf("inode_reset_entry: invalid entry inumber\n");
return FAIL;
}
for (int i = 0; i < MAX_DIR_ENTRIES; i++) {
if (inode_table[inumber].data.dirEntries[i].inumber == sub_inumber) {
inode_table[inumber].data.dirEntries[i].inumber = FREE_INODE;
inode_table[inumber].data.dirEntries[i].name[0] = '\0';
return SUCCESS;
}
}
return FAIL;
}
/*
* Adds an entry to the i-node directory data.
* Input:
* - inumber: identifier of the i-node
* - sub_inumber: identifier of the sub i-node entry
* - sub_name: name of the sub i-node entry
* Returns: SUCCESS or FAIL
*/
int dir_add_entry(int inumber, int sub_inumber, char *sub_name) {
/* Used for testing synchronization speedup */
insert_delay(DELAY);
if ((inumber < 0) || (inumber > INODE_TABLE_SIZE) || (inode_table[inumber].nodeType == T_NONE)) {
printf("inode_add_entry: invalid inumber\n");
return FAIL;
}
if (inode_table[inumber].nodeType != T_DIRECTORY) {
printf("inode_add_entry: can only add entry to directories\n");
return FAIL;
}
if ((sub_inumber < 0) || (sub_inumber > INODE_TABLE_SIZE) || (inode_table[sub_inumber].nodeType == T_NONE)) {
printf("inode_add_entry: invalid entry inumber\n");
return FAIL;
}
if (strlen(sub_name) == 0 ) {
printf("inode_add_entry: \
entry name must be non-empty\n");
return FAIL;
}
for (int i = 0; i < MAX_DIR_ENTRIES; i++) {
if (inode_table[inumber].data.dirEntries[i].inumber == FREE_INODE) {
inode_table[inumber].data.dirEntries[i].inumber = sub_inumber;
strcpy(inode_table[inumber].data.dirEntries[i].name, sub_name);
return SUCCESS;
}
}
return FAIL;
}
/*
* Prints the i-nodes table.
* Input:
* - inumber: identifier of the i-node
* - name: pointer to the name of current file/dir
*/
void inode_print_tree(FILE *fp, int inumber, char *name) {
if (inode_table[inumber].nodeType == T_FILE) {
fprintf(fp, "%s\n", name);
return;
}
if (inode_table[inumber].nodeType == T_DIRECTORY) {
fprintf(fp, "%s\n", name);
for (int i = 0; i < MAX_DIR_ENTRIES; i++) {
if (inode_table[inumber].data.dirEntries[i].inumber != FREE_INODE) {
char path[MAX_FILE_NAME];
if (snprintf(path, sizeof(path), "%s/%s", name, inode_table[inumber].data.dirEntries[i].name) > sizeof(path)) {
fprintf(stderr, "truncation when building full path\n");
}
inode_print_tree(fp, inode_table[inumber].data.dirEntries[i].inumber, path);
}
}
}
}