Skip to content

Commit 6eafa54

Browse files
committed
extmod/vfs: Expose lookup_path_raw as mp_vfs_lookup_path.
It can be useful for low-level lookup of paths by ports.
1 parent ec32743 commit 6eafa54

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

extmod/vfs.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,17 @@
3535

3636
#if MICROPY_VFS
3737

38-
// ROOT is 0 so that the default current directory is the root directory
39-
#define VFS_NONE ((mp_vfs_mount_t*)1)
40-
#define VFS_ROOT ((mp_vfs_mount_t*)0)
41-
4238
// path is the path to lookup and *path_out holds the path within the VFS
4339
// object (starts with / if an absolute path).
44-
// Returns VFS_ROOT for root dir (and then path_out is undefined) and VFS_NONE
45-
// for path not found.
46-
STATIC mp_vfs_mount_t *lookup_path_raw(const char *path, const char **path_out) {
40+
// Returns MP_VFS_ROOT for root dir (and then path_out is undefined) and
41+
// MP_VFS_NONE for path not found.
42+
mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out) {
4743
if (path[0] == '/' && path[1] == 0) {
48-
return VFS_ROOT;
49-
} else if (MP_STATE_VM(vfs_cur) == VFS_ROOT) {
44+
return MP_VFS_ROOT;
45+
} else if (MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) {
5046
// in root dir
5147
if (path[0] == 0) {
52-
return VFS_ROOT;
48+
return MP_VFS_ROOT;
5349
}
5450
} else if (*path != '/') {
5551
// a relative path within a mounted device
@@ -70,27 +66,27 @@ STATIC mp_vfs_mount_t *lookup_path_raw(const char *path, const char **path_out)
7066
}
7167

7268
// mount point not found
73-
return VFS_NONE;
69+
return MP_VFS_NONE;
7470
}
7571

76-
// Version of lookup_path_raw that takes and returns uPy string objects.
72+
// Version of mp_vfs_lookup_path that takes and returns uPy string objects.
7773
STATIC mp_vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) {
7874
const char *path = mp_obj_str_get_str(path_in);
7975
const char *p_out;
80-
mp_vfs_mount_t *vfs = lookup_path_raw(path, &p_out);
81-
if (vfs != VFS_NONE && vfs != VFS_ROOT) {
76+
mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &p_out);
77+
if (vfs != MP_VFS_NONE && vfs != MP_VFS_ROOT) {
8278
*path_out = mp_obj_new_str_of_type(mp_obj_get_type(path_in),
8379
(const byte*)p_out, strlen(p_out));
8480
}
8581
return vfs;
8682
}
8783

8884
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) {
89-
if (vfs == VFS_NONE) {
85+
if (vfs == MP_VFS_NONE) {
9086
// mount point not found
9187
mp_raise_OSError(MP_ENODEV);
9288
}
93-
if (vfs == VFS_ROOT) {
89+
if (vfs == MP_VFS_ROOT) {
9490
// can't do operation on root dir
9591
mp_raise_OSError(MP_EPERM);
9692
}
@@ -104,8 +100,8 @@ STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_
104100

105101
mp_import_stat_t mp_vfs_import_stat(const char *path) {
106102
const char *path_out;
107-
mp_vfs_mount_t *vfs = lookup_path_raw(path, &path_out);
108-
if (vfs == VFS_NONE || vfs == VFS_ROOT) {
103+
mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &path_out);
104+
if (vfs == MP_VFS_NONE || vfs == MP_VFS_ROOT) {
109105
return MP_IMPORT_STAT_NO_EXIST;
110106
}
111107
#if MICROPY_VFS_FAT
@@ -145,7 +141,7 @@ mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args
145141

146142
// check that the destination mount point is unused
147143
const char *path_out;
148-
if (lookup_path_raw(mp_obj_str_get_str(pos_args[1]), &path_out) != VFS_NONE) {
144+
if (mp_vfs_lookup_path(mp_obj_str_get_str(pos_args[1]), &path_out) != MP_VFS_NONE) {
149145
mp_raise_OSError(MP_EPERM);
150146
}
151147

@@ -182,7 +178,7 @@ mp_obj_t mp_vfs_umount(mp_obj_t mnt_in) {
182178

183179
// if we unmounted the current device then set current to root
184180
if (MP_STATE_VM(vfs_cur) == vfs) {
185-
MP_STATE_VM(vfs_cur) = VFS_ROOT;
181+
MP_STATE_VM(vfs_cur) = MP_VFS_ROOT;
186182
}
187183

188184
// call the underlying object to do any unmounting operation
@@ -211,7 +207,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_open_obj, 0, mp_vfs_open);
211207
mp_obj_t mp_vfs_chdir(mp_obj_t path_in) {
212208
mp_obj_t path_out;
213209
mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
214-
if (vfs != VFS_ROOT) {
210+
if (vfs != MP_VFS_ROOT) {
215211
mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out);
216212
}
217213
MP_STATE_VM(vfs_cur) = vfs;
@@ -220,7 +216,7 @@ mp_obj_t mp_vfs_chdir(mp_obj_t path_in) {
220216
MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj, mp_vfs_chdir);
221217

222218
mp_obj_t mp_vfs_getcwd(void) {
223-
if (MP_STATE_VM(vfs_cur) == VFS_ROOT) {
219+
if (MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) {
224220
return MP_OBJ_NEW_QSTR(MP_QSTR__slash_);
225221
}
226222
mp_obj_t cwd_o = mp_vfs_proxy_call(MP_STATE_VM(vfs_cur), MP_QSTR_getcwd, 0, NULL);
@@ -246,7 +242,7 @@ mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) {
246242
mp_obj_t path_out;
247243
mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out);
248244

249-
if (vfs == VFS_ROOT) {
245+
if (vfs == MP_VFS_ROOT) {
250246
// list the root directory
251247
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
252248
for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {

extmod/vfs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@
3030
#include "py/lexer.h"
3131
#include "py/obj.h"
3232

33+
// return values of mp_vfs_lookup_path
34+
// ROOT is 0 so that the default current directory is the root directory
35+
#define MP_VFS_NONE ((mp_vfs_mount_t*)1)
36+
#define MP_VFS_ROOT ((mp_vfs_mount_t*)0)
37+
3338
typedef struct _mp_vfs_mount_t {
3439
const char *str; // mount point with leading /
3540
size_t len;
3641
mp_obj_t obj;
3742
struct _mp_vfs_mount_t *next;
3843
} mp_vfs_mount_t;
3944

45+
mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out);
4046
mp_import_stat_t mp_vfs_import_stat(const char *path);
4147
mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
4248
mp_obj_t mp_vfs_umount(mp_obj_t mnt_in);

0 commit comments

Comments
 (0)