vfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. #include <stdint.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/objstr.h"
  30. #include "py/mperrno.h"
  31. #include "extmod/vfs.h"
  32. #if MICROPY_VFS
  33. #if MICROPY_VFS_FAT
  34. #include "extmod/vfs_fat.h"
  35. #endif
  36. #if MICROPY_VFS_POSIX
  37. #include "extmod/vfs_posix.h"
  38. #endif
  39. // For mp_vfs_proxy_call, the maximum number of additional args that can be passed.
  40. // A fixed maximum size is used to avoid the need for a costly variable array.
  41. #define PROXY_MAX_ARGS (2)
  42. // path is the path to lookup and *path_out holds the path within the VFS
  43. // object (starts with / if an absolute path).
  44. // Returns MP_VFS_ROOT for root dir (and then path_out is undefined) and
  45. // MP_VFS_NONE for path not found.
  46. mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out) {
  47. if (*path == '/' || MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) {
  48. // an absolute path, or the current volume is root, so search root dir
  49. bool is_abs = 0;
  50. if (*path == '/') {
  51. ++path;
  52. is_abs = 1;
  53. }
  54. if (*path == '\0') {
  55. // path is "" or "/" so return virtual root
  56. return MP_VFS_ROOT;
  57. }
  58. for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
  59. size_t len = vfs->len - 1;
  60. if (len == 0) {
  61. *path_out = path - is_abs;
  62. return vfs;
  63. }
  64. if (strncmp(path, vfs->str + 1, len) == 0) {
  65. if (path[len] == '/') {
  66. *path_out = path + len;
  67. return vfs;
  68. } else if (path[len] == '\0') {
  69. *path_out = "/";
  70. return vfs;
  71. }
  72. }
  73. }
  74. // if we get here then there's nothing mounted on /
  75. if (is_abs) {
  76. // path began with / and was not found
  77. return MP_VFS_NONE;
  78. }
  79. }
  80. // a relative path within a mounted device
  81. *path_out = path;
  82. return MP_STATE_VM(vfs_cur);
  83. }
  84. // Version of mp_vfs_lookup_path that takes and returns uPy string objects.
  85. STATIC mp_vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) {
  86. const char *path = mp_obj_str_get_str(path_in);
  87. const char *p_out;
  88. mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &p_out);
  89. if (vfs != MP_VFS_NONE && vfs != MP_VFS_ROOT) {
  90. *path_out = mp_obj_new_str_of_type(mp_obj_get_type(path_in),
  91. (const byte*)p_out, strlen(p_out));
  92. }
  93. return vfs;
  94. }
  95. 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) {
  96. assert(n_args <= PROXY_MAX_ARGS);
  97. if (vfs == MP_VFS_NONE) {
  98. // mount point not found
  99. mp_raise_OSError(MP_ENODEV);
  100. }
  101. if (vfs == MP_VFS_ROOT) {
  102. // can't do operation on root dir
  103. mp_raise_OSError(MP_EPERM);
  104. }
  105. mp_obj_t meth[2 + PROXY_MAX_ARGS];
  106. mp_load_method(vfs->obj, meth_name, meth);
  107. if (args != NULL) {
  108. memcpy(meth + 2, args, n_args * sizeof(*args));
  109. }
  110. return mp_call_method_n_kw(n_args, 0, meth);
  111. }
  112. mp_import_stat_t mp_vfs_import_stat(const char *path) {
  113. const char *path_out;
  114. mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &path_out);
  115. if (vfs == MP_VFS_NONE || vfs == MP_VFS_ROOT) {
  116. return MP_IMPORT_STAT_NO_EXIST;
  117. }
  118. // If the mounted object has the VFS protocol, call its import_stat helper
  119. const mp_vfs_proto_t *proto = mp_obj_get_type(vfs->obj)->protocol;
  120. if (proto != NULL) {
  121. return proto->import_stat(MP_OBJ_TO_PTR(vfs->obj), path_out);
  122. }
  123. // delegate to vfs.stat() method
  124. mp_obj_t path_o = mp_obj_new_str(path_out, strlen(path_out));
  125. mp_obj_t stat;
  126. nlr_buf_t nlr;
  127. if (nlr_push(&nlr) == 0) {
  128. stat = mp_vfs_proxy_call(vfs, MP_QSTR_stat, 1, &path_o);
  129. nlr_pop();
  130. } else {
  131. // assume an exception means that the path is not found
  132. return MP_IMPORT_STAT_NO_EXIST;
  133. }
  134. mp_obj_t *items;
  135. mp_obj_get_array_fixed_n(stat, 10, &items);
  136. mp_int_t st_mode = mp_obj_get_int(items[0]);
  137. if (st_mode & MP_S_IFDIR) {
  138. return MP_IMPORT_STAT_DIR;
  139. } else {
  140. return MP_IMPORT_STAT_FILE;
  141. }
  142. }
  143. mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  144. enum { ARG_readonly, ARG_mkfs };
  145. static const mp_arg_t allowed_args[] = {
  146. { MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_false_obj)} },
  147. { MP_QSTR_mkfs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_false_obj)} },
  148. };
  149. // parse args
  150. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  151. mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  152. // get the mount point
  153. size_t mnt_len;
  154. const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len);
  155. // see if we need to auto-detect and create the filesystem
  156. mp_obj_t vfs_obj = pos_args[0];
  157. mp_obj_t dest[2];
  158. mp_load_method_maybe(vfs_obj, MP_QSTR_mount, dest);
  159. if (dest[0] == MP_OBJ_NULL) {
  160. // Input object has no mount method, assume it's a block device and try to
  161. // auto-detect the filesystem and create the corresponding VFS entity.
  162. // (At the moment we only support FAT filesystems.)
  163. #if MICROPY_VFS_FAT
  164. vfs_obj = mp_fat_vfs_type.make_new(&mp_fat_vfs_type, 1, 0, &vfs_obj);
  165. #endif
  166. }
  167. // create new object
  168. mp_vfs_mount_t *vfs = m_new_obj(mp_vfs_mount_t);
  169. vfs->str = mnt_str;
  170. vfs->len = mnt_len;
  171. vfs->obj = vfs_obj;
  172. vfs->next = NULL;
  173. // call the underlying object to do any mounting operation
  174. mp_vfs_proxy_call(vfs, MP_QSTR_mount, 2, (mp_obj_t*)&args);
  175. // check that the destination mount point is unused
  176. const char *path_out;
  177. mp_vfs_mount_t *existing_mount = mp_vfs_lookup_path(mp_obj_str_get_str(pos_args[1]), &path_out);
  178. if (existing_mount != MP_VFS_NONE && existing_mount != MP_VFS_ROOT) {
  179. if (vfs->len != 1 && existing_mount->len == 1) {
  180. // if root dir is mounted, still allow to mount something within a subdir of root
  181. } else {
  182. // mount point in use
  183. mp_raise_OSError(MP_EPERM);
  184. }
  185. }
  186. // insert the vfs into the mount table
  187. mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table);
  188. while (*vfsp != NULL) {
  189. if ((*vfsp)->len == 1) {
  190. // make sure anything mounted at the root stays at the end of the list
  191. vfs->next = *vfsp;
  192. break;
  193. }
  194. vfsp = &(*vfsp)->next;
  195. }
  196. *vfsp = vfs;
  197. return mp_const_none;
  198. }
  199. MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj, 2, mp_vfs_mount);
  200. mp_obj_t mp_vfs_umount(mp_obj_t mnt_in) {
  201. // remove vfs from the mount table
  202. mp_vfs_mount_t *vfs = NULL;
  203. size_t mnt_len;
  204. const char *mnt_str = NULL;
  205. if (MP_OBJ_IS_STR(mnt_in)) {
  206. mnt_str = mp_obj_str_get_data(mnt_in, &mnt_len);
  207. }
  208. for (mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) {
  209. if ((mnt_str != NULL && !memcmp(mnt_str, (*vfsp)->str, mnt_len + 1)) || (*vfsp)->obj == mnt_in) {
  210. vfs = *vfsp;
  211. *vfsp = (*vfsp)->next;
  212. break;
  213. }
  214. }
  215. if (vfs == NULL) {
  216. mp_raise_OSError(MP_EINVAL);
  217. }
  218. // if we unmounted the current device then set current to root
  219. if (MP_STATE_VM(vfs_cur) == vfs) {
  220. MP_STATE_VM(vfs_cur) = MP_VFS_ROOT;
  221. }
  222. // call the underlying object to do any unmounting operation
  223. mp_vfs_proxy_call(vfs, MP_QSTR_umount, 0, NULL);
  224. return mp_const_none;
  225. }
  226. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_umount_obj, mp_vfs_umount);
  227. // Note: buffering and encoding args are currently ignored
  228. mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  229. enum { ARG_file, ARG_mode, ARG_encoding };
  230. static const mp_arg_t allowed_args[] = {
  231. { MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
  232. { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR_r)} },
  233. { MP_QSTR_buffering, MP_ARG_INT, {.u_int = -1} },
  234. { MP_QSTR_encoding, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
  235. };
  236. // parse args
  237. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  238. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  239. #if MICROPY_VFS_POSIX
  240. // If the file is an integer then delegate straight to the POSIX handler
  241. if (MP_OBJ_IS_SMALL_INT(args[ARG_file].u_obj)) {
  242. return mp_vfs_posix_file_open(&mp_type_textio, args[ARG_file].u_obj, args[ARG_mode].u_obj);
  243. }
  244. #endif
  245. mp_vfs_mount_t *vfs = lookup_path(args[ARG_file].u_obj, &args[ARG_file].u_obj);
  246. return mp_vfs_proxy_call(vfs, MP_QSTR_open, 2, (mp_obj_t*)&args);
  247. }
  248. MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_open_obj, 0, mp_vfs_open);
  249. mp_obj_t mp_vfs_chdir(mp_obj_t path_in) {
  250. mp_obj_t path_out;
  251. mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
  252. MP_STATE_VM(vfs_cur) = vfs;
  253. if (vfs == MP_VFS_ROOT) {
  254. // If we change to the root dir and a VFS is mounted at the root then
  255. // we must change that VFS's current dir to the root dir so that any
  256. // subsequent relative paths begin at the root of that VFS.
  257. for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
  258. if (vfs->len == 1) {
  259. mp_obj_t root = MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
  260. mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &root);
  261. break;
  262. }
  263. }
  264. } else {
  265. mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out);
  266. }
  267. return mp_const_none;
  268. }
  269. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj, mp_vfs_chdir);
  270. mp_obj_t mp_vfs_getcwd(void) {
  271. if (MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) {
  272. return MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
  273. }
  274. mp_obj_t cwd_o = mp_vfs_proxy_call(MP_STATE_VM(vfs_cur), MP_QSTR_getcwd, 0, NULL);
  275. if (MP_STATE_VM(vfs_cur)->len == 1) {
  276. // don't prepend "/" for vfs mounted at root
  277. return cwd_o;
  278. }
  279. const char *cwd = mp_obj_str_get_str(cwd_o);
  280. vstr_t vstr;
  281. vstr_init(&vstr, MP_STATE_VM(vfs_cur)->len + strlen(cwd) + 1);
  282. vstr_add_strn(&vstr, MP_STATE_VM(vfs_cur)->str, MP_STATE_VM(vfs_cur)->len);
  283. if (!(cwd[0] == '/' && cwd[1] == 0)) {
  284. vstr_add_str(&vstr, cwd);
  285. }
  286. return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
  287. }
  288. MP_DEFINE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj, mp_vfs_getcwd);
  289. typedef struct _mp_vfs_ilistdir_it_t {
  290. mp_obj_base_t base;
  291. mp_fun_1_t iternext;
  292. union {
  293. mp_vfs_mount_t *vfs;
  294. mp_obj_t iter;
  295. } cur;
  296. bool is_str;
  297. bool is_iter;
  298. } mp_vfs_ilistdir_it_t;
  299. STATIC mp_obj_t mp_vfs_ilistdir_it_iternext(mp_obj_t self_in) {
  300. mp_vfs_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
  301. if (self->is_iter) {
  302. // continue delegating to root dir
  303. return mp_iternext(self->cur.iter);
  304. } else if (self->cur.vfs == NULL) {
  305. // finished iterating mount points and no root dir is mounted
  306. return MP_OBJ_STOP_ITERATION;
  307. } else {
  308. // continue iterating mount points
  309. mp_vfs_mount_t *vfs = self->cur.vfs;
  310. self->cur.vfs = vfs->next;
  311. if (vfs->len == 1) {
  312. // vfs is mounted at root dir, delegate to it
  313. mp_obj_t root = MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
  314. self->is_iter = true;
  315. self->cur.iter = mp_vfs_proxy_call(vfs, MP_QSTR_ilistdir, 1, &root);
  316. return mp_iternext(self->cur.iter);
  317. } else {
  318. // a mounted directory
  319. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
  320. t->items[0] = mp_obj_new_str_of_type(
  321. self->is_str ? &mp_type_str : &mp_type_bytes,
  322. (const byte*)vfs->str + 1, vfs->len - 1);
  323. t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
  324. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
  325. return MP_OBJ_FROM_PTR(t);
  326. }
  327. }
  328. }
  329. mp_obj_t mp_vfs_ilistdir(size_t n_args, const mp_obj_t *args) {
  330. mp_obj_t path_in;
  331. if (n_args == 1) {
  332. path_in = args[0];
  333. } else {
  334. path_in = MP_OBJ_NEW_QSTR(MP_QSTR_);
  335. }
  336. mp_obj_t path_out;
  337. mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
  338. if (vfs == MP_VFS_ROOT) {
  339. // list the root directory
  340. mp_vfs_ilistdir_it_t *iter = m_new_obj(mp_vfs_ilistdir_it_t);
  341. iter->base.type = &mp_type_polymorph_iter;
  342. iter->iternext = mp_vfs_ilistdir_it_iternext;
  343. iter->cur.vfs = MP_STATE_VM(vfs_mount_table);
  344. iter->is_str = mp_obj_get_type(path_in) == &mp_type_str;
  345. iter->is_iter = false;
  346. return MP_OBJ_FROM_PTR(iter);
  347. }
  348. return mp_vfs_proxy_call(vfs, MP_QSTR_ilistdir, 1, &path_out);
  349. }
  350. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_ilistdir_obj, 0, 1, mp_vfs_ilistdir);
  351. mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) {
  352. mp_obj_t iter = mp_vfs_ilistdir(n_args, args);
  353. mp_obj_t dir_list = mp_obj_new_list(0, NULL);
  354. mp_obj_t next;
  355. while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
  356. mp_obj_list_append(dir_list, mp_obj_subscr(next, MP_OBJ_NEW_SMALL_INT(0), MP_OBJ_SENTINEL));
  357. }
  358. return dir_list;
  359. }
  360. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj, 0, 1, mp_vfs_listdir);
  361. mp_obj_t mp_vfs_mkdir(mp_obj_t path_in) {
  362. mp_obj_t path_out;
  363. mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
  364. if (vfs == MP_VFS_ROOT || (vfs != MP_VFS_NONE && !strcmp(mp_obj_str_get_str(path_out), "/"))) {
  365. mp_raise_OSError(MP_EEXIST);
  366. }
  367. return mp_vfs_proxy_call(vfs, MP_QSTR_mkdir, 1, &path_out);
  368. }
  369. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj, mp_vfs_mkdir);
  370. mp_obj_t mp_vfs_remove(mp_obj_t path_in) {
  371. mp_obj_t path_out;
  372. mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
  373. return mp_vfs_proxy_call(vfs, MP_QSTR_remove, 1, &path_out);
  374. }
  375. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_remove_obj, mp_vfs_remove);
  376. mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) {
  377. mp_obj_t args[2];
  378. mp_vfs_mount_t *old_vfs = lookup_path(old_path_in, &args[0]);
  379. mp_vfs_mount_t *new_vfs = lookup_path(new_path_in, &args[1]);
  380. if (old_vfs != new_vfs) {
  381. // can't rename across filesystems
  382. mp_raise_OSError(MP_EPERM);
  383. }
  384. return mp_vfs_proxy_call(old_vfs, MP_QSTR_rename, 2, args);
  385. }
  386. MP_DEFINE_CONST_FUN_OBJ_2(mp_vfs_rename_obj, mp_vfs_rename);
  387. mp_obj_t mp_vfs_rmdir(mp_obj_t path_in) {
  388. mp_obj_t path_out;
  389. mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
  390. return mp_vfs_proxy_call(vfs, MP_QSTR_rmdir, 1, &path_out);
  391. }
  392. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_rmdir_obj, mp_vfs_rmdir);
  393. mp_obj_t mp_vfs_stat(mp_obj_t path_in) {
  394. mp_obj_t path_out;
  395. mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
  396. if (vfs == MP_VFS_ROOT) {
  397. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  398. t->items[0] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR); // st_mode
  399. for (int i = 1; i <= 9; ++i) {
  400. t->items[i] = MP_OBJ_NEW_SMALL_INT(0); // dev, nlink, uid, gid, size, atime, mtime, ctime
  401. }
  402. return MP_OBJ_FROM_PTR(t);
  403. }
  404. return mp_vfs_proxy_call(vfs, MP_QSTR_stat, 1, &path_out);
  405. }
  406. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_stat_obj, mp_vfs_stat);
  407. mp_obj_t mp_vfs_statvfs(mp_obj_t path_in) {
  408. mp_obj_t path_out;
  409. mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
  410. if (vfs == MP_VFS_ROOT) {
  411. // statvfs called on the root directory, see if there's anything mounted there
  412. for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
  413. if (vfs->len == 1) {
  414. break;
  415. }
  416. }
  417. // If there's nothing mounted at root then return a mostly-empty tuple
  418. if (vfs == NULL) {
  419. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  420. // fill in: bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flags
  421. for (int i = 0; i <= 8; ++i) {
  422. t->items[i] = MP_OBJ_NEW_SMALL_INT(0);
  423. }
  424. // Put something sensible in f_namemax
  425. t->items[9] = MP_OBJ_NEW_SMALL_INT(MICROPY_ALLOC_PATH_MAX);
  426. return MP_OBJ_FROM_PTR(t);
  427. }
  428. // VFS mounted at root so delegate the call to it
  429. path_out = MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
  430. }
  431. return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out);
  432. }
  433. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_statvfs_obj, mp_vfs_statvfs);
  434. #endif // MICROPY_VFS