-
Notifications
You must be signed in to change notification settings - Fork 1
/
libnnfs_filesystem.c
419 lines (360 loc) · 13.8 KB
/
libnnfs_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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "libnnfs_filesystem.h"
#include "nnfs_constants.h"
//considered to be root of the filesystem
char *hosting_directory = NULL;
#define LISTING_DIRECTORY_FORMAT "DIR: %s\n"
#define LISTING_FILE_FORMAT "FIL: %s\n"
#define LISTING_FORMAT_SIZE (sizeof(LISTING_DIRECTORY_FORMAT) -2)//2 is %s as they do not go into count
#define PATH_HOME "~/"
#define PATH_PARENT_DIRECTORY "/.."
int create_new_dir_descriptor(struct dir_descriptor *descr){
descr->curdir_path = NULL;
descr->current_entity = NULL;
int retval = DIRECTORY_SUCCESS;
if(hosting_directory == NULL){
printf("ERROR: hosting_directory wasnt set\n");
return DIRECTORY_FAILED_TO_OPEN;
}
descr->directory = opendir(hosting_directory);
if(descr->directory == NULL){
printf("ERROR: couldnt open hosting directory\n");
return DIRECTORY_FAILED_TO_OPEN;
}
return DIRECTORY_SUCCESS;
}
int close_descriptor(struct dir_descriptor *dir){
free(dir->curdir_path);
return closedir(dir->directory);
}
int set_hosting_directory(const char *path){
free(hosting_directory);
hosting_directory = strdup(path);
// hosting_directory = calloc(sizeof(char), strlen(path));
// strncpy(hosting_directory, path, strlen(path));
return DIRECTORY_SUCCESS;
}
int list_directory(struct dir_descriptor *descr){
if(descr->directory == NULL){
printf("ERROR: the dir_descriptor wasnt created\n");
return DIRECTORY_FAILED_TO_OPEN;
}
descr->current_entity = readdir(descr->directory);
while(descr->current_entity != NULL){
if(descr->current_entity->d_type == DT_DIR)
if(strcmp(descr->current_entity->d_name, ".") != 0 && strcmp(descr->current_entity->d_name, "..") != 0)
printf("DIR: %s\n", descr->current_entity->d_name);
if(descr->current_entity->d_type == DT_REG)
printf("FIL: %s\n", descr->current_entity->d_name);
descr->current_entity = readdir(descr->directory);
}
return DIRECTORY_SUCCESS;
}
int slist_directory(struct dir_descriptor *descr, char *str, uint32_t str_size){
if(descr->directory == NULL){
printf("ERROR: the dir_descriptor wasnt created\n");
return DIRECTORY_FAILED_TO_OPEN;
}
uint32_t current_fillament = 0u;
uint32_t space_needed = 0u;
if(descr->current_entity == NULL)
descr->current_entity = readdir(descr->directory);
bool enough_space_in_str = true;
str_size-=1u;
while(enough_space_in_str && descr->current_entity != NULL){
//printf("\nSTATUS: currently \nspace_needed = %d\nenough_space_in_str = %d\ncurrent_fillament = %d\nfile_name = %s\n\n", space_needed, enough_space_in_str, current_fillament, descr->current_entity->d_name);
if(strcmp(descr->current_entity->d_name, ".") != 0 && strcmp(descr->current_entity->d_name, "..") != 0){
space_needed = LISTING_FORMAT_SIZE + strlen(descr->current_entity->d_name);
//it doesnt calculate the actual size of the buffer that is needed for the whole listing of files and dirs
//it only tries to write every single listing entry in the buffer
//while its possible to calculate how much space is needed for the whole listing
//it requires reading everything at once
//and its solves the problem of segmentating the output to smaller pieces that can be sent
//in msg->payload, because we can give it NNFS_MSG_MAX_lENGTH size buffer
//and repeat untill its empty
if(space_needed + current_fillament < str_size){
if(descr->current_entity->d_type == DT_REG)
snprintf(str + current_fillament,space_needed, LISTING_FILE_FORMAT, descr->current_entity->d_name);
if(descr->current_entity->d_type == DT_DIR)
snprintf(str + current_fillament,space_needed, LISTING_DIRECTORY_FORMAT, descr->current_entity->d_name);
current_fillament += LISTING_FORMAT_SIZE + strlen(descr->current_entity->d_name) - 1;
}
else enough_space_in_str = false;
}
if(enough_space_in_str)
descr->current_entity = readdir(descr->directory);
}
str[current_fillament] = '\0';
return enough_space_in_str ? DIRECTORY_SUCCESS : DIRECTORY_NOT_EVERYTHING_LISTED;
}
//checks if its an absolute path
bool is_absolute_path(const char *path){
if(strncmp(path, PATH_HOME, sizeof(PATH_HOME) - 1) == 0)
return true;
return false;
}
//counts .. in array
int count_parentcalls(const char *beg_of_path){
if(beg_of_path == NULL)
return 0;
int counter = 0;
char *pch=strstr(beg_of_path,"..");
while (pch!=NULL) {
counter++;
pch=strstr(pch+1,"..");
}
return counter;
}
//counts directories in the array
int count_dir_in_path(const char *beg_of_path){
if(beg_of_path == NULL)
return 0;
int counter = 1;
char *pch=strchr(beg_of_path,'/');
while (pch!=NULL) {
counter++;
pch=strchr(pch+1,'/');
}
return counter;
}
//checks for dots after words, they so that its not possible to write example./../../ and escape hosting dir
bool dots_are_in_right_placces(const char * path){
const char *pch=strchr(path,'.');
if(pch == path)
pch = strchr(pch+1,'.');
while (pch!=NULL){
if(pch[-1] != '.' && pch[-1] != '/'){
return false;
}
pch=strchr(pch+1,'.');
}
return true;
}
//returns true if all parentcalls are preceeded by only parentcalls
bool parentcall_preceeding(const char * path){
//bool parentcall_prec = (strncmp(path, "../", sizeof("../") - 1) == 0);
const char *pch=strchr(path,'/');
while (pch!=NULL){
if(pch - path + 1 < strlen(path)){
if(pch[1] == '.' && pch[-1] != '.'){
return false;
}
}
pch=strchr(pch+1,'/');
}
return dots_are_in_right_placces(path);
}
//checks for safety in the path(to not escape hosting dir)
bool is_safe_path(const char *path, const struct dir_descriptor* descr){
//path cant start or end with /
if(strchr(path, '/') == path || path[strlen(path) - 1] == '/')
return false;
//it's not allowed to reference /. in any path (can cause memory overflow)
if(strcmp(path, ".") == 0)
return false;
if(strncmp(path, "./", sizeof("./") -1) == 0)
return false;
if(strstr(path, "/./") != NULL)
return false;
if(strcmp(path + strlen(path) - sizeof("/."), "/.") == 0)
return false;
//checking if its an absolute path it cant have parentcalls /..
//if it doesnt have parentcalls it's considered safe
if(is_absolute_path(path))
if(strstr(path, PATH_HOME) != NULL){
return false;
}
else
return true;
//only relative path are here
//all parentcalls must be in the beginning /../.. is ok, but /whatever/.. is not
if(!parentcall_preceeding(path))
return false;
//the amount of parentcalls must be lower than the amount of directories opened from the home directory
if(count_parentcalls(path) > count_dir_in_path(descr->curdir_path))
return false;
return true;
}
//finds last dir to be saved before ..
int find_last_dir(const char *beg_of_path, int number_of_slashes){
const char * pch = beg_of_path;
while(number_of_slashes > 0){
pch = strchr(pch + 1, '/');
number_of_slashes--;
}
return (int) (pch - beg_of_path);
}
//gets rid of .. to optimise memory
void build_whole_path(const struct dir_descriptor *descr, const char *path, char **new_path){
if(is_safe_path(path, descr)){
if(is_absolute_path(path)){
*new_path = calloc(1, strlen(path));
strcpy(*new_path, path);
return;
}
int parentcalls_number = count_parentcalls(path);
int cur_dir_left_dir = count_dir_in_path(descr->curdir_path) - parentcalls_number;
if(ENABLE_LOGGING != 0){
printf("parent calls: %d\n", parentcalls_number);
printf("cur_dir = %d\n", cur_dir_left_dir);
}
if(cur_dir_left_dir < 0)
return;
if(parentcalls_number == 0){
if(descr->curdir_path != NULL)
cur_dir_left_dir = strlen(descr->curdir_path);
else
cur_dir_left_dir = parentcalls_number;
}
else
cur_dir_left_dir = find_last_dir(descr->curdir_path, cur_dir_left_dir);
free(*new_path);
//the first parentcall is without a slash
const char * after_the_parentcalls = path;
if(parentcalls_number != 0){
after_the_parentcalls+= parentcalls_number * sizeof(PATH_PARENT_DIRECTORY) - 2;
}
*new_path = calloc(1, cur_dir_left_dir + strlen(after_the_parentcalls) + 1);
if(cur_dir_left_dir == 0){
strcat(*new_path, after_the_parentcalls);
}
else{
strncat(*new_path, descr->curdir_path, cur_dir_left_dir);
strcat(*new_path, "/");
strcat(*new_path, after_the_parentcalls);
}
}
else *new_path = NULL;
}
int change_directory(struct dir_descriptor *descr, const char *dir){
if(hosting_directory == NULL){
printf("ERROR: hosting_directory wasnt set\n");
return DIRECTORY_FAILED_TO_OPEN;
}
char *new_path = NULL;
build_whole_path(descr, dir, &new_path);
if(new_path == NULL)
return DIRECTORY_PERMISSION_DENIED;
if(ENABLE_LOGGING != 0)
printf("STATUS: new path equals = \"%s\"\n",new_path);
char *absolute_path = calloc(1, strlen(hosting_directory) + strlen(new_path) + 1);
strcat(absolute_path, hosting_directory);
strcat(absolute_path, "/");
strcat(absolute_path, new_path);
DIR *new_dir = opendir(absolute_path);
if(ENABLE_LOGGING != 0)
printf("STATUS: opening at \"%s\"\n", absolute_path);
free(absolute_path);
if(new_dir == NULL){
printf("ERROR: couldnt open directory\n");
free(new_path);
return DIRECTORY_FAILED_TO_OPEN;
}
else{
if(ENABLE_LOGGING != 0)
printf("SUCCESS: opened directory\n");
closedir(descr->directory);
descr->directory = new_dir;
new_dir = NULL;
free(descr->curdir_path);
if(strlen(new_path) != 0){
descr->curdir_path = calloc(1, strlen(new_path));
strcpy(descr->curdir_path, new_path);
}
else
descr->curdir_path = NULL;
free(new_path);
return DIRECTORY_SUCCESS;
}
}
void rewind_directory(struct dir_descriptor *dir){
rewinddir(dir->directory);
}
bool is_safe_filename(const char *filename){
if(strchr(filename, '/') != NULL){
printf("ERROR: files can be opened only in the current directory\n");
return false;
}
return true;
}
int write_to_file_internal(const char *filename, const char *buffer){
FILE *file_descr = fopen(filename, "a");
if(file_descr == NULL){
printf("ERROR: failed to create a file : \"%s\"\n", filename);
return DIRECTORY_FAIL_TO_CREATE_FILE;
}
fprintf(file_descr, "%s\n", buffer);
fclose(file_descr);
return DIRECTORY_SUCCESS;
}
int write_to_file(const struct dir_descriptor *descr, const char *filename, const char *buffer){
char *file_path = NULL;
if(!is_safe_filename(filename)){
printf("ERROR: file can only be opened in the current directory\n");
return DIRECTORY_FILE_OUTSIDE_OF_CURDIR;
}
if(descr->curdir_path != NULL)
file_path = calloc(1, strlen(filename) + strlen(hosting_directory) + strlen(descr->curdir_path) + 3);
else
file_path = calloc(1, strlen(filename) + strlen(hosting_directory) + 3);
strcat(file_path, hosting_directory);
strcat(file_path, "/");
if(descr->curdir_path != NULL){
strcat(file_path, descr->curdir_path);
strcat(file_path, "/");
}
strcat(file_path, filename);
int result = write_to_file_internal(file_path, buffer);
free(file_path);
return result;
}
int read_from_file_internal(const char *filename, char *buffer, uint32_t size_of_buffer, uint32_t offset){
FILE *file_descr = fopen(filename, "r");
if(file_descr == NULL){
printf("ERROR: failed to read from file: \"%s\"\n", filename);
return DIRECTORY_FAILED_TO_OPEN;
}
//saving last character for NULL char
size_of_buffer--;
if(fseek(file_descr, offset, SEEK_SET) != 0){
printf("ERROR: couldnt get to the position %d\n", offset);
return DIRECTORY_FAILED_TO_GET_TO_OFFSET;
}
char ch = fgetc(file_descr);
uint32_t index = 0;
while(size_of_buffer > index && ch != EOF){
buffer[index] = ch;
index++;
ch = fgetc(file_descr);
}
buffer[index] = 0;
fclose(file_descr);
if(ch != EOF)
return offset + index;
else
return DIRECTORY_SUCCESS;
}
int read_from_file(const struct dir_descriptor *descr,const char *filename, char *buffer, uint32_t size_of_buffer, uint32_t offset){
char *file_path = NULL;
if(!is_safe_filename(filename)){
printf("ERROR: file can only be opened in the current directory\n");
return DIRECTORY_FILE_OUTSIDE_OF_CURDIR;
}
if(descr->curdir_path != NULL)
file_path = calloc(1, strlen(filename) + strlen(hosting_directory) + strlen(descr->curdir_path) + 3);
else
file_path = calloc(1, strlen(filename) + strlen(hosting_directory) + 3);
strcat(file_path, hosting_directory);
strcat(file_path, "/");
if(descr->curdir_path != NULL){
strcat(file_path, descr->curdir_path);
strcat(file_path, "/");
}
strcat(file_path, filename);
int result = read_from_file_internal(file_path, buffer, size_of_buffer, offset);
free(file_path);
return result;
}