blob: 1f90ac14ced96bfeb9306ef35213ff96993ab20e [file] [log] [blame]
Paul Sokolovskycd6d1892016-02-28 17:17:24 +02001/*
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 George1808b2e2017-01-29 15:21:46 +110028#if MICROPY_VFS_FAT
Paul Sokolovskycd6d1892016-02-28 17:17:24 +020029
30#include <string.h>
Paul Sokolovskycd6d1892016-02-28 17:17:24 +020031#include "py/runtime.h"
Damien Georgef5f4cda2016-06-01 17:00:28 +010032#include "lib/oofatfs/ff.h"
Damien George32a11382017-01-27 15:04:17 +110033#include "extmod/vfs_fat.h"
Paul Sokolovsky6ef65e72016-02-29 00:44:12 +020034#include "py/lexer.h"
Paul Sokolovskycd6d1892016-02-28 17:17:24 +020035
Damien Georged4cd4832017-05-05 23:32:44 +100036typedef struct _mp_vfs_fat_ilistdir_it_t {
37 mp_obj_base_t base;
38 mp_fun_1_t iternext;
39 bool is_str;
Damien Georgeec327432017-01-29 23:05:33 +110040 FF_DIR dir;
Damien Georged4cd4832017-05-05 23:32:44 +100041} mp_vfs_fat_ilistdir_it_t;
Paul Sokolovskycd6d1892016-02-28 17:17:24 +020042
Damien Georged4cd4832017-05-05 23:32:44 +100043STATIC 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 Georgef95e4e72017-05-13 18:58:46 +100054
55 // Note that FatFS already filters . and .., so we don't need to
Damien Georged4cd4832017-05-05 23:32:44 +100056
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 George46017592017-11-16 13:17:51 +110060 t->items[0] = mp_obj_new_str(fn, strlen(fn));
Damien Georged4cd4832017-05-05 23:32:44 +100061 } 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
82mp_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 Sokolovskycd6d1892016-02-28 17:17:24 +020088 if (res != FR_OK) {
Damien George620c4c32016-10-07 13:44:55 +110089 mp_raise_OSError(fresult_to_errno_table[res]);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +020090 }
Damien Georged4cd4832017-05-05 23:32:44 +100091 return MP_OBJ_FROM_PTR(iter);
Paul Sokolovskycd6d1892016-02-28 17:17:24 +020092}
93
Damien George6c23c752017-01-27 17:17:54 +110094mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) {
Paul Sokolovsky6ef65e72016-02-29 00:44:12 +020095 FILINFO fno;
Damien George6c23c752017-01-27 17:17:54 +110096 assert(vfs != NULL);
Damien Georgef5f4cda2016-06-01 17:00:28 +010097 FRESULT res = f_stat(&vfs->fatfs, path, &fno);
Paul Sokolovsky6ef65e72016-02-29 00:44:12 +020098 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 Sokolovskycd6d1892016-02-28 17:17:24 +0200108#endif // MICROPY_VFS_FAT