Paul Sokolovsky | cd6d189 | 2016-02-28 17:17:24 +0200 | [diff] [blame] | 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 | |
| 27 | #include "py/mpconfig.h" |
Damien George | 1808b2e | 2017-01-29 15:21:46 +1100 | [diff] [blame] | 28 | #if MICROPY_VFS_FAT |
Paul Sokolovsky | cd6d189 | 2016-02-28 17:17:24 +0200 | [diff] [blame] | 29 | |
| 30 | #include <string.h> |
Paul Sokolovsky | cd6d189 | 2016-02-28 17:17:24 +0200 | [diff] [blame] | 31 | #include "py/runtime.h" |
Damien George | f5f4cda | 2016-06-01 17:00:28 +0100 | [diff] [blame] | 32 | #include "lib/oofatfs/ff.h" |
Damien George | 32a1138 | 2017-01-27 15:04:17 +1100 | [diff] [blame] | 33 | #include "extmod/vfs_fat.h" |
Paul Sokolovsky | 6ef65e7 | 2016-02-29 00:44:12 +0200 | [diff] [blame] | 34 | #include "py/lexer.h" |
Paul Sokolovsky | cd6d189 | 2016-02-28 17:17:24 +0200 | [diff] [blame] | 35 | |
Damien George | d4cd483 | 2017-05-05 23:32:44 +1000 | [diff] [blame] | 36 | typedef struct _mp_vfs_fat_ilistdir_it_t { |
| 37 | mp_obj_base_t base; |
| 38 | mp_fun_1_t iternext; |
| 39 | bool is_str; |
Damien George | ec32743 | 2017-01-29 23:05:33 +1100 | [diff] [blame] | 40 | FF_DIR dir; |
Damien George | d4cd483 | 2017-05-05 23:32:44 +1000 | [diff] [blame] | 41 | } mp_vfs_fat_ilistdir_it_t; |
Paul Sokolovsky | cd6d189 | 2016-02-28 17:17:24 +0200 | [diff] [blame] | 42 | |
Damien George | d4cd483 | 2017-05-05 23:32:44 +1000 | [diff] [blame] | 43 | STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) { |
| 44 | mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in); |
| 45 | |
| 46 | for (;;) { |
| 47 | FILINFO fno; |
| 48 | FRESULT res = f_readdir(&self->dir, &fno); |
| 49 | char *fn = fno.fname; |
| 50 | if (res != FR_OK || fn[0] == 0) { |
| 51 | // stop on error or end of dir |
| 52 | break; |
| 53 | } |
Damien George | f95e4e7 | 2017-05-13 18:58:46 +1000 | [diff] [blame] | 54 | |
| 55 | // Note that FatFS already filters . and .., so we don't need to |
Damien George | d4cd483 | 2017-05-05 23:32:44 +1000 | [diff] [blame] | 56 | |
| 57 | // make 3-tuple with info about this entry |
| 58 | mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); |
| 59 | if (self->is_str) { |
Damien George | 4601759 | 2017-11-16 13:17:51 +1100 | [diff] [blame^] | 60 | t->items[0] = mp_obj_new_str(fn, strlen(fn)); |
Damien George | d4cd483 | 2017-05-05 23:32:44 +1000 | [diff] [blame] | 61 | } else { |
| 62 | t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn)); |
| 63 | } |
| 64 | if (fno.fattrib & AM_DIR) { |
| 65 | // dir |
| 66 | t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR); |
| 67 | } else { |
| 68 | // file |
| 69 | t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG); |
| 70 | } |
| 71 | t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number |
| 72 | |
| 73 | return MP_OBJ_FROM_PTR(t); |
| 74 | } |
| 75 | |
| 76 | // ignore error because we may be closing a second time |
| 77 | f_closedir(&self->dir); |
| 78 | |
| 79 | return MP_OBJ_STOP_ITERATION; |
| 80 | } |
| 81 | |
| 82 | mp_obj_t fat_vfs_ilistdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) { |
| 83 | mp_vfs_fat_ilistdir_it_t *iter = m_new_obj(mp_vfs_fat_ilistdir_it_t); |
| 84 | iter->base.type = &mp_type_polymorph_iter; |
| 85 | iter->iternext = mp_vfs_fat_ilistdir_it_iternext; |
| 86 | iter->is_str = is_str_type; |
| 87 | FRESULT res = f_opendir(&vfs->fatfs, &iter->dir, path); |
Paul Sokolovsky | cd6d189 | 2016-02-28 17:17:24 +0200 | [diff] [blame] | 88 | if (res != FR_OK) { |
Damien George | 620c4c3 | 2016-10-07 13:44:55 +1100 | [diff] [blame] | 89 | mp_raise_OSError(fresult_to_errno_table[res]); |
Paul Sokolovsky | cd6d189 | 2016-02-28 17:17:24 +0200 | [diff] [blame] | 90 | } |
Damien George | d4cd483 | 2017-05-05 23:32:44 +1000 | [diff] [blame] | 91 | return MP_OBJ_FROM_PTR(iter); |
Paul Sokolovsky | cd6d189 | 2016-02-28 17:17:24 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Damien George | 6c23c75 | 2017-01-27 17:17:54 +1100 | [diff] [blame] | 94 | mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) { |
Paul Sokolovsky | 6ef65e7 | 2016-02-29 00:44:12 +0200 | [diff] [blame] | 95 | FILINFO fno; |
Damien George | 6c23c75 | 2017-01-27 17:17:54 +1100 | [diff] [blame] | 96 | assert(vfs != NULL); |
Damien George | f5f4cda | 2016-06-01 17:00:28 +0100 | [diff] [blame] | 97 | FRESULT res = f_stat(&vfs->fatfs, path, &fno); |
Paul Sokolovsky | 6ef65e7 | 2016-02-29 00:44:12 +0200 | [diff] [blame] | 98 | if (res == FR_OK) { |
| 99 | if ((fno.fattrib & AM_DIR) != 0) { |
| 100 | return MP_IMPORT_STAT_DIR; |
| 101 | } else { |
| 102 | return MP_IMPORT_STAT_FILE; |
| 103 | } |
| 104 | } |
| 105 | return MP_IMPORT_STAT_NO_EXIST; |
| 106 | } |
| 107 | |
Paul Sokolovsky | cd6d189 | 2016-02-28 17:17:24 +0200 | [diff] [blame] | 108 | #endif // MICROPY_VFS_FAT |