-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.c
238 lines (182 loc) · 4.9 KB
/
filesystem.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "libdisksimul.h"
#include "filesystem.h"
/**
* @brief Format disk.
*
*/
int fs_format(){
int ret, i;
struct table_directory root_dir;
struct sector_0 sector0;
struct sector_data sector;
if ( (ret = ds_init(FILENAME, SECTOR_SIZE, NUMBER_OF_SECTORS, 1)) != 0 ){
return ret;
}
memset (§or0, 0, sizeof(struct sector_0));
/* first free sector. */
sector0.free_sectors_list = 2;
ds_write_sector(0, (void*)§or0, SECTOR_SIZE);
memset(&root_dir, 0, sizeof(root_dir));
ds_write_sector(1, (void*)&root_dir, SECTOR_SIZE);
/* Create a list of free sectors. */
memset(§or, 0, sizeof(sector));
for(i=2;i<NUMBER_OF_SECTORS;i++){
if(i<NUMBER_OF_SECTORS-1){
sector.next_sector = i+1;
}else{
sector.next_sector = 0;
}
ds_write_sector(i, (void*)§or, SECTOR_SIZE);
}
ds_stop();
printf("Disk size %d kbytes, %d sectors.\n", (SECTOR_SIZE*NUMBER_OF_SECTORS)/1024, NUMBER_OF_SECTORS);
return 0;
}
/**
* @brief Create a new file on the simulated filesystem.
* @param input_file Source file path.
* @param simul_file Destination file path on the simulated file system.
* @return 0 on success.
*/
int fs_create(char* input_file, char* simul_file){
int ret;
if ( (ret = ds_init(FILENAME, SECTOR_SIZE, NUMBER_OF_SECTORS, 0)) != 0 ){
return ret;
}
/* Write the code to load a new file to the simulated filesystem. */
ds_stop();
return 0;
}
/**
* @brief Read file from the simulated filesystem.
* @param output_file Output file path.
* @param simul_file Source file path from the simulated file system.
* @return 0 on success.
*/
int fs_read(char* output_file, char* simul_file){
int ret;
if ( (ret = ds_init(FILENAME, SECTOR_SIZE, NUMBER_OF_SECTORS, 0)) != 0 ){
return ret;
}
/* Write the code to read a file from the simulated filesystem. */
ds_stop();
return 0;
}
/**
* @brief Delete file from file system.
* @param simul_file Source file path.
* @return 0 on success.
*/
int fs_del(char* simul_file){
int ret;
if ( (ret = ds_init(FILENAME, SECTOR_SIZE, NUMBER_OF_SECTORS, 0)) != 0 ){
return ret;
}
/* Write the code delete a file from the simulated filesystem. */
ds_stop();
return 0;
}
/**
* @brief List files from a directory.
* @param simul_file Source file path.
* @return 0 on success.
*/
int fs_ls(char *dir_path){
int ret;
if ( (ret = ds_init(FILENAME, SECTOR_SIZE, NUMBER_OF_SECTORS, 0)) != 0 ){
return ret;
}
/* Write the code to show files or directories. */
ds_stop();
return 0;
}
/**
* @brief Create a new directory on the simulated filesystem.
* @param directory_path directory path.
* @return 0 on success.
*/
int fs_mkdir(char* directory_path){
int ret;
if ( (ret = ds_init(FILENAME, SECTOR_SIZE, NUMBER_OF_SECTORS, 0)) != 0 ){
return ret;
}
/* Write the code to create a new directory. */
ds_stop();
return 0;
}
/**
* @brief Remove directory from the simulated filesystem.
* @param directory_path directory path.
* @return 0 on success.
*/
int fs_rmdir(char *directory_path){
int ret;
if ( (ret = ds_init(FILENAME, SECTOR_SIZE, NUMBER_OF_SECTORS, 0)) != 0 ){
return ret;
}
/* Write the code to delete a directory. */
ds_stop();
return 0;
}
/**
* @brief Generate a map of used/available sectors.
* @param log_f Log file with the sector map.
* @return 0 on success.
*/
int fs_free_map(char *log_f){
int ret, i, next;
//struct root_table_directory root_dir;
struct sector_0 sector0;
struct sector_data sector;
char *sector_array;
FILE* log;
int pid, status;
int free_space = 0;
char* exec_params[] = {"gnuplot", "sector_map.gnuplot" , NULL};
if ( (ret = ds_init(FILENAME, SECTOR_SIZE, NUMBER_OF_SECTORS, 0)) != 0 ){
return ret;
}
/* each byte represents a sector. */
sector_array = (char*)malloc(NUMBER_OF_SECTORS);
/* set 0 to all sectors. Zero means that the sector is used. */
memset(sector_array, 0, NUMBER_OF_SECTORS);
/* Read the sector 0 to get the free blocks list. */
ds_read_sector(0, (void*)§or0, SECTOR_SIZE);
next = sector0.free_sectors_list;
while(next){
/* The sector is in the free list, mark with 1. */
sector_array[next] = 1;
/* move to the next free sector. */
ds_read_sector(next, (void*)§or, SECTOR_SIZE);
next = sector.next_sector;
free_space += SECTOR_SIZE;
}
/* Create a log file. */
if( (log = fopen(log_f, "w")) == NULL){
perror("fopen()");
free(sector_array);
ds_stop();
return 1;
}
/* Write the the sector map to the log file. */
for(i=0;i<NUMBER_OF_SECTORS;i++){
if(i%32==0) fprintf(log, "%s", "\n");
fprintf(log, " %d", sector_array[i]);
}
fclose(log);
/* Execute gnuplot to generate the sector's free map. */
pid = fork();
if(pid==0){
execvp("gnuplot", exec_params);
}
/* Wait gnuplot to finish */
wait(&status);
free(sector_array);
ds_stop();
printf("Free space %d kbytes.\n", free_space/1024);
return 0;
}