-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmfs.c
602 lines (499 loc) · 13.8 KB
/
mfs.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
/*
Name: Kunal Samant & Shivangi Vyas
(1001534662) (1001xx0499)
Please compile with gcc mfs.c -o mfs
*/
// The MIT License (MIT)
// ;[l]
// Copyright (c) 2016, 2017 Trevor Bakker
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdint.h>
#define WHITESPACE " \t\n" // We want to split our command line up into tokens
// so we need to define what delimits our tokens.
// In this case white space
// will separate the tokens on our command line
#define MAX_COMMAND_SIZE 255 // The maximum command-line size
#define MAX_NUM_ARGUMENTS 5 // Mav shell only supports five arguments
#define TRUE 1
#define FALSE 0
int closed = TRUE;
FILE * in;
FILE * ofp;
FILE * put_file;
char buffer[512];
char BS_OEMName[8];
int16_t BPB_BytsPerSec;
int8_t BPB_SecPerClus;
int16_t BPB_RsvdSecCnt;
int8_t BPB_NumFATs;
int16_t BPB_RootEntCnt;
char BS_VolLab[11];
int32_t BPB_FATSz32;
int32_t BPB_RootClus;
int32_t RootDirSectors = 0;
int32_t FirstDataSectors = 0;
int32_t FirstSectorofCluster = 0;
int LBAToOffset(int32_t sector)
{
return ((sector - 2 ) * BPB_BytsPerSec) + (BPB_BytsPerSec * BPB_RsvdSecCnt) +
(BPB_NumFATs * BPB_FATSz32 * BPB_BytsPerSec);
}
struct __attribute__((__packed__)) DirectoryEntry {
char DIR_Name[11];
uint8_t DIR_Attr;
uint8_t Unused1[8];
uint16_t DIR_FirstClusterHigh;
uint8_t Unused2[4];
uint16_t DIR_FirstClusterLow;
uint32_t DIR_FileSize;
};
struct DirectoryEntry dir[16];
int16_t NextLB ( uint32_t sector )
{
uint32_t FATAddress = (BPB_BytsPerSec * BPB_RsvdSecCnt ) + ( sector * 4 );
int16_t val;
fseek( in, FATAddress, SEEK_SET);
fread( &val, 2, 1, in);
return val;
}
int CheckFile(char *token[5])
{
int flag=1;
int file;
if (token[1] == NULL)
{
printf("Must have a second argument!!\n\n");
file=100;
return file;
}
char input[12];
strcpy(input, token[1]);
char expanded_name[12];
memset(expanded_name, ' ', 12);
char *tok = strtok(input, ".");
strncpy(expanded_name, tok, strlen(tok));
tok = strtok( NULL, "." );
if (tok)
{
strncpy((char*)(expanded_name + 8), tok, strlen(tok));
}
expanded_name[11] = '\0';
int i;
for (i = 0; i < 11; i++)
{
expanded_name[i] = toupper(expanded_name[i]);
}
for (i = 0; i < 16; i++)
{
if (strncmp(expanded_name, dir[i].DIR_Name, 11) == 0)
{
if ((dir[i].DIR_Attr != 53) && ((dir[i].DIR_Attr == 32) || (dir[i].DIR_Attr == 16) || (dir[i].DIR_Attr == 1)))
{
flag = 0;
file = i;
break;
}
}
}
if(flag)
{
printf("Error: Sorry! File not found\n\n");
file=100;
}
return file;
}
char* resize(char *filename)
{
char expanded_name[12];
memset( expanded_name, ' ', 12 );
char *token = strtok(filename, "." );
strncpy( expanded_name, token, strlen( token ) );
token = strtok( NULL, "." );
if( token )
{
strncpy( (char*)(expanded_name+8), token, strlen(token ) );
}
expanded_name[11] = '\0';
int j;
for( j = 0; j < 11; j++ )
{
expanded_name[j] = toupper( expanded_name[j] );
}
token=NULL;
return expanded_name;
}
int compare_put(char *filename, char *buffer)
{
char expanded_name[12];
memset( expanded_name, ' ', 12 );
char *token = strtok(filename, "." );
strncpy( expanded_name, token, strlen( token ) );
token = strtok( NULL, "." );
if( token )
{
strncpy( (char*)(expanded_name+8), token, strlen(token ) );
}
expanded_name[11] = '\0';
int j;
for( j = 0; j < 11; j++ )
{
expanded_name[j] = toupper( expanded_name[j] );
}
char buff[12];
strcpy(buff, buffer);
buff[11] = '\0';
if( strncmp( expanded_name, buffer, 11 ) != 0 )
{
return 1;
}
else{
return 0;
}
}
void get(char *token[5])
{
int found = 0;
int i;
i = CheckFile(token);
if(i==100)
{
return ;
}
else
{
char *filename=token[1];
int cluster = dir[i].DIR_FirstClusterLow;
int size = dir[i].DIR_FileSize;
int offset = LBAToOffset(cluster);
fseek(in, offset, SEEK_SET);
ofp = fopen(filename, "w");
fread(&buffer[0], 512, 1, ofp);
fwrite(&buffer[0], 512, 1, in);
size = size - 512;
while (size > 0)
{
cluster = NextLB(cluster);
int addr = LBAToOffset(cluster);
fseek(in, addr, SEEK_SET);
fread(&buffer[0], size, 1, ofp);
fwrite(&buffer[0], size, 1, in);
size=size-512;
}
fclose(ofp);
}
}
void put(char *token[5])
{
int found = 0;
int i;
/*
i = CheckFile(token);
if(i==100)
{
return ;
}*/
put_file = fopen(token[1], "w");
if (put == NULL)
{
printf("Error: File not found");
}
else
{
//int i;
for(i=0;i<16;i++)
{
if (dir[i].DIR_Name[0] == '0x00' || dir[i].DIR_Name[0]=='0xE5') //&& ((dir[e].DIR_Attr == 32) || (dir[e].DIR_Attr == 16) || (dir[e].DIR_Attr == 1)))
{
char *filename=token[1];
int cluster = dir[i].DIR_FirstClusterLow;
int size = dir[i].DIR_FileSize;
int offset = LBAToOffset(cluster);
fseek(put, offset, SEEK_SET);
in = fopen(filename, "w");
fread(&buffer[0], 512, 1, in);
fwrite(&buffer[0], 512, 1, put);
size = size - 512;
while (size > 0)
{
cluster = NextLB(cluster);
int addr = LBAToOffset(cluster);
fseek(put, addr, SEEK_SET);
fread(&buffer[0], size, 1, in);
fwrite(&buffer[0], size, 1, put);
size = size - 512;
}
fclose(put);
}
}
}
}
int findIndex(int clus, char *filename)
{
if (!clus)
{
return -1;
}
char *file_name = resize(filename);
int i;
for (i = 0; i < 16; i++)
{
char string[12];
memcpy(string, dir[i].DIR_Name, 11);
string[11] = '\0';
if (strcmp(file_name, string) == 0)
{
return i;
}
}
return -1;
}
int main()
{
system("clear");
char * cmd_str = (char*) malloc( MAX_COMMAND_SIZE );
int RootClus, currClus;
while( 1 )
{
// Print out the msh prompt
printf ("msf> ");
// Read the command from the commandline. The
// maximum command that will be read is MAX_COMMAND_SIZE
// This while command will wait here until the user
// inputs something since fgets returns NULL when there
// is no input
while(!fgets (cmd_str, MAX_COMMAND_SIZE, stdin));
if (strcmp(cmd_str, "\n") == 0) {
continue;
}
/* Parse input */
char *token[MAX_NUM_ARGUMENTS];
int token_count = 0;
// Pointer to point to the token
// parsed by strsep
char *arg_ptr;
char *working_str = strdup( cmd_str );
// we are going to move the working_str pointer so
// keep track of its original value so we can deallocate
// the correct amount at the end
char *working_root = working_str;
// Tokenize the input stringswith whitespace used as the delimiter
while ( ( (arg_ptr = strsep(&working_str, WHITESPACE ) ) != NULL) &&
(token_count<MAX_NUM_ARGUMENTS))
{
token[token_count] = strndup( arg_ptr, MAX_COMMAND_SIZE );
if( strlen( token[token_count] ) == 0 )
{
token[token_count] = NULL;
}
token_count++;
}
// Now print the tokenized input as a debug check
// \TODO Remove this code and replace with your shell functionality
if(closed)
{
if (strcmp(token[0], "exit") == 0 || strcmp(token[0], "quit") == 0)
{
break;
}
if (strcmp(token[0], "open") == 0)
{
if (token[1] == NULL)
{
printf("<Open must have a second argument>\n\n");
continue;
}
in = fopen(token[1], "rb");
if (in == NULL)
{
printf("Error: File system image not found!\n\n");
continue;
}
closed = FALSE;
fseek(in, 11, SEEK_SET);
fread(&BPB_BytsPerSec, 2, 1, in);
fseek(in, 13, SEEK_SET);
fread(&BPB_SecPerClus, 2, 1, in);
fseek(in, 14, SEEK_SET);
fread(&BPB_RsvdSecCnt, 2, 1, in);
fseek(in, 16, SEEK_SET);
fread(&BPB_NumFATs, 2, 1, in);
fseek(in, 36, SEEK_SET);
fread(&BPB_FATSz32, 2, 1, in);
RootClus = (BPB_NumFATs * BPB_FATSz32 * BPB_BytsPerSec) +(BPB_RsvdSecCnt * BPB_BytsPerSec);
currClus = (BPB_NumFATs * BPB_FATSz32 * BPB_BytsPerSec) +(BPB_RsvdSecCnt * BPB_BytsPerSec);
fseek(in, ((BPB_NumFATs * BPB_FATSz32 * BPB_BytsPerSec) +(BPB_RsvdSecCnt * BPB_BytsPerSec)), SEEK_SET);
int i;
for (i = 0; i < 16; i++)
{
fread(&dir[i], 32, 1, in);
}
continue;
}
else{
printf("OPEN A FILE FIRST!!\n\n");
continue;
}
}
if(!closed)
{
if (strcmp(token[0], "open") == 0)
{
printf("File already opened!!\n\n");
continue;
}
if (strcmp(token[0], "close") == 0)
{
fclose(in);
closed = TRUE;
}
if (strcmp(token[0], "exit") == 0)
{
return 0;
}
if (strcmp(token[0], "info") == 0)
{
printf("BPB_BytesPerSec: \n\tDecimal: %d\tHexadecimal: 0x%04x\n", BPB_BytsPerSec, BPB_BytsPerSec);
printf("BPB_SecPerClus: \n\tDecimal: %d\tHexadecimal: 0x%04x\n", BPB_SecPerClus, BPB_SecPerClus);
printf("BPB_RsvdSecCnt: \n\tDecimal: %d\tHexadecimal: 0x%04x\n", BPB_RsvdSecCnt, BPB_RsvdSecCnt);
printf("BPB_NumFATs: \n\tDecimal: %d\tHexadecimal: 0x%04x\n", BPB_NumFATs, BPB_NumFATs);
printf("BPB_FATSz32: \n\tDecimal: %d\tHexadecimal: 0x%04x\n", BPB_FATSz32, BPB_FATSz32);
}
if (strcmp(token[0], "stat") == 0)
{
if (token[1] == NULL)
{
printf("<stat must have a second argument>\n\n");
continue;
}
int positive = CheckFile(token);
if(positive < 15)
{
printf("Attribute: 0x%02x\t\tStarting Cluster Number: %d\n\n", dir[positive].DIR_Attr, dir[positive].DIR_FirstClusterLow);
}
}
if (strcmp(token[0], "ls") == 0)
{
int i;
for (i = 0; i < 16; i++)
{
char string[12];
strcpy(string, dir[i].DIR_Name);
string[11] = '\0';
if ((dir[i].DIR_Attr != 53) && ((dir[i].DIR_Attr == 32) || (dir[i].DIR_Attr == 16) || (dir[i].DIR_Attr == 1)))
{
printf("%s\n", string);
}
}
}
if (strcmp(token[0], "cd") == 0)
{
int i,parClus;
if (token[1] == NULL)
{
printf("cd must have a second arguments\n\n");
continue;
}
if (strcmp(token[1], "..") == 0)
{
if (RootClus = currClus)
{
continue;
}
i =CheckFile(token);
if (i!=100)
{
parClus = dir[i].DIR_FirstClusterLow;
}
if (parClus == 0)
{
currClus = RootClus;
}
else{
currClus = LBAToOffset(parClus);
}
continue;
}
int index = findIndex(currClus, token[1]);
if (index != -1)
{
int newClus = dir[index].DIR_FirstClusterLow;
currClus = LBAToOffset(newClus);
for(i=0;i<16;i++)
{
fseek(in,currClus+(i*32),SEEK_SET);
fread(&dir[i],32,1,in);
}
}
else{
printf("Error: File not found.\n");
continue;
}
}
if (strcmp(token[0], "get") == 0)
{
get(token);
}
if (strcmp(token[0], "put") == 0)
{
put(token);
}
if (strcmp(token[0], "read") == 0)
{
if ((token[1] == NULL) || (token[2] == NULL) || (token[3] == NULL))
{
printf("read must have 3 arguments\n\n");
continue;
}
int index = findIndex(currClus, token[1]);
if (index != -1)
{
int position = atoi(token[2]);
int numByts = atoi(token[3]);
int lowClus = dir[index].DIR_FirstClusterLow;
int size = dir[index].DIR_FileSize;
int offset = LBAToOffset(lowClus);
fseek(in, offset, SEEK_SET);
uint8_t buff[position + numByts];
fread(&buff, position + numByts, 1, in);
int i;
for (i = 0; i < position + numByts; i++)
{
printf("%x", buff[i]);
printf("\n");
}
}
else{
printf("Error: File not found\n");
continue;
}
}
free(working_root);
}
}
return 0;
}