-
Notifications
You must be signed in to change notification settings - Fork 12
/
sfs_util.h
167 lines (129 loc) · 3.59 KB
/
sfs_util.h
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
#ifndef SFS_API_UTIL
#define SFS_API_UTIL
char vd[100][30000];
int fatID = 0;
void FAT_init(FileAllocationTable * fat) {
for (fatID = 2; fatID < FAT_SIZE - 1; fatID++) {
fat->table[fatID].db_index = fatID * sizeof(DiskBlock);
fat->table[fatID].next = EOF;
fat->table[fatID].free = 1;
}
fat->count = 0;
}
int FAT_getFreeNode(FileAllocationTable * fat) {
if (++fat->count < FAT_SIZE - 1) {
return fat->count;
} else {
int i;
for (i = 2; i < FAT_SIZE - 1; i++) {
if (fat->table[i].free) {
return i;
}
}
}
fat->count--;
fprintf(stderr, "Error: Cannot get free block in FAT.\n");
exit(0);
return -5;
}
char * FAT_getPartFile(FileDescriptor file, FileAllocationTable fat, int length) {
int curr, next, red = 0; // this is not a typo, It needs to sound lik read
int positionOnDisk = 0;
void * positionOnBuff = 0;
char buff[length];
curr = file.fat_index;
next = fat.table[curr].next;
for (; red * sizeof(DiskBlock) < length && curr != EOF; curr = fat.table[curr].next, next = fat.table[curr].next, red++) {
positionOnDisk = fat.table[curr].db_index;
positionOnBuff = (void *) ((int) (&buff) + red * sizeof(DiskBlock));
read_blocks(positionOnDisk, length, positionOnBuff);
}
//printf("D:%i B:%p\n", positionOnDisk, positionOnBuff);
return strdup(buff);
}
char * FAT_getFullFile(FileDescriptor file, FileAllocationTable fat) {
return FAT_getPartFile(file, fat, file.size);
}
void DirectoryDescriptor_init(DirectoryDescriptor * root) {
int i;
for (i = 0; i < MAX_FILE; i++) {
root->table[i].size = EMPTY;
}
}
int DirectoryDescriptor_getFreeSpot(DirectoryDescriptor * root) {
if (++root->count < MAX_FILE) {
return root->count;
} else {
int i;
for (i = 0; i < MAX_FILE; i++) {
if (root->table[i].size == EMPTY) {
return i;
}
}
}
root->count--;
fprintf(stderr, "Error: Cannot get free block in DD.\n");
exit(0);
return -1;
}
int getIndexOfFileInDirectory(char* name, DirectoryDescriptor * rootDir) {
int i, fileID = -1;
// Looks for the file and gets its index.
for(i = 0; i < MAX_FILE; i++) {
if (!strcmp(rootDir->table[i].filename, name)) {
fileID = i;
break;
}
}
// Checks if the file was correctly closed.
if ( fileID != -1) {
if(rootDir->table[fileID].fas.opened == 1) {
//fprintf(stderr, "Warning: '%s' is already open.\n", name);
return -2;
}
}
return fileID;
}
void FileDescriptor_createFile(char* name, FileDescriptor * file) {
strcpy(file->filename, name);
file->fas.opened = 1;
file->fas.rd_ptr = 0;
file->fas.wr_ptr = 0;
file->size = 0;
file->timestamp = time(NULL);
}
int FileDescriptor_getNextWritable(FileDescriptor * file, FileAllocationTable * fat) {
int currNode = file->fat_index;
// Goes to the end of the file.
while (fat->table[currNode].next != EOF) {
currNode = fat->table[currNode].next;
file->fas.wr_ptr = currNode;
}
int nextNode = FAT_getFreeNode(fat);
fat->table[currNode].next = nextNode;
fat->table[nextNode].next = EOF;
// Returns -1 if there is no more free spots.
return nextNode < 0 ? fat->table[nextNode].db_index : -1;
}
void VirtualDisk_write(int address, int length, void * buff) {
char * string = (char *) buff;
printf("%i\n", string[length+1]);
strncpy(vd[address], string, length);
//vd[address][pos+1] = '\0';
}
void VirtualDisk_read(int address, void * buff, int length) {
char * string = (char *) buff;
vd[address][length] = '\0';
strncpy(string, vd[address], length);
}
int FreeBlockList_getBlock(freeblocklist * fbl) {
int i;
for(i = 2; i < NB_BLOCK - 1; i++) {
if (fbl->list[i] == 0) {
fbl->list[i] = 1;
return i;
}
}
return -1;
}
#endif