blob: 67c95d9a1712b8a465174c5ecb0a60fdf3f6334b [file] [log] [blame]
Paul Sokolovskye9be6a32016-02-13 22:51:21 +02001/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2014 Damien P. George
7 * Copyright (c) 2016 Paul Sokolovsky
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28#include "py/mpconfig.h"
29#if MICROPY_VFS_FAT
30
Andrew Leech4e0964b2022-09-09 09:48:01 +100031#if !MICROPY_ENABLE_FINALISER
32#error "MICROPY_VFS_FAT requires MICROPY_ENABLE_FINALISER"
33#endif
34
Damien Georgefb3ae172017-01-27 15:13:32 +110035#if !MICROPY_VFS
36#error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_VFS"
37#endif
38
Paul Sokolovskycac6c972016-05-29 18:23:59 +030039#include <string.h>
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020040#include "py/runtime.h"
Alex Marchd02f3a52016-09-28 14:51:35 +010041#include "py/mperrno.h"
Damien Georgef5f4cda2016-06-01 17:00:28 +010042#include "lib/oofatfs/ff.h"
Damien George32a11382017-01-27 15:04:17 +110043#include "extmod/vfs_fat.h"
Damien George136369d2021-07-09 14:19:15 +100044#include "shared/timeutils/timeutils.h"
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020045
Damien Georgee959f212019-02-25 23:46:03 +110046#if FF_MAX_SS == FF_MIN_SS
47#define SECSIZE(fs) (FF_MIN_SS)
Damien George8aa8a0a2017-01-27 22:42:06 +110048#else
49#define SECSIZE(fs) ((fs)->ssize)
50#endif
51
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020052#define mp_obj_fat_vfs_t fs_user_mount_t
53
Damien Georgec117eff2018-06-06 14:24:23 +100054STATIC mp_import_stat_t fat_vfs_import_stat(void *vfs_in, const char *path) {
55 fs_user_mount_t *vfs = vfs_in;
Damien George638b8602018-02-23 17:24:57 +110056 FILINFO fno;
57 assert(vfs != NULL);
58 FRESULT res = f_stat(&vfs->fatfs, path, &fno);
59 if (res == FR_OK) {
60 if ((fno.fattrib & AM_DIR) != 0) {
61 return MP_IMPORT_STAT_DIR;
62 } else {
63 return MP_IMPORT_STAT_FILE;
64 }
65 }
66 return MP_IMPORT_STAT_NO_EXIST;
67}
68
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020069STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien Georgefb3ae172017-01-27 15:13:32 +110070 mp_arg_check_num(n_args, n_kw, 1, 1, false);
71
72 // create new object
Jim Mussared0e7bfc82022-04-22 17:09:15 +100073 fs_user_mount_t *vfs = mp_obj_malloc(fs_user_mount_t, type);
Damien Georgefb3ae172017-01-27 15:13:32 +110074 vfs->fatfs.drv = vfs;
75
Damien Georgee1c7b1c2019-09-08 22:01:09 +100076 // Initialise underlying block device
77 vfs->blockdev.flags = MP_BLOCKDEV_FLAG_FREE_OBJ;
Damien Georgecfe1c5a2019-10-29 12:25:30 +110078 vfs->blockdev.block_size = FF_MIN_SS; // default, will be populated by call to MP_BLOCKDEV_IOCTL_BLOCK_SIZE
Damien Georgee1c7b1c2019-09-08 22:01:09 +100079 mp_vfs_blockdev_init(&vfs->blockdev, args[0]);
Damien Georgefb3ae172017-01-27 15:13:32 +110080
Damien George12ad64b2017-11-16 16:01:47 +110081 // mount the block device so the VFS methods can be used
82 FRESULT res = f_mount(&vfs->fatfs);
83 if (res == FR_NO_FILESYSTEM) {
84 // don't error out if no filesystem, to let mkfs()/mount() create one if wanted
Damien George9aabb6c2019-09-07 14:03:41 +100085 vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_NO_FILESYSTEM;
Damien George12ad64b2017-11-16 16:01:47 +110086 } else if (res != FR_OK) {
87 mp_raise_OSError(fresult_to_errno_table[res]);
88 }
89
Paul Sokolovsky1bb15ca2016-02-14 16:21:27 +020090 return MP_OBJ_FROM_PTR(vfs);
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020091}
92
Damien George12ad64b2017-11-16 16:01:47 +110093#if _FS_REENTRANT
94STATIC mp_obj_t fat_vfs_del(mp_obj_t self_in) {
95 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(self_in);
96 // f_umount only needs to be called to release the sync object
97 f_umount(&self->fatfs);
98 return mp_const_none;
99}
100STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_del_obj, fat_vfs_del);
101#endif
102
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200103STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) {
Damien Georgefb3ae172017-01-27 15:13:32 +1100104 // create new object
105 fs_user_mount_t *vfs = MP_OBJ_TO_PTR(fat_vfs_make_new(&mp_fat_vfs_type, 1, 0, &bdev_in));
106
107 // make the filesystem
Damien Georgee959f212019-02-25 23:46:03 +1100108 uint8_t working_buf[FF_MAX_SS];
Damien Georgefb3ae172017-01-27 15:13:32 +1100109 FRESULT res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
Andrew Leech74d07462019-03-25 11:20:54 +1100110 if (res == FR_MKFS_ABORTED) { // Probably doesn't support FAT16
111 res = f_mkfs(&vfs->fatfs, FM_FAT32, 0, working_buf, sizeof(working_buf));
112 }
Damien Georgefb3ae172017-01-27 15:13:32 +1100113 if (res != FR_OK) {
114 mp_raise_OSError(fresult_to_errno_table[res]);
115 }
116
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200117 return mp_const_none;
118}
119STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs);
120STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj));
121
Damien Georgeae4a0772018-02-23 17:17:32 +1100122typedef struct _mp_vfs_fat_ilistdir_it_t {
123 mp_obj_base_t base;
124 mp_fun_1_t iternext;
Andrew Leech4e0964b2022-09-09 09:48:01 +1000125 mp_fun_1_t finaliser;
Damien Georgeae4a0772018-02-23 17:17:32 +1100126 bool is_str;
127 FF_DIR dir;
128} mp_vfs_fat_ilistdir_it_t;
129
130STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
131 mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
132
133 for (;;) {
134 FILINFO fno;
135 FRESULT res = f_readdir(&self->dir, &fno);
136 char *fn = fno.fname;
137 if (res != FR_OK || fn[0] == 0) {
138 // stop on error or end of dir
139 break;
140 }
141
142 // Note that FatFS already filters . and .., so we don't need to
143
Tom Collins4d3a92c2018-03-08 16:02:26 -0800144 // make 4-tuple with info about this entry
145 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
Damien Georgeae4a0772018-02-23 17:17:32 +1100146 if (self->is_str) {
147 t->items[0] = mp_obj_new_str(fn, strlen(fn));
148 } else {
Damien George69661f32020-02-27 15:36:53 +1100149 t->items[0] = mp_obj_new_bytes((const byte *)fn, strlen(fn));
Damien Georgeae4a0772018-02-23 17:17:32 +1100150 }
151 if (fno.fattrib & AM_DIR) {
152 // dir
153 t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
154 } else {
155 // file
156 t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
157 }
158 t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
Tom Collins4d3a92c2018-03-08 16:02:26 -0800159 t->items[3] = mp_obj_new_int_from_uint(fno.fsize);
Damien Georgeae4a0772018-02-23 17:17:32 +1100160
161 return MP_OBJ_FROM_PTR(t);
162 }
163
164 // ignore error because we may be closing a second time
165 f_closedir(&self->dir);
166
167 return MP_OBJ_STOP_ITERATION;
168}
169
Andrew Leech4e0964b2022-09-09 09:48:01 +1000170STATIC mp_obj_t mp_vfs_fat_ilistdir_it_del(mp_obj_t self_in) {
171 mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
172 // ignore result / error because we may be closing a second time.
173 f_closedir(&self->dir);
174 return mp_const_none;
175}
176
Damien Georged4cd4832017-05-05 23:32:44 +1000177STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100178 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200179 bool is_str_type = true;
180 const char *path;
181 if (n_args == 2) {
182 if (mp_obj_get_type(args[1]) == &mp_type_bytes) {
183 is_str_type = false;
184 }
185 path = mp_obj_str_get_str(args[1]);
186 } else {
187 path = "";
188 }
189
Damien Georgeae4a0772018-02-23 17:17:32 +1100190 // Create a new iterator object to list the dir
Damien Georgecae690d2024-02-16 11:02:58 +1100191 mp_vfs_fat_ilistdir_it_t *iter = mp_obj_malloc_with_finaliser(mp_vfs_fat_ilistdir_it_t, &mp_type_polymorph_iter_with_finaliser);
Damien Georgeae4a0772018-02-23 17:17:32 +1100192 iter->iternext = mp_vfs_fat_ilistdir_it_iternext;
Andrew Leech4e0964b2022-09-09 09:48:01 +1000193 iter->finaliser = mp_vfs_fat_ilistdir_it_del;
Damien Georgeae4a0772018-02-23 17:17:32 +1100194 iter->is_str = is_str_type;
195 FRESULT res = f_opendir(&self->fatfs, &iter->dir, path);
196 if (res != FR_OK) {
197 mp_raise_OSError(fresult_to_errno_table[res]);
198 }
199
200 return MP_OBJ_FROM_PTR(iter);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200201}
Damien Georged4cd4832017-05-05 23:32:44 +1000202STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_ilistdir_func);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200203
Damien Georgef5f4cda2016-06-01 17:00:28 +0100204STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) {
205 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200206 const char *path = mp_obj_str_get_str(path_in);
Alex Marchd02f3a52016-09-28 14:51:35 +0100207
208 FILINFO fno;
Damien Georgef5f4cda2016-06-01 17:00:28 +0100209 FRESULT res = f_stat(&self->fatfs, path, &fno);
Alex Marchd02f3a52016-09-28 14:51:35 +0100210
211 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100212 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200213 }
Alex Marchd02f3a52016-09-28 14:51:35 +0100214
215 // check if path is a file or directory
216 if ((fno.fattrib & AM_DIR) == attr) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100217 res = f_unlink(&self->fatfs, path);
Alex Marchd02f3a52016-09-28 14:51:35 +0100218
219 if (res != FR_OK) {
220 mp_raise_OSError(fresult_to_errno_table[res]);
221 }
222 return mp_const_none;
223 } else {
224 mp_raise_OSError(attr ? MP_ENOTDIR : MP_EISDIR);
225 }
226}
227
228STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100229 return fat_vfs_remove_internal(vfs_in, path_in, 0); // 0 == file attribute
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200230}
231STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
232
Paul Sokolovsky0a6f5992016-07-16 03:46:42 +0300233STATIC mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100234 return fat_vfs_remove_internal(vfs_in, path_in, AM_DIR);
Paul Sokolovsky0a6f5992016-07-16 03:46:42 +0300235}
236STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir);
237
Paul Sokolovskye0821832016-02-29 01:22:38 +0200238STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100239 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskye0821832016-02-29 01:22:38 +0200240 const char *old_path = mp_obj_str_get_str(path_in);
241 const char *new_path = mp_obj_str_get_str(path_out);
Damien Georgef5f4cda2016-06-01 17:00:28 +0100242 FRESULT res = f_rename(&self->fatfs, old_path, new_path);
Damien Georgeb7df3e52016-12-02 15:06:09 +1100243 if (res == FR_EXIST) {
244 // if new_path exists then try removing it (but only if it's a file)
Damien Georgef5f4cda2016-06-01 17:00:28 +0100245 fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute
Damien Georgeb7df3e52016-12-02 15:06:09 +1100246 // try to rename again
Damien Georgef5f4cda2016-06-01 17:00:28 +0100247 res = f_rename(&self->fatfs, old_path, new_path);
Damien Georgeb7df3e52016-12-02 15:06:09 +1100248 }
Robert HH7c004e72016-05-27 22:28:00 +0200249 if (res == FR_OK) {
250 return mp_const_none;
251 } else {
Damien George620c4c32016-10-07 13:44:55 +1100252 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskye0821832016-02-29 01:22:38 +0200253 }
254
255}
256STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename);
257
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200258STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100259 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200260 const char *path = mp_obj_str_get_str(path_o);
Damien Georgef5f4cda2016-06-01 17:00:28 +0100261 FRESULT res = f_mkdir(&self->fatfs, path);
Robert HH7c004e72016-05-27 22:28:00 +0200262 if (res == FR_OK) {
263 return mp_const_none;
264 } else {
Damien George620c4c32016-10-07 13:44:55 +1100265 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200266 }
267}
268STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);
269
Damien George4791d292021-05-06 12:11:51 +1000270// Change current directory.
Paul Sokolovskyee5e3f62016-05-29 18:52:41 +0300271STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100272 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300273 const char *path;
274 path = mp_obj_str_get_str(path_in);
275
Damien Georgef5f4cda2016-06-01 17:00:28 +0100276 FRESULT res = f_chdir(&self->fatfs, path);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300277
278 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100279 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300280 }
281
282 return mp_const_none;
283}
Paul Sokolovskyee5e3f62016-05-29 18:52:41 +0300284STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300285
Damien George4791d292021-05-06 12:11:51 +1000286// Get the current directory.
Paul Sokolovskyee5e3f62016-05-29 18:52:41 +0300287STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100288 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300289 char buf[MICROPY_ALLOC_PATH_MAX + 1];
Damien Georgefb3ae172017-01-27 15:13:32 +1100290 FRESULT res = f_getcwd(&self->fatfs, buf, sizeof(buf));
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300291 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100292 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300293 }
Damien George46017592017-11-16 13:17:51 +1100294 return mp_obj_new_str(buf, strlen(buf));
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300295}
Paul Sokolovskyee5e3f62016-05-29 18:52:41 +0300296STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300297
Damien George4791d292021-05-06 12:11:51 +1000298// Get the status of a file or directory.
Robert HHee009d72016-05-30 21:09:20 +0200299STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100300 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Robert HHee009d72016-05-30 21:09:20 +0200301 const char *path = mp_obj_str_get_str(path_in);
302
303 FILINFO fno;
Damien Georgefb3ae172017-01-27 15:13:32 +1100304 if (path[0] == 0 || (path[0] == '/' && path[1] == 0)) {
Robert HHee009d72016-05-30 21:09:20 +0200305 // stat root directory
306 fno.fsize = 0;
Robert HH23067a12016-06-16 18:17:59 +0200307 fno.fdate = 0x2821; // Jan 1, 2000
Robert HHee009d72016-05-30 21:09:20 +0200308 fno.ftime = 0;
309 fno.fattrib = AM_DIR;
310 } else {
Damien Georgefb3ae172017-01-27 15:13:32 +1100311 FRESULT res = f_stat(&self->fatfs, path, &fno);
Robert HHee009d72016-05-30 21:09:20 +0200312 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100313 mp_raise_OSError(fresult_to_errno_table[res]);
Robert HHee009d72016-05-30 21:09:20 +0200314 }
315 }
316
317 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
318 mp_int_t mode = 0;
319 if (fno.fattrib & AM_DIR) {
Damien Georged70f6882017-05-10 12:30:34 +1000320 mode |= MP_S_IFDIR;
Robert HHee009d72016-05-30 21:09:20 +0200321 } else {
Damien Georged70f6882017-05-10 12:30:34 +1000322 mode |= MP_S_IFREG;
Robert HHee009d72016-05-30 21:09:20 +0200323 }
Damien George8f20cdc2020-09-14 12:15:03 +1000324 mp_int_t seconds = timeutils_seconds_since_epoch(
Robert HHee009d72016-05-30 21:09:20 +0200325 1980 + ((fno.fdate >> 9) & 0x7f),
326 (fno.fdate >> 5) & 0x0f,
327 fno.fdate & 0x1f,
328 (fno.ftime >> 11) & 0x1f,
329 (fno.ftime >> 5) & 0x3f,
330 2 * (fno.ftime & 0x1f)
Damien George69661f32020-02-27 15:36:53 +1100331 );
Robert HHee009d72016-05-30 21:09:20 +0200332 t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
333 t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
334 t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
335 t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
336 t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
337 t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
Damien George4c736ea2017-08-21 20:47:22 +1000338 t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
Damien Georgec70e5992020-08-31 14:25:20 +1000339 t->items[7] = mp_obj_new_int_from_uint(seconds); // st_atime
340 t->items[8] = mp_obj_new_int_from_uint(seconds); // st_mtime
341 t->items[9] = mp_obj_new_int_from_uint(seconds); // st_ctime
Robert HHee009d72016-05-30 21:09:20 +0200342
343 return MP_OBJ_FROM_PTR(t);
344}
345STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_stat_obj, fat_vfs_stat);
346
Alex Marchdcf14c12016-09-12 18:13:44 +0100347// Get the status of a VFS.
348STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100349 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
350 (void)path_in;
Alex Marchdcf14c12016-09-12 18:13:44 +0100351
Alex Marchdcf14c12016-09-12 18:13:44 +0100352 DWORD nclst;
Damien Georgef5f4cda2016-06-01 17:00:28 +0100353 FATFS *fatfs = &self->fatfs;
354 FRESULT res = f_getfree(fatfs, &nclst);
Alex Marchdcf14c12016-09-12 18:13:44 +0100355 if (FR_OK != res) {
Damien George620c4c32016-10-07 13:44:55 +1100356 mp_raise_OSError(fresult_to_errno_table[res]);
Alex Marchdcf14c12016-09-12 18:13:44 +0100357 }
358
359 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
360
Damien George8aa8a0a2017-01-27 22:42:06 +1100361 t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * SECSIZE(fatfs)); // f_bsize
Alex Marchdcf14c12016-09-12 18:13:44 +0100362 t->items[1] = t->items[0]; // f_frsize
Damien Georgef7816182017-03-29 12:53:35 +1100363 t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2)); // f_blocks
Alex Marchdcf14c12016-09-12 18:13:44 +0100364 t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree
365 t->items[4] = t->items[3]; // f_bavail
366 t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files
367 t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree
368 t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail
369 t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
Damien Georgee959f212019-02-25 23:46:03 +1100370 t->items[9] = MP_OBJ_NEW_SMALL_INT(FF_MAX_LFN); // f_namemax
Alex Marchdcf14c12016-09-12 18:13:44 +0100371
372 return MP_OBJ_FROM_PTR(t);
373}
374STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs);
375
Damien Georgefb3ae172017-01-27 15:13:32 +1100376STATIC mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) {
377 fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
378
379 // Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
380 // User can specify read-only device by:
381 // 1. readonly=True keyword argument
382 // 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
383 if (mp_obj_is_true(readonly)) {
Damien George9aabb6c2019-09-07 14:03:41 +1000384 self->blockdev.writeblocks[0] = MP_OBJ_NULL;
Damien Georgefb3ae172017-01-27 15:13:32 +1100385 }
386
Damien Georgefb3ae172017-01-27 15:13:32 +1100387 // check if we need to make the filesystem
Damien George9aabb6c2019-09-07 14:03:41 +1000388 FRESULT res = (self->blockdev.flags & MP_BLOCKDEV_FLAG_NO_FILESYSTEM) ? FR_NO_FILESYSTEM : FR_OK;
Damien Georgefb3ae172017-01-27 15:13:32 +1100389 if (res == FR_NO_FILESYSTEM && mp_obj_is_true(mkfs)) {
Damien Georgee959f212019-02-25 23:46:03 +1100390 uint8_t working_buf[FF_MAX_SS];
Damien Georgefb3ae172017-01-27 15:13:32 +1100391 res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
392 }
393 if (res != FR_OK) {
394 mp_raise_OSError(fresult_to_errno_table[res]);
395 }
Damien George9aabb6c2019-09-07 14:03:41 +1000396 self->blockdev.flags &= ~MP_BLOCKDEV_FLAG_NO_FILESYSTEM;
Damien Georgefb3ae172017-01-27 15:13:32 +1100397
Radomir Dopieralskid29ca282016-08-22 16:10:34 +0200398 return mp_const_none;
399}
Damien Georgefb3ae172017-01-27 15:13:32 +1100400STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_fat_mount_obj, vfs_fat_mount);
401
402STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
Damien George12ad64b2017-11-16 16:01:47 +1100403 (void)self_in;
404 // keep the FAT filesystem mounted internally so the VFS methods can still be used
Damien Georgefb3ae172017-01-27 15:13:32 +1100405 return mp_const_none;
406}
407STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
Radomir Dopieralskid29ca282016-08-22 16:10:34 +0200408
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200409STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
Damien George12ad64b2017-11-16 16:01:47 +1100410 #if _FS_REENTRANT
411 { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&fat_vfs_del_obj) },
412 #endif
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200413 { MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
414 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
Damien Georged4cd4832017-05-05 23:32:44 +1000415 { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&fat_vfs_ilistdir_obj) },
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200416 { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&fat_vfs_mkdir_obj) },
Paul Sokolovsky0a6f5992016-07-16 03:46:42 +0300417 { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&fat_vfs_rmdir_obj) },
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300418 { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&fat_vfs_chdir_obj) },
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300419 { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&fat_vfs_getcwd_obj) },
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200420 { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&fat_vfs_remove_obj) },
Paul Sokolovskye0821832016-02-29 01:22:38 +0200421 { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) },
Robert HHee009d72016-05-30 21:09:20 +0200422 { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&fat_vfs_stat_obj) },
Alex Marchdcf14c12016-09-12 18:13:44 +0100423 { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
Damien Georgefb3ae172017-01-27 15:13:32 +1100424 { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
Radomir Dopieralskid29ca282016-08-22 16:10:34 +0200425 { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200426};
427STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
428
Damien Georgec117eff2018-06-06 14:24:23 +1000429STATIC const mp_vfs_proto_t fat_vfs_proto = {
430 .import_stat = fat_vfs_import_stat,
431};
432
Jim Mussared662b9762021-07-14 14:38:38 +1000433MP_DEFINE_CONST_OBJ_TYPE(
434 mp_fat_vfs_type,
435 MP_QSTR_VfsFat,
436 MP_TYPE_FLAG_NONE,
Jim Mussared94beeab2022-09-17 00:31:23 +1000437 make_new, fat_vfs_make_new,
Jim Mussared662b9762021-07-14 14:38:38 +1000438 protocol, &fat_vfs_proto,
Jim Mussared9dce8272022-06-24 16:27:46 +1000439 locals_dict, &fat_vfs_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000440 );
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200441
442#endif // MICROPY_VFS_FAT