vfs.c 19 KB

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