1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2019-2020 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 <stdio.h>
28 #include <string.h>
29 
30 #include "py/runtime.h"
31 #include "py/stream.h"
32 #include "py/mperrno.h"
33 #include "extmod/vfs.h"
34 
MP_VFS_LFSx(check_open)35 STATIC void MP_VFS_LFSx(check_open)(MP_OBJ_VFS_LFSx_FILE * self) {
36     if (self->vfs == NULL) {
37         mp_raise_ValueError(NULL);
38     }
39 }
40 
MP_VFS_LFSx(file_print)41 STATIC void MP_VFS_LFSx(file_print)(const mp_print_t * print, mp_obj_t self_in, mp_print_kind_t kind) {
42     (void)self_in;
43     (void)kind;
44     mp_printf(print, "<io.%s>", mp_obj_get_type_str(self_in));
45 }
46 
MP_VFS_LFSx(file_open)47 mp_obj_t MP_VFS_LFSx(file_open)(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in) {
48     MP_OBJ_VFS_LFSx *self = MP_OBJ_TO_PTR(self_in);
49 
50     int flags = 0;
51     const mp_obj_type_t *type = &MP_TYPE_VFS_LFSx_(_textio);
52     const char *mode_str = mp_obj_str_get_str(mode_in);
53     for (; *mode_str; ++mode_str) {
54         int new_flags = 0;
55         switch (*mode_str) {
56             case 'r':
57                 new_flags = LFSx_MACRO(_O_RDONLY);
58                 break;
59             case 'w':
60                 new_flags = LFSx_MACRO(_O_WRONLY) | LFSx_MACRO(_O_CREAT) | LFSx_MACRO(_O_TRUNC);
61                 break;
62             case 'x':
63                 new_flags = LFSx_MACRO(_O_WRONLY) | LFSx_MACRO(_O_CREAT) | LFSx_MACRO(_O_EXCL);
64                 break;
65             case 'a':
66                 new_flags = LFSx_MACRO(_O_WRONLY) | LFSx_MACRO(_O_CREAT) | LFSx_MACRO(_O_APPEND);
67                 break;
68             case '+':
69                 flags |= LFSx_MACRO(_O_RDWR);
70                 break;
71             #if MICROPY_PY_IO_FILEIO
72             case 'b':
73                 type = &MP_TYPE_VFS_LFSx_(_fileio);
74                 break;
75             #endif
76             case 't':
77                 type = &MP_TYPE_VFS_LFSx_(_textio);
78                 break;
79         }
80         if (new_flags) {
81             if (flags) {
82                 mp_raise_ValueError(NULL);
83             }
84             flags = new_flags;
85         }
86     }
87     if (flags == 0) {
88         flags = LFSx_MACRO(_O_RDONLY);
89     }
90 
91     #if LFS_BUILD_VERSION == 1
92     MP_OBJ_VFS_LFSx_FILE *o = m_new_obj_var_with_finaliser(MP_OBJ_VFS_LFSx_FILE, uint8_t, self->lfs.cfg->prog_size);
93     #else
94     MP_OBJ_VFS_LFSx_FILE *o = m_new_obj_var_with_finaliser(MP_OBJ_VFS_LFSx_FILE, uint8_t, self->lfs.cfg->cache_size);
95     #endif
96     o->base.type = type;
97     o->vfs = self;
98     #if !MICROPY_GC_CONSERVATIVE_CLEAR
99     memset(&o->file, 0, sizeof(o->file));
100     memset(&o->cfg, 0, sizeof(o->cfg));
101     #endif
102     o->cfg.buffer = &o->file_buffer[0];
103 
104     #if LFS_BUILD_VERSION == 2
105     if (self->enable_mtime) {
106         lfs_get_mtime(&o->mtime[0]);
107         o->attrs[0].type = LFS_ATTR_MTIME;
108         o->attrs[0].buffer = &o->mtime[0];
109         o->attrs[0].size = sizeof(o->mtime);
110         o->cfg.attrs = &o->attrs[0];
111         o->cfg.attr_count = MP_ARRAY_SIZE(o->attrs);
112     }
113     #endif
114 
115     const char *path = MP_VFS_LFSx(make_path)(self, path_in);
116     int ret = LFSx_API(file_opencfg)(&self->lfs, &o->file, path, flags, &o->cfg);
117     if (ret < 0) {
118         o->vfs = NULL;
119         mp_raise_OSError(-ret);
120     }
121 
122     return MP_OBJ_FROM_PTR(o);
123 }
124 
MP_VFS_LFSx(file___exit__)125 STATIC mp_obj_t MP_VFS_LFSx(file___exit__)(size_t n_args, const mp_obj_t *args) {
126     (void)n_args;
127     return mp_stream_close(args[0]);
128 }
129 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(MP_VFS_LFSx(file___exit___obj), 4, 4, MP_VFS_LFSx(file___exit__));
130 
MP_VFS_LFSx(file_read)131 STATIC mp_uint_t MP_VFS_LFSx(file_read)(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
132     MP_OBJ_VFS_LFSx_FILE *self = MP_OBJ_TO_PTR(self_in);
133     MP_VFS_LFSx(check_open)(self);
134     LFSx_API(ssize_t) sz = LFSx_API(file_read)(&self->vfs->lfs, &self->file, buf, size);
135     if (sz < 0) {
136         *errcode = -sz;
137         return MP_STREAM_ERROR;
138     }
139     return sz;
140 }
141 
MP_VFS_LFSx(file_write)142 STATIC mp_uint_t MP_VFS_LFSx(file_write)(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
143     MP_OBJ_VFS_LFSx_FILE *self = MP_OBJ_TO_PTR(self_in);
144     MP_VFS_LFSx(check_open)(self);
145     #if LFS_BUILD_VERSION == 2
146     if (self->vfs->enable_mtime) {
147         lfs_get_mtime(&self->mtime[0]);
148     }
149     #endif
150     LFSx_API(ssize_t) sz = LFSx_API(file_write)(&self->vfs->lfs, &self->file, buf, size);
151     if (sz < 0) {
152         *errcode = -sz;
153         return MP_STREAM_ERROR;
154     }
155     return sz;
156 }
157 
MP_VFS_LFSx(file_ioctl)158 STATIC mp_uint_t MP_VFS_LFSx(file_ioctl)(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
159     MP_OBJ_VFS_LFSx_FILE *self = MP_OBJ_TO_PTR(self_in);
160 
161     if (request != MP_STREAM_CLOSE) {
162         MP_VFS_LFSx(check_open)(self);
163     }
164 
165     if (request == MP_STREAM_SEEK) {
166         struct mp_stream_seek_t *s = (struct mp_stream_seek_t *)(uintptr_t)arg;
167         int res = LFSx_API(file_seek)(&self->vfs->lfs, &self->file, s->offset, s->whence);
168         if (res < 0) {
169             *errcode = -res;
170             return MP_STREAM_ERROR;
171         }
172         res = LFSx_API(file_tell)(&self->vfs->lfs, &self->file);
173         if (res < 0) {
174             *errcode = -res;
175             return MP_STREAM_ERROR;
176         }
177         s->offset = res;
178         return 0;
179     } else if (request == MP_STREAM_FLUSH) {
180         int res = LFSx_API(file_sync)(&self->vfs->lfs, &self->file);
181         if (res < 0) {
182             *errcode = -res;
183             return MP_STREAM_ERROR;
184         }
185         return 0;
186     } else if (request == MP_STREAM_CLOSE) {
187         if (self->vfs == NULL) {
188             return 0;
189         }
190         int res = LFSx_API(file_close)(&self->vfs->lfs, &self->file);
191         self->vfs = NULL; // indicate a closed file
192         if (res < 0) {
193             *errcode = -res;
194             return MP_STREAM_ERROR;
195         }
196         return 0;
197     } else {
198         *errcode = MP_EINVAL;
199         return MP_STREAM_ERROR;
200     }
201 }
202 
203 STATIC const mp_rom_map_elem_t MP_VFS_LFSx(file_locals_dict_table)[] = {
204     { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
205     { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
206     { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
207     { MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj) },
208     { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
209     { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
210     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
211     { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
212     { MP_ROM_QSTR(MP_QSTR_tell), MP_ROM_PTR(&mp_stream_tell_obj) },
213     { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
214     { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
215     { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&MP_VFS_LFSx(file___exit___obj)) },
216 };
217 STATIC MP_DEFINE_CONST_DICT(MP_VFS_LFSx(file_locals_dict), MP_VFS_LFSx(file_locals_dict_table));
218 
219 #if MICROPY_PY_IO_FILEIO
220 STATIC const mp_stream_p_t MP_VFS_LFSx(fileio_stream_p) = {
221     .read = MP_VFS_LFSx(file_read),
222     .write = MP_VFS_LFSx(file_write),
223     .ioctl = MP_VFS_LFSx(file_ioctl),
224 };
225 
226 const mp_obj_type_t MP_TYPE_VFS_LFSx_(_fileio) = {
227     { &mp_type_type },
228     .name = MP_QSTR_FileIO,
229     .print = MP_VFS_LFSx(file_print),
230     .getiter = mp_identity_getiter,
231     .iternext = mp_stream_unbuffered_iter,
232     .protocol = &MP_VFS_LFSx(fileio_stream_p),
233     .locals_dict = (mp_obj_dict_t *)&MP_VFS_LFSx(file_locals_dict),
234 };
235 #endif
236 
237 STATIC const mp_stream_p_t MP_VFS_LFSx(textio_stream_p) = {
238     .read = MP_VFS_LFSx(file_read),
239     .write = MP_VFS_LFSx(file_write),
240     .ioctl = MP_VFS_LFSx(file_ioctl),
241     .is_text = true,
242 };
243 
244 const mp_obj_type_t MP_TYPE_VFS_LFSx_(_textio) = {
245     { &mp_type_type },
246     .name = MP_QSTR_TextIOWrapper,
247     .print = MP_VFS_LFSx(file_print),
248     .getiter = mp_identity_getiter,
249     .iternext = mp_stream_unbuffered_iter,
250     .protocol = &MP_VFS_LFSx(textio_stream_p),
251     .locals_dict = (mp_obj_dict_t *)&MP_VFS_LFSx(file_locals_dict),
252 };
253