blob: e832992f46f4ea372307f1365c312c79dc35750b [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
Angus Grattondecf8e62024-02-27 15:32:29 +110054static mp_import_stat_t fat_vfs_import_stat(void *vfs_in, const char *path) {
Damien Georgec117eff2018-06-06 14:24:23 +100055 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
Angus Grattondecf8e62024-02-27 15:32:29 +110069static 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
Angus Grattondecf8e62024-02-27 15:32:29 +110094static mp_obj_t fat_vfs_del(mp_obj_t self_in) {
Damien George12ad64b2017-11-16 16:01:47 +110095 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}
Angus Grattondecf8e62024-02-27 15:32:29 +1100100static MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_del_obj, fat_vfs_del);
Damien George12ad64b2017-11-16 16:01:47 +1100101#endif
102
Angus Grattondecf8e62024-02-27 15:32:29 +1100103static 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
Terence Stenvold390390e2024-07-19 12:38:44 +0200117 // set the filesystem label if it's configured
118 #ifdef MICROPY_HW_FLASH_FS_LABEL
119 f_setlabel(&vfs->fatfs, MICROPY_HW_FLASH_FS_LABEL);
120 #endif
121
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200122 return mp_const_none;
123}
Angus Grattondecf8e62024-02-27 15:32:29 +1100124static MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs);
125static MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj));
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200126
Damien Georgeae4a0772018-02-23 17:17:32 +1100127typedef struct _mp_vfs_fat_ilistdir_it_t {
128 mp_obj_base_t base;
129 mp_fun_1_t iternext;
Andrew Leech4e0964b2022-09-09 09:48:01 +1000130 mp_fun_1_t finaliser;
Damien Georgeae4a0772018-02-23 17:17:32 +1100131 bool is_str;
132 FF_DIR dir;
133} mp_vfs_fat_ilistdir_it_t;
134
Angus Grattondecf8e62024-02-27 15:32:29 +1100135static mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
Damien Georgeae4a0772018-02-23 17:17:32 +1100136 mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
137
138 for (;;) {
139 FILINFO fno;
140 FRESULT res = f_readdir(&self->dir, &fno);
141 char *fn = fno.fname;
142 if (res != FR_OK || fn[0] == 0) {
143 // stop on error or end of dir
144 break;
145 }
146
147 // Note that FatFS already filters . and .., so we don't need to
148
Tom Collins4d3a92c2018-03-08 16:02:26 -0800149 // make 4-tuple with info about this entry
150 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
Damien Georgeae4a0772018-02-23 17:17:32 +1100151 if (self->is_str) {
Jon Foster92484d82024-04-01 19:23:49 +0100152 t->items[0] = mp_obj_new_str_from_cstr(fn);
Damien Georgeae4a0772018-02-23 17:17:32 +1100153 } else {
Damien George69661f32020-02-27 15:36:53 +1100154 t->items[0] = mp_obj_new_bytes((const byte *)fn, strlen(fn));
Damien Georgeae4a0772018-02-23 17:17:32 +1100155 }
156 if (fno.fattrib & AM_DIR) {
157 // dir
158 t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
159 } else {
160 // file
161 t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
162 }
163 t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
Tom Collins4d3a92c2018-03-08 16:02:26 -0800164 t->items[3] = mp_obj_new_int_from_uint(fno.fsize);
Damien Georgeae4a0772018-02-23 17:17:32 +1100165
166 return MP_OBJ_FROM_PTR(t);
167 }
168
169 // ignore error because we may be closing a second time
170 f_closedir(&self->dir);
171
172 return MP_OBJ_STOP_ITERATION;
173}
174
Angus Grattondecf8e62024-02-27 15:32:29 +1100175static mp_obj_t mp_vfs_fat_ilistdir_it_del(mp_obj_t self_in) {
Andrew Leech4e0964b2022-09-09 09:48:01 +1000176 mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
177 // ignore result / error because we may be closing a second time.
178 f_closedir(&self->dir);
179 return mp_const_none;
180}
181
Angus Grattondecf8e62024-02-27 15:32:29 +1100182static mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100183 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200184 bool is_str_type = true;
185 const char *path;
186 if (n_args == 2) {
187 if (mp_obj_get_type(args[1]) == &mp_type_bytes) {
188 is_str_type = false;
189 }
190 path = mp_obj_str_get_str(args[1]);
191 } else {
192 path = "";
193 }
194
Damien Georgeae4a0772018-02-23 17:17:32 +1100195 // Create a new iterator object to list the dir
Damien Georgecae690d2024-02-16 11:02:58 +1100196 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 +1100197 iter->iternext = mp_vfs_fat_ilistdir_it_iternext;
Andrew Leech4e0964b2022-09-09 09:48:01 +1000198 iter->finaliser = mp_vfs_fat_ilistdir_it_del;
Damien Georgeae4a0772018-02-23 17:17:32 +1100199 iter->is_str = is_str_type;
200 FRESULT res = f_opendir(&self->fatfs, &iter->dir, path);
201 if (res != FR_OK) {
202 mp_raise_OSError(fresult_to_errno_table[res]);
203 }
204
205 return MP_OBJ_FROM_PTR(iter);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200206}
Angus Grattondecf8e62024-02-27 15:32:29 +1100207static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_ilistdir_func);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200208
Angus Grattondecf8e62024-02-27 15:32:29 +1100209static mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100210 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200211 const char *path = mp_obj_str_get_str(path_in);
Alex Marchd02f3a52016-09-28 14:51:35 +0100212
213 FILINFO fno;
Damien Georgef5f4cda2016-06-01 17:00:28 +0100214 FRESULT res = f_stat(&self->fatfs, path, &fno);
Alex Marchd02f3a52016-09-28 14:51:35 +0100215
216 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100217 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200218 }
Alex Marchd02f3a52016-09-28 14:51:35 +0100219
220 // check if path is a file or directory
221 if ((fno.fattrib & AM_DIR) == attr) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100222 res = f_unlink(&self->fatfs, path);
Alex Marchd02f3a52016-09-28 14:51:35 +0100223
224 if (res != FR_OK) {
225 mp_raise_OSError(fresult_to_errno_table[res]);
226 }
227 return mp_const_none;
228 } else {
229 mp_raise_OSError(attr ? MP_ENOTDIR : MP_EISDIR);
230 }
231}
232
Angus Grattondecf8e62024-02-27 15:32:29 +1100233static mp_obj_t fat_vfs_remove(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, 0); // 0 == file attribute
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200235}
Angus Grattondecf8e62024-02-27 15:32:29 +1100236static MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200237
Angus Grattondecf8e62024-02-27 15:32:29 +1100238static mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100239 return fat_vfs_remove_internal(vfs_in, path_in, AM_DIR);
Paul Sokolovsky0a6f5992016-07-16 03:46:42 +0300240}
Angus Grattondecf8e62024-02-27 15:32:29 +1100241static MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir);
Paul Sokolovsky0a6f5992016-07-16 03:46:42 +0300242
Angus Grattondecf8e62024-02-27 15:32:29 +1100243static 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 +0100244 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskye0821832016-02-29 01:22:38 +0200245 const char *old_path = mp_obj_str_get_str(path_in);
246 const char *new_path = mp_obj_str_get_str(path_out);
Damien Georgef5f4cda2016-06-01 17:00:28 +0100247 FRESULT res = f_rename(&self->fatfs, old_path, new_path);
Damien Georgeb7df3e52016-12-02 15:06:09 +1100248 if (res == FR_EXIST) {
249 // if new_path exists then try removing it (but only if it's a file)
Damien Georgef5f4cda2016-06-01 17:00:28 +0100250 fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute
Damien Georgeb7df3e52016-12-02 15:06:09 +1100251 // try to rename again
Damien Georgef5f4cda2016-06-01 17:00:28 +0100252 res = f_rename(&self->fatfs, old_path, new_path);
Damien Georgeb7df3e52016-12-02 15:06:09 +1100253 }
Robert HH7c004e72016-05-27 22:28:00 +0200254 if (res == FR_OK) {
255 return mp_const_none;
256 } else {
Damien George620c4c32016-10-07 13:44:55 +1100257 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskye0821832016-02-29 01:22:38 +0200258 }
259
260}
Angus Grattondecf8e62024-02-27 15:32:29 +1100261static MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename);
Paul Sokolovskye0821832016-02-29 01:22:38 +0200262
Angus Grattondecf8e62024-02-27 15:32:29 +1100263static mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100264 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200265 const char *path = mp_obj_str_get_str(path_o);
Damien Georgef5f4cda2016-06-01 17:00:28 +0100266 FRESULT res = f_mkdir(&self->fatfs, path);
Robert HH7c004e72016-05-27 22:28:00 +0200267 if (res == FR_OK) {
268 return mp_const_none;
269 } else {
Damien George620c4c32016-10-07 13:44:55 +1100270 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200271 }
272}
Angus Grattondecf8e62024-02-27 15:32:29 +1100273static MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200274
Damien George4791d292021-05-06 12:11:51 +1000275// Change current directory.
Angus Grattondecf8e62024-02-27 15:32:29 +1100276static mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100277 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300278 const char *path;
279 path = mp_obj_str_get_str(path_in);
280
Damien Georgef5f4cda2016-06-01 17:00:28 +0100281 FRESULT res = f_chdir(&self->fatfs, path);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300282
283 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100284 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300285 }
286
287 return mp_const_none;
288}
Angus Grattondecf8e62024-02-27 15:32:29 +1100289static MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300290
Damien George4791d292021-05-06 12:11:51 +1000291// Get the current directory.
Angus Grattondecf8e62024-02-27 15:32:29 +1100292static mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100293 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300294 char buf[MICROPY_ALLOC_PATH_MAX + 1];
Damien Georgefb3ae172017-01-27 15:13:32 +1100295 FRESULT res = f_getcwd(&self->fatfs, buf, sizeof(buf));
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300296 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100297 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300298 }
Jon Foster92484d82024-04-01 19:23:49 +0100299 return mp_obj_new_str_from_cstr(buf);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300300}
Angus Grattondecf8e62024-02-27 15:32:29 +1100301static MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300302
Damien George4791d292021-05-06 12:11:51 +1000303// Get the status of a file or directory.
Angus Grattondecf8e62024-02-27 15:32:29 +1100304static mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100305 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Robert HHee009d72016-05-30 21:09:20 +0200306 const char *path = mp_obj_str_get_str(path_in);
307
308 FILINFO fno;
Damien Georgefb3ae172017-01-27 15:13:32 +1100309 if (path[0] == 0 || (path[0] == '/' && path[1] == 0)) {
Robert HHee009d72016-05-30 21:09:20 +0200310 // stat root directory
311 fno.fsize = 0;
Robert HH23067a12016-06-16 18:17:59 +0200312 fno.fdate = 0x2821; // Jan 1, 2000
Robert HHee009d72016-05-30 21:09:20 +0200313 fno.ftime = 0;
314 fno.fattrib = AM_DIR;
315 } else {
Damien Georgefb3ae172017-01-27 15:13:32 +1100316 FRESULT res = f_stat(&self->fatfs, path, &fno);
Robert HHee009d72016-05-30 21:09:20 +0200317 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100318 mp_raise_OSError(fresult_to_errno_table[res]);
Robert HHee009d72016-05-30 21:09:20 +0200319 }
320 }
321
322 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
323 mp_int_t mode = 0;
324 if (fno.fattrib & AM_DIR) {
Damien Georged70f6882017-05-10 12:30:34 +1000325 mode |= MP_S_IFDIR;
Robert HHee009d72016-05-30 21:09:20 +0200326 } else {
Damien Georged70f6882017-05-10 12:30:34 +1000327 mode |= MP_S_IFREG;
Robert HHee009d72016-05-30 21:09:20 +0200328 }
Yoctopuce devdf05cae2025-07-01 13:16:20 +0200329 mp_timestamp_t seconds = timeutils_seconds_since_epoch(
Robert HHee009d72016-05-30 21:09:20 +0200330 1980 + ((fno.fdate >> 9) & 0x7f),
331 (fno.fdate >> 5) & 0x0f,
332 fno.fdate & 0x1f,
333 (fno.ftime >> 11) & 0x1f,
334 (fno.ftime >> 5) & 0x3f,
335 2 * (fno.ftime & 0x1f)
Damien George69661f32020-02-27 15:36:53 +1100336 );
Robert HHee009d72016-05-30 21:09:20 +0200337 t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
338 t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
339 t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
340 t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
341 t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
342 t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
Damien George4c736ea2017-08-21 20:47:22 +1000343 t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
Yoctopuce devdf05cae2025-07-01 13:16:20 +0200344 t->items[7] = timeutils_obj_from_timestamp(seconds); // st_atime
345 t->items[8] = timeutils_obj_from_timestamp(seconds); // st_mtime
346 t->items[9] = timeutils_obj_from_timestamp(seconds); // st_ctime
Robert HHee009d72016-05-30 21:09:20 +0200347
348 return MP_OBJ_FROM_PTR(t);
349}
Angus Grattondecf8e62024-02-27 15:32:29 +1100350static MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_stat_obj, fat_vfs_stat);
Robert HHee009d72016-05-30 21:09:20 +0200351
Alex Marchdcf14c12016-09-12 18:13:44 +0100352// Get the status of a VFS.
Angus Grattondecf8e62024-02-27 15:32:29 +1100353static mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100354 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
355 (void)path_in;
Alex Marchdcf14c12016-09-12 18:13:44 +0100356
Alex Marchdcf14c12016-09-12 18:13:44 +0100357 DWORD nclst;
Damien Georgef5f4cda2016-06-01 17:00:28 +0100358 FATFS *fatfs = &self->fatfs;
359 FRESULT res = f_getfree(fatfs, &nclst);
Alex Marchdcf14c12016-09-12 18:13:44 +0100360 if (FR_OK != res) {
Damien George620c4c32016-10-07 13:44:55 +1100361 mp_raise_OSError(fresult_to_errno_table[res]);
Alex Marchdcf14c12016-09-12 18:13:44 +0100362 }
363
364 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
365
Damien George8aa8a0a2017-01-27 22:42:06 +1100366 t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * SECSIZE(fatfs)); // f_bsize
Alex Marchdcf14c12016-09-12 18:13:44 +0100367 t->items[1] = t->items[0]; // f_frsize
Damien Georgef7816182017-03-29 12:53:35 +1100368 t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2)); // f_blocks
Alex Marchdcf14c12016-09-12 18:13:44 +0100369 t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree
370 t->items[4] = t->items[3]; // f_bavail
371 t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files
372 t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree
373 t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail
374 t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
Damien Georgee959f212019-02-25 23:46:03 +1100375 t->items[9] = MP_OBJ_NEW_SMALL_INT(FF_MAX_LFN); // f_namemax
Alex Marchdcf14c12016-09-12 18:13:44 +0100376
377 return MP_OBJ_FROM_PTR(t);
378}
Angus Grattondecf8e62024-02-27 15:32:29 +1100379static MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs);
Alex Marchdcf14c12016-09-12 18:13:44 +0100380
Angus Grattondecf8e62024-02-27 15:32:29 +1100381static mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) {
Damien Georgefb3ae172017-01-27 15:13:32 +1100382 fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
383
384 // Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
385 // User can specify read-only device by:
386 // 1. readonly=True keyword argument
387 // 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
388 if (mp_obj_is_true(readonly)) {
Damien George9aabb6c2019-09-07 14:03:41 +1000389 self->blockdev.writeblocks[0] = MP_OBJ_NULL;
Damien Georgefb3ae172017-01-27 15:13:32 +1100390 }
391
Damien Georgefb3ae172017-01-27 15:13:32 +1100392 // check if we need to make the filesystem
Damien George9aabb6c2019-09-07 14:03:41 +1000393 FRESULT res = (self->blockdev.flags & MP_BLOCKDEV_FLAG_NO_FILESYSTEM) ? FR_NO_FILESYSTEM : FR_OK;
Damien Georgefb3ae172017-01-27 15:13:32 +1100394 if (res == FR_NO_FILESYSTEM && mp_obj_is_true(mkfs)) {
Damien Georgee959f212019-02-25 23:46:03 +1100395 uint8_t working_buf[FF_MAX_SS];
Damien Georgefb3ae172017-01-27 15:13:32 +1100396 res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
397 }
398 if (res != FR_OK) {
399 mp_raise_OSError(fresult_to_errno_table[res]);
400 }
Damien George9aabb6c2019-09-07 14:03:41 +1000401 self->blockdev.flags &= ~MP_BLOCKDEV_FLAG_NO_FILESYSTEM;
Damien Georgefb3ae172017-01-27 15:13:32 +1100402
Radomir Dopieralskid29ca282016-08-22 16:10:34 +0200403 return mp_const_none;
404}
Angus Grattondecf8e62024-02-27 15:32:29 +1100405static MP_DEFINE_CONST_FUN_OBJ_3(vfs_fat_mount_obj, vfs_fat_mount);
Damien Georgefb3ae172017-01-27 15:13:32 +1100406
Angus Grattondecf8e62024-02-27 15:32:29 +1100407static mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
Damien George12ad64b2017-11-16 16:01:47 +1100408 (void)self_in;
409 // keep the FAT filesystem mounted internally so the VFS methods can still be used
Damien Georgefb3ae172017-01-27 15:13:32 +1100410 return mp_const_none;
411}
Angus Grattondecf8e62024-02-27 15:32:29 +1100412static MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
Radomir Dopieralskid29ca282016-08-22 16:10:34 +0200413
Angus Grattondecf8e62024-02-27 15:32:29 +1100414static const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
Damien George12ad64b2017-11-16 16:01:47 +1100415 #if _FS_REENTRANT
416 { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&fat_vfs_del_obj) },
417 #endif
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200418 { MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
419 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
Damien Georged4cd4832017-05-05 23:32:44 +1000420 { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&fat_vfs_ilistdir_obj) },
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200421 { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&fat_vfs_mkdir_obj) },
Paul Sokolovsky0a6f5992016-07-16 03:46:42 +0300422 { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&fat_vfs_rmdir_obj) },
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300423 { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&fat_vfs_chdir_obj) },
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300424 { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&fat_vfs_getcwd_obj) },
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200425 { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&fat_vfs_remove_obj) },
Paul Sokolovskye0821832016-02-29 01:22:38 +0200426 { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) },
Robert HHee009d72016-05-30 21:09:20 +0200427 { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&fat_vfs_stat_obj) },
Alex Marchdcf14c12016-09-12 18:13:44 +0100428 { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
Damien Georgefb3ae172017-01-27 15:13:32 +1100429 { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
Radomir Dopieralskid29ca282016-08-22 16:10:34 +0200430 { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200431};
Angus Grattondecf8e62024-02-27 15:32:29 +1100432static MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200433
Angus Grattondecf8e62024-02-27 15:32:29 +1100434static const mp_vfs_proto_t fat_vfs_proto = {
Damien Georgec117eff2018-06-06 14:24:23 +1000435 .import_stat = fat_vfs_import_stat,
436};
437
Jim Mussared662b9762021-07-14 14:38:38 +1000438MP_DEFINE_CONST_OBJ_TYPE(
439 mp_fat_vfs_type,
440 MP_QSTR_VfsFat,
441 MP_TYPE_FLAG_NONE,
Jim Mussared94beeab2022-09-17 00:31:23 +1000442 make_new, fat_vfs_make_new,
Jim Mussared662b9762021-07-14 14:38:38 +1000443 protocol, &fat_vfs_proto,
Jim Mussared9dce8272022-06-24 16:27:46 +1000444 locals_dict, &fat_vfs_locals_dict
Jim Mussared662b9762021-07-14 14:38:38 +1000445 );
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200446
447#endif // MICROPY_VFS_FAT