blob: ec7aaed388c6934c9fd553ecb41e721624e0fdab [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
Damien Georgefb3ae172017-01-27 15:13:32 +110031#if !MICROPY_VFS
32#error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_VFS"
33#endif
34
Paul Sokolovskycac6c972016-05-29 18:23:59 +030035#include <string.h>
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020036#include "py/runtime.h"
Alex Marchd02f3a52016-09-28 14:51:35 +010037#include "py/mperrno.h"
Damien Georgef5f4cda2016-06-01 17:00:28 +010038#include "lib/oofatfs/ff.h"
Damien George32a11382017-01-27 15:04:17 +110039#include "extmod/vfs_fat.h"
Andrew Gatt10dbf232017-01-30 11:28:37 +000040#include "lib/timeutils/timeutils.h"
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020041
Damien Georgee959f212019-02-25 23:46:03 +110042#if FF_MAX_SS == FF_MIN_SS
43#define SECSIZE(fs) (FF_MIN_SS)
Damien George8aa8a0a2017-01-27 22:42:06 +110044#else
45#define SECSIZE(fs) ((fs)->ssize)
46#endif
47
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020048#define mp_obj_fat_vfs_t fs_user_mount_t
49
Damien Georgec117eff2018-06-06 14:24:23 +100050STATIC mp_import_stat_t fat_vfs_import_stat(void *vfs_in, const char *path) {
51 fs_user_mount_t *vfs = vfs_in;
Damien George638b8602018-02-23 17:24:57 +110052 FILINFO fno;
53 assert(vfs != NULL);
54 FRESULT res = f_stat(&vfs->fatfs, path, &fno);
55 if (res == FR_OK) {
56 if ((fno.fattrib & AM_DIR) != 0) {
57 return MP_IMPORT_STAT_DIR;
58 } else {
59 return MP_IMPORT_STAT_FILE;
60 }
61 }
62 return MP_IMPORT_STAT_NO_EXIST;
63}
64
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020065STATIC 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 +110066 mp_arg_check_num(n_args, n_kw, 1, 1, false);
67
68 // create new object
69 fs_user_mount_t *vfs = m_new_obj(fs_user_mount_t);
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020070 vfs->base.type = type;
Damien Georgefb3ae172017-01-27 15:13:32 +110071 vfs->flags = FSUSER_FREE_OBJ;
Damien Georgefb3ae172017-01-27 15:13:32 +110072 vfs->fatfs.drv = vfs;
73
74 // load block protocol methods
75 mp_load_method(args[0], MP_QSTR_readblocks, vfs->readblocks);
76 mp_load_method_maybe(args[0], MP_QSTR_writeblocks, vfs->writeblocks);
77 mp_load_method_maybe(args[0], MP_QSTR_ioctl, vfs->u.ioctl);
78 if (vfs->u.ioctl[0] != MP_OBJ_NULL) {
79 // device supports new block protocol, so indicate it
80 vfs->flags |= FSUSER_HAVE_IOCTL;
81 } else {
82 // no ioctl method, so assume the device uses the old block protocol
83 mp_load_method_maybe(args[0], MP_QSTR_sync, vfs->u.old.sync);
84 mp_load_method(args[0], MP_QSTR_count, vfs->u.old.count);
85 }
86
Damien George12ad64b2017-11-16 16:01:47 +110087 // mount the block device so the VFS methods can be used
88 FRESULT res = f_mount(&vfs->fatfs);
89 if (res == FR_NO_FILESYSTEM) {
90 // don't error out if no filesystem, to let mkfs()/mount() create one if wanted
91 vfs->flags |= FSUSER_NO_FILESYSTEM;
92 } else if (res != FR_OK) {
93 mp_raise_OSError(fresult_to_errno_table[res]);
94 }
95
Paul Sokolovsky1bb15ca2016-02-14 16:21:27 +020096 return MP_OBJ_FROM_PTR(vfs);
Paul Sokolovskye9be6a32016-02-13 22:51:21 +020097}
98
Damien George12ad64b2017-11-16 16:01:47 +110099#if _FS_REENTRANT
100STATIC mp_obj_t fat_vfs_del(mp_obj_t self_in) {
101 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(self_in);
102 // f_umount only needs to be called to release the sync object
103 f_umount(&self->fatfs);
104 return mp_const_none;
105}
106STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_del_obj, fat_vfs_del);
107#endif
108
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200109STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) {
Damien Georgefb3ae172017-01-27 15:13:32 +1100110 // create new object
111 fs_user_mount_t *vfs = MP_OBJ_TO_PTR(fat_vfs_make_new(&mp_fat_vfs_type, 1, 0, &bdev_in));
112
113 // make the filesystem
Damien Georgee959f212019-02-25 23:46:03 +1100114 uint8_t working_buf[FF_MAX_SS];
Damien Georgefb3ae172017-01-27 15:13:32 +1100115 FRESULT res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
Andrew Leech74d07462019-03-25 11:20:54 +1100116 if (res == FR_MKFS_ABORTED) { // Probably doesn't support FAT16
117 res = f_mkfs(&vfs->fatfs, FM_FAT32, 0, working_buf, sizeof(working_buf));
118 }
Damien Georgefb3ae172017-01-27 15:13:32 +1100119 if (res != FR_OK) {
120 mp_raise_OSError(fresult_to_errno_table[res]);
121 }
122
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200123 return mp_const_none;
124}
125STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs);
126STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj));
127
Damien Georgeae4a0772018-02-23 17:17:32 +1100128typedef struct _mp_vfs_fat_ilistdir_it_t {
129 mp_obj_base_t base;
130 mp_fun_1_t iternext;
131 bool is_str;
132 FF_DIR dir;
133} mp_vfs_fat_ilistdir_it_t;
134
135STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
136 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) {
152 t->items[0] = mp_obj_new_str(fn, strlen(fn));
153 } else {
154 t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn));
155 }
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
Damien Georged4cd4832017-05-05 23:32:44 +1000175STATIC mp_obj_t fat_vfs_ilistdir_func(size_t n_args, const mp_obj_t *args) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100176 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200177 bool is_str_type = true;
178 const char *path;
179 if (n_args == 2) {
180 if (mp_obj_get_type(args[1]) == &mp_type_bytes) {
181 is_str_type = false;
182 }
183 path = mp_obj_str_get_str(args[1]);
184 } else {
185 path = "";
186 }
187
Damien Georgeae4a0772018-02-23 17:17:32 +1100188 // Create a new iterator object to list the dir
189 mp_vfs_fat_ilistdir_it_t *iter = m_new_obj(mp_vfs_fat_ilistdir_it_t);
190 iter->base.type = &mp_type_polymorph_iter;
191 iter->iternext = mp_vfs_fat_ilistdir_it_iternext;
192 iter->is_str = is_str_type;
193 FRESULT res = f_opendir(&self->fatfs, &iter->dir, path);
194 if (res != FR_OK) {
195 mp_raise_OSError(fresult_to_errno_table[res]);
196 }
197
198 return MP_OBJ_FROM_PTR(iter);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200199}
Damien Georged4cd4832017-05-05 23:32:44 +1000200STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_ilistdir_func);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +0200201
Damien Georgef5f4cda2016-06-01 17:00:28 +0100202STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) {
203 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200204 const char *path = mp_obj_str_get_str(path_in);
Alex Marchd02f3a52016-09-28 14:51:35 +0100205
206 FILINFO fno;
Damien Georgef5f4cda2016-06-01 17:00:28 +0100207 FRESULT res = f_stat(&self->fatfs, path, &fno);
Alex Marchd02f3a52016-09-28 14:51:35 +0100208
209 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100210 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200211 }
Alex Marchd02f3a52016-09-28 14:51:35 +0100212
213 // check if path is a file or directory
214 if ((fno.fattrib & AM_DIR) == attr) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100215 res = f_unlink(&self->fatfs, path);
Alex Marchd02f3a52016-09-28 14:51:35 +0100216
217 if (res != FR_OK) {
218 mp_raise_OSError(fresult_to_errno_table[res]);
219 }
220 return mp_const_none;
221 } else {
222 mp_raise_OSError(attr ? MP_ENOTDIR : MP_EISDIR);
223 }
224}
225
226STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100227 return fat_vfs_remove_internal(vfs_in, path_in, 0); // 0 == file attribute
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200228}
229STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
230
Paul Sokolovsky0a6f5992016-07-16 03:46:42 +0300231STATIC mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100232 return fat_vfs_remove_internal(vfs_in, path_in, AM_DIR);
Paul Sokolovsky0a6f5992016-07-16 03:46:42 +0300233}
234STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir);
235
Paul Sokolovskye0821832016-02-29 01:22:38 +0200236STATIC 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 +0100237 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskye0821832016-02-29 01:22:38 +0200238 const char *old_path = mp_obj_str_get_str(path_in);
239 const char *new_path = mp_obj_str_get_str(path_out);
Damien Georgef5f4cda2016-06-01 17:00:28 +0100240 FRESULT res = f_rename(&self->fatfs, old_path, new_path);
Damien Georgeb7df3e52016-12-02 15:06:09 +1100241 if (res == FR_EXIST) {
242 // if new_path exists then try removing it (but only if it's a file)
Damien Georgef5f4cda2016-06-01 17:00:28 +0100243 fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute
Damien Georgeb7df3e52016-12-02 15:06:09 +1100244 // try to rename again
Damien Georgef5f4cda2016-06-01 17:00:28 +0100245 res = f_rename(&self->fatfs, old_path, new_path);
Damien Georgeb7df3e52016-12-02 15:06:09 +1100246 }
Robert HH7c004e72016-05-27 22:28:00 +0200247 if (res == FR_OK) {
248 return mp_const_none;
249 } else {
Damien George620c4c32016-10-07 13:44:55 +1100250 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskye0821832016-02-29 01:22:38 +0200251 }
252
253}
254STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename);
255
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200256STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100257 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200258 const char *path = mp_obj_str_get_str(path_o);
Damien Georgef5f4cda2016-06-01 17:00:28 +0100259 FRESULT res = f_mkdir(&self->fatfs, path);
Robert HH7c004e72016-05-27 22:28:00 +0200260 if (res == FR_OK) {
261 return mp_const_none;
262 } else {
Damien George620c4c32016-10-07 13:44:55 +1100263 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200264 }
265}
266STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);
267
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300268/// Change current directory.
Paul Sokolovskyee5e3f62016-05-29 18:52:41 +0300269STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100270 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300271 const char *path;
272 path = mp_obj_str_get_str(path_in);
273
Damien Georgef5f4cda2016-06-01 17:00:28 +0100274 FRESULT res = f_chdir(&self->fatfs, path);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300275
276 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100277 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300278 }
279
280 return mp_const_none;
281}
Paul Sokolovskyee5e3f62016-05-29 18:52:41 +0300282STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir);
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300283
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300284/// Get the current directory.
Paul Sokolovskyee5e3f62016-05-29 18:52:41 +0300285STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100286 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300287 char buf[MICROPY_ALLOC_PATH_MAX + 1];
Damien Georgefb3ae172017-01-27 15:13:32 +1100288 FRESULT res = f_getcwd(&self->fatfs, buf, sizeof(buf));
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300289 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100290 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300291 }
Damien George46017592017-11-16 13:17:51 +1100292 return mp_obj_new_str(buf, strlen(buf));
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300293}
Paul Sokolovskyee5e3f62016-05-29 18:52:41 +0300294STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd);
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300295
Robert HHee009d72016-05-30 21:09:20 +0200296/// \function stat(path)
297/// Get the status of a file or directory.
298STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100299 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
Robert HHee009d72016-05-30 21:09:20 +0200300 const char *path = mp_obj_str_get_str(path_in);
301
302 FILINFO fno;
Damien Georgefb3ae172017-01-27 15:13:32 +1100303 if (path[0] == 0 || (path[0] == '/' && path[1] == 0)) {
Robert HHee009d72016-05-30 21:09:20 +0200304 // stat root directory
305 fno.fsize = 0;
Robert HH23067a12016-06-16 18:17:59 +0200306 fno.fdate = 0x2821; // Jan 1, 2000
Robert HHee009d72016-05-30 21:09:20 +0200307 fno.ftime = 0;
308 fno.fattrib = AM_DIR;
309 } else {
Damien Georgefb3ae172017-01-27 15:13:32 +1100310 FRESULT res = f_stat(&self->fatfs, path, &fno);
Robert HHee009d72016-05-30 21:09:20 +0200311 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +1100312 mp_raise_OSError(fresult_to_errno_table[res]);
Robert HHee009d72016-05-30 21:09:20 +0200313 }
314 }
315
316 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
317 mp_int_t mode = 0;
318 if (fno.fattrib & AM_DIR) {
Damien Georged70f6882017-05-10 12:30:34 +1000319 mode |= MP_S_IFDIR;
Robert HHee009d72016-05-30 21:09:20 +0200320 } else {
Damien Georged70f6882017-05-10 12:30:34 +1000321 mode |= MP_S_IFREG;
Robert HHee009d72016-05-30 21:09:20 +0200322 }
323 mp_int_t seconds = timeutils_seconds_since_2000(
324 1980 + ((fno.fdate >> 9) & 0x7f),
325 (fno.fdate >> 5) & 0x0f,
326 fno.fdate & 0x1f,
327 (fno.ftime >> 11) & 0x1f,
328 (fno.ftime >> 5) & 0x3f,
329 2 * (fno.ftime & 0x1f)
330 );
331 t->items[0] = MP_OBJ_NEW_SMALL_INT(mode); // st_mode
332 t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino
333 t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev
334 t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink
335 t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid
336 t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid
Damien George4c736ea2017-08-21 20:47:22 +1000337 t->items[6] = mp_obj_new_int_from_uint(fno.fsize); // st_size
Robert HHee009d72016-05-30 21:09:20 +0200338 t->items[7] = MP_OBJ_NEW_SMALL_INT(seconds); // st_atime
339 t->items[8] = MP_OBJ_NEW_SMALL_INT(seconds); // st_mtime
340 t->items[9] = MP_OBJ_NEW_SMALL_INT(seconds); // st_ctime
341
342 return MP_OBJ_FROM_PTR(t);
343}
344STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_stat_obj, fat_vfs_stat);
345
Alex Marchdcf14c12016-09-12 18:13:44 +0100346// Get the status of a VFS.
347STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) {
Damien Georgef5f4cda2016-06-01 17:00:28 +0100348 mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
349 (void)path_in;
Alex Marchdcf14c12016-09-12 18:13:44 +0100350
Alex Marchdcf14c12016-09-12 18:13:44 +0100351 DWORD nclst;
Damien Georgef5f4cda2016-06-01 17:00:28 +0100352 FATFS *fatfs = &self->fatfs;
353 FRESULT res = f_getfree(fatfs, &nclst);
Alex Marchdcf14c12016-09-12 18:13:44 +0100354 if (FR_OK != res) {
Damien George620c4c32016-10-07 13:44:55 +1100355 mp_raise_OSError(fresult_to_errno_table[res]);
Alex Marchdcf14c12016-09-12 18:13:44 +0100356 }
357
358 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
359
Damien George8aa8a0a2017-01-27 22:42:06 +1100360 t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * SECSIZE(fatfs)); // f_bsize
Alex Marchdcf14c12016-09-12 18:13:44 +0100361 t->items[1] = t->items[0]; // f_frsize
Damien Georgef7816182017-03-29 12:53:35 +1100362 t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2)); // f_blocks
Alex Marchdcf14c12016-09-12 18:13:44 +0100363 t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree
364 t->items[4] = t->items[3]; // f_bavail
365 t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files
366 t->items[6] = MP_OBJ_NEW_SMALL_INT(0); // f_ffree
367 t->items[7] = MP_OBJ_NEW_SMALL_INT(0); // f_favail
368 t->items[8] = MP_OBJ_NEW_SMALL_INT(0); // f_flags
Damien Georgee959f212019-02-25 23:46:03 +1100369 t->items[9] = MP_OBJ_NEW_SMALL_INT(FF_MAX_LFN); // f_namemax
Alex Marchdcf14c12016-09-12 18:13:44 +0100370
371 return MP_OBJ_FROM_PTR(t);
372}
373STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs);
374
Damien Georgefb3ae172017-01-27 15:13:32 +1100375STATIC mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) {
376 fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
377
378 // Read-only device indicated by writeblocks[0] == MP_OBJ_NULL.
379 // User can specify read-only device by:
380 // 1. readonly=True keyword argument
381 // 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already)
382 if (mp_obj_is_true(readonly)) {
383 self->writeblocks[0] = MP_OBJ_NULL;
384 }
385
Damien Georgefb3ae172017-01-27 15:13:32 +1100386 // check if we need to make the filesystem
Damien George12ad64b2017-11-16 16:01:47 +1100387 FRESULT res = (self->flags & FSUSER_NO_FILESYSTEM) ? FR_NO_FILESYSTEM : FR_OK;
Damien Georgefb3ae172017-01-27 15:13:32 +1100388 if (res == FR_NO_FILESYSTEM && mp_obj_is_true(mkfs)) {
Damien Georgee959f212019-02-25 23:46:03 +1100389 uint8_t working_buf[FF_MAX_SS];
Damien Georgefb3ae172017-01-27 15:13:32 +1100390 res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf));
391 }
392 if (res != FR_OK) {
393 mp_raise_OSError(fresult_to_errno_table[res]);
394 }
Damien George12ad64b2017-11-16 16:01:47 +1100395 self->flags &= ~FSUSER_NO_FILESYSTEM;
Damien Georgefb3ae172017-01-27 15:13:32 +1100396
Radomir Dopieralskid29ca282016-08-22 16:10:34 +0200397 return mp_const_none;
398}
Damien Georgefb3ae172017-01-27 15:13:32 +1100399STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_fat_mount_obj, vfs_fat_mount);
400
401STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) {
Damien George12ad64b2017-11-16 16:01:47 +1100402 (void)self_in;
403 // keep the FAT filesystem mounted internally so the VFS methods can still be used
Damien Georgefb3ae172017-01-27 15:13:32 +1100404 return mp_const_none;
405}
406STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount);
Radomir Dopieralskid29ca282016-08-22 16:10:34 +0200407
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200408STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = {
Damien George12ad64b2017-11-16 16:01:47 +1100409 #if _FS_REENTRANT
410 { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&fat_vfs_del_obj) },
411 #endif
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200412 { MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) },
413 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) },
Damien Georged4cd4832017-05-05 23:32:44 +1000414 { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&fat_vfs_ilistdir_obj) },
Paul Sokolovskybbe832a2016-02-29 00:02:50 +0200415 { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&fat_vfs_mkdir_obj) },
Paul Sokolovsky0a6f5992016-07-16 03:46:42 +0300416 { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&fat_vfs_rmdir_obj) },
Paul Sokolovskyf12146c2016-05-29 18:17:00 +0300417 { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&fat_vfs_chdir_obj) },
Paul Sokolovskycac6c972016-05-29 18:23:59 +0300418 { MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&fat_vfs_getcwd_obj) },
Paul Sokolovsky19749db2016-02-28 20:30:07 +0200419 { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&fat_vfs_remove_obj) },
Paul Sokolovskye0821832016-02-29 01:22:38 +0200420 { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) },
Robert HHee009d72016-05-30 21:09:20 +0200421 { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&fat_vfs_stat_obj) },
Alex Marchdcf14c12016-09-12 18:13:44 +0100422 { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) },
Damien Georgefb3ae172017-01-27 15:13:32 +1100423 { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) },
Radomir Dopieralskid29ca282016-08-22 16:10:34 +0200424 { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) },
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200425};
426STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table);
427
Damien Georgec117eff2018-06-06 14:24:23 +1000428STATIC const mp_vfs_proto_t fat_vfs_proto = {
429 .import_stat = fat_vfs_import_stat,
430};
431
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200432const mp_obj_type_t mp_fat_vfs_type = {
433 { &mp_type_type },
434 .name = MP_QSTR_VfsFat,
435 .make_new = fat_vfs_make_new,
Damien Georgec117eff2018-06-06 14:24:23 +1000436 .protocol = &fat_vfs_proto,
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200437 .locals_dict = (mp_obj_dict_t*)&fat_vfs_locals_dict,
Damien Georgec117eff2018-06-06 14:24:23 +1000438
Paul Sokolovskye9be6a32016-02-13 22:51:21 +0200439};
440
441#endif // MICROPY_VFS_FAT