Skip to content

Commit 8d82b0e

Browse files
committed
extmod: Add VfsPosix filesystem component.
This VFS component allows to mount a host POSIX filesystem within the uPy VFS sub-system. All traditional POSIX file access then goes through the VFS, allowing to sandbox a uPy process to a certain sub-dir of the host system, as well as mount other filesystem types alongside the host filesystem.
1 parent f35aae3 commit 8d82b0e

File tree

5 files changed

+666
-0
lines changed

5 files changed

+666
-0
lines changed

extmod/vfs_posix.c

Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017-2018 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 "py/runtime.h"
28+
#include "py/mperrno.h"
29+
#include "extmod/vfs.h"
30+
#include "extmod/vfs_posix.h"
31+
32+
#if MICROPY_VFS_POSIX
33+
34+
#include <string.h>
35+
#include <sys/stat.h>
36+
#include <dirent.h>
37+
38+
typedef struct _mp_obj_vfs_posix_t {
39+
mp_obj_base_t base;
40+
vstr_t root;
41+
size_t root_len;
42+
bool readonly;
43+
} mp_obj_vfs_posix_t;
44+
45+
STATIC const char *vfs_posix_get_path_str(mp_obj_vfs_posix_t *self, mp_obj_t path) {
46+
if (self->root_len == 0) {
47+
return mp_obj_str_get_str(path);
48+
} else {
49+
self->root.len = self->root_len;
50+
vstr_add_str(&self->root, mp_obj_str_get_str(path));
51+
return vstr_null_terminated_str(&self->root);
52+
}
53+
}
54+
55+
STATIC mp_obj_t vfs_posix_get_path_obj(mp_obj_vfs_posix_t *self, mp_obj_t path) {
56+
if (self->root_len == 0) {
57+
return path;
58+
} else {
59+
self->root.len = self->root_len;
60+
vstr_add_str(&self->root, mp_obj_str_get_str(path));
61+
return mp_obj_new_str(self->root.buf, self->root.len);
62+
}
63+
}
64+
65+
STATIC mp_obj_t vfs_posix_fun1_helper(mp_obj_t self_in, mp_obj_t path_in, int (*f)(const char*)) {
66+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
67+
int ret = f(vfs_posix_get_path_str(self, path_in));
68+
if (ret != 0) {
69+
mp_raise_OSError(errno);
70+
}
71+
return mp_const_none;
72+
}
73+
74+
mp_import_stat_t mp_vfs_posix_import_stat(mp_obj_vfs_posix_t *self, const char *path) {
75+
if (self->root_len != 0) {
76+
self->root.len = self->root_len;
77+
vstr_add_str(&self->root, path);
78+
path = vstr_null_terminated_str(&self->root);
79+
}
80+
struct stat st;
81+
if (stat(path, &st) == 0) {
82+
if (S_ISDIR(st.st_mode)) {
83+
return MP_IMPORT_STAT_DIR;
84+
} else if (S_ISREG(st.st_mode)) {
85+
return MP_IMPORT_STAT_FILE;
86+
}
87+
}
88+
return MP_IMPORT_STAT_NO_EXIST;
89+
}
90+
91+
STATIC mp_obj_t vfs_posix_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
92+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
93+
94+
mp_obj_vfs_posix_t *vfs = m_new_obj(mp_obj_vfs_posix_t);
95+
vfs->base.type = type;
96+
vstr_init(&vfs->root, 0);
97+
if (n_args == 1) {
98+
vstr_add_str(&vfs->root, mp_obj_str_get_str(args[0]));
99+
vstr_add_char(&vfs->root, '/');
100+
}
101+
vfs->root_len = vfs->root.len;
102+
vfs->readonly = false;
103+
104+
return MP_OBJ_FROM_PTR(vfs);
105+
}
106+
107+
STATIC mp_obj_t vfs_posix_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) {
108+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
109+
if (mp_obj_is_true(readonly)) {
110+
self->readonly = true;
111+
}
112+
if (mp_obj_is_true(mkfs)) {
113+
mp_raise_OSError(MP_EPERM);
114+
}
115+
return mp_const_none;
116+
}
117+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_posix_mount_obj, vfs_posix_mount);
118+
119+
STATIC mp_obj_t vfs_posix_umount(mp_obj_t self_in) {
120+
(void)self_in;
121+
return mp_const_none;
122+
}
123+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_umount_obj, vfs_posix_umount);
124+
125+
STATIC mp_obj_t vfs_posix_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in) {
126+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
127+
const char *mode = mp_obj_str_get_str(mode_in);
128+
if (self->readonly
129+
&& (strchr(mode, 'w') != NULL || strchr(mode, 'a') != NULL || strchr(mode, '+') != NULL)) {
130+
mp_raise_OSError(MP_EROFS);
131+
}
132+
if (!MP_OBJ_IS_SMALL_INT(path_in)) {
133+
path_in = vfs_posix_get_path_obj(self, path_in);
134+
}
135+
return mp_vfs_posix_file_open(&mp_type_textio, path_in, mode_in);
136+
}
137+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_posix_open_obj, vfs_posix_open);
138+
139+
STATIC mp_obj_t vfs_posix_chdir(mp_obj_t self_in, mp_obj_t path_in) {
140+
return vfs_posix_fun1_helper(self_in, path_in, chdir);
141+
}
142+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_chdir_obj, vfs_posix_chdir);
143+
144+
STATIC mp_obj_t vfs_posix_getcwd(mp_obj_t self_in) {
145+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
146+
char buf[MICROPY_ALLOC_PATH_MAX + 1];
147+
const char *ret = getcwd(buf, sizeof(buf));
148+
if (ret == NULL) {
149+
mp_raise_OSError(errno);
150+
}
151+
ret += self->root_len;
152+
return mp_obj_new_str(ret, strlen(ret));
153+
}
154+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_getcwd_obj, vfs_posix_getcwd);
155+
156+
typedef struct _vfs_posix_ilistdir_it_t {
157+
mp_obj_base_t base;
158+
mp_fun_1_t iternext;
159+
bool is_str;
160+
DIR *dir;
161+
} vfs_posix_ilistdir_it_t;
162+
163+
STATIC mp_obj_t vfs_posix_ilistdir_it_iternext(mp_obj_t self_in) {
164+
vfs_posix_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
165+
166+
if (self->dir == NULL) {
167+
return MP_OBJ_STOP_ITERATION;
168+
}
169+
170+
for (;;) {
171+
struct dirent *dirent = readdir(self->dir);
172+
if (dirent == NULL) {
173+
closedir(self->dir);
174+
self->dir = NULL;
175+
return MP_OBJ_STOP_ITERATION;
176+
}
177+
const char *fn = dirent->d_name;
178+
179+
if (fn[0] == '.' && (fn[1] == 0 || fn[1] == '.')) {
180+
// skip . and ..
181+
continue;
182+
}
183+
184+
// make 3-tuple with info about this entry
185+
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
186+
187+
if (self->is_str) {
188+
t->items[0] = mp_obj_new_str(fn, strlen(fn));
189+
} else {
190+
t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn));
191+
}
192+
193+
#ifdef _DIRENT_HAVE_D_TYPE
194+
if (dirent->d_type == DT_DIR) {
195+
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
196+
} else if (dirent->d_type == DT_REG) {
197+
t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG);
198+
} else {
199+
t->items[1] = MP_OBJ_NEW_SMALL_INT(dirent->d_type);
200+
}
201+
#else
202+
// DT_UNKNOWN should have 0 value on any reasonable system
203+
t->items[1] = MP_OBJ_NEW_SMALL_INT(0);
204+
#endif
205+
#ifdef _DIRENT_HAVE_D_INO
206+
t->items[2] = MP_OBJ_NEW_SMALL_INT(dirent->d_ino);
207+
#else
208+
t->items[2] = MP_OBJ_NEW_SMALL_INT(0);
209+
#endif
210+
211+
return MP_OBJ_FROM_PTR(t);
212+
}
213+
}
214+
215+
STATIC mp_obj_t vfs_posix_ilistdir(mp_obj_t self_in, mp_obj_t path_in) {
216+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
217+
vfs_posix_ilistdir_it_t *iter = m_new_obj(vfs_posix_ilistdir_it_t);
218+
iter->base.type = &mp_type_polymorph_iter;
219+
iter->iternext = vfs_posix_ilistdir_it_iternext;
220+
iter->is_str = mp_obj_get_type(path_in) == &mp_type_str;
221+
const char *path = vfs_posix_get_path_str(self, path_in);
222+
iter->dir = opendir(path);
223+
if (iter->dir == NULL) {
224+
mp_raise_OSError(errno);
225+
}
226+
return MP_OBJ_FROM_PTR(iter);
227+
}
228+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_ilistdir_obj, vfs_posix_ilistdir);
229+
230+
typedef struct _mp_obj_listdir_t {
231+
mp_obj_base_t base;
232+
mp_fun_1_t iternext;
233+
DIR *dir;
234+
} mp_obj_listdir_t;
235+
236+
STATIC mp_obj_t vfs_posix_mkdir(mp_obj_t self_in, mp_obj_t path_in) {
237+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
238+
int ret = mkdir(vfs_posix_get_path_str(self, path_in), 0777);
239+
if (ret != 0) {
240+
mp_raise_OSError(errno);
241+
}
242+
return mp_const_none;
243+
}
244+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_mkdir_obj, vfs_posix_mkdir);
245+
246+
STATIC mp_obj_t vfs_posix_remove(mp_obj_t self_in, mp_obj_t path_in) {
247+
return vfs_posix_fun1_helper(self_in, path_in, unlink);
248+
}
249+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_remove_obj, vfs_posix_remove);
250+
251+
STATIC mp_obj_t vfs_posix_rename(mp_obj_t self_in, mp_obj_t old_path_in, mp_obj_t new_path_in) {
252+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
253+
const char *old_path = vfs_posix_get_path_str(self, old_path_in);
254+
const char *new_path = vfs_posix_get_path_str(self, new_path_in);
255+
int ret = rename(old_path, new_path);
256+
if (ret != 0) {
257+
mp_raise_OSError(errno);
258+
}
259+
return mp_const_none;
260+
}
261+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_posix_rename_obj, vfs_posix_rename);
262+
263+
STATIC mp_obj_t vfs_posix_rmdir(mp_obj_t self_in, mp_obj_t path_in) {
264+
return vfs_posix_fun1_helper(self_in, path_in, rmdir);
265+
}
266+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_rmdir_obj, vfs_posix_rmdir);
267+
268+
STATIC mp_obj_t vfs_posix_stat(mp_obj_t self_in, mp_obj_t path_in) {
269+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
270+
struct stat sb;
271+
int ret = stat(vfs_posix_get_path_str(self, path_in), &sb);
272+
if (ret != 0) {
273+
mp_raise_OSError(errno);
274+
}
275+
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
276+
t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.st_mode);
277+
t->items[1] = MP_OBJ_NEW_SMALL_INT(sb.st_ino);
278+
t->items[2] = MP_OBJ_NEW_SMALL_INT(sb.st_dev);
279+
t->items[3] = MP_OBJ_NEW_SMALL_INT(sb.st_nlink);
280+
t->items[4] = MP_OBJ_NEW_SMALL_INT(sb.st_uid);
281+
t->items[5] = MP_OBJ_NEW_SMALL_INT(sb.st_gid);
282+
t->items[6] = MP_OBJ_NEW_SMALL_INT(sb.st_size);
283+
t->items[7] = MP_OBJ_NEW_SMALL_INT(sb.st_atime);
284+
t->items[8] = MP_OBJ_NEW_SMALL_INT(sb.st_mtime);
285+
t->items[9] = MP_OBJ_NEW_SMALL_INT(sb.st_ctime);
286+
return MP_OBJ_FROM_PTR(t);
287+
}
288+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_stat_obj, vfs_posix_stat);
289+
290+
#ifdef __ANDROID__
291+
#define USE_STATFS 1
292+
#endif
293+
294+
#if USE_STATFS
295+
#include <sys/vfs.h>
296+
#define STRUCT_STATVFS struct statfs
297+
#define STATVFS statfs
298+
#define F_FAVAIL sb.f_ffree
299+
#define F_NAMEMAX sb.f_namelen
300+
#define F_FLAG sb.f_flags
301+
#else
302+
#include <sys/statvfs.h>
303+
#define STRUCT_STATVFS struct statvfs
304+
#define STATVFS statvfs
305+
#define F_FAVAIL sb.f_favail
306+
#define F_NAMEMAX sb.f_namemax
307+
#define F_FLAG sb.f_flag
308+
#endif
309+
310+
STATIC mp_obj_t vfs_posix_statvfs(mp_obj_t self_in, mp_obj_t path_in) {
311+
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
312+
STRUCT_STATVFS sb;
313+
const char *path = vfs_posix_get_path_str(self, path_in);
314+
int ret = STATVFS(path, &sb);
315+
if (ret != 0) {
316+
mp_raise_OSError(errno);
317+
}
318+
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
319+
t->items[0] = MP_OBJ_NEW_SMALL_INT(sb.f_bsize);
320+
t->items[1] = MP_OBJ_NEW_SMALL_INT(sb.f_frsize);
321+
t->items[2] = MP_OBJ_NEW_SMALL_INT(sb.f_blocks);
322+
t->items[3] = MP_OBJ_NEW_SMALL_INT(sb.f_bfree);
323+
t->items[4] = MP_OBJ_NEW_SMALL_INT(sb.f_bavail);
324+
t->items[5] = MP_OBJ_NEW_SMALL_INT(sb.f_files);
325+
t->items[6] = MP_OBJ_NEW_SMALL_INT(sb.f_ffree);
326+
t->items[7] = MP_OBJ_NEW_SMALL_INT(F_FAVAIL);
327+
t->items[8] = MP_OBJ_NEW_SMALL_INT(F_FLAG);
328+
t->items[9] = MP_OBJ_NEW_SMALL_INT(F_NAMEMAX);
329+
return MP_OBJ_FROM_PTR(t);
330+
}
331+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(vfs_posix_statvfs_obj, vfs_posix_statvfs);
332+
333+
STATIC const mp_rom_map_elem_t vfs_posix_locals_dict_table[] = {
334+
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_posix_mount_obj) },
335+
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&vfs_posix_umount_obj) },
336+
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&vfs_posix_open_obj) },
337+
338+
{ MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&vfs_posix_chdir_obj) },
339+
{ MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&vfs_posix_getcwd_obj) },
340+
{ MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&vfs_posix_ilistdir_obj) },
341+
{ MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&vfs_posix_mkdir_obj) },
342+
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&vfs_posix_remove_obj) },
343+
{ MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&vfs_posix_rename_obj) },
344+
{ MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&vfs_posix_rmdir_obj) },
345+
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&vfs_posix_stat_obj) },
346+
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&vfs_posix_statvfs_obj) },
347+
};
348+
STATIC MP_DEFINE_CONST_DICT(vfs_posix_locals_dict, vfs_posix_locals_dict_table);
349+
350+
const mp_obj_type_t mp_type_vfs_posix = {
351+
{ &mp_type_type },
352+
.name = MP_QSTR_VfsPosix,
353+
.make_new = vfs_posix_make_new,
354+
.locals_dict = (mp_obj_dict_t*)&vfs_posix_locals_dict,
355+
};
356+
357+
#endif // MICROPY_VFS_POSIX

extmod/vfs_posix.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 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+
#ifndef MICROPY_INCLUDED_EXTMOD_VFS_POSIX_H
27+
#define MICROPY_INCLUDED_EXTMOD_VFS_POSIX_H
28+
29+
#include "py/lexer.h"
30+
#include "py/obj.h"
31+
32+
struct _mp_obj_vfs_posix_t;
33+
34+
extern const mp_obj_type_t mp_type_vfs_posix;
35+
extern const mp_obj_type_t mp_type_vfs_posix_fileio;
36+
extern const mp_obj_type_t mp_type_vfs_posix_textio;
37+
38+
mp_import_stat_t mp_vfs_posix_import_stat(struct _mp_obj_vfs_posix_t *self, const char *path_in);
39+
mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_obj_t mode_in);
40+
41+
#endif // MICROPY_INCLUDED_EXTMOD_VFS_POSIX_H

0 commit comments

Comments
 (0)