-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
623 lines (533 loc) · 14.4 KB
/
server.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#include <stdio.h>
#include "udp.h"
#include "mfs.h"
#define BUFFER_SIZE (4096)
//Prototypes
int MyLookup(int pinum, char *name);
int MyStat(int inum);
int MyWrite(int inum, char *buffer, int block);
int MyRead(int inum, int block);
int MyCreat(int pinum, int type, char *name);
int MyUnlink(int pinum, char *name);
int createFile(int pinum, char *name);
int addDirectory(int pinum, char *name);
int addDirectoryEntry(int inum, char *name);
int findFreeInode();
int findMapPiece(int inum);
//Global Variables
void *image, *end, *position, *temp;
MFS_Checkpoint_t *checkpoint;
MFS_InodeMap_t *inodeMap[256];
MFS_Inode_t *inodes[4096];
int imageSize;
static ClientMessage_t *client;
static ServerMessage_t *server;
int
main(int argc, char *argv[]){
//Variables
int sd, fd, rc, returnCode, i, size;
struct sockaddr_in s;
int problem = sizeof(MFS_Dir_t) + sizeof(MFS_Inode_t) +
sizeof(MFS_InodeMap_t) + (2 * sizeof(MFS_DirEnt_t));
//Initialize the client and server messages
client = (ClientMessage_t *)malloc(sizeof(ClientMessage_t));
server = (ServerMessage_t *)malloc(sizeof(ServerMessage_t));
//Check for file image
/*
if(argc == 3){
sd = UDP_Open(atoi(argv[1]));
fd = open(argv[2], O_RDWR);
assert(sd > -1);
assert(fd > -1);
}
*/
if(argc == 3){
printf("Check: %d\n", atoi(argv[1]));
//Open the socket and the image
sd = UDP_Open(atoi(argv[1]));
//sd = UDP_Open(29344);
fd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
assert(sd > -1);
assert(fd > -1);
//Create image
imageSize = sizeof(MFS_Checkpoint_t) + (256 * sizeof(MFS_InodeMap_t)) +
(4096 * sizeof(MFS_Inode_t)) + (14 * 4096 * 4096);
image = malloc(imageSize);
if(image == NULL)
printf("MALLOC PROBLEM\n");
size = 0;
//Create and initialize the checkpoint
temp = image;
position = image;
end = image;
checkpoint = (MFS_Checkpoint_t *)temp;
position += sizeof(MFS_Checkpoint_t);
//Initialize the arrays
for(i = 0; i < 256; i++)
inodeMap[i] = NULL;
for(i = 0; i < 4096; i++)
inodes[i] = NULL;
//Create the root directory
rc = addDirectory(0, "/");
}
else{
printf("Usage: server portnum [file-system-image]\n");
exit(-1);
}
//Start server loop
while(1){
//Check to make sure size is okay
//Get the message from the Client
rc = UDP_Read(sd, &s, (char *)client, sizeof(ClientMessage_t));
//Perform the correct operation
if(rc > 0){
if(client->syscallNumber == LOOKUP){
//Lookup the file
returnCode = MyLookup(client->inum, client->buffer);
printf("RETURNCODE: %d\n", returnCode);
//Setup the return message
server->returnCode = returnCode;
printf("server server return: %d\n", server->returnCode);
rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t));
}
else if(client->syscallNumber == STAT){
//Get the Stat info about the specific inum
returnCode = MyStat(client->inum);
//Setup the return message
server->returnCode = returnCode;
rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t));
}
else if(client->syscallNumber == WRITE){
printf("ENTERED WRITE 1\n");
printf("here here here: %d\n", client->inum);
//Update image
returnCode = MyWrite(client->inum, client->buffer, client->block);
//Update checkpoint
checkpoint->end = end - image;
//Write to disk
rc = write(fd, image, imageSize);
if(rc <= 0)
exit(-1);
fsync(fd);
//Setup the return message
server->returnCode = returnCode;
rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t));
}
else if(client->syscallNumber == READ){
//Read the specified block of data
returnCode = MyRead(client->inum, client->block);
printf("server server server buffer: %s\n", server->buffer);
//Setup the return message
server->returnCode = returnCode;
rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t));
}
else if(client->syscallNumber == CREAT){
//Update image
returnCode = MyCreat(client->inum, client->type, client->buffer);
//Update checkpoint
checkpoint->end = end - image;
//Write to disk
rc = write(fd, image, imageSize);
if(rc <= 0)
exit(-1);
fsync(fd);
//Setup the return message
server->returnCode = returnCode;
rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t));
}
else if(client->syscallNumber == UNLINK){
//Unlink and update message
returnCode = MyUnlink(client->inum, client->buffer);
//Update checkpoint
checkpoint->end = end - image;
//Write to disk
rc = write(fd, image, imageSize);
if(rc <= 0)
exit(-1);
fsync(fd);
//Setup the return message
server->returnCode = returnCode;
rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t));
}
else{
//Update checkpoint
checkpoint->end = end - image;
//This is the case for shutdown
rc = write(fd, image, imageSize);
if(rc <= 0)
exit(-1);
fsync(fd);
//Set up the return message
server->returnCode = 0;
rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t));
exit(0);
}
}
}
return 0;
}
int MyLookup(int pinum, char *name){
//Variables
int inum = -1;
int i;
MFS_Inode_t *inode;
MFS_Dir_t *dir;
MFS_DirEnt_t *dirEnt;
int done = 0;
//Check for invalid inum
if(inodes[pinum] == NULL)
return -1;
printf("asldkf: %s\n", name);
printf("length1: %d\n", strlen(name));
//Get the inode and find info
inode = inodes[pinum];
dir = image + inode->addrs[0];
for(i = 0; i < 128 && !done; i++){
if(dir->entries[i] == -1)
done = 1;
dirEnt = image + dir->entries[i];
printf("dirEnt Name: %s\n", dirEnt->name);
printf("length2: %d\n", strlen(dirEnt->name));
printf("dirEnt Num: %d\n", dirEnt->inum);
if(strcmp(dirEnt->name, name)==0)
inum = dirEnt->inum;
}
printf("Lookup inum: %d\n", inum);
return inum;
}
int MyStat(int inum){
//Variables
MFS_Inode_t *inode;
void *tmp;
int count = 0;
int i;
//Check for invalid inum
if(inodes[inum] == NULL)
return -1;
//Get the inode
inode = inodes[inum];
//Update server message
server->type = inode->type;
server->size = inode->size;
//Count the number of blocks used
for(i = 0; i < 14; i++){
if(inode->addrs[i] != -1)
count++;
}
server->blocksUsed = count;
//Have to copy only certain length of blocks because a pointer
//would end up copying everything from that point until the end
for(i = 0; i < 14; i++){
if(inode->addrs[i] != -1){
tmp = image + inode->addrs[i];
memmove(server->blocks[i], tmp, BUFFER_SIZE);
}
else
server->blocks[i] = NULL;
}
return 0;
}
int MyWrite(int inum, char *buffer, int block){
printf("ENTERED WRITE 2\n");
printf("inode: %d\n", inum);
//Variables
MFS_Inode_t *inode;
void *location;
//Check for invalid inum
if(inodes[inum] == NULL)
return -1;
//Get the inode
inode = inodes[inum];
//Can't write into a directory
if(inode->type == MFS_DIRECTORY)
return -1;
//Check for invalid block
if(inode->addrs[block] != -1 || block < 0 || block > 13)
return -1;
//Put the information in the block
location = position;
memmove(location, buffer, BUFFER_SIZE);
position += BUFFER_SIZE;
//Update inode
inode->addrs[block] = location - image;
return 0;
}
int MyRead(int inum, int block){
//Variables
MFS_Inode_t *inode;
void *tmp;
//Check for invalid inum
if(inodes[inum] == NULL)
return -1;
//Get the inode
inode = inodes[inum];
//Get the correct info
if(inode->type == MFS_DIRECTORY){
MFS_Dir_t *dir = image + inode->addrs[0];
server->type = MFS_DIRECTORY;
server->blocksUsed = dir->inum;
strcpy(server->buffer, dir->name);
}
else{
//Check for invalid block
if(inode->addrs[block] == -1)
return -1;
//Set the info
server->type = MFS_REGULAR_FILE;
server->blocksUsed = inum;
tmp = image + inode->addrs[block];
memmove(server->buffer, tmp, BUFFER_SIZE);
}
return 0;
}
int MyCreat(int pinum, int type, char *name){
//Variables
int len, rc;
//Check for invalid inum
if(inodes[pinum] == NULL)
return -1;
//Check to see if the name is too long
len = strlen(name);
if(len > 28)
return -1;
//Check to see if name already exists
//INSERT CHECK HERE
//Create the directory or file
if(type == MFS_DIRECTORY)
rc = addDirectory(pinum, name);
else
rc = createFile(pinum, name);
return rc;
}
int MyUnlink(int pinum, char *name){
//Variables
MFS_Inode_t *childInode, *parentInode;
MFS_Dir_t *dir;
MFS_DirEnt_t *dirEnt;
int cinum, i, spot;
//Check for invalid pinum
if(inodes[pinum] == NULL)
return -1;
//Lookup the specified inode
cinum = MyLookup(pinum, name);
if(cinum == -1)
return 0;
//Set the inodes
parentInode = inodes[pinum];
childInode = inodes[cinum];
//2 Cases: Directory or File
if(childInode->type == MFS_DIRECTORY){
dir = image + childInode->addrs[0];
//Make sure that the directory is empty
for(i = 2; i < 128; i++){
//Start at position 2 because there will always be at
//least two entries for "." and ".."
if(dir->entries[i] != -1)
return -1;
}
//Directory is empty, have the parent unlink
dir = image + parentInode->addrs[0];
for(i = 0; i < 128; i++){
dirEnt = dir->entries[i] + image;
if(strcmp(name, dirEnt->name) == 1)
spot = i;
break;
}
dir->entries[i] = -1;
inodes[cinum] = NULL;
}else{
//'name' is a file
//Directory is empty, have the parent unlink
dir = image + parentInode->addrs[0];
for(i = 0; i < 128; i++){
dirEnt = dir->entries[i] + image;
if(strcmp(name, dirEnt->name) == 1)
spot = i;
break;
}
dir->entries[spot] = -1;
inodes[cinum] = NULL;
}
return 0;
}
int createFile(int pinum, char *name){
//Variables
MFS_InodeMap_t *tempMap, *tempMap2;
MFS_Inode_t *tempInode;
MFS_Dir_t *tempDir;
int inodeNumber, mapNumber;
int mapPos = 0;
int inodePos = 0;
int pos = 0;
int i;
//Create the inode
temp = position;
inodeNumber = findFreeInode();
inodePos = temp - image;
tempInode = (MFS_Inode_t *)temp;
tempInode->size = 0;
tempInode->type = MFS_REGULAR_FILE;
for(i = 0; i < 14; i++)
tempInode->addrs[i] = -1;
inodes[inodeNumber] = tempInode;
position += sizeof(MFS_Inode_t);
//Create the map piece
mapNumber = findMapPiece(inodeNumber);
if(inodeMap[mapNumber] == NULL){
temp = position;
mapPos = temp - image;
tempMap = (MFS_InodeMap_t *)temp;
tempMap->inodes[(inodeNumber%16)] = inodePos;
inodeMap[mapNumber] = tempMap;
position += sizeof(MFS_InodeMap_t);
}
else{
temp = position;
tempMap2 = inodeMap[mapNumber];
mapPos = temp - image;
tempMap = (MFS_InodeMap_t *)temp;
for(i = 0; i < 16; i++)
tempMap->inodes[i] = tempMap2->inodes[i];
tempMap->inodes[(inodeNumber%16)] = inodePos;
inodeMap[mapNumber] = tempMap;
checkpoint->mapPieces[mapNumber] = mapPos;
position += sizeof(MFS_InodeMap_t);
}
//Update the parent directory
tempInode = inodes[pinum];
tempDir = image + tempInode->addrs[0];
for(i = 0; i < 128; i++){
if(tempDir->entries[i] == -1){
pos = i;
break;
}
}
printf("INODESSS: %d\n", inodeNumber);
tempDir->entries[pos] = addDirectoryEntry(inodeNumber, name);
tempInode->size += 4096;
//Update end
end = position;
return 0;
}
int addDirectory(int pinum, char *name){
//Variables
MFS_InodeMap_t *tempMap, *tempMap2;
MFS_Inode_t *tempInode;
MFS_Dir_t *tempDir;
int inodeNumber, mapNumber;
int mapPos = 0;
int inodePos = 0;
int dirPos = 0;
int dirEntPos = 0;
int i;
//Create the inode
temp = position;
inodeNumber = findFreeInode();
inodePos = temp - image;
tempInode = (MFS_Inode_t *)temp;
tempInode->size = sizeof(MFS_Dir_t) + (2 * sizeof(MFS_DirEnt_t));
tempInode->type = MFS_DIRECTORY;
inodes[inodeNumber] = tempInode;
position += sizeof(MFS_Inode_t);
//Create the map piece
mapNumber = findMapPiece(inodeNumber);
if(inodeMap[mapNumber] == NULL){
temp = position;
mapPos = temp - image;
tempMap = (MFS_InodeMap_t *)temp;
tempMap->inodes[(inodeNumber%16)] = inodePos;
inodeMap[mapNumber] = tempMap;
position += sizeof(MFS_InodeMap_t);
}
else{
temp = position;
tempMap2 = inodeMap[mapNumber];
mapPos = temp - image;
tempMap = (MFS_InodeMap_t *)temp;
for(i = 0; i < 16; i++)
tempMap->inodes[i] = tempMap2->inodes[i];
tempMap->inodes[(inodeNumber%16)] = inodePos;
inodeMap[mapNumber] = tempMap;
checkpoint->mapPieces[mapNumber] = mapPos;
position += sizeof(MFS_InodeMap_t);
}
//Create the directory
temp = position;
dirPos = temp - image;
tempInode->addrs[0] = dirPos;
tempDir = (MFS_Dir_t *)temp;
strcpy(tempDir->name, name);
tempDir->inum = inodeNumber;
for(i = 0; i < 128; i++){
tempDir->entries[i] = -1;
}
position += sizeof(MFS_Dir_t);
//Create the directory entries
tempDir->entries[0] = addDirectoryEntry(inodeNumber, ".");
tempDir->entries[1] = addDirectoryEntry(pinum, "..");
//Update the parent directory
if(strcmp(name, "/") == 1){
tempInode = inodes[pinum];
tempDir = image + tempInode->addrs[0];
for(i = 0; i < 128; i++){
if(tempDir->entries[i] == -1){
dirEntPos = i;
break;
}
}
tempDir->entries[dirEntPos] = addDirectoryEntry(inodeNumber, name);
}
//Update end
end = position;
return 0;
}
int addDirectoryEntry(int inum, char *name){
//Variables
int dirEntPos = 0;
MFS_DirEnt_t *tempDirEnt;
//Add entry
temp = position;
dirEntPos = temp - image;
tempDirEnt = (MFS_DirEnt_t *)temp;
tempDirEnt->inum = inum;
printf("NAME1: %s\n", name);
strcpy(tempDirEnt->name, name);
printf("NAME2: %s\n", tempDirEnt->name);
position += sizeof(MFS_DirEnt_t);
return dirEntPos;
}
int findFreeInode(){
//Variable
int inum, i;
for(i = 0; i < 4096; i++){
if(inodes[i] == NULL){
inum = i;
break;
}
}
return inum;
}
int findMapPiece(int inode){
//Variables
int place;
//Find the correct map piece
place = inode/16;
return place;
}
/*
int sd = UDP_Open(10000);
assert(sd > -1);
printf("waiting in loop\n");
while (1) {
struct sockaddr_in s;
char buffer[BUFFER_SIZE];
int rc = UDP_Read(sd, &s, buffer, BUFFER_SIZE);
if (rc > 0) {
printf("SERVER:: read %d bytes (message: '%s')\n", rc, buffer);
char reply[BUFFER_SIZE];
sprintf(reply, "reply");
rc = UDP_Write(sd, &s, reply, BUFFER_SIZE);
}
}
return 0;
*/