2016-02-28 10:17:24 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
2017-01-28 23:21:46 -05:00
|
|
|
#if MICROPY_VFS_FAT
|
2016-02-28 10:17:24 -05:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
2016-06-01 12:00:28 -04:00
|
|
|
#include "lib/oofatfs/ff.h"
|
2017-01-26 23:04:17 -05:00
|
|
|
#include "extmod/vfs_fat.h"
|
2016-02-28 17:44:12 -05:00
|
|
|
#include "py/lexer.h"
|
2016-02-28 10:17:24 -05:00
|
|
|
|
2017-05-05 09:32:44 -04:00
|
|
|
typedef struct _mp_vfs_fat_ilistdir_it_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_fun_1_t iternext;
|
|
|
|
bool is_str;
|
2017-01-29 07:05:33 -05:00
|
|
|
FF_DIR dir;
|
2017-05-05 09:32:44 -04:00
|
|
|
} mp_vfs_fat_ilistdir_it_t;
|
2016-02-28 10:17:24 -05:00
|
|
|
|
2017-05-05 09:32:44 -04:00
|
|
|
STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
|
|
|
|
mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-02-28 10:17:24 -05:00
|
|
|
|
|
|
|
for (;;) {
|
2017-05-05 09:32:44 -04:00
|
|
|
FILINFO fno;
|
|
|
|
FRESULT res = f_readdir(&self->dir, &fno);
|
2016-02-28 10:17:24 -05:00
|
|
|
char *fn = fno.fname;
|
2017-05-05 09:32:44 -04:00
|
|
|
if (res != FR_OK || fn[0] == 0) {
|
|
|
|
// stop on error or end of dir
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fn[0] == '.' && (fn[1] == 0 || (fn[1] == '.' && fn[2] == 0))) {
|
|
|
|
// skip . and ..
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-28 10:17:24 -05:00
|
|
|
|
2017-05-05 09:32:44 -04:00
|
|
|
// make 3-tuple with info about this entry
|
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
|
|
|
|
if (self->is_str) {
|
|
|
|
t->items[0] = mp_obj_new_str(fn, strlen(fn), false);
|
|
|
|
} else {
|
|
|
|
t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn));
|
|
|
|
}
|
2016-02-28 10:17:24 -05:00
|
|
|
if (fno.fattrib & AM_DIR) {
|
|
|
|
// dir
|
2017-05-05 09:32:44 -04:00
|
|
|
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
|
2016-02-28 10:17:24 -05:00
|
|
|
} else {
|
|
|
|
// file
|
2017-05-05 09:32:44 -04:00
|
|
|
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
|
2016-02-28 10:17:24 -05:00
|
|
|
}
|
2017-05-05 09:32:44 -04:00
|
|
|
t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
|
2016-02-28 10:17:24 -05:00
|
|
|
|
2017-05-05 09:32:44 -04:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2016-02-28 10:17:24 -05:00
|
|
|
}
|
|
|
|
|
2017-05-05 09:32:44 -04:00
|
|
|
// ignore error because we may be closing a second time
|
|
|
|
f_closedir(&self->dir);
|
2016-02-28 10:17:24 -05:00
|
|
|
|
2017-05-05 09:32:44 -04:00
|
|
|
return MP_OBJ_STOP_ITERATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t fat_vfs_ilistdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) {
|
|
|
|
mp_vfs_fat_ilistdir_it_t *iter = m_new_obj(mp_vfs_fat_ilistdir_it_t);
|
|
|
|
iter->base.type = &mp_type_polymorph_iter;
|
|
|
|
iter->iternext = mp_vfs_fat_ilistdir_it_iternext;
|
|
|
|
iter->is_str = is_str_type;
|
|
|
|
FRESULT res = f_opendir(&vfs->fatfs, &iter->dir, path);
|
|
|
|
if (res != FR_OK) {
|
|
|
|
mp_raise_OSError(fresult_to_errno_table[res]);
|
|
|
|
}
|
|
|
|
return MP_OBJ_FROM_PTR(iter);
|
2016-02-28 10:17:24 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 01:17:54 -05:00
|
|
|
mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) {
|
2016-02-28 17:44:12 -05:00
|
|
|
FILINFO fno;
|
2017-01-27 01:17:54 -05:00
|
|
|
assert(vfs != NULL);
|
2016-06-01 12:00:28 -04:00
|
|
|
FRESULT res = f_stat(&vfs->fatfs, path, &fno);
|
2016-02-28 17:44:12 -05:00
|
|
|
if (res == FR_OK) {
|
|
|
|
if ((fno.fattrib & AM_DIR) != 0) {
|
|
|
|
return MP_IMPORT_STAT_DIR;
|
|
|
|
} else {
|
|
|
|
return MP_IMPORT_STAT_FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MP_IMPORT_STAT_NO_EXIST;
|
|
|
|
}
|
|
|
|
|
2016-02-28 10:17:24 -05:00
|
|
|
#endif // MICROPY_VFS_FAT
|