Split uos module into os and storage.

os is a subset of CPython's os. storage contains additional
file system mounting functionality based on UNIX's mount
management.

Fixes #140
This commit is contained in:
Scott Shawcroft 2017-06-27 15:17:30 -07:00
parent e87a61ffb4
commit 778e975936
17 changed files with 852 additions and 220 deletions

View File

@ -187,7 +187,6 @@ SRC_C = \
fatfs_port.c \
flash_api.c \
main.c \
moduos.c \
mphalport.c \
samd21_pins.c \
shared_dma.c \
@ -228,25 +227,26 @@ STM_SRC_C = $(addprefix stmhal/,\
)
SRC_COMMON_HAL = \
board/__init__.c \
microcontroller/__init__.c \
microcontroller/Pin.c \
analogio/__init__.c \
analogio/AnalogIn.c \
analogio/AnalogOut.c \
audioio/__init__.c \
audioio/AudioOut.c \
digitalio/__init__.c \
digitalio/DigitalInOut.c \
pulseio/__init__.c \
pulseio/PulseIn.c \
pulseio/PulseOut.c \
pulseio/PWMOut.c \
board/__init__.c \
busio/__init__.c \
busio/I2C.c \
busio/SPI.c \
busio/UART.c \
digitalio/__init__.c \
digitalio/DigitalInOut.c \
microcontroller/__init__.c \
microcontroller/Pin.c \
neopixel_write/__init__.c \
os/__init__.c \
pulseio/__init__.c \
pulseio/PulseIn.c \
pulseio/PulseOut.c \
pulseio/PWMOut.c \
time/__init__.c \
touchio/__init__.c \
touchio/TouchIn.c \
@ -271,6 +271,8 @@ SRC_SHARED_MODULE = \
bitbangio/OneWire.c \
bitbangio/SPI.c \
busio/OneWire.c \
os/__init__.c \
storage/__init__.c \
uheap/__init__.c \
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \

View File

@ -0,0 +1,57 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* 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 "genhdr/mpversion.h"
#include "py/mpconfig.h"
#include "py/objstr.h"
#include "py/objtuple.h"
#include "py/qstr.h"
STATIC const qstr os_uname_info_fields[] = {
MP_QSTR_sysname, MP_QSTR_nodename,
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
};
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, "samd21");
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "samd21");
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING);
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE);
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME);
STATIC MP_DEFINE_ATTRTUPLE(
os_uname_info_obj,
os_uname_info_fields,
5,
(mp_obj_t)&os_uname_info_sysname_obj,
(mp_obj_t)&os_uname_info_nodename_obj,
(mp_obj_t)&os_uname_info_release_obj,
(mp_obj_t)&os_uname_info_version_obj,
(mp_obj_t)&os_uname_info_machine_obj
);
mp_obj_t common_hal_os_uname(void) {
return (mp_obj_t)&os_uname_info_obj;
}

View File

@ -1,122 +0,0 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 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 <stdint.h>
#include <string.h>
#include "py/mpstate.h"
#include "py/objtuple.h"
#include "py/objstr.h"
#include "py/runtime.h"
#include "genhdr/mpversion.h"
#include "lib/oofatfs/ff.h"
#include "lib/oofatfs/diskio.h"
#include "extmod/vfs.h"
#include "extmod/vfs_fat.h"
#include "timeutils.h"
/// \module os - basic "operating system" services
///
/// The `os` module contains functions for filesystem access and `urandom`.
///
/// The filesystem has `/` as the root directory, and the available physical
/// drives are accessible from here. They are currently:
///
/// /flash -- the internal flash filesystem
/// /sd -- the SD card (if it exists)
///
/// On boot up, the current directory is `/flash` if no SD card is inserted,
/// otherwise it is `/sd`.
STATIC const qstr os_uname_info_fields[] = {
MP_QSTR_sysname, MP_QSTR_nodename,
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
};
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_sysname_obj, "samd21");
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "samd21");
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_release_obj, MICROPY_VERSION_STRING);
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE);
STATIC const MP_DEFINE_STR_OBJ(os_uname_info_machine_obj, MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME);
STATIC MP_DEFINE_ATTRTUPLE(
os_uname_info_obj,
os_uname_info_fields,
5,
(mp_obj_t)&os_uname_info_sysname_obj,
(mp_obj_t)&os_uname_info_nodename_obj,
(mp_obj_t)&os_uname_info_release_obj,
(mp_obj_t)&os_uname_info_version_obj,
(mp_obj_t)&os_uname_info_machine_obj
);
STATIC mp_obj_t os_uname(void) {
return (mp_obj_t)&os_uname_info_obj;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname);
/// \function sync()
/// Sync all filesystems.
STATIC mp_obj_t os_sync(void) {
for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
// this assumes that vfs->obj is fs_user_mount_t with block device functions
disk_ioctl(MP_OBJ_TO_PTR(vfs->obj), CTRL_SYNC, NULL);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(mod_os_sync_obj, os_sync);
STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_uname), MP_ROM_PTR(&os_uname_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&mp_vfs_chdir_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&mp_vfs_getcwd_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&mp_vfs_listdir_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mp_vfs_mkdir_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_remove), MP_ROM_PTR(&mp_vfs_remove_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mp_vfs_rename_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mp_vfs_rmdir_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), MP_ROM_PTR(&mp_vfs_stat_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&mp_vfs_statvfs_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&mp_vfs_remove_obj) }, // unlink aliases to remove
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), MP_ROM_PTR(&mod_os_sync_obj) },
/// \constant sep - separation character used in paths
{ MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_OBJ_NEW_QSTR(MP_QSTR__slash_) },
// these are MicroPython extensions
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
};
STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
const mp_obj_module_t uos_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&os_module_globals,
};

View File

@ -137,7 +137,8 @@ extern const struct _mp_obj_module_t digitalio_module;
extern const struct _mp_obj_module_t pulseio_module;
extern const struct _mp_obj_module_t busio_module;
extern const struct _mp_obj_module_t board_module;
extern const struct _mp_obj_module_t uos_module;
extern const struct _mp_obj_module_t os_module;
extern const struct _mp_obj_module_t storage_module;
extern const struct _mp_obj_module_t time_module;
extern const struct _mp_obj_module_t neopixel_write_module;
extern const struct _mp_obj_module_t uheap_module;
@ -174,7 +175,8 @@ extern const struct _mp_obj_module_t usb_hid_module;
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_samd),(mp_obj_t)&samd_module }, \

View File

@ -1,11 +1,11 @@
Adafruit's MicroPython Documentation
Adafruit's CircuitPython Documentation
=========================
The latest documentation can be found at:
http://adafruit-micropython.readthedocs.io/en/latest/
http://circuitpython.readthedocs.io/en/latest/
The documentation you see there is generated from the files in the whole tree:
https://github.com/adafruit/micropython/tree/master
https://github.com/adafruit/circuitpython/tree/master
Building the documentation locally
----------------------------------
@ -19,8 +19,8 @@ preferably in a virtualenv:
pip install sphinx
pip install sphinx_rtd_theme
In `micropython/`, build the docs:
In `circuitpython/`, build the docs:
sphinx-build -v -b html . _build/html
You'll find the index page at `micropython/docs/_build/html/index.html`.
You'll find the index page at `_build/html/index.html`.

View File

@ -93,8 +93,6 @@ SRC_C = \
machine_hspi.c \
modesp.c \
modnetwork.c \
modutime.c \
moduos.c \
modonewire.c \
ets_alt_task.c \
fatfs_port.c \
@ -119,6 +117,7 @@ SRC_COMMON_HAL = \
busio/SPI.c \
busio/UART.c \
neopixel_write/__init__.c \
os/__init__.c \
time/__init__.c \
board/__init__.c
@ -139,6 +138,8 @@ SRC_SHARED_MODULE = \
bitbangio/SPI.c \
busio/I2C.c \
busio/OneWire.c \
os/__init__.c \
storage/__init__.c \
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
$(addprefix shared-module/, $(SRC_SHARED_MODULE))

View File

@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2015 Josef Gajdusek
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -28,11 +29,7 @@
#include "py/objtuple.h"
#include "py/objstr.h"
#include "extmod/misc.h"
#include "extmod/vfs.h"
#include "extmod/vfs_fat.h"
#include "genhdr/mpversion.h"
#include "esp_mphal.h"
#include "user_interface.h"
STATIC const qstr os_uname_info_fields[] = {
@ -57,60 +54,9 @@ STATIC mp_obj_tuple_t os_uname_info_obj = {
}
};
STATIC mp_obj_t os_uname(void) {
mp_obj_t common_hal_os_uname(void) {
// We must populate the "release" field each time in case it was GC'd since the last call.
const char *ver = system_get_sdk_version();
os_uname_info_obj.items[2] = mp_obj_new_str(ver, strlen(ver), false);
return (mp_obj_t)&os_uname_info_obj;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname);
STATIC mp_obj_t os_urandom(mp_obj_t num) {
mp_int_t n = mp_obj_get_int(num);
vstr_t vstr;
vstr_init_len(&vstr, n);
for (int i = 0; i < n; i++) {
vstr.buf[i] = *WDEV_HWRNG;
}
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
STATIC mp_obj_t os_dupterm_notify(mp_obj_t obj_in) {
(void)obj_in;
mp_hal_signal_dupterm_input();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_dupterm_notify_obj, os_dupterm_notify);
STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) },
{ MP_ROM_QSTR(MP_QSTR_uname), MP_ROM_PTR(&os_uname_obj) },
{ MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&os_urandom_obj) },
#if MICROPY_PY_OS_DUPTERM
{ MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mp_uos_dupterm_obj) },
{ MP_ROM_QSTR(MP_QSTR_dupterm_notify), MP_ROM_PTR(&os_dupterm_notify_obj) },
#endif
#if MICROPY_VFS_FAT
{ MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
{ MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&mp_vfs_ilistdir_obj) },
{ MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&mp_vfs_listdir_obj) },
{ MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mp_vfs_mkdir_obj) },
{ MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mp_vfs_rmdir_obj) },
{ MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&mp_vfs_chdir_obj) },
{ MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&mp_vfs_getcwd_obj) },
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&mp_vfs_remove_obj) },
{ MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mp_vfs_rename_obj) },
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&mp_vfs_stat_obj) },
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&mp_vfs_statvfs_obj) },
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) },
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) },
#endif
};
STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
const mp_obj_module_t uos_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&os_module_globals,
};

View File

@ -156,7 +156,8 @@ void *esp_native_code_commit(void*, size_t);
extern const struct _mp_obj_module_t esp_module;
extern const struct _mp_obj_module_t network_module;
extern const struct _mp_obj_module_t utime_module;
extern const struct _mp_obj_module_t uos_module;
extern const struct _mp_obj_module_t os_module;
extern const struct _mp_obj_module_t storage_module;
extern const struct _mp_obj_module_t mp_module_lwip;
extern const struct _mp_obj_module_t mp_module_machine;
extern const struct _mp_obj_module_t onewire_module;
@ -174,8 +175,7 @@ extern const struct _mp_obj_module_t time_module;
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&mp_module_lwip }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_lwip }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&network_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_utime), (mp_obj_t)&utime_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&onewire_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
@ -185,10 +185,10 @@ extern const struct _mp_obj_module_t time_module;
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
{ MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&uos_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_json), (mp_obj_t)&mp_module_ujson }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_errno), (mp_obj_t)&mp_module_uerrno }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&mp_module_uselect }, \

View File

@ -289,18 +289,7 @@ mp_obj_t mp_vfs_getcwd(void) {
}
MP_DEFINE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj, mp_vfs_getcwd);
typedef struct _mp_vfs_ilistdir_it_t {
mp_obj_base_t base;
mp_fun_1_t iternext;
union {
mp_vfs_mount_t *vfs;
mp_obj_t iter;
} cur;
bool is_str;
bool is_iter;
} mp_vfs_ilistdir_it_t;
STATIC mp_obj_t mp_vfs_ilistdir_it_iternext(mp_obj_t self_in) {
mp_obj_t mp_vfs_ilistdir_it_iternext(mp_obj_t self_in) {
mp_vfs_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
if (self->is_iter) {
// continue delegating to root dir

View File

@ -53,6 +53,17 @@ typedef struct _mp_vfs_mount_t {
struct _mp_vfs_mount_t *next;
} mp_vfs_mount_t;
typedef struct _mp_vfs_ilistdir_it_t {
mp_obj_base_t base;
mp_fun_1_t iternext;
union {
mp_vfs_mount_t *vfs;
mp_obj_t iter;
} cur;
bool is_str;
bool is_iter;
} mp_vfs_ilistdir_it_t;
mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out);
mp_import_stat_t mp_vfs_import_stat(const char *path);
mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
@ -69,6 +80,8 @@ mp_obj_t mp_vfs_rmdir(mp_obj_t path_in);
mp_obj_t mp_vfs_stat(mp_obj_t path_in);
mp_obj_t mp_vfs_statvfs(mp_obj_t path_in);
mp_obj_t mp_vfs_ilistdir_it_iternext(mp_obj_t self_in);
MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj);
MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_umount_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_open_obj);

View File

@ -12,13 +12,13 @@ limited. For example, a microcontroller without analog features will not have
Support Matrix
---------------
=============== ========== ========= =========== ======= ======= =========== ================= ================ ========= ======== ========= ======= =========
Port `analogio` `audioio` `bitbangio` `board` `busio` `digitalio` `microcontroller` `neopixel_write` `pulseio` `time` `touchio` `uheap` `usb_hid`
=============== ========== ========= =========== ======= ======= =========== ================= ================ ========= ======== ========= ======= =========
SAMD21 **Yes** No No **Yes** **Yes** **Yes** **Yes** **Yes** No **Yes** **Yes** Debug **Yes**
SAMD21 Express **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** Debug **Yes**
ESP8266 **Yes** No **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** No **Yes** No Debug No
=============== ========== ========= =========== ======= ======= =========== ================= ================ ========= ======== ========= ======= =========
=============== ========== ========= =========== ======= ======= =========== ================= ================ ======= ========= ========= ======== ========= ======= =========
Port `analogio` `audioio` `bitbangio` `board` `busio` `digitalio` `microcontroller` `neopixel_write` `os` `pulseio` `storage` `time` `touchio` `uheap` `usb_hid`
=============== ========== ========= =========== ======= ======= =========== ================= ================ ======= ========= ========= ======== ========= ======= =========
SAMD21 **Yes** No No **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** No **Yes** **Yes** **Yes** Debug **Yes**
SAMD21 Express **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** Debug **Yes**
ESP8266 **Yes** No **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** No **Yes** **Yes** No Debug No
=============== ========== ========= =========== ======= ======= =========== ================= ================ ======= ========= ========= ======== ========= ======= =========
Modules
---------

View File

@ -0,0 +1,221 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2015 Josef Gajdusek
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
*
* 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 <string.h>
#include "extmod/vfs.h"
#include "lib/oofatfs/ff.h"
#include "lib/oofatfs/diskio.h"
#include "py/mpstate.h"
#include "py/obj.h"
#include "shared-bindings/os/__init__.h"
//| :mod:`os` --- functions that an OS normally provides
//| ========================================================
//|
//| .. module:: os
//| :synopsis: functions that an OS normally provides
//| :platform: SAMD21
//|
//| The `os` module is a strict subset of the CPython `cpython:os` module. So,
//| code written in CircuitPython will work in CPython but not necessarily the
//| other way around.
//|
//| .. function:: uname()
//|
//| Returns a named tuple of operating specific and CircuitPython port
//| specific information.
//|
STATIC mp_obj_t os_uname(void) {
return common_hal_os_uname();
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname);
//| .. function:: chdir(path)
//|
//| Change current directory.
//|
mp_obj_t os_chdir(mp_obj_t path_in) {
const char *path = mp_obj_str_get_str(path_in);
common_hal_os_chdir(path);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(os_chdir_obj, os_chdir);
//| .. function:: getcwd()
//|
//| Get the current directory.
//|
mp_obj_t os_getcwd(void) {
return common_hal_os_getcwd();
}
MP_DEFINE_CONST_FUN_OBJ_0(os_getcwd_obj, os_getcwd);
//| .. function:: listdir([dir])
//|
//| With no argument, list the current directory. Otherwise list the given directory.
//|
mp_obj_t os_listdir(size_t n_args, const mp_obj_t *args) {
const char* path;
if (n_args == 1) {
path = mp_obj_str_get_str(args[0]);
} else {
path = "";
}
return common_hal_os_listdir(path);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
//| .. function:: mkdir(path)
//|
//| Create a new directory.
//|
mp_obj_t os_mkdir(mp_obj_t path_in) {
const char *path = mp_obj_str_get_str(path_in);
common_hal_os_mkdir(path);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
//| .. function:: remove(path)
//|
//| Remove a file.
//|
mp_obj_t os_remove(mp_obj_t path_in) {
const char *path = mp_obj_str_get_str(path_in);
common_hal_os_remove(path);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
//| .. function:: rmdir(path)
//|
//| Remove a directory.
//|
mp_obj_t os_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) {
const char *old_path = mp_obj_str_get_str(old_path_in);
const char *new_path = mp_obj_str_get_str(new_path_in);
common_hal_os_rename(old_path, new_path);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(os_rename_obj, os_rename);
//| .. function:: rename(old_path, new_path)
//|
//| Rename a file.
//|
mp_obj_t os_rmdir(mp_obj_t path_in) {
const char *path = mp_obj_str_get_str(path_in);
common_hal_os_rmdir(path);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
//| .. function:: stat(path)
//|
//| Get the status of a file or directory.
//|
mp_obj_t os_stat(mp_obj_t path_in) {
const char *path = mp_obj_str_get_str(path_in);
return common_hal_os_stat(path);
}
MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
//| .. function:: statvfs(path)
//|
//| Get the status of a fileystem.
//|
//| Returns a tuple with the filesystem information in the following order:
//|
//| * ``f_bsize`` -- file system block size
//| * ``f_frsize`` -- fragment size
//| * ``f_blocks`` -- size of fs in f_frsize units
//| * ``f_bfree`` -- number of free blocks
//| * ``f_bavail`` -- number of free blocks for unpriviliged users
//| * ``f_files`` -- number of inodes
//| * ``f_ffree`` -- number of free inodes
//| * ``f_favail`` -- number of free inodes for unpriviliged users
//| * ``f_flag`` -- mount flags
//| * ``f_namemax`` -- maximum filename length
//|
//| Parameters related to inodes: ``f_files``, ``f_ffree``, ``f_avail``
//| and the ``f_flags`` parameter may return ``0`` as they can be unavailable
//| in a port-specific implementation.
//|
mp_obj_t os_statvfs(mp_obj_t path_in) {
const char *path = mp_obj_str_get_str(path_in);
return common_hal_os_statvfs(path);
}
MP_DEFINE_CONST_FUN_OBJ_1(os_statvfs_obj, os_statvfs);
//| .. function:: sync()
//|
//| Sync all filesystems.
//|
STATIC mp_obj_t os_sync(void) {
for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
// this assumes that vfs->obj is fs_user_mount_t with block device functions
disk_ioctl(MP_OBJ_TO_PTR(vfs->obj), CTRL_SYNC, NULL);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(os_sync_obj, os_sync);
STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_os) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_uname), MP_ROM_PTR(&os_uname_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&os_chdir_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&os_getcwd_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&os_listdir_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&os_mkdir_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_remove), MP_ROM_PTR(&os_remove_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_rename), MP_ROM_PTR(&os_rename_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&os_rmdir_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), MP_ROM_PTR(&os_stat_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&os_statvfs_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink), MP_ROM_PTR(&os_remove_obj) }, // unlink aliases to remove
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), MP_ROM_PTR(&os_sync_obj) },
//| .. data:: sep
//|
//| Separator used to dileneate path components such as folder and file names.
//|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sep), MP_OBJ_NEW_QSTR(MP_QSTR__slash_) },
};
STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table);
const mp_obj_module_t os_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&os_module_globals,
};

View File

@ -0,0 +1,48 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
*
* 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.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_OS___INIT___H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_OS___INIT___H__
#include <stdint.h>
#include <stdbool.h>
#include "py/objtuple.h"
const mp_rom_obj_tuple_t common_hal_os_uname_info_obj;
mp_obj_t common_hal_os_uname(void);
void common_hal_os_chdir(const char* path);
mp_obj_t common_hal_os_getcwd(void);
mp_obj_t common_hal_os_listdir(const char* path);
void common_hal_os_mkdir(const char* path);
void common_hal_os_remove(const char* path);
void common_hal_os_rename(const char* old_path, const char* new_path);
void common_hal_os_rmdir(const char* path);
mp_obj_t common_hal_os_stat(const char* path);
mp_obj_t common_hal_os_statvfs(const char* path);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_OS___INIT___H__

View File

@ -0,0 +1,117 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2015 Josef Gajdusek
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
*
* 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 <string.h>
#include "extmod/vfs_fat.h"
#include "py/obj.h"
#include "py/objnamedtuple.h"
#include "shared-bindings/storage/__init__.h"
//| :mod:`storage` --- storage management
//| ========================================================
//|
//| .. module:: storage
//| :synopsis: storage management
//| :platform: SAMD21
//|
//| The `storage` provides storage management functionality such as mounting and
//| unmounting which is typically handled by the operating system hosting Python.
//| CircuitPython does not have an OS, so this module provides this functionality
//| directly. Its based on MicroPython's `uos` module but deliberately separates
//| CPython compatible functionality (in `os`) from incompatible functionality
//| (in `storage`).
//|
//| .. function:: mount(filesystem, mount_path, \*, readonly=False)
//|
//| Mounts the given filesystem object at the given path.
//|
//| This is the CircuitPython analog to the UNIX ``mount`` command.
//|
mp_obj_t storage_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_readonly };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_false} },
};
// parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// get the mount point
const char *mnt_str = mp_obj_str_get_str(pos_args[1]);
// Make sure we're given an object we can mount.
// TODO(tannewt): Make sure we have all the methods we need to operating it
// as a file system.
mp_obj_t vfs_obj = pos_args[0];
mp_obj_t dest[2];
mp_load_method_maybe(vfs_obj, MP_QSTR_mount, dest);
if (dest[0] == MP_OBJ_NULL) {
mp_raise_ValueError("filesystem must provide mount method");
}
common_hal_storage_mount(vfs_obj, mnt_str, mp_obj_is_true(args[ARG_readonly].u_obj));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(storage_mount_obj, 2, storage_mount);
//| .. function:: umount(mount)
//|
//| Unmounts the given filesystem object or if *mount* is a path, then unmount
//| the filesystem mounted at that location.
//|
//| This is the CircuitPython analog to the UNIX ``umount`` command.
//|
mp_obj_t storage_umount(mp_obj_t mnt_in) {
if (MP_OBJ_IS_STR(mnt_in)) {
common_hal_storage_umount_path(mp_obj_str_get_str(mnt_in));
} else {
common_hal_storage_umount_object(mnt_in);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(storage_umount_obj, storage_umount);
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_storage) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
};
STATIC MP_DEFINE_CONST_DICT(storage_module_globals, storage_module_globals_table);
const mp_obj_module_t storage_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&storage_module_globals,
};

View File

@ -0,0 +1,37 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* 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.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H__
#include <stdint.h>
#include <stdbool.h>
void common_hal_storage_mount(mp_obj_t vfs_obj, const char* path, bool readonly);
void common_hal_storage_umount_path(const char* path);
void common_hal_storage_umount_object(mp_obj_t vfs_obj);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H__

188
shared-module/os/__init__.c Normal file
View File

@ -0,0 +1,188 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2015 Josef Gajdusek
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* 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 <string.h>
#include "extmod/vfs.h"
#include "py/mperrno.h"
#include "py/mpstate.h"
#include "py/obj.h"
#include "py/objstr.h"
#include "py/runtime.h"
#include "shared-bindings/os/__init__.h"
// This provides all VFS related OS functions so that ports can share the code
// as needed. It does not provide uname.
// Version of mp_vfs_lookup_path that takes and returns uPy string objects.
STATIC mp_vfs_mount_t *lookup_path(const char* path, mp_obj_t *path_out) {
const char *p_out;
mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &p_out);
if (vfs != MP_VFS_NONE && vfs != MP_VFS_ROOT) {
*path_out = mp_obj_new_str_of_type(mp_obj_get_type(path),
(const byte*)p_out, strlen(p_out));
}
return vfs;
}
STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) {
if (vfs == MP_VFS_NONE) {
// mount point not found
mp_raise_OSError(MP_ENODEV);
}
if (vfs == MP_VFS_ROOT) {
// can't do operation on root dir
mp_raise_OSError(MP_EPERM);
}
mp_obj_t meth[n_args + 2];
mp_load_method(vfs->obj, meth_name, meth);
if (args != NULL) {
memcpy(meth + 2, args, n_args * sizeof(*args));
}
return mp_call_method_n_kw(n_args, 0, meth);
}
void common_hal_os_chdir(const char* path) {
mp_obj_t path_out;
mp_vfs_mount_t *vfs = lookup_path(path, &path_out);
MP_STATE_VM(vfs_cur) = vfs;
if (vfs == MP_VFS_ROOT) {
// If we change to the root dir and a VFS is mounted at the root then
// we must change that VFS's current dir to the root dir so that any
// subsequent relative paths begin at the root of that VFS.
for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
if (vfs->len == 1) {
mp_obj_t root = mp_obj_new_str("/", 1, false);
mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &root);
break;
}
}
} else {
mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out);
}
}
mp_obj_t common_hal_os_getcwd(void) {
return mp_vfs_getcwd();
}
mp_obj_t common_hal_os_listdir(const char* path) {
mp_obj_t path_out;
mp_vfs_mount_t *vfs = lookup_path(path, &path_out);
if (vfs == MP_VFS_ROOT) {
// list the root directory
mp_vfs_ilistdir_it_t *iter = m_new_obj(mp_vfs_ilistdir_it_t);
iter->base.type = &mp_type_polymorph_iter;
iter->iternext = mp_vfs_ilistdir_it_iternext;
iter->cur.vfs = MP_STATE_VM(vfs_mount_table);
iter->is_str = mp_obj_get_type(path) == &mp_type_str;
iter->is_iter = false;
return MP_OBJ_FROM_PTR(iter);
}
return mp_vfs_proxy_call(vfs, MP_QSTR_ilistdir, 1, &path_out);
}
void common_hal_os_mkdir(const char* path) {
mp_obj_t path_out;
mp_vfs_mount_t *vfs = lookup_path(path, &path_out);
if (vfs == MP_VFS_ROOT || (vfs != MP_VFS_NONE && !strcmp(mp_obj_str_get_str(path_out), "/"))) {
mp_raise_OSError(MP_EEXIST);
}
mp_vfs_proxy_call(vfs, MP_QSTR_mkdir, 1, &path_out);
}
void common_hal_os_remove(const char* path) {
mp_obj_t path_out;
mp_vfs_mount_t *vfs = lookup_path(path, &path_out);
mp_vfs_proxy_call(vfs, MP_QSTR_remove, 1, &path_out);
}
void common_hal_os_rename(const char* old_path, const char* new_path) {
mp_obj_t args[2];
mp_vfs_mount_t *old_vfs = lookup_path(old_path, &args[0]);
mp_vfs_mount_t *new_vfs = lookup_path(new_path, &args[1]);
if (old_vfs != new_vfs) {
// can't rename across filesystems
mp_raise_OSError(MP_EPERM);
}
mp_vfs_proxy_call(old_vfs, MP_QSTR_rename, 2, args);
}
void common_hal_os_rmdir(const char* path) {
mp_obj_t path_out;
mp_vfs_mount_t *vfs = lookup_path(path, &path_out);
mp_vfs_proxy_call(vfs, MP_QSTR_rmdir, 1, &path_out);
}
mp_obj_t common_hal_os_stat(const char* path) {
mp_obj_t path_out;
mp_vfs_mount_t *vfs = lookup_path(path, &path_out);
if (vfs == MP_VFS_ROOT) {
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
t->items[0] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR); // st_mode
for (int i = 1; i <= 9; ++i) {
t->items[i] = MP_OBJ_NEW_SMALL_INT(0); // dev, nlink, uid, gid, size, atime, mtime, ctime
}
return MP_OBJ_FROM_PTR(t);
}
return mp_vfs_proxy_call(vfs, MP_QSTR_stat, 1, &path_out);
}
mp_obj_t common_hal_os_statvfs(const char* path) {
mp_obj_t path_out;
mp_vfs_mount_t *vfs = lookup_path(path, &path_out);
if (vfs == MP_VFS_ROOT) {
// statvfs called on the root directory, see if there's anything mounted there
for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
if (vfs->len == 1) {
break;
}
}
// If there's nothing mounted at root then return a mostly-empty tuple
if (vfs == NULL) {
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
// fill in: bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flags
for (int i = 0; i <= 8; ++i) {
t->items[i] = MP_OBJ_NEW_SMALL_INT(0);
}
// Put something sensible in f_namemax
t->items[9] = MP_OBJ_NEW_SMALL_INT(MICROPY_ALLOC_PATH_MAX);
return MP_OBJ_FROM_PTR(t);
}
// VFS mounted at root so delegate the call to it
path_out = MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
}
return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out);
}

View File

@ -0,0 +1,133 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2015 Josef Gajdusek
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* 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 <string.h>
#include "extmod/vfs.h"
#include "py/mperrno.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/storage/__init__.h"
STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) {
if (vfs == MP_VFS_NONE) {
// mount point not found
mp_raise_OSError(MP_ENODEV);
}
if (vfs == MP_VFS_ROOT) {
// can't do operation on root dir
mp_raise_OSError(MP_EPERM);
}
mp_obj_t meth[n_args + 2];
mp_load_method(vfs->obj, meth_name, meth);
if (args != NULL) {
memcpy(meth + 2, args, n_args * sizeof(*args));
}
return mp_call_method_n_kw(n_args, 0, meth);
}
void common_hal_storage_mount(mp_obj_t vfs_obj, const char* mount_path, bool readonly) {
// create new object
mp_vfs_mount_t *vfs = m_new_obj(mp_vfs_mount_t);
vfs->str = mount_path;
vfs->len = strlen(mount_path);
vfs->obj = vfs_obj;
vfs->next = NULL;
mp_obj_t args[2];
args[0] = readonly ? mp_const_true : mp_const_false;
args[1] = mp_const_false; // Don't make the file system automatically when mounting.
// call the underlying object to do any mounting operation
mp_vfs_proxy_call(vfs, MP_QSTR_mount, 2, (mp_obj_t*)&args);
// check that the destination mount point is unused
const char *path_out;
mp_vfs_mount_t *existing_mount = mp_vfs_lookup_path(mount_path, &path_out);
if (existing_mount != MP_VFS_NONE && existing_mount != MP_VFS_ROOT) {
if (vfs->len != 1 && existing_mount->len == 1) {
// if root dir is mounted, still allow to mount something within a subdir of root
} else {
// mount point in use
mp_raise_OSError(MP_EPERM);
}
}
// insert the vfs into the mount table
mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table);
while (*vfsp != NULL) {
if ((*vfsp)->len == 1) {
// make sure anything mounted at the root stays at the end of the list
vfs->next = *vfsp;
break;
}
vfsp = &(*vfsp)->next;
}
*vfsp = vfs;
}
void common_hal_storage_umount_object(mp_obj_t vfs_obj) {
// remove vfs from the mount table
mp_vfs_mount_t *vfs = NULL;
for (mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) {
if ((*vfsp)->obj == vfs_obj) {
vfs = *vfsp;
*vfsp = (*vfsp)->next;
break;
}
}
if (vfs == NULL) {
mp_raise_OSError(MP_EINVAL);
}
// if we unmounted the current device then set current to root
if (MP_STATE_VM(vfs_cur) == vfs) {
MP_STATE_VM(vfs_cur) = MP_VFS_ROOT;
}
// call the underlying object to do any unmounting operation
mp_vfs_proxy_call(vfs, MP_QSTR_umount, 0, NULL);
}
void common_hal_storage_umount_path(const char* mount_path) {
// remove vfs from the mount table
mp_obj_t *vfs_obj = NULL;
for (mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) {
if (strcmp(mount_path, (*vfsp)->str) == 0) {
vfs_obj = (*vfsp)->obj;
break;
}
}
if (vfs_obj == NULL) {
mp_raise_OSError(MP_EINVAL);
}
common_hal_storage_umount_object(vfs_obj);
}