-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.c
More file actions
188 lines (170 loc) · 5.04 KB
/
Copy pathfs.c
File metadata and controls
188 lines (170 loc) · 5.04 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
#include "fs.h"
typedef struct {
char name[FS_NAME_LEN];
uint32_t size;
uint8_t *data;
int used;
} fs_entry_t;
static fs_entry_t entries[FS_MAX_FILES];
static uint8_t data_pool[FS_POOL_SIZE];
static uint32_t pool_offset;
static int initialized;
extern int shell_run(const char *cmd);
static int strieq(const char *a, const char *b) {
while (*a && *b) { if (*a != *b) return 0; a++; b++; }
return *a == *b;
}
static void strcpy(char *dst, const char *src) {
while (*src) *dst++ = *src++;
*dst = 0;
}
static int strln(const char *s) {
int n = 0;
while (*s++) n++;
return n;
}
static int endswith(const char *name, const char *ext) {
int nl = strln(name), el = strln(ext);
if (nl < el) return 0;
name += nl - el;
while (*name && *ext) {
if (*name != *ext) return 0;
name++; ext++;
}
return 1;
}
int fs_init(void) {
if (initialized) return 1;
for (int i = 0; i < FS_MAX_FILES; i++)
entries[i].used = 0;
pool_offset = 0;
initialized = 1;
return 1;
}
int fs_create(const char *name) {
if (!initialized || !name || !*name) return 0;
if (fs_exists(name)) return 0;
int slot = -1;
for (int i = 0; i < FS_MAX_FILES; i++)
if (!entries[i].used) { slot = i; break; }
if (slot < 0) return 0;
strcpy(entries[slot].name, name);
entries[slot].size = 0;
entries[slot].data = 0;
entries[slot].used = 1;
return 1;
}
int fs_delete(const char *name) {
if (!initialized || !name) return 0;
for (int i = 0; i < FS_MAX_FILES; i++) {
if (entries[i].used && strieq(entries[i].name, name)) {
entries[i].used = 0;
entries[i].data = 0;
entries[i].size = 0;
return 1;
}
}
return 0;
}
int fs_rename(const char *oldname, const char *newname) {
if (!initialized || !oldname || !newname || !*newname) return 0;
for (int i = 0; i < FS_MAX_FILES; i++) {
if (entries[i].used && strieq(entries[i].name, oldname)) {
strcpy(entries[i].name, newname);
return 1;
}
}
return 0;
}
int fs_write(const char *name, const uint8_t *data, uint32_t size) {
if (!initialized || !name || !data) return 0;
if (size > FS_MAX_SIZE) return 0;
int slot = -1;
for (int i = 0; i < FS_MAX_FILES; i++)
if (entries[i].used && strieq(entries[i].name, name)) { slot = i; break; }
if (slot < 0) {
for (int i = 0; i < FS_MAX_FILES; i++)
if (!entries[i].used) { slot = i; break; }
if (slot < 0) return 0;
strcpy(entries[slot].name, name);
entries[slot].used = 1;
entries[slot].size = 0;
entries[slot].data = 0;
}
if (pool_offset + size > FS_POOL_SIZE) return 0;
entries[slot].data = &data_pool[pool_offset];
for (uint32_t i = 0; i < size; i++)
data_pool[pool_offset + i] = data[i];
pool_offset += size;
entries[slot].size = size;
return 1;
}
int fs_read(const char *name, uint8_t *buf, uint32_t max) {
if (!initialized || !name || !buf) return -1;
for (int i = 0; i < FS_MAX_FILES; i++) {
if (entries[i].used && strieq(entries[i].name, name)) {
uint32_t sz = entries[i].size;
if (sz > max) sz = max;
for (uint32_t j = 0; j < sz; j++)
buf[j] = entries[i].data[j];
return (int)sz;
}
}
return -1;
}
uint32_t fs_size(const char *name) {
if (!initialized || !name) return 0;
for (int i = 0; i < FS_MAX_FILES; i++)
if (entries[i].used && strieq(entries[i].name, name))
return entries[i].size;
return 0;
}
int fs_exists(const char *name) {
if (!initialized || !name) return 0;
for (int i = 0; i < FS_MAX_FILES; i++)
if (entries[i].used && strieq(entries[i].name, name))
return 1;
return 0;
}
int fs_list(char names[][FS_NAME_LEN], int max) {
if (!initialized) return 0;
int count = 0;
for (int i = 0; i < FS_MAX_FILES && count < max; i++)
if (entries[i].used)
strcpy(names[count++], entries[i].name);
return count;
}
int fs_run(const char *name) {
if (!initialized || !name) return 0;
uint32_t sz = fs_size(name);
if (sz == 0 || sz >= 4096) return 0;
uint8_t buf[4096];
if (fs_read(name, buf, 4095) < 0) return 0;
buf[sz] = 0;
if (endswith(name, ".py")) {
extern int py_run(const char *src);
py_run((const char *)buf);
return 1;
}
if (endswith(name, ".sh")) {
char *p = (char *)buf;
while (*p) {
while (*p == ' ' || *p == '\t') p++;
if (*p == '#' || *p == '\n' || *p == '\r') {
while (*p && *p != '\n') p++;
if (*p == '\n') p++;
continue;
}
char line[256];
int li = 0;
while (*p && *p != '\n' && li < 255)
line[li++] = *p++;
line[li] = 0;
if (*p == '\n') p++;
if (li > 0)
shell_run(line);
}
return 1;
}
return 0;
}