-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstd_file.cpp
364 lines (306 loc) · 7.27 KB
/
std_file.cpp
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
/*
* author : Shuichi TAKANO
* since : Sun Aug 18 2019 15:29:28
*/
#include <stdio.h>
#include <array>
#include <fcntl.h>
#include <sd_card/ff.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <syslog.h>
namespace
{
constexpr const char *TAG = "STDFILE";
template <class T, int N>
class Allocator
{
std::array<T, N> objs_;
uint32_t freeObjMask_ = (1 << N) - 1;
public:
int allocate()
{
for (int i = 0; i < N; ++i)
{
if (freeObjMask_ & (1 << i))
{
freeObjMask_ &= ~(1 << i);
return i;
}
}
return -1;
}
void free(int i)
{
freeObjMask_ |= (1 << i);
}
void free(T *p)
{
free(p - objs_.data());
}
int getIndex(T *p)
{
return p - objs_.data();
}
T &get(int i)
{
return objs_[i];
}
static Allocator &instance()
{
static Allocator inst;
return inst;
}
};
constexpr int FD_OFFSET = 1024;
constexpr int MAX_FILES = 8;
constexpr int MAX_DIRS = 8;
std::array<dirent, MAX_DIRS> dirents_;
Allocator<FIL, MAX_FILES> &getFileAllocator()
{
return Allocator<FIL, MAX_FILES>::instance();
}
Allocator<DIR, MAX_FILES> &getDirAllocator()
{
return Allocator<DIR, MAX_DIRS>::instance();
}
int allocateFile()
{
auto i = getFileAllocator().allocate();
if (i < 0)
{
LOGV(TAG, "file overflow");
return -1;
}
return i + FD_OFFSET;
}
void freeFile(int fp)
{
fp -= FD_OFFSET;
getFileAllocator().free(fp);
}
FIL *getFile(int fp)
{
return &getFileAllocator().get(fp - FD_OFFSET);
}
int allocateDir()
{
auto i = getDirAllocator().allocate();
if (i < 0)
{
LOGV(TAG, "dir overflow");
return -1;
}
return i;
}
void freeDir(int i)
{
getDirAllocator().free(i);
}
void freeDir(DIR *d)
{
getDirAllocator().free(d);
}
DIR *getDir(int i)
{
return &getDirAllocator().get(i);
}
} // namespace
extern "C"
{
int
sys_file_open(const char *name, int flags, int mode)
{
// LOGV(TAG, "open(%s, %d, %d)", name, flags, mode);
int ffmode = 0;
if ((flags & 3) == O_RDONLY)
{
// LOGV(TAG, " read only.\n");
ffmode |= FA_READ;
}
if ((flags & 3) == O_WRONLY)
{
// LOGV(TAG, " write only.\n");
ffmode |= FA_WRITE;
}
if ((flags & 3) == O_RDWR)
{
// LOGV(TAG, " read/write.\n");
ffmode |= FA_READ | FA_WRITE;
}
if (flags & O_CREAT)
{
// LOGV(TAG, " create.\n");
ffmode |= FA_CREATE_NEW;
}
if (flags & O_TRUNC)
{
// LOGV(TAG, " trunc.\n");
ffmode |= FA_CREATE_ALWAYS;
}
int fp = allocateFile();
if (fp < 0)
{
return -EBADF;
}
auto fil = getFile(fp);
if (f_open(fil, name, ffmode) != FR_OK)
{
freeFile(fp);
return -ENOENT;
}
return fp;
}
ssize_t
sys_file_write(int file, const void *ptr, size_t len)
{
// LOGV(TAG, "file_write(%d, %p, %zd)\n", file, ptr, len);
UINT count;
f_write(getFile(file), ptr, len, &count);
return count;
}
ssize_t
sys_file_read(int file, void *ptr, size_t len)
{
// LOGV(TAG, "file_read(%d, %p, %zd)\n", file, ptr, len);
UINT count;
f_read(getFile(file), ptr, len, &count);
return count;
}
int
sys_file_fstat(int file, struct stat *st)
{
// LOGV(TAG, "file_fstat(%d, %p)\n", file, st);
auto fil = getFile(file);
memset(st, 0, sizeof(*st));
st->st_size = f_size(fil);
st->st_mode |= _IFREG;
return 0;
}
int
sys_file_stat(const char *name, struct stat *st)
{
// LOGV(TAG, "file_stat(%s, %p)", name, st);
FILINFO fi;
if (f_stat(name, &fi) != FR_OK)
{
return -1;
}
memset(st, 0, sizeof(*st));
st->st_size = fi.fsize;
if (fi.fattrib & AM_DIR)
{
st->st_mode |= _IFDIR;
}
else
{
st->st_mode |= _IFREG;
}
// LOGV(TAG, "st: %p, %p, %zd, d %d\n", st, st + 1, sizeof(*st), S_ISDIR(st->st_mode));
return 0;
}
int
sys_file_close(int file)
{
// LOGV(TAG, "file_close(%d)\n", file);
f_close(getFile(file));
freeFile(file);
return 0;
}
ssize_t
sys_file_lseek(int file, ssize_t ptr, int dir)
{
// LOGV(TAG, "file_lseek(%d, %d, %d)", file, (int)ptr, dir);
auto fil = getFile(file);
switch (dir)
{
case SEEK_CUR:
ptr += f_tell(fil);
break;
case SEEK_END:
ptr += f_size(fil);
break;
default:
break;
}
if (f_lseek(fil, ptr) != FR_OK)
{
return -1;
}
return f_tell(fil);
}
////
// void rewind(FILE *fp)
// {
// fseek(fp, 0L, SEEK_SET);
// }
DIR *opendir(const char *name)
{
// printf("opendir: %s\n", name);
auto di = allocateDir();
if (di < 0)
{
return nullptr;
}
auto *d = getDir(di);
if (f_opendir(d, name) != FR_OK)
{
freeDir(di);
return 0;
}
return d;
}
int closedir(DIR *dir)
{
// printf("closedir: %p\n", dir);
freeDir(dir);
return 0;
}
struct dirent *readdir(DIR *dir)
{
// printf("readdir: %p\n", dir);
FILINFO fi;
do
{
if (f_readdir(dir, &fi) != FR_OK)
{
printf("read dir error.\n");
}
if (!fi.fname[0])
{
return nullptr;
}
} while (fi.fattrib & AM_HID);
int di = getDirAllocator().getIndex(dir);
auto &d = dirents_[di];
// printf("%d:%p:%p: name = %s\n", di, &d, d.d_name, fi.fname);
strcpy(d.d_name, fi.fname);
return &d;
}
void rewinddir(DIR *dir)
{
// printf("rewinddir: %p\n", dir);
if (f_rewinddir(dir) != FR_OK)
{
printf("rewind dir error.\n");
}
}
std::string currentDir_;
int chdir(const char *path)
{
// printf("chdir: %s\n", path);
f_chdir(path);
currentDir_ = path;
return 0;
}
char *getcwd(char *buffer, size_t size)
{
if (size)
{
strncpy(buffer, currentDir_.c_str(), size);
// f_getcwd(buffer, size);
}
return buffer;
}
}