vfs_posix_file.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include "py/runtime.h"
  27. #include "py/stream.h"
  28. #include "extmod/vfs_posix.h"
  29. #if MICROPY_VFS_POSIX
  30. #include <fcntl.h>
  31. #ifdef _WIN32
  32. #define fsync _commit
  33. #endif
  34. typedef struct _mp_obj_vfs_posix_file_t {
  35. mp_obj_base_t base;
  36. int fd;
  37. } mp_obj_vfs_posix_file_t;
  38. #ifdef MICROPY_CPYTHON_COMPAT
  39. STATIC void check_fd_is_open(const mp_obj_vfs_posix_file_t *o) {
  40. if (o->fd < 0) {
  41. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "I/O operation on closed file"));
  42. }
  43. }
  44. #else
  45. #define check_fd_is_open(o)
  46. #endif
  47. STATIC void vfs_posix_file_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  48. (void)kind;
  49. mp_obj_vfs_posix_file_t *self = MP_OBJ_TO_PTR(self_in);
  50. mp_printf(print, "<io.%s %d>", mp_obj_get_type_str(self_in), self->fd);
  51. }
  52. mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_obj_t mode_in) {
  53. mp_obj_vfs_posix_file_t *o = m_new_obj(mp_obj_vfs_posix_file_t);
  54. const char *mode_s = mp_obj_str_get_str(mode_in);
  55. int mode_rw = 0, mode_x = 0;
  56. while (*mode_s) {
  57. switch (*mode_s++) {
  58. case 'r':
  59. mode_rw = O_RDONLY;
  60. break;
  61. case 'w':
  62. mode_rw = O_WRONLY;
  63. mode_x = O_CREAT | O_TRUNC;
  64. break;
  65. case 'a':
  66. mode_rw = O_WRONLY;
  67. mode_x = O_CREAT | O_APPEND;
  68. break;
  69. case '+':
  70. mode_rw = O_RDWR;
  71. break;
  72. #if MICROPY_PY_IO_FILEIO
  73. // If we don't have io.FileIO, then files are in text mode implicitly
  74. case 'b':
  75. type = &mp_type_vfs_posix_fileio;
  76. break;
  77. case 't':
  78. type = &mp_type_vfs_posix_textio;
  79. break;
  80. #endif
  81. }
  82. }
  83. o->base.type = type;
  84. mp_obj_t fid = file_in;
  85. if (MP_OBJ_IS_SMALL_INT(fid)) {
  86. o->fd = MP_OBJ_SMALL_INT_VALUE(fid);
  87. return MP_OBJ_FROM_PTR(o);
  88. }
  89. const char *fname = mp_obj_str_get_str(fid);
  90. int fd = open(fname, mode_x | mode_rw, 0644);
  91. if (fd == -1) {
  92. mp_raise_OSError(errno);
  93. }
  94. o->fd = fd;
  95. return MP_OBJ_FROM_PTR(o);
  96. }
  97. 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) {
  98. static const mp_arg_t allowed_args[] = {
  99. { MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
  100. { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR_r)} },
  101. };
  102. mp_arg_val_t arg_vals[MP_ARRAY_SIZE(allowed_args)];
  103. mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, arg_vals);
  104. return mp_vfs_posix_file_open(type, arg_vals[0].u_obj, arg_vals[1].u_obj);
  105. }
  106. STATIC mp_obj_t vfs_posix_file_fileno(mp_obj_t self_in) {
  107. mp_obj_vfs_posix_file_t *self = MP_OBJ_TO_PTR(self_in);
  108. check_fd_is_open(self);
  109. return MP_OBJ_NEW_SMALL_INT(self->fd);
  110. }
  111. STATIC MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_file_fileno_obj, vfs_posix_file_fileno);
  112. STATIC mp_obj_t vfs_posix_file___exit__(size_t n_args, const mp_obj_t *args) {
  113. (void)n_args;
  114. return mp_stream_close(args[0]);
  115. }
  116. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(vfs_posix_file___exit___obj, 4, 4, vfs_posix_file___exit__);
  117. STATIC mp_uint_t vfs_posix_file_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
  118. mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in);
  119. check_fd_is_open(o);
  120. mp_int_t r = read(o->fd, buf, size);
  121. if (r == -1) {
  122. *errcode = errno;
  123. return MP_STREAM_ERROR;
  124. }
  125. return r;
  126. }
  127. STATIC mp_uint_t vfs_posix_file_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
  128. mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in);
  129. check_fd_is_open(o);
  130. #if MICROPY_PY_OS_DUPTERM
  131. if (o->fd <= STDERR_FILENO) {
  132. mp_hal_stdout_tx_strn(buf, size);
  133. return size;
  134. }
  135. #endif
  136. mp_int_t r = write(o->fd, buf, size);
  137. while (r == -1 && errno == EINTR) {
  138. if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
  139. mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
  140. MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
  141. nlr_raise(obj);
  142. }
  143. r = write(o->fd, buf, size);
  144. }
  145. if (r == -1) {
  146. *errcode = errno;
  147. return MP_STREAM_ERROR;
  148. }
  149. return r;
  150. }
  151. STATIC mp_uint_t vfs_posix_file_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
  152. mp_obj_vfs_posix_file_t *o = MP_OBJ_TO_PTR(o_in);
  153. check_fd_is_open(o);
  154. switch (request) {
  155. case MP_STREAM_FLUSH:
  156. if (fsync(o->fd) < 0) {
  157. *errcode = errno;
  158. return MP_STREAM_ERROR;
  159. }
  160. return 0;
  161. case MP_STREAM_SEEK: {
  162. struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)arg;
  163. off_t off = lseek(o->fd, s->offset, s->whence);
  164. if (off == (off_t)-1) {
  165. *errcode = errno;
  166. return MP_STREAM_ERROR;
  167. }
  168. s->offset = off;
  169. return 0;
  170. }
  171. case MP_STREAM_CLOSE:
  172. close(o->fd);
  173. #ifdef MICROPY_CPYTHON_COMPAT
  174. o->fd = -1;
  175. #endif
  176. return 0;
  177. default:
  178. *errcode = EINVAL;
  179. return MP_STREAM_ERROR;
  180. }
  181. }
  182. STATIC const mp_rom_map_elem_t rawfile_locals_dict_table[] = {
  183. { MP_ROM_QSTR(MP_QSTR_fileno), MP_ROM_PTR(&vfs_posix_file_fileno_obj) },
  184. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  185. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
  186. { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
  187. { MP_ROM_QSTR(MP_QSTR_readlines), MP_ROM_PTR(&mp_stream_unbuffered_readlines_obj) },
  188. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
  189. { MP_ROM_QSTR(MP_QSTR_seek), MP_ROM_PTR(&mp_stream_seek_obj) },
  190. { MP_ROM_QSTR(MP_QSTR_tell), MP_ROM_PTR(&mp_stream_tell_obj) },
  191. { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
  192. { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
  193. { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
  194. { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&vfs_posix_file___exit___obj) },
  195. };
  196. STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict, rawfile_locals_dict_table);
  197. #if MICROPY_PY_IO_FILEIO
  198. STATIC const mp_stream_p_t fileio_stream_p = {
  199. .read = vfs_posix_file_read,
  200. .write = vfs_posix_file_write,
  201. .ioctl = vfs_posix_file_ioctl,
  202. };
  203. const mp_obj_type_t mp_type_vfs_posix_fileio = {
  204. { &mp_type_type },
  205. .name = MP_QSTR_FileIO,
  206. .print = vfs_posix_file_print,
  207. .make_new = vfs_posix_file_make_new,
  208. .getiter = mp_identity_getiter,
  209. .iternext = mp_stream_unbuffered_iter,
  210. .protocol = &fileio_stream_p,
  211. .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict,
  212. };
  213. #endif
  214. STATIC const mp_stream_p_t textio_stream_p = {
  215. .read = vfs_posix_file_read,
  216. .write = vfs_posix_file_write,
  217. .ioctl = vfs_posix_file_ioctl,
  218. .is_text = true,
  219. };
  220. const mp_obj_type_t mp_type_vfs_posix_textio = {
  221. { &mp_type_type },
  222. .name = MP_QSTR_TextIOWrapper,
  223. .print = vfs_posix_file_print,
  224. .make_new = vfs_posix_file_make_new,
  225. .getiter = mp_identity_getiter,
  226. .iternext = mp_stream_unbuffered_iter,
  227. .protocol = &textio_stream_p,
  228. .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict,
  229. };
  230. const mp_obj_vfs_posix_file_t mp_sys_stdin_obj = {{&mp_type_textio}, STDIN_FILENO};
  231. const mp_obj_vfs_posix_file_t mp_sys_stdout_obj = {{&mp_type_textio}, STDOUT_FILENO};
  232. const mp_obj_vfs_posix_file_t mp_sys_stderr_obj = {{&mp_type_textio}, STDERR_FILENO};
  233. #endif // MICROPY_VFS_POSIX