diff options
| author | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-06-27 15:17:30 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-06-27 15:17:30 -0700 |
| commit | 778e975936b9e892c4f1b85ce73462a0ce6d36d2 (patch) | |
| tree | 9e365392d9150c3c724bb1f14fa07025270dbe21 | |
| parent | e87a61ffb4ad089b7eb5eb4512cb57c085fd9136 (diff) | |
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
| -rw-r--r-- | atmel-samd/Makefile | 20 | ||||
| -rw-r--r-- | atmel-samd/common-hal/os/__init__.c | 57 | ||||
| -rw-r--r-- | atmel-samd/moduos.c | 122 | ||||
| -rw-r--r-- | atmel-samd/mpconfigport.h | 6 | ||||
| -rw-r--r-- | docs/README.md | 10 | ||||
| -rw-r--r-- | esp8266/Makefile | 5 | ||||
| -rw-r--r-- | esp8266/common-hal/os/__init__.c (renamed from esp8266/moduos.c) | 58 | ||||
| -rw-r--r-- | esp8266/mpconfigport.h | 8 | ||||
| -rw-r--r-- | extmod/vfs.c | 13 | ||||
| -rw-r--r-- | extmod/vfs.h | 13 | ||||
| -rw-r--r-- | shared-bindings/index.rst | 14 | ||||
| -rw-r--r-- | shared-bindings/os/__init__.c | 221 | ||||
| -rw-r--r-- | shared-bindings/os/__init__.h | 48 | ||||
| -rw-r--r-- | shared-bindings/storage/__init__.c | 117 | ||||
| -rw-r--r-- | shared-bindings/storage/__init__.h | 37 | ||||
| -rw-r--r-- | shared-module/os/__init__.c | 188 | ||||
| -rw-r--r-- | shared-module/storage/__init__.c | 133 |
17 files changed, 851 insertions, 219 deletions
diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile index 43312fc56..239c80189 100644 --- a/atmel-samd/Makefile +++ b/atmel-samd/Makefile @@ -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 \ + 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 \ - busio/__init__.c \ - busio/I2C.c \ - busio/SPI.c \ - busio/UART.c \ - neopixel_write/__init__.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)) \ diff --git a/atmel-samd/common-hal/os/__init__.c b/atmel-samd/common-hal/os/__init__.c new file mode 100644 index 000000000..2d2067ee0 --- /dev/null +++ b/atmel-samd/common-hal/os/__init__.c @@ -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; +} diff --git a/atmel-samd/moduos.c b/atmel-samd/moduos.c deleted file mode 100644 index cdc6325f2..000000000 --- a/atmel-samd/moduos.c +++ /dev/null @@ -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, -}; diff --git a/atmel-samd/mpconfigport.h b/atmel-samd/mpconfigport.h index b6175edb9..06e789ed1 100644 --- a/atmel-samd/mpconfigport.h +++ b/atmel-samd/mpconfigport.h @@ -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 }, \ diff --git a/docs/README.md b/docs/README.md index 6e7925811..9b188a98a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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`. diff --git a/esp8266/Makefile b/esp8266/Makefile index c1ec3ca93..54c826351 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -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)) diff --git a/esp8266/moduos.c b/esp8266/common-hal/os/__init__.c index 807d2e18a..df241ecae 100644 --- a/esp8266/moduos.c +++ b/esp8266/common-hal/os/__init__.c @@ -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, -}; diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index fec1eb0f0..066788832 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -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)µcontroller_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 }, \ diff --git a/extmod/vfs.c b/extmod/vfs.c index 3bdce80db..3aa1d7744 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -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 diff --git a/extmod/vfs.h b/extmod/vfs.h index edaeb5349..3302f55f4 100644 --- a/extmod/vfs.h +++ b/extmod/vfs.h @@ -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); diff --git a/shared-bindings/index.rst b/shared-bindings/index.rst index e44c60f83..918fb5675 100644 --- a/shared-bindings/index.rst +++ b/shared-bindings/index.rst @@ -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 --------- diff --git a/shared-bindings/os/__init__.c b/shared-bindings/os/__init__.c new file mode 100644 index 000000000..6185f0bf2 --- /dev/null +++ b/shared-bindings/os/__init__.c @@ -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, +}; diff --git a/shared-bindings/os/__init__.h b/shared-bindings/os/__init__.h new file mode 100644 index 000000000..e04a39319 --- /dev/null +++ b/shared-bindings/os/__init__.h @@ -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__ diff --git a/shared-bindings/storage/__init__.c b/shared-bindings/storage/__init__.c new file mode 100644 index 000000000..2b289a11f --- /dev/null +++ b/shared-bindings/storage/__init__.c @@ -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, +}; diff --git a/shared-bindings/storage/__init__.h b/shared-bindings/storage/__init__.h new file mode 100644 index 000000000..806bbc8b3 --- /dev/null +++ b/shared-bindings/storage/__init__.h @@ -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__ diff --git a/shared-module/os/__init__.c b/shared-module/os/__init__.c new file mode 100644 index 000000000..b628d4971 --- /dev/null +++ b/shared-module/os/__init__.c @@ -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); +} diff --git a/shared-module/storage/__init__.c b/shared-module/storage/__init__.c new file mode 100644 index 000000000..582b91a5b --- /dev/null +++ b/shared-module/storage/__init__.c @@ -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); +} |
