-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfs_littlefs.c
341 lines (277 loc) · 8.47 KB
/
fs_littlefs.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
/*
fs_littlefs.c - VFS wrapper/mount for littlefs
Part of grblHAL
Copyright (c) 2022-2024 Terje Io
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(ARDUINO)
#include "../driver.h"
#else
#include "driver.h"
#endif
#if LITTLEFS_ENABLE
#if defined(ARDUINO)
#include "../grbl/protocol.h"
#include "../grbl/platform.h"
#include "../grbl/vfs.h"
#else
#include "../grbl/protocol.h"
#include "../grbl/platform.h"
#include "../grbl/vfs.h"
#endif
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include "../littlefs/lfs.h"
#include "../littlefs/lfs_util.h"
#define ATTR_TIMESTAMP 0x74 // 't'
typedef struct time_file {
lfs_file_t file;
bool modified;
time_t timestamp;
struct lfs_attr attrs[1];
struct lfs_file_config cfg;
} time_file_t;
static lfs_t lfs = {0};
static bool is_rootfs;
static const struct lfs_config *lfs_config;
static vfs_file_t *fs_open (const char *filename, const char *mode)
{
int flags = 0;
vfs_file_t *file = malloc(sizeof(vfs_file_t) + sizeof(time_file_t));
if(file) {
time_file_t *f = (time_file_t *)&file->handle;
// set up description of timestamp attribute
f->modified = false;
f->timestamp = 0;
f->attrs[0].type = ATTR_TIMESTAMP;
f->attrs[0].buffer = &f->timestamp;
f->attrs[0].size = sizeof(time_t);
// set up config to indicate file has custom attributes
memset(&f->cfg, 0, sizeof(struct lfs_file_config));
f->cfg.attrs = f->attrs;
f->cfg.attr_count = 1;
while (*mode != '\0') {
if (*mode == 'r')
flags |= LFS_O_RDONLY;
else if (*mode == 'w') {
flags |= LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC;
if(hal.rtc.get_datetime) {
struct tm dt;
if(hal.rtc.get_datetime(&dt))
f->timestamp = mktime(&dt);
}
} else if (*mode == 'a')
flags |= LFS_O_APPEND;
mode++;
}
if((vfs_errno = lfs_file_opencfg(&lfs, &f->file, filename, flags, &f->cfg)) != LFS_ERR_OK) {
free(file);
file = NULL;
} else
file->size = lfs_file_size(&lfs, &f->file);
}
return file;
}
static void fs_close (vfs_file_t *file)
{
time_file_t *f = (time_file_t *)&file->handle;
if(f->modified && hal.rtc.get_datetime) {
struct tm dt;
if(hal.rtc.get_datetime(&dt))
f->timestamp = mktime(&dt);
}
lfs_file_close(&lfs, &f->file);
free(file);
}
static size_t fs_read (void *buffer, size_t size, size_t count, vfs_file_t *file)
{
return lfs_file_read(&lfs, &((time_file_t *)&file->handle)->file, buffer, size * count);
}
static size_t fs_write (const void *buffer, size_t size, size_t count, vfs_file_t *file)
{
time_file_t *f = (time_file_t *)&file->handle;
f->modified = true;
return lfs_file_write(&lfs, &f->file, buffer, size * count);
}
static size_t fs_tell (vfs_file_t *file)
{
return lfs_file_tell(&lfs, &((time_file_t *)&file->handle)->file);
}
static int fs_seek (vfs_file_t *file, size_t offset)
{
return lfs_file_seek(&lfs, &((time_file_t *)&file->handle)->file, offset, LFS_SEEK_SET);
}
static bool fs_eof (vfs_file_t *file)
{
return lfs_file_tell(&lfs, &((time_file_t *)&file->handle)->file) == file->size;
}
static int fs_rename (const char *from, const char *to)
{
return lfs_rename(&lfs, from, to);
}
static int fs_unlink (const char *filename)
{
return lfs_remove(&lfs, filename);
}
static int fs_mkdir (const char *path)
{
int res;
if((res = lfs_mkdir(&lfs, path)) == LFS_ERR_OK) {
struct tm dt;
if(hal.rtc.get_datetime && hal.rtc.get_datetime(&dt)) {
time_t t = mktime(&dt);
lfs_setattr(&lfs, path, ATTR_TIMESTAMP, &t, sizeof(time_t));
}
}
return res;
}
static int fs_chdir (const char *path)
{
#if FF_FS_RPATH
return f_chdir(path);
#else
return is_rootfs && !strcmp(path, "/") ? 0 : -1;
#endif
}
/*
static char *fs_getcwd (char *buf, size_t size)
{
static char cwd[255];
#if FF_FS_RPATH
if ((vfs_errno = f_getcwd(cwd, 255)) == FR_OK) {
char *s1, *s2;
// Strip drive information
if((s2 = strchr(cwd, ':'))) {
s1 = cwd;
s2++;
while(*s2)
*s1++ = *s2++;
*s1 = '\0';
}
}
#else
*cwd = '\0'; // TODO: return mount path?
#endif
return cwd;
}
*/
static vfs_dir_t *fs_opendir (const char *path)
{
vfs_dir_t *dir = malloc(sizeof(vfs_dir_t) + sizeof(lfs_dir_t));
if (dir && (vfs_errno = lfs_dir_open(&lfs, (lfs_dir_t *)&dir->handle, path)) != LFS_ERR_OK)
{
free(dir);
dir = NULL;
}
return dir;
}
static char *fs_readdir (vfs_dir_t *dir, vfs_dirent_t *dirent)
{
static struct lfs_info f;
*dirent->name = '\0';
if ((vfs_errno = lfs_dir_read(&lfs, (lfs_dir_t *)&dir->handle, &f)) <= 0)
return NULL;
if(!strcmp(f.name, ".") && (vfs_errno = lfs_dir_read(&lfs, (lfs_dir_t *)&dir->handle, &f)) <= 0)
return NULL;
if(!strcmp(f.name, "..") && (vfs_errno = lfs_dir_read(&lfs, (lfs_dir_t *)&dir->handle, &f)) <= 0)
return NULL;
if(f.name && *f.name != '\0')
strcpy(dirent->name, f.name);
vfs_errno = 0;
dirent->size = f.size;
dirent->st_mode.mode = 0;
dirent->st_mode.directory = f.type == LFS_TYPE_DIR;
return *f.name ? dirent->name : NULL;
}
static void fs_closedir (vfs_dir_t *dir)
{
if (dir) {
vfs_errno = lfs_dir_close(&lfs, (lfs_dir_t *)&dir->handle);
free(dir);
}
}
static int fs_stat (const char *filename, vfs_stat_t *st)
{
struct lfs_info f;
if ((vfs_errno = lfs_stat(&lfs, filename, &f)) == LFS_ERR_OK) {
st->st_size = f.size;
st->st_mode.mode = 0;
st->st_mode.directory = f.type == LFS_TYPE_DIR;
#if ESP_PLATFORM
if(lfs_getattr(&lfs, filename, ATTR_TIMESTAMP, &st->st_mtim, sizeof(time_t)) != sizeof(time_t))
st->st_mtim = (time_t)0;
#else
if(lfs_getattr(&lfs, filename, ATTR_TIMESTAMP, &st->st_mtime, sizeof(time_t)) != sizeof(time_t))
st->st_mtime = (time_t)0;
#endif
} else
return -1;
return 0;
}
static int fs_utime (const char *filename, struct tm *modified)
{
time_t t = mktime(modified);
return lfs_setattr(&lfs, filename, ATTR_TIMESTAMP, &t, sizeof(time_t));
}
static bool fs_getfree (vfs_free_t *free)
{
free->size = lfs_config->block_count * lfs_config->block_size;
free->used = lfs_fs_size(&lfs) * lfs_config->block_size;
return true;
}
static int fs_format (void)
{
int ret = lfs_format(&lfs, lfs_config);
lfs_mount(&lfs, lfs_config);
return ret;
}
void fs_littlefs_mount (const char *path, const struct lfs_config *config)
{
static const vfs_t littlefs = {
.fs_name = "littlefs",
.fopen = fs_open,
.fclose = fs_close,
.fread = fs_read,
.fwrite = fs_write,
.ftell = fs_tell,
.fseek = fs_seek,
.feof = fs_eof,
.frename = fs_rename,
.funlink = fs_unlink,
.fmkdir = fs_mkdir,
.fchdir = fs_chdir,
.frmdir = fs_unlink,
.fopendir = fs_opendir,
.readdir = fs_readdir,
.fclosedir = fs_closedir,
.fstat = fs_stat,
.futime = fs_utime,
// .fgetcwd = fs_getcwd,
.fgetfree = fs_getfree,
.format = fs_format
};
if((lfs_config = config) == NULL)
return;
if (lfs_mount(&lfs, config) != LFS_ERR_OK)
lfs_format(&lfs, config);
if (lfs_mount(&lfs, config) == LFS_ERR_OK) {
vfs_st_mode_t mode = {0};
mode.hidden = settings.fs_options.lfs_hidden;
is_rootfs = !strcmp(path, "/");
hal.driver_cap.littlefs = vfs_mount(path, &littlefs, mode);
} else
protocol_enqueue_foreground_task(report_warning, "LittleFS mount failed!");
}
#endif // LITTLEFS_ENABLE