2017-06-27 18:17:30 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2017-06-27 18:17:30 -04:00
|
|
|
* 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"
|
2018-07-13 22:51:10 -04:00
|
|
|
#include "py/runtime.h"
|
2017-06-27 18:17:30 -04:00
|
|
|
#include "shared-bindings/storage/__init__.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2017-06-27 18:17:30 -04:00
|
|
|
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Storage management
|
2017-06-27 18:17:30 -04:00
|
|
|
//|
|
|
|
|
//| 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
|
2020-05-11 13:40:02 -04:00
|
|
|
//| directly."""
|
2017-06-27 18:17:30 -04:00
|
|
|
//|
|
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def mount(filesystem: VfsFat, mount_path: str, *, readonly: bool = False) -> None:
|
2020-05-11 13:40:02 -04:00
|
|
|
//| """Mounts the given filesystem object at the given path.
|
2017-06-27 18:17:30 -04:00
|
|
|
//|
|
2020-05-11 13:40:02 -04:00
|
|
|
//| This is the CircuitPython analog to the UNIX ``mount`` command.
|
2017-06-27 18:17:30 -04:00
|
|
|
//|
|
2020-05-11 13:40:02 -04:00
|
|
|
//| :param bool readonly: True when the filesystem should be readonly to CircuitPython."""
|
|
|
|
//| ...
|
2019-02-19 17:45:45 -05:00
|
|
|
//|
|
2017-06-27 18:17:30 -04:00
|
|
|
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[] = {
|
2019-02-19 17:45:45 -05:00
|
|
|
{ MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
2017-06-27 18:17:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_ValueError(translate("filesystem must provide mount method"));
|
2017-06-27 18:17:30 -04:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:45:45 -05:00
|
|
|
common_hal_storage_mount(vfs_obj, mnt_str, args[ARG_readonly].u_bool);
|
2017-06-27 18:17:30 -04:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(storage_mount_obj, 2, storage_mount);
|
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def umount(mount: Union[str, VfsFat]) -> None:
|
2020-05-11 13:40:02 -04:00
|
|
|
//| """Unmounts the given filesystem object or if *mount* is a path, then unmount
|
|
|
|
//| the filesystem mounted at that location.
|
2017-06-27 18:17:30 -04:00
|
|
|
//|
|
2020-05-11 13:40:02 -04:00
|
|
|
//| This is the CircuitPython analog to the UNIX ``umount`` command."""
|
|
|
|
//| ...
|
2017-06-27 18:17:30 -04:00
|
|
|
//|
|
|
|
|
mp_obj_t storage_umount(mp_obj_t mnt_in) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_str(mnt_in)) {
|
2017-06-27 18:17:30 -04:00
|
|
|
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);
|
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def remount(mount_path: str, readonly: bool = False, *, disable_concurrent_write_protection: bool = False) -> None:
|
2020-05-11 13:40:02 -04:00
|
|
|
//| """Remounts the given path with new parameters.
|
2017-06-28 19:18:13 -04:00
|
|
|
//|
|
2020-05-11 13:40:02 -04:00
|
|
|
//| :param bool readonly: True when the filesystem should be readonly to CircuitPython.
|
|
|
|
//| :param bool disable_concurrent_write_protection: When True, the check that makes sure the
|
|
|
|
//| underlying filesystem data is written by one computer is disabled. Disabling the protection
|
|
|
|
//| allows CircuitPython and a host to write to the same filesystem with the risk that the
|
|
|
|
//| filesystem will be corrupted."""
|
|
|
|
//| ...
|
2019-02-19 17:45:45 -05:00
|
|
|
//|
|
2017-10-19 19:34:31 -04:00
|
|
|
mp_obj_t storage_remount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2019-02-19 17:45:45 -05:00
|
|
|
enum { ARG_readonly, ARG_disable_concurrent_write_protection };
|
2017-10-19 19:34:31 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2019-02-19 17:45:45 -05:00
|
|
|
{ MP_QSTR_readonly, MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
{ MP_QSTR_disable_concurrent_write_protection, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
2017-10-19 19:34:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// get the mount point
|
|
|
|
const char *mnt_str = mp_obj_str_get_str(pos_args[0]);
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2017-06-28 19:18:13 -04:00
|
|
|
|
2019-02-19 17:45:45 -05:00
|
|
|
common_hal_storage_remount(mnt_str, args[ARG_readonly].u_bool, args[ARG_disable_concurrent_write_protection].u_bool);
|
2017-06-28 19:18:13 -04:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-10-19 19:34:31 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(storage_remount_obj, 1, storage_remount);
|
2017-06-28 19:18:13 -04:00
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def getmount(mount_path: str) -> VfsFat:
|
2020-05-11 13:40:02 -04:00
|
|
|
//| """Retrieves the mount object associated with the mount path"""
|
|
|
|
//| ...
|
2018-03-27 22:28:18 -04:00
|
|
|
//|
|
|
|
|
mp_obj_t storage_getmount(const mp_obj_t mnt_in) {
|
|
|
|
return common_hal_storage_getmount(mp_obj_str_get_str(mnt_in));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(storage_getmount_obj, storage_getmount);
|
|
|
|
|
2020-07-03 15:38:40 -04:00
|
|
|
//| def erase_filesystem() -> None:
|
2020-05-11 13:40:02 -04:00
|
|
|
//| """Erase and re-create the ``CIRCUITPY`` filesystem.
|
2018-04-09 12:52:42 -04:00
|
|
|
//|
|
2020-05-11 13:40:02 -04:00
|
|
|
//| On boards that present USB-visible ``CIRCUITPY`` drive (e.g., SAMD21 and SAMD51),
|
|
|
|
//| then call `microcontroller.reset()` to restart CircuitPython and have the
|
|
|
|
//| host computer remount CIRCUITPY.
|
2018-04-10 12:08:41 -04:00
|
|
|
//|
|
2020-05-11 13:40:02 -04:00
|
|
|
//| This function can be called from the REPL when ``CIRCUITPY``
|
|
|
|
//| has become corrupted.
|
2018-04-09 12:52:42 -04:00
|
|
|
//|
|
2020-05-11 13:40:02 -04:00
|
|
|
//| .. warning:: All the data on ``CIRCUITPY`` will be lost, and
|
|
|
|
//| CircuitPython will restart on certain boards."""
|
|
|
|
//| ...
|
2018-04-09 12:52:42 -04:00
|
|
|
//|
|
|
|
|
|
|
|
|
mp_obj_t storage_erase_filesystem(void) {
|
|
|
|
common_hal_storage_erase_filesystem();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(storage_erase_filesystem_obj, storage_erase_filesystem);
|
|
|
|
|
2021-05-03 22:22:29 -04:00
|
|
|
//| def disable_usb_drive() -> None:
|
|
|
|
//| """Disable presenting ``CIRCUITPY`` as a USB mass storage device.
|
2021-04-14 22:10:09 -04:00
|
|
|
//| By default, the device is enabled and ``CIRCUITPY`` is visible.
|
2021-05-03 22:22:29 -04:00
|
|
|
//| Can be called in ``boot.py``, before USB is connected."""
|
|
|
|
//| ...
|
2021-04-14 22:10:09 -04:00
|
|
|
//|
|
2021-05-03 22:22:29 -04:00
|
|
|
STATIC mp_obj_t storage_disable_usb_drive(void) {
|
|
|
|
if (!common_hal_storage_disable_usb_drive()) {
|
2021-05-05 12:35:12 -04:00
|
|
|
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
|
2021-05-03 22:22:29 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(storage_disable_usb_drive_obj, storage_disable_usb_drive);
|
|
|
|
|
|
|
|
//| def enable_usb_drive() -> None:
|
|
|
|
//| """Enabled presenting ``CIRCUITPY`` as a USB mass storage device.
|
|
|
|
//| By default, the device is enabled and ``CIRCUITPY`` is visible,
|
|
|
|
//| so you do not normally need to call this function.
|
|
|
|
//| Can be called in ``boot.py``, before USB is connected."""
|
2021-04-13 23:33:44 -04:00
|
|
|
//| ...
|
|
|
|
//|
|
2021-05-03 22:22:29 -04:00
|
|
|
STATIC mp_obj_t storage_enable_usb_drive(void) {
|
|
|
|
if (!common_hal_storage_enable_usb_drive()) {
|
2021-05-05 12:35:12 -04:00
|
|
|
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
|
2021-04-13 23:33:44 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2021-05-03 22:22:29 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(storage_enable_usb_drive_obj, storage_enable_usb_drive);
|
2021-04-13 23:33:44 -04:00
|
|
|
|
2017-06-27 18:17:30 -04:00
|
|
|
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
|
2017-08-27 15:02:50 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) },
|
2017-06-27 18:17:30 -04:00
|
|
|
|
2021-05-03 22:22:29 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getmount), MP_ROM_PTR(&storage_getmount_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_erase_filesystem), MP_ROM_PTR(&storage_erase_filesystem_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_disable_usb_drive), MP_ROM_PTR(&storage_disable_usb_drive_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_enable_usb_drive), MP_ROM_PTR(&storage_enable_usb_drive_obj) },
|
2017-08-07 19:20:19 -04:00
|
|
|
|
2020-05-31 11:35:42 -04:00
|
|
|
//| class VfsFat:
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def __init__(self, block_device: str) -> None:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Create a new VfsFat filesystem around the given block device.
|
|
|
|
//|
|
|
|
|
//| :param block_device: Block device the the filesystem lives on"""
|
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| label: str
|
|
|
|
//| """The filesystem label, up to 11 case-insensitive bytes. Note that
|
|
|
|
//| this property can only be set when the device is writable by the
|
|
|
|
//| microcontroller."""
|
|
|
|
//| ...
|
2020-05-31 11:35:42 -04:00
|
|
|
//|
|
2020-07-03 15:38:40 -04:00
|
|
|
//| def mkfs(self) -> None:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Format the block device, deleting any data that may have been there"""
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def open(self, path: str, mode: str) -> None:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Like builtin ``open()``"""
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| def ilistdir(self, path: str) -> Iterator[Union[Tuple[AnyStr, int, int, int], Tuple[AnyStr, int, int]]]:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Return an iterator whose values describe files and folders within
|
|
|
|
//| ``path``"""
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def mkdir(self, path: str) -> None:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Like `os.mkdir`"""
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def rmdir(self, path: str) -> None:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Like `os.rmdir`"""
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| def stat(self, path: str) -> Tuple[int, int, int, int, int, int, int, int, int, int]:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Like `os.stat`"""
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| def statvfs(self, path: int) -> Tuple[int, int, int, int, int, int, int, int, int, int]:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Like `os.statvfs`"""
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-07-03 15:38:40 -04:00
|
|
|
//| def mount(self, readonly: bool, mkfs: VfsFat) -> None:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Don't call this directly, call `storage.mount`."""
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-07-03 15:38:40 -04:00
|
|
|
//| def umount(self) -> None:
|
2020-05-31 11:35:42 -04:00
|
|
|
//| """Don't call this directly, call `storage.umount`."""
|
|
|
|
//| ...
|
|
|
|
//|
|
2017-08-27 15:02:50 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
|
2017-06-27 18:17:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(storage_module_globals, storage_module_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t storage_module = {
|
|
|
|
.base = { &mp_type_module },
|
2021-03-15 09:57:36 -04:00
|
|
|
.globals = (mp_obj_dict_t *)&storage_module_globals,
|
2017-06-27 18:17:30 -04:00
|
|
|
};
|