1 | /* |
2 | * This file is part of the MicroPython project, http://micropython.org/ |
3 | * |
4 | * The MIT License (MIT) |
5 | * |
6 | * Copyright (c) 2013-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/mphal.h" |
28 | #include "py/mpthread.h" |
29 | #include "py/runtime.h" |
30 | #include "py/stream.h" |
31 | #include "extmod/vfs_posix.h" |
32 | |
33 | #if MICROPY_VFS_POSIX || MICROPY_VFS_POSIX_FILE |
34 | |
35 | #include <fcntl.h> |
36 | #include <unistd.h> |
37 | |
38 | #ifdef _WIN32 |
39 | #define fsync _commit |
40 | #endif |
41 | |
42 | typedef struct _mp_obj_vfs_posix_file_t { |
43 | mp_obj_base_t base; |
44 | int fd; |
45 | } mp_obj_vfs_posix_file_t; |
46 | |
47 | #ifdef MICROPY_CPYTHON_COMPAT |
48 | STATIC void check_fd_is_open(const mp_obj_vfs_posix_file_t *o) { |
49 | if (o->fd < 0) { |
50 | mp_raise_ValueError(MP_ERROR_TEXT("I/O operation on closed file" )); |
51 | } |
52 | } |
53 | #else |
54 | #define check_fd_is_open(o) |
55 | #endif |
56 | |
57 | STATIC void vfs_posix_file_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
58 | (void)kind; |
59 | mp_obj_vfs_posix_file_t *self = MP_OBJ_TO_PTR(self_in); |
60 | mp_printf(print, "<io.%s %d>" , mp_obj_get_type_str(self_in), self->fd); |
61 | } |
62 | |
63 | mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_obj_t mode_in) { |
64 | mp_obj_vfs_posix_file_t *o = m_new_obj(mp_obj_vfs_posix_file_t); |
65 | const char *mode_s = mp_obj_str_get_str(mode_in); |
66 | |
67 | int mode_rw = 0, mode_x = 0; |
68 | while (*mode_s) { |
69 | switch (*mode_s++) { |
70 | case 'r': |
71 | mode_rw = O_RDONLY; |
72 | break; |
73 | case 'w': |
74 | mode_rw = O_WRONLY; |
75 | mode_x = O_CREAT | O_TRUNC; |
76 | break; |
77 | case 'a': |
78 | mode_rw = O_WRONLY; |
79 | mode_x = O_CREAT | O_APPEND; |
80 | break; |
81 | case '+': |
82 | mode_rw = O_RDWR; |
83 | break; |
84 | #if MICROPY_PY_IO_FILEIO |
85 | // If we don't have io.FileIO, then files are in text mode implicitly |
86 | case 'b': |
87 | type = &mp_type_vfs_posix_fileio; |
88 | break; |
89 | case 't': |
90 | type = &mp_type_vfs_posix_textio; |
91 | break; |
92 | #endif |
93 | } |
94 | } |
95 | |
96 | o->base.type = type; |
97 | |
98 | mp_obj_t fid = file_in; |
99 | |
100 | if (mp_obj_is_small_int(fid)) { |
101 | o->fd = MP_OBJ_SMALL_INT_VALUE(fid); |
102 | return MP_OBJ_FROM_PTR(o); |
103 | } |
104 | |
105 | const char *fname = mp_obj_str_get_str(fid); |
106 | int fd; |
107 | MP_HAL_RETRY_SYSCALL(fd, open(fname, mode_x | mode_rw, 0644), mp_raise_OSError(err)); |
108 | o->fd = fd; |
109 | return MP_OBJ_FROM_PTR(o); |
110 | } |
111 | |
112 | STATIC mp_obj_t vfs_posix_file_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
113 | static const mp_arg_t allowed_args[] = { |
114 | { MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE} }, |
115 | { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR_r)} }, |
116 | }; |
117 | |
118 | mp_arg_val_t arg_vals[MP_ARRAY_SIZE(allowed_args)]; |
119 | mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, arg_vals); |
120 | return mp_vfs_posix_file_open(type, arg_vals[0].u_obj, arg_vals[1].u_obj); |
121 | } |
122 | |
123 | STATIC mp_obj_t vfs_posix_file_fileno(mp_obj_t self_in) { |
124 | mp_obj_vfs_posix_file_t *self = MP_OBJ_TO_PTR(self_in); |
125 | check_fd_is_open(self); |
126 | return MP_OBJ_NEW_SMALL_INT(self->fd); |
127 | } |
128 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_file_fileno_obj, vfs_posix_file_fileno); |
129 | |
130 | STATIC mp_obj_t vfs_posix_file___exit__(size_t n_args, const mp_obj_t *args) { |
131 | (void)n_args; |
132 | return mp_stream_close(args[0]); |
133 | } |
134 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(vfs_posix_file___exit___obj, 4, 4, vfs_posix_file___exit__); |
135 | |
136 | STATIC mp_uint_t vfs_posix_file_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { |
137 | mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in); |
138 | check_fd_is_open(o); |
139 | ssize_t r; |
140 | MP_HAL_RETRY_SYSCALL(r, read(o->fd, buf, size), { |
141 | *errcode = err; |
142 | return MP_STREAM_ERROR; |
143 | }); |
144 | return (mp_uint_t)r; |
145 | } |
146 | |
147 | STATIC mp_uint_t vfs_posix_file_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) { |
148 | mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in); |
149 | check_fd_is_open(o); |
150 | #if MICROPY_PY_OS_DUPTERM |
151 | if (o->fd <= STDERR_FILENO) { |
152 | mp_hal_stdout_tx_strn(buf, size); |
153 | return size; |
154 | } |
155 | #endif |
156 | ssize_t r; |
157 | MP_HAL_RETRY_SYSCALL(r, write(o->fd, buf, size), { |
158 | *errcode = err; |
159 | return MP_STREAM_ERROR; |
160 | }); |
161 | return (mp_uint_t)r; |
162 | } |
163 | |
164 | STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) { |
165 | mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in); |
166 | |
167 | if (request != MP_STREAM_CLOSE) { |
168 | check_fd_is_open(o); |
169 | } |
170 | |
171 | switch (request) { |
172 | case MP_STREAM_FLUSH: { |
173 | int ret; |
174 | MP_HAL_RETRY_SYSCALL(ret, fsync(o->fd), { |
175 | if (err == EINVAL |
176 | && (o->fd == STDIN_FILENO || o->fd == STDOUT_FILENO || o->fd == STDERR_FILENO)) { |
177 | // fsync(stdin/stdout/stderr) may fail with EINVAL, but don't propagate that |
178 | // error out. Because data is not buffered by us, and stdin/out/err.flush() |
179 | // should just be a no-op. |
180 | return 0; |
181 | } |
182 | *errcode = err; |
183 | return MP_STREAM_ERROR; |
184 | }); |
185 | return 0; |
186 | } |
187 | case MP_STREAM_SEEK: { |
188 | struct mp_stream_seek_t *s = (struct mp_stream_seek_t *)arg; |
189 | MP_THREAD_GIL_EXIT(); |
190 | off_t off = lseek(o->fd, s->offset, s->whence); |
191 | MP_THREAD_GIL_ENTER(); |
192 | if (off == (off_t)-1) { |
193 | *errcode = errno; |
194 | return MP_STREAM_ERROR; |
195 | } |
196 | s->offset = off; |
197 | return 0; |
198 | } |
199 | case MP_STREAM_CLOSE: |
200 | MP_THREAD_GIL_EXIT(); |
201 | close(o->fd); |
202 | MP_THREAD_GIL_ENTER(); |
203 | #ifdef MICROPY_CPYTHON_COMPAT |
204 | o->fd = -1; |
205 | #endif |
206 | return 0; |
207 | case MP_STREAM_GET_FILENO: |
208 | return o->fd; |
209 | default: |
210 | *errcode = EINVAL; |
211 | return MP_STREAM_ERROR; |
212 | } |
213 | } |
214 | |
215 | STATIC const mp_rom_map_elem_t vfs_posix_rawfile_locals_dict_table[] = { |
216 | { MP_ROM_QSTR(MP_QSTR_fileno), MP_ROM_PTR(&vfs_posix_file_fileno_obj) }, |
217 | { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, |
218 | { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, |
219 | { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, |
220 | { MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj) }, |
221 | { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, |
222 | { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) }, |
223 | { MP_ROM_QSTR(MP_QSTR_tell), MP_ROM_PTR(&mp_stream_tell_obj) }, |
224 | { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) }, |
225 | { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) }, |
226 | { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) }, |
227 | { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&vfs_posix_file___exit___obj) }, |
228 | }; |
229 | |
230 | STATIC MP_DEFINE_CONST_DICT(vfs_posix_rawfile_locals_dict, vfs_posix_rawfile_locals_dict_table); |
231 | |
232 | #if MICROPY_PY_IO_FILEIO |
233 | STATIC const mp_stream_p_t vfs_posix_fileio_stream_p = { |
234 | .read = vfs_posix_file_read, |
235 | .write = vfs_posix_file_write, |
236 | .ioctl = vfs_posix_file_ioctl, |
237 | }; |
238 | |
239 | const mp_obj_type_t mp_type_vfs_posix_fileio = { |
240 | { &mp_type_type }, |
241 | .name = MP_QSTR_FileIO, |
242 | .print = vfs_posix_file_print, |
243 | .make_new = vfs_posix_file_make_new, |
244 | .getiter = mp_identity_getiter, |
245 | .iternext = mp_stream_unbuffered_iter, |
246 | .protocol = &vfs_posix_fileio_stream_p, |
247 | .locals_dict = (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict, |
248 | }; |
249 | #endif |
250 | |
251 | STATIC const mp_stream_p_t vfs_posix_textio_stream_p = { |
252 | .read = vfs_posix_file_read, |
253 | .write = vfs_posix_file_write, |
254 | .ioctl = vfs_posix_file_ioctl, |
255 | .is_text = true, |
256 | }; |
257 | |
258 | const mp_obj_type_t mp_type_vfs_posix_textio = { |
259 | { &mp_type_type }, |
260 | .name = MP_QSTR_TextIOWrapper, |
261 | .print = vfs_posix_file_print, |
262 | .make_new = vfs_posix_file_make_new, |
263 | .getiter = mp_identity_getiter, |
264 | .iternext = mp_stream_unbuffered_iter, |
265 | .protocol = &vfs_posix_textio_stream_p, |
266 | .locals_dict = (mp_obj_dict_t *)&vfs_posix_rawfile_locals_dict, |
267 | }; |
268 | |
269 | const mp_obj_vfs_posix_file_t mp_sys_stdin_obj = {{&mp_type_textio}, STDIN_FILENO}; |
270 | const mp_obj_vfs_posix_file_t mp_sys_stdout_obj = {{&mp_type_textio}, STDOUT_FILENO}; |
271 | const mp_obj_vfs_posix_file_t mp_sys_stderr_obj = {{&mp_type_textio}, STDERR_FILENO}; |
272 | |
273 | #endif // MICROPY_VFS_POSIX || MICROPY_VFS_POSIX_FILE |
274 | |