-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfat.cpp
More file actions
282 lines (234 loc) · 8.01 KB
/
fat.cpp
File metadata and controls
282 lines (234 loc) · 8.01 KB
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
/* Copyright (c) 2025, Rye Stahle-Smith
December 2nd, 2025 - fat.cpp
Description: In-memory FAT-like filesystem implementation supporting directories, files, path traversal, CRUD operations, and resource reporting. */
#include "shell.h"
#include "fat.h"
// Constructor initializes root and pools
FAT::FAT() {
// mark all pool objects as free
for (int i = 0; i < MAX_DIRS; i++) dir_pool[i].used = false;
for (int i = 0; i < MAX_FILES; i++) file_pool[i].used = false;
// init root
strcpy(root.name, "/");
root.parent = nullptr;
root.subdir_count = 0;
root.file_count = 0;
root.used = true;
}
Directory* FAT::get_root() { return &root; }
Directory* FAT::find_subdir_recursive(Directory* start, const char* path) {
if (!start || !path || !*path) return start;
// Copy path to buffer so we can split
char buf[32];
int i = 0;
while (path[i] && path[i] != '/' && i < sizeof(buf)-1) {
buf[i] = path[i];
i++;
}
buf[i] = '\0';
Directory* next = find_subdir(start, buf); // find immediate child
if (!next) return nullptr;
// If we hit a '/', continue recursively
if (path[i] == '/') return find_subdir_recursive(next, path + i + 1);
return next; // last component
}
Directory* FAT::find_subdir(Directory* dir, const char* name) {
for (int i = 0; i < dir->subdir_count; i++) {
if (strcmp(dir->subdirs[i]->name, name) == 0) return dir->subdirs[i];
}
return nullptr;
}
File* FAT::find_file(Directory* dir, const char* name) {
for (int i = 0; i < dir->file_count; i++) {
if (strcmp(dir->files[i]->name, name) == 0) return dir->files[i];
}
return nullptr;
}
static bool is_name_invalid(const char* name) {
if (!name || !name[0]) return true; // empty or null
bool all_spaces = true;
for (int i = 0; name[i]; i++) {
if (name[i] == '/') return true; // reject: contains '/'
if (name[i] != ' ') all_spaces = false;
}
return all_spaces; // reject: name is only spaces
}
// recursive mkdir
Directory* FAT::mkdir_recursive(Directory* start, const char* path) {
if (!start || !path || *path == '\0') return nullptr;
Directory* curr = start;
const char* seg_start = path;
char seg_name[MAX_NAME_LEN];
while (*seg_start) {
// find end of current segment
const char* seg_end = seg_start;
while (*seg_end && *seg_end != '/') seg_end++;
int len = seg_end - seg_start;
if (len == 0 || len >= MAX_NAME_LEN) return nullptr; // reject empty or too long names
// copy segment
for (int i = 0; i < len; i++) seg_name[i] = seg_start[i];
seg_name[len] = '\0';
// try to find existing subdir
Directory* next = find_subdir(curr, seg_name);
if (!next) {
// create it
next = mkdir(curr, seg_name);
if (!next) return nullptr; // creation failed
}
curr = next;
// skip '/' if present
seg_start = (*seg_end) ? seg_end + 1 : seg_end;
}
return curr;
}
// mkdir uses static pool
Directory* FAT::mkdir(Directory* dir, const char* name) {
if (is_name_invalid(name)) return nullptr; // reject empty names
if (dir->subdir_count >= MAX_DIRS) return nullptr;
if (find_subdir(dir, name)) return nullptr;
// find free directory in pool
for (int i = 0; i < MAX_DIRS; i++) {
if (!dir_pool[i].used) {
Directory* new_dir = &dir_pool[i];
new_dir->used = true;
strcpy(new_dir->name, name);
new_dir->parent = dir;
new_dir->subdir_count = 0;
new_dir->file_count = 0;
dir->subdirs[dir->subdir_count++] = new_dir;
return new_dir;
}
}
return nullptr; // no free directories
}
// rmdir
bool FAT::rmdir(Directory* dir, const char* name) {
for (int i = 0; i < dir->subdir_count; i++) {
Directory* sub = dir->subdirs[i];
if (strcmp(sub->name, name) == 0) {
if (sub->subdir_count > 0 || sub->file_count > 0) return false; // not empty
sub->used = false;
for (int j = i; j < dir->subdir_count - 1; j++) dir->subdirs[j] = dir->subdirs[j+1];
dir->subdir_count--;
return true;
}
}
return false;
}
// recursive touch
Directory* FAT::touch_recursive(Directory* start, const char* path, char* out_name) {
Directory* current = start;
char part[32];
int pi = 0;
int last_slash = -1;
int len = strlen(path);
// find last slash to separate filename
for (int i = 0; i < len; i++)
if (path[i] == '/') last_slash = i;
if (last_slash < 0) {
// no slash → parent is current dir
strncpy(out_name, path, 31);
out_name[31] = '\0';
return current;
}
// copy filename
int idx = 0;
for (int i = last_slash + 1; i < len && idx < 31; i++)
out_name[idx++] = path[i];
out_name[idx] = '\0';
if (is_name_invalid(out_name))
return nullptr;
// resolve parent path
const char* parent_path = path;
char buf[64];
for (int i = 0; i < last_slash && i < 63; i++)
buf[i] = path[i];
buf[last_slash] = '\0';
return find_subdir_recursive(current, buf);
}
// touch uses static pool
File* FAT::touch(Directory* dir, const char* name) {
if (is_name_invalid(name)) return nullptr; // reject empty names
if (dir->file_count >= MAX_FILES) return nullptr;
if (find_file(dir, name)) return nullptr;
for (int i = 0; i < MAX_FILES; i++) {
if (!file_pool[i].used) {
File* f = &file_pool[i];
f->used = true;
strcpy(f->name, name);
f->size = 0;
dir->files[dir->file_count++] = f;
return f;
}
}
return nullptr;
}
// rm
bool FAT::rm(Directory* dir, const char* name) {
for (int i = 0; i < dir->file_count; i++) {
if (strcmp(dir->files[i]->name, name) == 0) {
dir->files[i]->used = false;
for (int j = i; j < dir->file_count - 1; j++) dir->files[j] = dir->files[j+1];
dir->file_count--;
return true;
}
}
return false;
}
// mv
bool FAT::mv(Directory* src_dir, const char* name, Directory* dest_dir) {
File* f = find_file(src_dir, name);
if (!f || dest_dir->file_count >= MAX_FILES) return false;
rm(src_dir, name);
dest_dir->files[dest_dir->file_count++] = f;
return true;
}
// ls
void FAT::ls(Directory* cwd, const char* path) {
Directory* dir = cwd;
if (path && path[0] != '\0') {
dir = find_subdir_recursive(cwd, path);
if (!dir) {
print_str("Error: invalid directory\n");
return;
}
}
print_str("Directories:\n");
if (dir->subdir_count == 0) print_str(" • (none)\n");
for (int i = 0; i < dir->subdir_count; i++) {
print_str(" • ");
print_str(dir->subdirs[i]->name);
print_str("\n");
}
print_str("Files:\n");
if (dir->file_count == 0) print_str(" • (none)\n");
for (int i = 0; i < dir->file_count; i++) {
print_str(" • ");
print_str(dir->files[i]->name);
print_str("\n");
}
}
// Returns the number of used directories in the pool
int FAT::count_used_dirs() const {
int cnt = 0;
for (int i = 0; i < MAX_DIRS; i++) if (dir_pool[i].used) cnt++;
return cnt;
}
// Returns the number of free directories
int FAT::count_free_dirs() const { return MAX_DIRS - count_used_dirs(); }
// Returns the number of used files in the pool
int FAT::count_used_files() const {
int cnt = 0;
for (int i = 0; i < MAX_FILES; i++) if (file_pool[i].used) cnt++;
return cnt;
}
// Returns the number of free files
int FAT::count_free_files() const { return MAX_FILES - count_used_files(); }
// Returns total bytes used by all files
uint32_t FAT::total_file_bytes() const {
uint32_t total = 0;
for (int i = 0; i < MAX_FILES; i++) {
if (file_pool[i].used) total += file_pool[i].size;
}
return total;
}