vfs.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifndef MICROPY_INCLUDED_EXTMOD_VFS_H
  27. #define MICROPY_INCLUDED_EXTMOD_VFS_H
  28. #include "py/lexer.h"
  29. #include "py/obj.h"
  30. // return values of mp_vfs_lookup_path
  31. // ROOT is 0 so that the default current directory is the root directory
  32. #define MP_VFS_NONE ((mp_vfs_mount_t*)1)
  33. #define MP_VFS_ROOT ((mp_vfs_mount_t*)0)
  34. // MicroPython's port-standardized versions of stat constants
  35. #define MP_S_IFDIR (0x4000)
  36. #define MP_S_IFREG (0x8000)
  37. // these are the values for mp_vfs_blockdev_t.flags
  38. #define MP_BLOCKDEV_FLAG_NATIVE (0x0001) // readblocks[2]/writeblocks[2] contain native func
  39. #define MP_BLOCKDEV_FLAG_FREE_OBJ (0x0002) // fs_user_mount_t obj should be freed on umount
  40. #define MP_BLOCKDEV_FLAG_HAVE_IOCTL (0x0004) // new protocol with ioctl
  41. #define MP_BLOCKDEV_FLAG_NO_FILESYSTEM (0x0008) // the block device has no filesystem on it
  42. // constants for block protocol ioctl
  43. #define MP_BLOCKDEV_IOCTL_INIT (1)
  44. #define MP_BLOCKDEV_IOCTL_DEINIT (2)
  45. #define MP_BLOCKDEV_IOCTL_SYNC (3)
  46. #define MP_BLOCKDEV_IOCTL_BLOCK_COUNT (4)
  47. #define MP_BLOCKDEV_IOCTL_BLOCK_SIZE (5)
  48. #define MP_BLOCKDEV_IOCTL_BLOCK_ERASE (6)
  49. // At the moment the VFS protocol just has import_stat, but could be extended to other methods
  50. typedef struct _mp_vfs_proto_t {
  51. mp_import_stat_t (*import_stat)(void *self, const char *path);
  52. } mp_vfs_proto_t;
  53. typedef struct _mp_vfs_blockdev_t {
  54. uint16_t flags;
  55. size_t block_size;
  56. mp_obj_t readblocks[5];
  57. mp_obj_t writeblocks[5];
  58. // new protocol uses just ioctl, old uses sync (optional) and count
  59. union {
  60. mp_obj_t ioctl[4];
  61. struct {
  62. mp_obj_t sync[2];
  63. mp_obj_t count[2];
  64. } old;
  65. } u;
  66. } mp_vfs_blockdev_t;
  67. typedef struct _mp_vfs_mount_t {
  68. const char *str; // mount point with leading /
  69. size_t len;
  70. mp_obj_t obj;
  71. struct _mp_vfs_mount_t *next;
  72. } mp_vfs_mount_t;
  73. void mp_vfs_blockdev_init(mp_vfs_blockdev_t *self, mp_obj_t bdev);
  74. int mp_vfs_blockdev_read(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, uint8_t *buf);
  75. int mp_vfs_blockdev_read_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t block_off, size_t len, uint8_t *buf);
  76. int mp_vfs_blockdev_write(mp_vfs_blockdev_t *self, size_t block_num, size_t num_blocks, const uint8_t *buf);
  77. int mp_vfs_blockdev_write_ext(mp_vfs_blockdev_t *self, size_t block_num, size_t block_off, size_t len, const uint8_t *buf);
  78. mp_obj_t mp_vfs_blockdev_ioctl(mp_vfs_blockdev_t *self, uintptr_t cmd, uintptr_t arg);
  79. mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out);
  80. mp_import_stat_t mp_vfs_import_stat(const char *path);
  81. mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
  82. mp_obj_t mp_vfs_umount(mp_obj_t mnt_in);
  83. mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
  84. mp_obj_t mp_vfs_chdir(mp_obj_t path_in);
  85. mp_obj_t mp_vfs_getcwd(void);
  86. mp_obj_t mp_vfs_ilistdir(size_t n_args, const mp_obj_t *args);
  87. mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args);
  88. mp_obj_t mp_vfs_mkdir(mp_obj_t path_in);
  89. mp_obj_t mp_vfs_remove(mp_obj_t path_in);
  90. mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in);
  91. mp_obj_t mp_vfs_rmdir(mp_obj_t path_in);
  92. mp_obj_t mp_vfs_stat(mp_obj_t path_in);
  93. mp_obj_t mp_vfs_statvfs(mp_obj_t path_in);
  94. MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj);
  95. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_umount_obj);
  96. MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_open_obj);
  97. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj);
  98. MP_DECLARE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj);
  99. MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_ilistdir_obj);
  100. MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj);
  101. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj);
  102. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_remove_obj);
  103. MP_DECLARE_CONST_FUN_OBJ_2(mp_vfs_rename_obj);
  104. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_rmdir_obj);
  105. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_stat_obj);
  106. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_statvfs_obj);
  107. #endif // MICROPY_INCLUDED_EXTMOD_VFS_H