|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "py/runtime.h" |
| 31 | +#include "py/objstr.h" |
| 32 | +#include "py/mperrno.h" |
| 33 | +#include "extmod/vfs.h" |
| 34 | + |
| 35 | +#if MICROPY_VFS |
| 36 | + |
| 37 | +// ROOT is 0 so that the default current directory is the root directory |
| 38 | +#define VFS_NONE ((vfs_mount_t*)1) |
| 39 | +#define VFS_ROOT ((vfs_mount_t*)0) |
| 40 | + |
| 41 | +typedef struct _vfs_mount_t { |
| 42 | + const char *str; // mount point with leading / |
| 43 | + size_t len; |
| 44 | + mp_obj_t obj; |
| 45 | + struct _vfs_mount_t *next; |
| 46 | +} vfs_mount_t; |
| 47 | + |
| 48 | +// path is the path to lookup and *path_out holds the path within the VFS |
| 49 | +// object (starts with / if an absolute path). |
| 50 | +// Returns VFS_ROOT for root dir (and then path_out is undefined) and VFS_NONE |
| 51 | +// for path not found. |
| 52 | +STATIC vfs_mount_t *lookup_path_raw(const char *path, const char **path_out) { |
| 53 | + if (path[0] == '/' && path[1] == 0) { |
| 54 | + return VFS_ROOT; |
| 55 | + } else if (MP_STATE_VM(vfs_cur) == VFS_ROOT) { |
| 56 | + // in root dir |
| 57 | + if (path[0] == 0) { |
| 58 | + return VFS_ROOT; |
| 59 | + } |
| 60 | + } else if (*path != '/') { |
| 61 | + // a relative path within a mounted device |
| 62 | + *path_out = path; |
| 63 | + return MP_STATE_VM(vfs_cur); |
| 64 | + } |
| 65 | + |
| 66 | + for (vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { |
| 67 | + if (strncmp(path, vfs->str, vfs->len) == 0) { |
| 68 | + if (path[vfs->len] == '/') { |
| 69 | + *path_out = path + vfs->len; |
| 70 | + return vfs; |
| 71 | + } else if (path[vfs->len] == '\0') { |
| 72 | + *path_out = "/"; |
| 73 | + return vfs; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // mount point not found |
| 79 | + return VFS_NONE; |
| 80 | +} |
| 81 | + |
| 82 | +// Version of lookup_path_raw that takes and returns uPy string objects. |
| 83 | +STATIC vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) { |
| 84 | + const char *path = mp_obj_str_get_str(path_in); |
| 85 | + const char *p_out; |
| 86 | + vfs_mount_t *vfs = lookup_path_raw(path, &p_out); |
| 87 | + if (vfs != VFS_NONE && vfs != VFS_ROOT) { |
| 88 | + *path_out = mp_obj_new_str_of_type(mp_obj_get_type(path_in), |
| 89 | + (const byte*)p_out, strlen(p_out)); |
| 90 | + } |
| 91 | + return vfs; |
| 92 | +} |
| 93 | + |
| 94 | +STATIC mp_obj_t mp_vfs_proxy_call(vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) { |
| 95 | + if (vfs == VFS_NONE) { |
| 96 | + // mount point not found |
| 97 | + mp_raise_OSError(MP_ENODEV); |
| 98 | + } |
| 99 | + if (vfs == VFS_ROOT) { |
| 100 | + // can't do operation on root dir |
| 101 | + mp_raise_OSError(MP_EPERM); |
| 102 | + } |
| 103 | + mp_obj_t meth[n_args + 2]; |
| 104 | + mp_load_method(vfs->obj, meth_name, meth); |
| 105 | + if (args != NULL) { |
| 106 | + memcpy(meth + 2, args, n_args * sizeof(*args)); |
| 107 | + } |
| 108 | + return mp_call_method_n_kw(n_args, 0, meth); |
| 109 | +} |
| 110 | + |
| 111 | +mp_import_stat_t mp_vfs_import_stat(const char *path) { |
| 112 | + const char *path_out; |
| 113 | + vfs_mount_t *vfs = lookup_path_raw(path, &path_out); |
| 114 | + if (vfs == VFS_NONE || vfs == VFS_ROOT) { |
| 115 | + return MP_IMPORT_STAT_NO_EXIST; |
| 116 | + } |
| 117 | + // TODO delegate to vfs.stat() method |
| 118 | + return MP_IMPORT_STAT_NO_EXIST; |
| 119 | +} |
| 120 | + |
| 121 | +mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 122 | + enum { ARG_readonly, ARG_mkfs }; |
| 123 | + static const mp_arg_t allowed_args[] = { |
| 124 | + { MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_false} }, |
| 125 | + { MP_QSTR_mkfs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_false} }, |
| 126 | + }; |
| 127 | + |
| 128 | + // parse args |
| 129 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 130 | + mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 131 | + |
| 132 | + // get the mount point |
| 133 | + mp_uint_t mnt_len; |
| 134 | + const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len); |
| 135 | + |
| 136 | + // create new object |
| 137 | + vfs_mount_t *vfs = m_new_obj(vfs_mount_t); |
| 138 | + vfs->str = mnt_str; |
| 139 | + vfs->len = mnt_len; |
| 140 | + vfs->obj = pos_args[0]; |
| 141 | + vfs->next = NULL; |
| 142 | + |
| 143 | + // call the underlying object to do any mounting operation |
| 144 | + mp_vfs_proxy_call(vfs, MP_QSTR_mount, 2, (mp_obj_t*)&args); |
| 145 | + |
| 146 | + // check that the destination mount point is unused |
| 147 | + const char *path_out; |
| 148 | + if (lookup_path_raw(mp_obj_str_get_str(pos_args[1]), &path_out) != VFS_NONE) { |
| 149 | + mp_raise_OSError(MP_EPERM); |
| 150 | + } |
| 151 | + |
| 152 | + // insert the vfs into the mount table |
| 153 | + vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); |
| 154 | + while (*vfsp != NULL) { |
| 155 | + vfsp = &(*vfsp)->next; |
| 156 | + } |
| 157 | + *vfsp = vfs; |
| 158 | + |
| 159 | + return mp_const_none; |
| 160 | +} |
| 161 | +MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj, 2, mp_vfs_mount); |
| 162 | + |
| 163 | +mp_obj_t mp_vfs_umount(mp_obj_t mnt_in) { |
| 164 | + // remove vfs from the mount table |
| 165 | + vfs_mount_t *vfs = NULL; |
| 166 | + mp_uint_t mnt_len; |
| 167 | + const char *mnt_str = NULL; |
| 168 | + if (MP_OBJ_IS_STR(mnt_in)) { |
| 169 | + mnt_str = mp_obj_str_get_data(mnt_in, &mnt_len); |
| 170 | + } |
| 171 | + for (vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) { |
| 172 | + if ((mnt_str != NULL && !memcmp(mnt_str, (*vfsp)->str, mnt_len + 1)) || (*vfsp)->obj == mnt_in) { |
| 173 | + vfs = *vfsp; |
| 174 | + *vfsp = (*vfsp)->next; |
| 175 | + break; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if (vfs == NULL) { |
| 180 | + mp_raise_OSError(MP_EINVAL); |
| 181 | + } |
| 182 | + |
| 183 | + // if we unmounted the current device then set current to root |
| 184 | + if (MP_STATE_VM(vfs_cur) == vfs) { |
| 185 | + MP_STATE_VM(vfs_cur) = VFS_ROOT; |
| 186 | + } |
| 187 | + |
| 188 | + // call the underlying object to do any unmounting operation |
| 189 | + mp_vfs_proxy_call(vfs, MP_QSTR_umount, 0, NULL); |
| 190 | + |
| 191 | + return mp_const_none; |
| 192 | +} |
| 193 | +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_umount_obj, mp_vfs_umount); |
| 194 | + |
| 195 | +mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 196 | + enum { ARG_file, ARG_mode, ARG_encoding }; |
| 197 | + static const mp_arg_t allowed_args[] = { |
| 198 | + { MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} }, |
| 199 | + { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR_r)} }, |
| 200 | + }; |
| 201 | + |
| 202 | + // parse args |
| 203 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 204 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 205 | + |
| 206 | + vfs_mount_t *vfs = lookup_path((mp_obj_t)args[ARG_file].u_rom_obj, &args[ARG_file].u_obj); |
| 207 | + return mp_vfs_proxy_call(vfs, MP_QSTR_open, 2, (mp_obj_t*)&args); |
| 208 | +} |
| 209 | +MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_open_obj, 0, mp_vfs_open); |
| 210 | + |
| 211 | +mp_obj_t mp_vfs_chdir(mp_obj_t path_in) { |
| 212 | + mp_obj_t path_out; |
| 213 | + vfs_mount_t *vfs = lookup_path(path_in, &path_out); |
| 214 | + if (vfs != VFS_ROOT) { |
| 215 | + mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out); |
| 216 | + } |
| 217 | + MP_STATE_VM(vfs_cur) = vfs; |
| 218 | + return mp_const_none; |
| 219 | +} |
| 220 | +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj, mp_vfs_chdir); |
| 221 | + |
| 222 | +mp_obj_t mp_vfs_getcwd(void) { |
| 223 | + if (MP_STATE_VM(vfs_cur) == VFS_ROOT) { |
| 224 | + return MP_OBJ_NEW_QSTR(MP_QSTR__slash_); |
| 225 | + } |
| 226 | + mp_obj_t cwd_o = mp_vfs_proxy_call(MP_STATE_VM(vfs_cur), MP_QSTR_getcwd, 0, NULL); |
| 227 | + const char *cwd = mp_obj_str_get_str(cwd_o); |
| 228 | + vstr_t vstr; |
| 229 | + vstr_init(&vstr, MP_STATE_VM(vfs_cur)->len + strlen(cwd) + 1); |
| 230 | + vstr_add_strn(&vstr, MP_STATE_VM(vfs_cur)->str, MP_STATE_VM(vfs_cur)->len); |
| 231 | + if (!(cwd[0] == '/' && cwd[1] == 0)) { |
| 232 | + vstr_add_str(&vstr, cwd); |
| 233 | + } |
| 234 | + return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); |
| 235 | +} |
| 236 | +MP_DEFINE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj, mp_vfs_getcwd); |
| 237 | + |
| 238 | +mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) { |
| 239 | + mp_obj_t path_in; |
| 240 | + if (n_args == 1) { |
| 241 | + path_in = args[0]; |
| 242 | + } else { |
| 243 | + path_in = MP_OBJ_NEW_QSTR(MP_QSTR_); |
| 244 | + } |
| 245 | + |
| 246 | + mp_obj_t path_out; |
| 247 | + vfs_mount_t *vfs = lookup_path(path_in, &path_out); |
| 248 | + |
| 249 | + if (vfs == VFS_ROOT) { |
| 250 | + // list the root directory |
| 251 | + mp_obj_t dir_list = mp_obj_new_list(0, NULL); |
| 252 | + for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { |
| 253 | + mp_obj_list_append(dir_list, mp_obj_new_str_of_type(mp_obj_get_type(path_in), |
| 254 | + (const byte*)vfs->str + 1, vfs->len - 1)); |
| 255 | + } |
| 256 | + return dir_list; |
| 257 | + } |
| 258 | + |
| 259 | + return mp_vfs_proxy_call(vfs, MP_QSTR_listdir, 1, &path_out); |
| 260 | +} |
| 261 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj, 0, 1, mp_vfs_listdir); |
| 262 | + |
| 263 | +mp_obj_t mp_vfs_mkdir(mp_obj_t path_in) { |
| 264 | + mp_obj_t path_out; |
| 265 | + vfs_mount_t *vfs = lookup_path(path_in, &path_out); |
| 266 | + return mp_vfs_proxy_call(vfs, MP_QSTR_mkdir, 1, &path_out); |
| 267 | +} |
| 268 | +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj, mp_vfs_mkdir); |
| 269 | + |
| 270 | +mp_obj_t mp_vfs_remove(mp_obj_t path_in) { |
| 271 | + mp_obj_t path_out; |
| 272 | + vfs_mount_t *vfs = lookup_path(path_in, &path_out); |
| 273 | + return mp_vfs_proxy_call(vfs, MP_QSTR_remove, 1, &path_out); |
| 274 | +} |
| 275 | +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_remove_obj, mp_vfs_remove); |
| 276 | + |
| 277 | +mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) { |
| 278 | + mp_obj_t args[2]; |
| 279 | + vfs_mount_t *old_vfs = lookup_path(old_path_in, &args[0]); |
| 280 | + vfs_mount_t *new_vfs = lookup_path(new_path_in, &args[1]); |
| 281 | + if (old_vfs != new_vfs) { |
| 282 | + // can't rename across filesystems |
| 283 | + mp_raise_OSError(MP_EPERM); |
| 284 | + } |
| 285 | + return mp_vfs_proxy_call(old_vfs, MP_QSTR_rename, 2, args); |
| 286 | +} |
| 287 | +MP_DEFINE_CONST_FUN_OBJ_2(mp_vfs_rename_obj, mp_vfs_rename); |
| 288 | + |
| 289 | +mp_obj_t mp_vfs_rmdir(mp_obj_t path_in) { |
| 290 | + mp_obj_t path_out; |
| 291 | + vfs_mount_t *vfs = lookup_path(path_in, &path_out); |
| 292 | + return mp_vfs_proxy_call(vfs, MP_QSTR_rmdir, 1, &path_out); |
| 293 | +} |
| 294 | +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_rmdir_obj, mp_vfs_rmdir); |
| 295 | + |
| 296 | +mp_obj_t mp_vfs_stat(mp_obj_t path_in) { |
| 297 | + mp_obj_t path_out; |
| 298 | + vfs_mount_t *vfs = lookup_path(path_in, &path_out); |
| 299 | + return mp_vfs_proxy_call(vfs, MP_QSTR_stat, 1, &path_out); |
| 300 | +} |
| 301 | +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_stat_obj, mp_vfs_stat); |
| 302 | + |
| 303 | +mp_obj_t mp_vfs_statvfs(mp_obj_t path_in) { |
| 304 | + mp_obj_t path_out; |
| 305 | + vfs_mount_t *vfs = lookup_path(path_in, &path_out); |
| 306 | + return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out); |
| 307 | +} |
| 308 | +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_statvfs_obj, mp_vfs_statvfs); |
| 309 | + |
| 310 | +#endif // MICROPY_VFS |
0 commit comments