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
|
#include <comus/fs.h>
/*#include <lib.h>
//#include <comus/tar.h>
//#include <string.h>
#include <comus/ramfs.h>
#define NOERROR 0
#define ERROR 1
int ramfs_check_exists(const char *name) {
for(int i = 0; i < numberOfFiles; i++) {
if(memcmp(allTheFiles[i].name, name, strlen(allTheFiles[i].name)) == 0) {
// found it
return i;
}
}
// doesn't exist
return -1;
}
int ramfs_create(const char *name) {
if(ramfs_check_exists(name) != -1) {
// already exists
return ERROR;
}
struct file *newFile;
*newFile->name = name;
newFile->size = 0;
newFile->data = kalloc(MAX_FILE_SIZE);
newFile = &allTheFiles[numberOfFiles];
}
int ramfs_delete(const char *name) {
int i = ramfs_check_exists(name);
if(i >= 0) {
kfree(allTheFiles[i].data);
for(int j = i; j < numberOfFiles; j++) {
allTheFiles[j] = allTheFiles[j+1];
numberOfFiles -= 1;
}
return NOERROR;
}
return ERROR;
}
int ramfs_read(const char *name, char *buffer, size_t len) {
int i = ramfs_check_exists(name);
if(i >= 0) {
memcpy(buffer, allTheFiles[i].data, len);
return NOERROR;
}
return ERROR;
}
int ramfs_write(const char *name, char *buffer) {
int i = ramfs_check_exists(name);
if(i >= 0) {
memcpy(buffer)
}
}
// here we return the index of the file as well.
*int ramfs_find_file(root *r, const char *fullpath, const char *name, file *out, directory *outDirectory) {
directory *location = r->root;
if(ramfs_find_directory(r, fullpath, name, location) == NOERROR) {
for(int i = 0; i < location->file_count; i++) {
if(strcmp(location->files[i]->name, name) == 0) {
outDirectory = location;
out = location->files[i];
return i;
}
}
}
return -1;
}
int ramfs_find_directory(root *r, const char *fullpath, const char *name, directory *out) {
directory *location = r->root;
char *path = fullpath;
char *tempPath = strtok(tempPath, "/");
while(tempPath != NULL) {
bool wasItFound = false;
for(int i = 0; i < location->directory_count; i++) {
if(strcmp(location->directories[i]->name, tempPath) == 0) {
// yay we found it;
location = location->directories[i];
wasItFound = true;
break;
}
}
if(!wasItFound) {
return ERROR;
}
tempPath = strtok(NULL, "/");
}
out = location;
return NOERROR;
}
int ramfs_create_file(root *r, const char *fullpath, const char *name) {
// trace through the path
// struct ramfs_directory *location = r->root;
directory *location = r->root;
if(ramfs_find_directory(r, fullpath, name, location) == NOERROR) {
if(location->file_count >= MAX_FILES) {
return ERROR;
}
file *newFile = malloc(sizeof(file));
strcpy(newFile->name, name);
newFile->size = 0;
newFile->data = malloc(MAX_FILE_SIZE);
location->files[location->file_count] = newFile;
location->file_count += 1;
return NOERROR;
}
return ERROR;
}
int ramfs_delete_file(root *r, const char *fullpath, const char *name) {
directory *location;
if(ramfs_find_directory(r, fullpath, name, location) == NOERROR) {
file *find_file;
int file_index = ramfs_find_file(r, fullpath, name, find_file, location) >= 0;
if(file_index >= 0) {
free(location->files[file_index]->data);
free(location->files[file_index]);
for(int i = file_index + 1; i < location->file_count; i++) {
location->files[i-1] = location->files[i];
//memcpy(location->files[i-1], location->files[i], location->files[i]->size);
//free(location->files[i]);
}
location->file_count -= 1;
return NOERROR;
}
return ERROR;
}
return ERROR;
}
int ramfs_create_directory() {
}
int ramfs_delete_directory() {
}
int ramfs_write() {
}
root *ramfs_init() {
root *r = malloc(sizeof(root));
r->root = malloc(sizeof(directory));
strcpy(r->root->name, "/");
r->root->file_count = 0;
r->root->directory_count = 0;
return r;
}
*/
|