1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2017 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include <stdint.h>
28 #include <string.h>
29
30 #include "py/runtime.h"
31 #include "py/objstr.h"
32 #include "py/mperrno.h"
33 #include "extmod/vfs.h"
34
35 #if MICROPY_VFS
36
37 #if MICROPY_VFS_FAT
38 #include "extmod/vfs_fat.h"
39 #endif
40
41 #if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2
42 #include "extmod/vfs_lfs.h"
43 #endif
44
45 #if MICROPY_VFS_POSIX
46 #include "extmod/vfs_posix.h"
47 #endif
48
49 // For mp_vfs_proxy_call, the maximum number of additional args that can be passed.
50 // A fixed maximum size is used to avoid the need for a costly variable array.
51 #define PROXY_MAX_ARGS (2)
52
53 // path is the path to lookup and *path_out holds the path within the VFS
54 // object (starts with / if an absolute path).
55 // Returns MP_VFS_ROOT for root dir (and then path_out is undefined) and
56 // MP_VFS_NONE for path not found.
mp_vfs_lookup_path(const char * path,const char ** path_out)57 mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out) {
58 if (*path == '/' || MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) {
59 // an absolute path, or the current volume is root, so search root dir
60 bool is_abs = 0;
61 if (*path == '/') {
62 ++path;
63 is_abs = 1;
64 }
65 if (*path == '\0') {
66 // path is "" or "/" so return virtual root
67 return MP_VFS_ROOT;
68 }
69 for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
70 size_t len = vfs->len - 1;
71 if (len == 0) {
72 *path_out = path - is_abs;
73 return vfs;
74 }
75 if (strncmp(path, vfs->str + 1, len) == 0) {
76 if (path[len] == '/') {
77 *path_out = path + len;
78 return vfs;
79 } else if (path[len] == '\0') {
80 *path_out = "/";
81 return vfs;
82 }
83 }
84 }
85
86 // if we get here then there's nothing mounted on /, so the path doesn't exist
87 return MP_VFS_NONE;
88 }
89
90 // a relative path within a mounted device
91 *path_out = path;
92 return MP_STATE_VM(vfs_cur);
93 }
94
95 // Version of mp_vfs_lookup_path that takes and returns uPy string objects.
lookup_path(mp_obj_t path_in,mp_obj_t * path_out)96 STATIC mp_vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) {
97 const char *path = mp_obj_str_get_str(path_in);
98 const char *p_out;
99 mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &p_out);
100 if (vfs != MP_VFS_NONE && vfs != MP_VFS_ROOT) {
101 *path_out = mp_obj_new_str_of_type(mp_obj_get_type(path_in),
102 (const byte *)p_out, strlen(p_out));
103 }
104 return vfs;
105 }
106
mp_vfs_proxy_call(mp_vfs_mount_t * vfs,qstr meth_name,size_t n_args,const mp_obj_t * args)107 STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) {
108 assert(n_args <= PROXY_MAX_ARGS);
109 if (vfs == MP_VFS_NONE) {
110 // mount point not found
111 mp_raise_OSError(MP_ENODEV);
112 }
113 if (vfs == MP_VFS_ROOT) {
114 // can't do operation on root dir
115 mp_raise_OSError(MP_EPERM);
116 }
117 mp_obj_t meth[2 + PROXY_MAX_ARGS];
118 mp_load_method(vfs->obj, meth_name, meth);
119 if (args != NULL) {
120 memcpy(meth + 2, args, n_args * sizeof(*args));
121 }
122 return mp_call_method_n_kw(n_args, 0, meth);
123 }
124
mp_vfs_import_stat(const char * path)125 mp_import_stat_t mp_vfs_import_stat(const char *path) {
126 const char *path_out;
127 mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &path_out);
128 if (vfs == MP_VFS_NONE || vfs == MP_VFS_ROOT) {
129 return MP_IMPORT_STAT_NO_EXIST;
130 }
131
132 // If the mounted object has the VFS protocol, call its import_stat helper
133 const mp_vfs_proto_t *proto = mp_obj_get_type(vfs->obj)->protocol;
134 if (proto != NULL) {
135 return proto->import_stat(MP_OBJ_TO_PTR(vfs->obj), path_out);
136 }
137
138 // delegate to vfs.stat() method
139 mp_obj_t path_o = mp_obj_new_str(path_out, strlen(path_out));
140 mp_obj_t stat;
141 nlr_buf_t nlr;
142 if (nlr_push(&nlr) == 0) {
143 stat = mp_vfs_proxy_call(vfs, MP_QSTR_stat, 1, &path_o);
144 nlr_pop();
145 } else {
146 // assume an exception means that the path is not found
147 return MP_IMPORT_STAT_NO_EXIST;
148 }
149 mp_obj_t *items;
150 mp_obj_get_array_fixed_n(stat, 10, &items);
151 mp_int_t st_mode = mp_obj_get_int(items[0]);
152 if (st_mode & MP_S_IFDIR) {
153 return MP_IMPORT_STAT_DIR;
154 } else {
155 return MP_IMPORT_STAT_FILE;
156 }
157 }
158
mp_vfs_autodetect(mp_obj_t bdev_obj)159 STATIC mp_obj_t mp_vfs_autodetect(mp_obj_t bdev_obj) {
160 #if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2
161 nlr_buf_t nlr;
162 if (nlr_push(&nlr) == 0) {
163 // The superblock for littlefs is in both block 0 and 1, but block 0 may be erased
164 // or partially written, so search both blocks 0 and 1 for the littlefs signature.
165 mp_vfs_blockdev_t blockdev;
166 mp_vfs_blockdev_init(&blockdev, bdev_obj);
167 uint8_t buf[44];
168 for (size_t block_num = 0; block_num <= 1; ++block_num) {
169 mp_vfs_blockdev_read_ext(&blockdev, block_num, 8, sizeof(buf), buf);
170 #if MICROPY_VFS_LFS1
171 if (memcmp(&buf[32], "littlefs", 8) == 0) {
172 // LFS1
173 mp_obj_t vfs = mp_type_vfs_lfs1.make_new(&mp_type_vfs_lfs1, 1, 0, &bdev_obj);
174 nlr_pop();
175 return vfs;
176 }
177 #endif
178 #if MICROPY_VFS_LFS2
179 if (memcmp(&buf[0], "littlefs", 8) == 0) {
180 // LFS2
181 mp_obj_t vfs = mp_type_vfs_lfs2.make_new(&mp_type_vfs_lfs2, 1, 0, &bdev_obj);
182 nlr_pop();
183 return vfs;
184 }
185 #endif
186 }
187 nlr_pop();
188 } else {
189 // Ignore exception (eg block device doesn't support extended readblocks)
190 }
191 #endif
192
193 #if MICROPY_VFS_FAT
194 return mp_fat_vfs_type.make_new(&mp_fat_vfs_type, 1, 0, &bdev_obj);
195 #endif
196
197 // no filesystem found
198 mp_raise_OSError(MP_ENODEV);
199 }
200
mp_vfs_mount(size_t n_args,const mp_obj_t * pos_args,mp_map_t * kw_args)201 mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
202 enum { ARG_readonly, ARG_mkfs };
203 static const mp_arg_t allowed_args[] = {
204 { MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_FALSE} },
205 { MP_QSTR_mkfs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_FALSE} },
206 };
207
208 // parse args
209 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
210 mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
211
212 // get the mount point
213 size_t mnt_len;
214 const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len);
215
216 // see if we need to auto-detect and create the filesystem
217 mp_obj_t vfs_obj = pos_args[0];
218 mp_obj_t dest[2];
219 mp_load_method_maybe(vfs_obj, MP_QSTR_mount, dest);
220 if (dest[0] == MP_OBJ_NULL) {
221 // Input object has no mount method, assume it's a block device and try to
222 // auto-detect the filesystem and create the corresponding VFS entity.
223 vfs_obj = mp_vfs_autodetect(vfs_obj);
224 }
225
226 // create new object
227 mp_vfs_mount_t *vfs = m_new_obj(mp_vfs_mount_t);
228 vfs->str = mnt_str;
229 vfs->len = mnt_len;
230 vfs->obj = vfs_obj;
231 vfs->next = NULL;
232
233 // call the underlying object to do any mounting operation
234 mp_vfs_proxy_call(vfs, MP_QSTR_mount, 2, (mp_obj_t *)&args);
235
236 // check that the destination mount point is unused
237 const char *path_out;
238 mp_vfs_mount_t *existing_mount = mp_vfs_lookup_path(mp_obj_str_get_str(pos_args[1]), &path_out);
239 if (existing_mount != MP_VFS_NONE && existing_mount != MP_VFS_ROOT) {
240 if (vfs->len != 1 && existing_mount->len == 1) {
241 // if root dir is mounted, still allow to mount something within a subdir of root
242 } else {
243 // mount point in use
244 mp_raise_OSError(MP_EPERM);
245 }
246 }
247
248 // insert the vfs into the mount table
249 mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table);
250 while (*vfsp != NULL) {
251 if ((*vfsp)->len == 1) {
252 // make sure anything mounted at the root stays at the end of the list
253 vfs->next = *vfsp;
254 break;
255 }
256 vfsp = &(*vfsp)->next;
257 }
258 *vfsp = vfs;
259
260 return mp_const_none;
261 }
262 MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj, 2, mp_vfs_mount);
263
mp_vfs_umount(mp_obj_t mnt_in)264 mp_obj_t mp_vfs_umount(mp_obj_t mnt_in) {
265 // remove vfs from the mount table
266 mp_vfs_mount_t *vfs = NULL;
267 size_t mnt_len;
268 const char *mnt_str = NULL;
269 if (mp_obj_is_str(mnt_in)) {
270 mnt_str = mp_obj_str_get_data(mnt_in, &mnt_len);
271 }
272 for (mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) {
273 if ((mnt_str != NULL && !memcmp(mnt_str, (*vfsp)->str, mnt_len + 1)) || (*vfsp)->obj == mnt_in) {
274 vfs = *vfsp;
275 *vfsp = (*vfsp)->next;
276 break;
277 }
278 }
279
280 if (vfs == NULL) {
281 mp_raise_OSError(MP_EINVAL);
282 }
283
284 // if we unmounted the current device then set current to root
285 if (MP_STATE_VM(vfs_cur) == vfs) {
286 MP_STATE_VM(vfs_cur) = MP_VFS_ROOT;
287 }
288
289 // call the underlying object to do any unmounting operation
290 mp_vfs_proxy_call(vfs, MP_QSTR_umount, 0, NULL);
291
292 return mp_const_none;
293 }
294 MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_umount_obj, mp_vfs_umount);
295
296 // Note: buffering and encoding args are currently ignored
mp_vfs_open(size_t n_args,const mp_obj_t * pos_args,mp_map_t * kw_args)297 mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
298 enum { ARG_file, ARG_mode, ARG_encoding };
299 static const mp_arg_t allowed_args[] = {
300 { MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE} },
301 { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR_r)} },
302 { MP_QSTR_buffering, MP_ARG_INT, {.u_int = -1} },
303 { MP_QSTR_encoding, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
304 };
305
306 // parse args
307 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
308 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
309
310 #if MICROPY_VFS_POSIX
311 // If the file is an integer then delegate straight to the POSIX handler
312 if (mp_obj_is_small_int(args[ARG_file].u_obj)) {
313 return mp_vfs_posix_file_open(&mp_type_textio, args[ARG_file].u_obj, args[ARG_mode].u_obj);
314 }
315 #endif
316
317 mp_vfs_mount_t *vfs = lookup_path(args[ARG_file].u_obj, &args[ARG_file].u_obj);
318 return mp_vfs_proxy_call(vfs, MP_QSTR_open, 2, (mp_obj_t *)&args);
319 }
320 MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_open_obj, 0, mp_vfs_open);
321
mp_vfs_chdir(mp_obj_t path_in)322 mp_obj_t mp_vfs_chdir(mp_obj_t path_in) {
323 mp_obj_t path_out;
324 mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
325 if (vfs == MP_VFS_ROOT) {
326 // If we change to the root dir and a VFS is mounted at the root then
327 // we must change that VFS's current dir to the root dir so that any
328 // subsequent relative paths begin at the root of that VFS.
329 for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
330 if (vfs->len == 1) {
331 mp_obj_t root = MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
332 mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &root);
333 break;
334 }
335 }
336 vfs = MP_VFS_ROOT;
337 } else {
338 mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out);
339 }
340 MP_STATE_VM(vfs_cur) = vfs;
341 return mp_const_none;
342 }
343 MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj, mp_vfs_chdir);
344
mp_vfs_getcwd(void)345 mp_obj_t mp_vfs_getcwd(void) {
346 if (MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) {
347 return MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
348 }
349 mp_obj_t cwd_o = mp_vfs_proxy_call(MP_STATE_VM(vfs_cur), MP_QSTR_getcwd, 0, NULL);
350 if (MP_STATE_VM(vfs_cur)->len == 1) {
351 // don't prepend "/" for vfs mounted at root
352 return cwd_o;
353 }
354 const char *cwd = mp_obj_str_get_str(cwd_o);
355 vstr_t vstr;
356 vstr_init(&vstr, MP_STATE_VM(vfs_cur)->len + strlen(cwd) + 1);
357 vstr_add_strn(&vstr, MP_STATE_VM(vfs_cur)->str, MP_STATE_VM(vfs_cur)->len);
358 if (!(cwd[0] == '/' && cwd[1] == 0)) {
359 vstr_add_str(&vstr, cwd);
360 }
361 return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
362 }
363 MP_DEFINE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj, mp_vfs_getcwd);
364
365 typedef struct _mp_vfs_ilistdir_it_t {
366 mp_obj_base_t base;
367 mp_fun_1_t iternext;
368 union {
369 mp_vfs_mount_t *vfs;
370 mp_obj_t iter;
371 } cur;
372 bool is_str;
373 bool is_iter;
374 } mp_vfs_ilistdir_it_t;
375
mp_vfs_ilistdir_it_iternext(mp_obj_t self_in)376 STATIC mp_obj_t mp_vfs_ilistdir_it_iternext(mp_obj_t self_in) {
377 mp_vfs_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
378 if (self->is_iter) {
379 // continue delegating to root dir
380 return mp_iternext(self->cur.iter);
381 } else if (self->cur.vfs == NULL) {
382 // finished iterating mount points and no root dir is mounted
383 return MP_OBJ_STOP_ITERATION;
384 } else {
385 // continue iterating mount points
386 mp_vfs_mount_t *vfs = self->cur.vfs;
387 self->cur.vfs = vfs->next;
388 if (vfs->len == 1) {
389 // vfs is mounted at root dir, delegate to it
390 mp_obj_t root = MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
391 self->is_iter = true;
392 self->cur.iter = mp_vfs_proxy_call(vfs, MP_QSTR_ilistdir, 1, &root);
393 return mp_iternext(self->cur.iter);
394 } else {
395 // a mounted directory
396 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
397 t->items[0] = mp_obj_new_str_of_type(
398 self->is_str ? &mp_type_str : &mp_type_bytes,
399 (const byte *)vfs->str + 1, vfs->len - 1);
400 t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
401 t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
402 return MP_OBJ_FROM_PTR(t);
403 }
404 }
405 }
406
mp_vfs_ilistdir(size_t n_args,const mp_obj_t * args)407 mp_obj_t mp_vfs_ilistdir(size_t n_args, const mp_obj_t *args) {
408 mp_obj_t path_in;
409 if (n_args == 1) {
410 path_in = args[0];
411 } else {
412 path_in = MP_OBJ_NEW_QSTR(MP_QSTR_);
413 }
414
415 mp_obj_t path_out;
416 mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
417
418 if (vfs == MP_VFS_ROOT) {
419 // list the root directory
420 mp_vfs_ilistdir_it_t *iter = m_new_obj(mp_vfs_ilistdir_it_t);
421 iter->base.type = &mp_type_polymorph_iter;
422 iter->iternext = mp_vfs_ilistdir_it_iternext;
423 iter->cur.vfs = MP_STATE_VM(vfs_mount_table);
424 iter->is_str = mp_obj_get_type(path_in) == &mp_type_str;
425 iter->is_iter = false;
426 return MP_OBJ_FROM_PTR(iter);
427 }
428
429 return mp_vfs_proxy_call(vfs, MP_QSTR_ilistdir, 1, &path_out);
430 }
431 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_ilistdir_obj, 0, 1, mp_vfs_ilistdir);
432
mp_vfs_listdir(size_t n_args,const mp_obj_t * args)433 mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) {
434 mp_obj_t iter = mp_vfs_ilistdir(n_args, args);
435 mp_obj_t dir_list = mp_obj_new_list(0, NULL);
436 mp_obj_t next;
437 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
438 mp_obj_list_append(dir_list, mp_obj_subscr(next, MP_OBJ_NEW_SMALL_INT(0), MP_OBJ_SENTINEL));
439 }
440 return dir_list;
441 }
442 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj, 0, 1, mp_vfs_listdir);
443
mp_vfs_mkdir(mp_obj_t path_in)444 mp_obj_t mp_vfs_mkdir(mp_obj_t path_in) {
445 mp_obj_t path_out;
446 mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
447 if (vfs == MP_VFS_ROOT || (vfs != MP_VFS_NONE && !strcmp(mp_obj_str_get_str(path_out), "/"))) {
448 mp_raise_OSError(MP_EEXIST);
449 }
450 return mp_vfs_proxy_call(vfs, MP_QSTR_mkdir, 1, &path_out);
451 }
452 MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj, mp_vfs_mkdir);
453
mp_vfs_remove(mp_obj_t path_in)454 mp_obj_t mp_vfs_remove(mp_obj_t path_in) {
455 mp_obj_t path_out;
456 mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
457 return mp_vfs_proxy_call(vfs, MP_QSTR_remove, 1, &path_out);
458 }
459 MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_remove_obj, mp_vfs_remove);
460
mp_vfs_rename(mp_obj_t old_path_in,mp_obj_t new_path_in)461 mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) {
462 mp_obj_t args[2];
463 mp_vfs_mount_t *old_vfs = lookup_path(old_path_in, &args[0]);
464 mp_vfs_mount_t *new_vfs = lookup_path(new_path_in, &args[1]);
465 if (old_vfs != new_vfs) {
466 // can't rename across filesystems
467 mp_raise_OSError(MP_EPERM);
468 }
469 return mp_vfs_proxy_call(old_vfs, MP_QSTR_rename, 2, args);
470 }
471 MP_DEFINE_CONST_FUN_OBJ_2(mp_vfs_rename_obj, mp_vfs_rename);
472
mp_vfs_rmdir(mp_obj_t path_in)473 mp_obj_t mp_vfs_rmdir(mp_obj_t path_in) {
474 mp_obj_t path_out;
475 mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
476 return mp_vfs_proxy_call(vfs, MP_QSTR_rmdir, 1, &path_out);
477 }
478 MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_rmdir_obj, mp_vfs_rmdir);
479
mp_vfs_stat(mp_obj_t path_in)480 mp_obj_t mp_vfs_stat(mp_obj_t path_in) {
481 mp_obj_t path_out;
482 mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
483 if (vfs == MP_VFS_ROOT) {
484 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
485 t->items[0] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR); // st_mode
486 for (int i = 1; i <= 9; ++i) {
487 t->items[i] = MP_OBJ_NEW_SMALL_INT(0); // dev, nlink, uid, gid, size, atime, mtime, ctime
488 }
489 return MP_OBJ_FROM_PTR(t);
490 }
491 return mp_vfs_proxy_call(vfs, MP_QSTR_stat, 1, &path_out);
492 }
493 MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_stat_obj, mp_vfs_stat);
494
mp_vfs_statvfs(mp_obj_t path_in)495 mp_obj_t mp_vfs_statvfs(mp_obj_t path_in) {
496 mp_obj_t path_out;
497 mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
498 if (vfs == MP_VFS_ROOT) {
499 // statvfs called on the root directory, see if there's anything mounted there
500 for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
501 if (vfs->len == 1) {
502 break;
503 }
504 }
505
506 // If there's nothing mounted at root then return a mostly-empty tuple
507 if (vfs == NULL) {
508 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
509
510 // fill in: bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flags
511 for (int i = 0; i <= 8; ++i) {
512 t->items[i] = MP_OBJ_NEW_SMALL_INT(0);
513 }
514
515 // Put something sensible in f_namemax
516 t->items[9] = MP_OBJ_NEW_SMALL_INT(MICROPY_ALLOC_PATH_MAX);
517
518 return MP_OBJ_FROM_PTR(t);
519 }
520
521 // VFS mounted at root so delegate the call to it
522 path_out = MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
523 }
524 return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out);
525 }
526 MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_statvfs_obj, mp_vfs_statvfs);
527
528 // This is a C-level helper function for ports to use if needed.
mp_vfs_mount_and_chdir_protected(mp_obj_t bdev,mp_obj_t mount_point)529 int mp_vfs_mount_and_chdir_protected(mp_obj_t bdev, mp_obj_t mount_point) {
530 nlr_buf_t nlr;
531 mp_int_t ret = -MP_EIO;
532 if (nlr_push(&nlr) == 0) {
533 mp_obj_t args[] = { bdev, mount_point };
534 mp_vfs_mount(2, args, (mp_map_t *)&mp_const_empty_map);
535 mp_vfs_chdir(mount_point);
536 ret = 0; // success
537 nlr_pop();
538 } else {
539 mp_obj_base_t *exc = nlr.ret_val;
540 if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(exc->type), MP_OBJ_FROM_PTR(&mp_type_OSError))) {
541 mp_obj_t v = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(exc));
542 mp_obj_get_int_maybe(v, &ret); // get errno value
543 ret = -ret;
544 }
545 }
546 return ret;
547 }
548
549 #endif // MICROPY_VFS
550