-
Notifications
You must be signed in to change notification settings - Fork 1
/
iolib.c
executable file
·560 lines (535 loc) · 14.8 KB
/
iolib.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#include <comp421/iolib.h>
#include <comp421/filesystem.h>
#include <stdio.h>
#include <string.h>
#include "utils.h"
#include "message.h"
typedef struct opened_file {
short type;
short inode;
int pos;
int reuse;
} OpenedFile;
void update_fd();
int check_pathname(char *pathname);
int check_fd(int fd);
void general_check(int result);
void short_check(int result);
static OpenedFile open_file_table[MAX_OPEN_FILES];
int next_fd = 0; //-1 if MAX_OPEN_FILES files are already opened
int current_dir_inode = ROOTINODE; //this should be changed back to ROOTINODE whenver directory reuse mismatch
int current_dir_reuse = 0;
extern int Open(char *pathname) {
if (check_pathname(pathname) || next_fd == -1) return ERROR;
MessageInode *msg = Malloc(32);
msg->message_type = INODE;
msg->inode = pathname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = pathname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(pathname);
msg->name_addr = (void *)pathname;
msg->sym_pursue = 1;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Open file %s failed: ", pathname);
general_check(msg->inode);
free(msg);
return ERROR;
}
open_file_table[next_fd].type = msg->file_type;
open_file_table[next_fd].inode = msg->inode;
open_file_table[next_fd].pos = 0;
open_file_table[next_fd].reuse = msg->reuse;
free(msg);
int res = next_fd;
update_fd();
return res;
}
extern int Close(int fd) {
if (check_fd(fd)) return ERROR;
if (open_file_table[fd].type == INODE_FREE) return ERROR;
open_file_table[fd].type = INODE_FREE;
if (next_fd == -1 || fd < next_fd) next_fd = fd;
return 0;
}
extern int Create(char *pathname) {
if (check_pathname(pathname) || next_fd == -1) return ERROR;
MessageCreate *msg = Malloc(32);
msg->message_type = CREATE;
msg->inode = pathname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = pathname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(pathname);
msg->name_addr = (void *)pathname;
msg->type = INODE_REGULAR;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Create file %s failed: ", pathname);
general_check(msg->inode);
switch(msg->inode) {
case DIR_EXIST: {
fprintf(stderr, "%s already exists as a directory\n", pathname);
break;
}
case NO_INODE: {
fprintf(stderr, "there is no more free inode for new file %s\n", pathname);
break;
}
}
free(msg);
return ERROR;
}
open_file_table[next_fd].type = INODE_REGULAR;
open_file_table[next_fd].inode = msg->inode;
open_file_table[next_fd].pos = 0;
open_file_table[next_fd].reuse = msg->reuse;
free(msg);
int res = next_fd;
update_fd();
return res;
}
extern int Read(int fd, void *buf, int size) {
if (check_fd(fd)) return ERROR;
if (open_file_table[fd].type == INODE_FREE) return ERROR;
MessageIO *msg = Malloc(32);
msg->message_type = IO;
msg->inode = open_file_table[fd].inode;
msg->reuse = open_file_table[fd].reuse;
msg->len = size;
msg->buf = buf;
msg->read = 1;
msg->pos = open_file_table[fd].pos;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Read file %d failed: ", fd);
short_check(msg->inode);
free(msg);
return ERROR;
}
open_file_table[fd].pos += msg->len;
int res = msg->len;
free(msg);
return res;
}
//ensure type is not dir
extern int Write(int fd, void *buf, int size) {
if (check_fd(fd)) return ERROR;
if (open_file_table[fd].type == INODE_FREE || open_file_table[fd].type == INODE_DIRECTORY) return ERROR;
MessageIO *msg = Malloc(32);
msg->message_type = IO;
msg->inode = open_file_table[fd].inode;
msg->reuse = open_file_table[fd].reuse;
msg->len = size;
msg->buf = buf;
msg->read = 0;
msg->pos = open_file_table[fd].pos;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Write file %d failed: ", fd);
short_check(msg->inode);
free(msg);
return ERROR;
}
open_file_table[fd].pos += msg->len;
int res = msg->len;
free(msg);
return res;
}
extern int Seek(int fd, int offset, int whence) {
if (check_fd(fd)) return ERROR;
if (open_file_table[fd].type == INODE_FREE) return ERROR;
MessageSeek *msg = Malloc(32);
msg->message_type = SEEK;
msg->inode = open_file_table[fd].inode;
msg->reuse = open_file_table[fd].reuse;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Get file size %d failed: ", fd);
short_check(msg->inode);
free(msg);
return ERROR;
}
int size = msg->size;
free(msg);
switch(whence) {
case SEEK_SET:
if (offset < 0) {
fprintf(stderr, "offset must be non-negative for SEEK_SET\n");
return ERROR;
}
open_file_table[fd].pos = offset;
return open_file_table[fd].pos;
case SEEK_CUR:
if (open_file_table[fd].pos + offset < 0) {
fprintf(stderr, "Seek beyond the beginning of the file\n");
return ERROR;
}
open_file_table[fd].pos += offset;
return open_file_table[fd].pos;
case SEEK_END:
if (offset > 0) {
fprintf(stderr, "offset must be non-positive for SEEK_END\n");
return ERROR;
}
if (size + offset < 0) {
fprintf(stderr, "Seek beyond the beginning of the file\n");
return ERROR;
}
open_file_table[fd].pos = size + offset;
return open_file_table[fd].pos;
}
fprintf(stderr, "Invalid whence value: %d\n", whence);
return ERROR;
}
extern int Link(char *oldname, char *newname) {
if (check_pathname(oldname) || check_pathname(newname)) return ERROR;
MessageInode *msg = Malloc(32);
msg->message_type = INODE;
msg->inode = oldname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = oldname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(oldname);
msg->name_addr = (void *)oldname;
msg->sym_pursue = 1;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Problematic old name: %s: ", oldname);
general_check(msg->inode);
free(msg);
return ERROR;
}
if (msg->file_type == INODE_DIRECTORY) {
fprintf(stderr, "Cannot link to a directory: %s\n", oldname);
free(msg);
return ERROR;
}
short target_inode = msg->inode;
int target_reuse = msg->reuse;
free(msg);
MessageLink *msg_link = Malloc(32);
msg_link->message_type = LINK;
msg_link->inode = newname[0]=='/'?ROOTINODE:current_dir_inode;
msg_link->reuse = newname[0]=='/'?0:current_dir_reuse;
msg_link->name_len = strlen(newname);
msg_link->name_addr = (void *)newname;
msg_link->target_inode = target_inode;
msg_link->target_reuse = target_reuse;
Send((void *)msg_link, -FILE_SERVER);
if (msg_link->inode < 0) {
fprintf(stderr, "Add link %s failed: ", newname);
general_check(msg_link->inode);
if (msg_link->inode == ENTRY_EXIST) {
fprintf(stderr, "the entry name already exist\n");
}
free(msg_link);
return ERROR;
}
free(msg_link);
return 0;
}
extern int Unlink(char *pathname) {
if (check_pathname(pathname)) return ERROR;
MessageUnlink *msg = Malloc(32);
msg->message_type = UNLINK;
msg->inode = pathname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = pathname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(pathname);
msg->name_addr = (void *)pathname;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Failed to unlink %s: ", pathname);
general_check(msg->inode);
switch(msg->inode) {
case UNLINK_REMOVE_DOT: {
fprintf(stderr, "cannot remove entry . and entry ..\n");
break;
}
case LINK_DIRECTORY: {
fprintf(stderr, "cannot remove link from directory\n");
break;
}
}
free(msg);
return ERROR;
}
return 0;
}
extern int SymLink(char *oldname, char *newname) {
if (check_pathname(oldname) || check_pathname(newname) || strlen(oldname) == 0) return ERROR;
MessageCreate *msg = Malloc(32);
msg->message_type = CREATE;
msg->inode = newname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = newname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(newname);
msg->name_addr = (void *)newname;
msg->type = INODE_SYMLINK;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Create symbolic link %s failed: ", newname);
general_check(msg->inode);
switch(msg->inode) {
case DIR_EXIST: {
fprintf(stderr, "%s already exists as a directory\n", newname);
break;
}
case NO_INODE: {
fprintf(stderr, "there is no more free inode for new file %s\n", newname);
break;
}
case ENTRY_EXIST: {
fprintf(stderr, "pathname is already used\n");
break;
}
}
free(msg);
return ERROR;
}
int inode = msg->inode;
int reuse = msg->reuse;
free(msg);
MessageIO *msg_write = Malloc(32);
msg_write->message_type = IO;
msg_write->inode = inode;
msg_write->reuse = reuse;
msg_write->len = strlen(oldname);
msg_write->buf = (void *)oldname;
msg_write->read = 0;
msg_write->pos = 0;
Send((void *)msg_write, -FILE_SERVER);
if (msg_write->inode < 0) {
fprintf(stderr, "Create symbolic link %s failed: ", newname);
short_check(msg_write->inode);
free(msg_write);
return ERROR;
}
if (msg_write->len != strlen(oldname)) {
fprintf(stderr, "Write symbolic file %s failed\n", newname);
Unlink(newname);
free(msg_write);
return ERROR;
}
free(msg_write);
return 0;
}
extern int ReadLink(char *pathname, char *buf, int len) {
if (check_pathname(pathname)) return ERROR;
MessageInode *msg = Malloc(32);
msg->message_type = INODE;
msg->inode = pathname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = pathname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(pathname);
msg->name_addr = (void *)pathname;
msg->sym_pursue = 0;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Read symbolic file %s failed: ", pathname);
general_check(msg->inode);
free(msg);
return ERROR;
}
int inode = msg->inode;
int reuse = msg->reuse;
free(msg);
MessageIO *msg_read = Malloc(32);
msg_read->message_type = IO;
msg_read->inode = inode;
msg_read->reuse = reuse;
msg_read->len = len;
msg_read->buf = (void *)buf;
msg_read->read = 1;
msg_read->pos = 0;
Send((void *)msg_read, -FILE_SERVER);
if (msg_read->inode < 0) {
fprintf(stderr, "ead symbolic file %s failed: ", pathname);
short_check(msg_read->inode);
free(msg_read);
return ERROR;
}
int res = msg_read->len;
free(msg_read);
return res;
}
extern int MkDir(char *pathname) {
if (check_pathname(pathname)) return ERROR;
MessageCreate *msg = Malloc(32);
msg->message_type = CREATE;
msg->inode = pathname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = pathname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(pathname);
msg->name_addr = (void *)pathname;
msg->type = INODE_DIRECTORY;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Create directory %s failed: ", pathname);
general_check(msg->inode);
switch(msg->inode) {
case DIR_EXIST: {
fprintf(stderr, "%s already exists as a directory\n", pathname);
break;
}
case NO_INODE: {
fprintf(stderr, "there is no more free inode for new file %s\n", pathname);
break;
}
case ENTRY_EXIST: {
fprintf(stderr, "pathname is already used\n");
break;
}
}
free(msg);
return ERROR;
}
free(msg);
return 0;
}
extern int RmDir(char *pathname) {
if (check_pathname(pathname)) return ERROR;
MessageRmDir *msg = Malloc(32);
msg->message_type = RMDIR;
msg->inode = pathname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = pathname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(pathname);
msg->name_addr = (void *)pathname;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Remove directory %s failed: ", pathname);
general_check(msg->inode);
switch(msg->inode) {
case UNLINK_REMOVE_DOT: {
fprintf(stderr, "cannot remove entry . and entry ..\n");
break;
}
case DIR_NOT_EMPTY: {
fprintf(stderr, "the directory is not empty\n");
break;
}
}
free(msg);
return ERROR;
}
return 0;
}
extern int ChDir(char *pathname) {
if (check_pathname(pathname)) return ERROR;
MessageInode *msg = Malloc(32);
msg->message_type = INODE;
msg->inode = pathname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = pathname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(pathname);
msg->name_addr = (void *)pathname;
msg->sym_pursue = 1;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Chang to directory %s failed: ", pathname);
general_check(msg->inode);
free(msg);
return ERROR;
}
if (msg->file_type != INODE_DIRECTORY) {
fprintf(stderr, "%s is not a directory\n", pathname);
free(msg);
return ERROR;
}
current_dir_inode = msg->inode;
current_dir_reuse = msg->reuse;
free(msg);
return 0;
}
extern int Stat(char *pathname, struct Stat *statbuf) {
if (check_pathname(pathname)) return ERROR;
MessageInode *msg = Malloc(32);
msg->message_type = INODE;
msg->inode = pathname[0]=='/'?ROOTINODE:current_dir_inode;
msg->reuse = pathname[0]=='/'?0:current_dir_reuse;
msg->name_len = strlen(pathname);
msg->name_addr = (void *)pathname;
msg->sym_pursue = 1;
Send((void *)msg, -FILE_SERVER);
if (msg->inode < 0) {
fprintf(stderr, "Get stat of file %s failed: ", pathname);
general_check(msg->inode);
free(msg);
return ERROR;
}
statbuf->inum = msg->inode;
statbuf->type = msg->file_type;
statbuf->size = msg->name_len;
statbuf->nlink = msg->nlink;
free(msg);
return 0;
}
extern int Sync() {
MessageSync *msg = Malloc(32);
msg->message_type = SYNC;
Send((void *)msg, -FILE_SERVER);
return 0;
}
extern int Shutdown() {
MessageShut *msg = Malloc(32);
msg->message_type = SHUT;
Send((void *)msg, -FILE_SERVER);
return 0;
}
void update_fd(){
int next = (next_fd + 1) % MAX_OPEN_FILES;
while (open_file_table[next].type != INODE_FREE) {
next = (next + 1) % MAX_OPEN_FILES;
if (next == next_fd) break;
}
next_fd = (next == next_fd?-1:next);
}
int check_pathname(char *pathname) {
if (pathname == NULL) {
fprintf(stderr, "NULL pathname\n");
return 1;
}
if (strlen(pathname) > MAXPATHNAMELEN - 1) {
fprintf(stderr, "pathname %s too long\n", pathname);
return 1;
}
else return 0;
}
int check_fd(int fd) {
if (fd < 0 || fd >= MAX_OPEN_FILES) {
fprintf(stderr, "Invalid file descriptor: %d\n", fd);
return 1;
}
if (open_file_table[fd].type == INODE_FREE) {
fprintf(stderr, "No open file with file descriptor %d\n", fd);
return 1;
}
return 0;
}
void general_check(int result) {
switch(result) {
case CURRENT_DIR_NOT_EXIST: {
fprintf(stderr, "current directory is freed\n");
break;
}
case CURRENT_DIR_REUSED: {
fprintf(stderr, "the inode (%d) of current directory is reused by other file\n", current_dir_inode);
break;
}
case NAME_TOO_LONG: {
fprintf(stderr, "file name too long\n");
break;
}
case TARGET_DIR_NOT_EXIST: {
fprintf(stderr, "the request directory does not exist\n");
break;
}
case MAX_SYM: {
fprintf(stderr, "too many symlinks, possible infinite loop\n");
break;
}
}
}
void short_check(int result) {
switch(result) {
case CURRENT_DIR_NOT_EXIST: {
fprintf(stderr, "the file does not exist\n");
break;
}
case CURRENT_DIR_REUSED: {
fprintf(stderr, "inode of the file has been reused\n");
break;
}
}
}