vfs_fat_misc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 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 "py/mpconfig.h"
  27. #if MICROPY_VFS_FAT
  28. #include <string.h>
  29. #include "py/runtime.h"
  30. #include "lib/oofatfs/ff.h"
  31. #include "extmod/vfs_fat.h"
  32. #include "py/lexer.h"
  33. typedef struct _mp_vfs_fat_ilistdir_it_t {
  34. mp_obj_base_t base;
  35. mp_fun_1_t iternext;
  36. bool is_str;
  37. FF_DIR dir;
  38. } mp_vfs_fat_ilistdir_it_t;
  39. STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
  40. mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
  41. for (;;) {
  42. FILINFO fno;
  43. FRESULT res = f_readdir(&self->dir, &fno);
  44. char *fn = fno.fname;
  45. if (res != FR_OK || fn[0] == 0) {
  46. // stop on error or end of dir
  47. break;
  48. }
  49. // Note that FatFS already filters . and .., so we don't need to
  50. // make 3-tuple with info about this entry
  51. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
  52. if (self->is_str) {
  53. t->items[0] = mp_obj_new_str(fn, strlen(fn), false);
  54. } else {
  55. t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn));
  56. }
  57. if (fno.fattrib & AM_DIR) {
  58. // dir
  59. t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
  60. } else {
  61. // file
  62. t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
  63. }
  64. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
  65. return MP_OBJ_FROM_PTR(t);
  66. }
  67. // ignore error because we may be closing a second time
  68. f_closedir(&self->dir);
  69. return MP_OBJ_STOP_ITERATION;
  70. }
  71. mp_obj_t fat_vfs_ilistdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) {
  72. mp_vfs_fat_ilistdir_it_t *iter = m_new_obj(mp_vfs_fat_ilistdir_it_t);
  73. iter->base.type = &mp_type_polymorph_iter;
  74. iter->iternext = mp_vfs_fat_ilistdir_it_iternext;
  75. iter->is_str = is_str_type;
  76. FRESULT res = f_opendir(&vfs->fatfs, &iter->dir, path);
  77. if (res != FR_OK) {
  78. mp_raise_OSError(fresult_to_errno_table[res]);
  79. }
  80. return MP_OBJ_FROM_PTR(iter);
  81. }
  82. mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) {
  83. FILINFO fno;
  84. assert(vfs != NULL);
  85. FRESULT res = f_stat(&vfs->fatfs, path, &fno);
  86. if (res == FR_OK) {
  87. if ((fno.fattrib & AM_DIR) != 0) {
  88. return MP_IMPORT_STAT_DIR;
  89. } else {
  90. return MP_IMPORT_STAT_FILE;
  91. }
  92. }
  93. return MP_IMPORT_STAT_NO_EXIST;
  94. }
  95. #endif // MICROPY_VFS_FAT