vfs.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // constants for block protocol ioctl
  38. #define BP_IOCTL_INIT (1)
  39. #define BP_IOCTL_DEINIT (2)
  40. #define BP_IOCTL_SYNC (3)
  41. #define BP_IOCTL_SEC_COUNT (4)
  42. #define BP_IOCTL_SEC_SIZE (5)
  43. // At the moment the VFS protocol just has import_stat, but could be extended to other methods
  44. typedef struct _mp_vfs_proto_t {
  45. mp_import_stat_t (*import_stat)(void *self, const char *path);
  46. } mp_vfs_proto_t;
  47. typedef struct _mp_vfs_mount_t {
  48. const char *str; // mount point with leading /
  49. size_t len;
  50. mp_obj_t obj;
  51. struct _mp_vfs_mount_t *next;
  52. } mp_vfs_mount_t;
  53. mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out);
  54. mp_import_stat_t mp_vfs_import_stat(const char *path);
  55. mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
  56. mp_obj_t mp_vfs_umount(mp_obj_t mnt_in);
  57. mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
  58. mp_obj_t mp_vfs_chdir(mp_obj_t path_in);
  59. mp_obj_t mp_vfs_getcwd(void);
  60. mp_obj_t mp_vfs_ilistdir(size_t n_args, const mp_obj_t *args);
  61. mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args);
  62. mp_obj_t mp_vfs_mkdir(mp_obj_t path_in);
  63. mp_obj_t mp_vfs_remove(mp_obj_t path_in);
  64. mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in);
  65. mp_obj_t mp_vfs_rmdir(mp_obj_t path_in);
  66. mp_obj_t mp_vfs_stat(mp_obj_t path_in);
  67. mp_obj_t mp_vfs_statvfs(mp_obj_t path_in);
  68. MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj);
  69. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_umount_obj);
  70. MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_open_obj);
  71. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj);
  72. MP_DECLARE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj);
  73. MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_ilistdir_obj);
  74. MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj);
  75. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj);
  76. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_remove_obj);
  77. MP_DECLARE_CONST_FUN_OBJ_2(mp_vfs_rename_obj);
  78. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_rmdir_obj);
  79. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_stat_obj);
  80. MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_statvfs_obj);
  81. #endif // MICROPY_INCLUDED_EXTMOD_VFS_H