Skip to content

Commit 0b14d7b

Browse files
Added existing files
0 parents  commit 0b14d7b

File tree

8 files changed

+413
-0
lines changed

8 files changed

+413
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
*.out
3+
*.exe

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# File Allocation Table Simulator

dir.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include "main.h"
4+
5+
#define MAX_DIR_SIZE 30
6+
#define MAX_FILENAME_SIZE 13
7+
8+
static const char* dir_filename = "dir.dat";
9+
10+
static char filenames[MAX_DIR_SIZE][MAX_FILENAME_SIZE];
11+
static int file_start_sectors[MAX_DIR_SIZE];
12+
13+
FILE* open_dir_file(char * options) {
14+
static FILE* dir_file;
15+
16+
if(dir_file == NULL) {
17+
dir_file = fopen(dir_filename, "w");
18+
fclose(dir_file);
19+
20+
dir_file = fopen(dir_filename, options);
21+
} else {
22+
dir_file = freopen(dir_filename, options, dir_file);
23+
}
24+
25+
return dir_file;
26+
}
27+
28+
// Reads the dir.dat file into the directory in memory, one entry per line
29+
void read_dir() {
30+
FILE* dir_file = open_dir_file("r");
31+
32+
char filename[MAX_FILENAME_SIZE];
33+
int file_start_sector;
34+
35+
int i = 0;
36+
while(fscanf(dir_file, "%s %d", filename, &file_start_sector) != EOF) {
37+
strcpy(filenames[i], filename);
38+
file_start_sectors[i] = file_start_sector;
39+
40+
i++;
41+
}
42+
43+
fclose(dir_file);
44+
}
45+
46+
// Writes the contents of the directory in memory to the dir.dat file, one entry per line
47+
void save_dir() {
48+
FILE* dir_file = open_dir_file("w");
49+
50+
for(int i = 0; i < MAX_DIR_SIZE; i++) {
51+
if(file_start_sectors[i] != 0) {
52+
fprintf(dir_file, "%s %d\n", filenames[i], file_start_sectors[i]);
53+
}
54+
}
55+
56+
fclose(dir_file);
57+
58+
}
59+
60+
// Prints the contents of the directory in memory, one entry per line
61+
void print_dir() {
62+
for(int i = 0; i < MAX_DIR_SIZE; i++) {
63+
if(file_start_sectors[i] != 0) {
64+
printf("%s %d\n", filenames[i], file_start_sectors[i]);
65+
}
66+
}
67+
}
68+
69+
// Returns the number of files that the directory has remaining space for
70+
int dir_space_left() {
71+
int space_left = 0;
72+
73+
for(int i = 0; i < MAX_DIR_SIZE; i++) {
74+
if(file_start_sectors[i] == 0) {
75+
space_left++;
76+
}
77+
}
78+
79+
return space_left;
80+
}
81+
82+
// Returns the directory index of the file with the given file name if it exists, -1 otherwise
83+
int does_file_exist_in_dir(char* file) {
84+
for(int i = 0; i < MAX_DIR_SIZE; i++) {
85+
if(strcmp(filenames[i], file) == 0) {
86+
return i;
87+
}
88+
}
89+
90+
return -1;
91+
}
92+
93+
// Returns the start sector of the file at directory index position
94+
int get_start_sector(int position) {
95+
return file_start_sectors[position];
96+
}
97+
98+
// Adds a file to the directory with the given file name and start sector
99+
void add_file_to_dir(char* file, int start_sector) {
100+
for(int i = 0; i < MAX_DIR_SIZE; i++) {
101+
if(file_start_sectors[i] == 0) {
102+
strcpy(filenames[i], file);
103+
file_start_sectors[i] = start_sector;
104+
return;
105+
}
106+
}
107+
}
108+
109+
// Deletes the file with directory index position from the directory
110+
void delete_file_from_dir(int position) {
111+
strcpy(filenames[position], "");
112+
file_start_sectors[position] = 0;
113+
}
114+
115+
116+
117+
118+
119+
120+

fat.c

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include <stdio.h>
2+
#include "main.h"
3+
4+
#define FAT_SIZE 32
5+
6+
static const char* fat_filename = "fat.dat";
7+
8+
static int fat[FAT_SIZE];
9+
10+
FILE* open_fat_file(char * options) {
11+
static FILE* fat_file;
12+
13+
if(fat_file == NULL) {
14+
fat_file = fopen(fat_filename, "w");
15+
fclose(fat_file);
16+
17+
fat_file = fopen(fat_filename, options);
18+
} else {
19+
fat_file = freopen(fat_filename, options, fat_file);
20+
}
21+
22+
return fat_file;
23+
}
24+
25+
// Reads the fat.dat file into the FAT in memory (int fat[32]), one int per line
26+
void read_fat() {
27+
FILE* fat_file = open_fat_file("r");
28+
29+
int entry;
30+
int i = 0;
31+
while(fscanf(fat_file, "%d", &entry) != EOF) {
32+
fat[i] = entry;
33+
i++;
34+
}
35+
36+
// fclose(fat_file);
37+
}
38+
39+
// Writes the FAT in memory (int fat[32]) to the fat.dat file, one int per line
40+
void save_fat() {
41+
FILE* fat_file = open_fat_file("w");
42+
43+
for(int i = 0; i < FAT_SIZE; i++) {
44+
fprintf(fat_file, "%d\n", fat[i]);
45+
}
46+
47+
fclose(fat_file);
48+
}
49+
50+
// Clears the FAT in memory (int fat[32])
51+
void reset_fat() {
52+
fat[0] = 0;
53+
fat[1] = 0;
54+
55+
for(int i = 2; i < FAT_SIZE; i++) {
56+
fat[i] = 1;
57+
}
58+
}
59+
60+
// Returns the number of free sectors in the FAT
61+
int free_sectors() {
62+
int num_free = 0;
63+
64+
for(int i = 0; i < FAT_SIZE; i++) {
65+
if(fat[i] == 1) {
66+
num_free++;
67+
}
68+
}
69+
70+
return num_free;
71+
}
72+
73+
// Returns the index of the lowest number sector that is currently free
74+
int first_free_sector() {
75+
for(int i = 0; i < FAT_SIZE; i++) {
76+
if(fat[i] == 1) {
77+
return i;
78+
}
79+
}
80+
81+
return -1;
82+
}
83+
84+
// Allocates a chain of sectors n long, starting at start_sector
85+
void allocate_sectors(int start_sector, int n) {
86+
if(n == 1) {
87+
fat[start_sector] = 0;
88+
} else {
89+
int sectors_allocated = 0;
90+
int previous_sector = start_sector;
91+
92+
for(int i = 0; i < FAT_SIZE; i++) {
93+
if(fat[i] == 1 && i != start_sector) {
94+
fat[previous_sector] = i;
95+
previous_sector = i;
96+
97+
sectors_allocated++;
98+
99+
if(sectors_allocated == n - 1) {
100+
fat[previous_sector] = 0;
101+
break;
102+
}
103+
}
104+
}
105+
}
106+
}
107+
108+
// Deallocates the chain of sectors starting at start_sector
109+
void deallocate_sectors(int start_sector) {
110+
int current_sector = start_sector;
111+
int next_sector;
112+
113+
do {
114+
next_sector = fat[current_sector];
115+
fat[current_sector] = 1;
116+
current_sector = next_sector;
117+
} while(next_sector != 0);
118+
}
119+
120+
// Prints the chain of sectors that starts at start_sector
121+
void print_sector_chain(int start_sector) {
122+
int current_sector = start_sector;
123+
int next_sector;
124+
125+
do {
126+
printf("%d", current_sector);
127+
next_sector = fat[current_sector];
128+
current_sector = next_sector;
129+
130+
if(next_sector != 0) {
131+
printf(" ");
132+
}
133+
} while(next_sector != 0);
134+
135+
printf("\n");
136+
}
137+
138+
139+
140+
141+
142+
143+

main.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include "main.h"
5+
6+
void command_sectors(char* filename);
7+
void command_add(char* filename, int num_sectors);
8+
void command_delete(char* filename);
9+
10+
int main() {
11+
reset_fat();
12+
13+
read_fat();
14+
read_dir();
15+
16+
char line[100];
17+
char command[20];
18+
char arg1[20];
19+
char arg2[20];
20+
21+
while(1) {
22+
printf(":");
23+
24+
fgets(line, sizeof(line) / sizeof(line[0]), stdin);
25+
if(sscanf(line, "%s %s %s", command, arg1, arg2) < 1) {
26+
break;
27+
}
28+
29+
if(strcmp(command, "sectors") == 0) {
30+
command_sectors(arg1);
31+
32+
} else if(strcmp(command, "add") == 0) {
33+
command_add(arg1, strtol(arg2, NULL, 10));
34+
35+
} else if(strcmp(command, "delete") == 0) {
36+
command_delete(arg1);
37+
38+
} else if(strcmp(command, "dir") == 0) {
39+
print_dir();
40+
41+
} else if(strcmp(command, "quit") == 0) {
42+
break;
43+
44+
}
45+
}
46+
47+
return 0;
48+
}
49+
50+
// Prints the sectors used by the file with the given filename
51+
void command_sectors(char* filename) {
52+
int file_position = does_file_exist_in_dir(filename);
53+
54+
if(file_position < 0) {
55+
printf("File does not exist\n");
56+
return;
57+
}
58+
59+
int start_sector = get_start_sector(file_position);
60+
print_sector_chain(start_sector);
61+
}
62+
63+
// Adds a file with the given filename to the directory and allocates it num_sectors in the FAT
64+
void command_add(char* filename, int num_sectors) {
65+
if(does_file_exist_in_dir(filename) >= 0) {
66+
printf("File already exists\n");
67+
return;
68+
}
69+
70+
if(dir_space_left() == 0) {
71+
printf("Directory is full\n");
72+
return;
73+
}
74+
75+
if(free_sectors() < num_sectors) {
76+
printf("Not enough free sectors\n");
77+
return;
78+
}
79+
80+
int start_sector = first_free_sector();
81+
allocate_sectors(start_sector, num_sectors);
82+
83+
add_file_to_dir(filename, start_sector);
84+
85+
save_dir();
86+
save_fat();
87+
}
88+
89+
// Deletes the file with the given filename from the directory and deallocates its sectors in the FAT
90+
void command_delete(char* filename) {
91+
int file_position = does_file_exist_in_dir(filename);
92+
93+
if(file_position < 0) {
94+
printf("File does not exist\n");
95+
return;
96+
}
97+
98+
int start_sector = get_start_sector(file_position);
99+
deallocate_sectors(start_sector);
100+
101+
delete_file_from_dir(file_position);
102+
103+
save_dir();
104+
save_fat();
105+
}

main.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// fat.c
2+
void read_fat();
3+
void save_fat();
4+
void reset_fat();
5+
int free_sectors();
6+
int first_free_sector();
7+
void allocate_sectors(int start_sector, int n);
8+
void deallocate_sectors(int start_sector);
9+
void print_sector_chain(int start_sector);
10+
11+
// dir.c
12+
void read_dir();
13+
void save_dir();
14+
void print_dir();
15+
int dir_space_left();
16+
int does_file_exist_in_dir(char* file);
17+
int get_start_sector(int position);
18+
void add_file_to_dir(char* file, int start_sector);
19+
void delete_file_from_dir(int position);

0 commit comments

Comments
 (0)