stream.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2014 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "py/objstr.h"
  30. #include "py/stream.h"
  31. #include "py/runtime.h"
  32. // This file defines generic Python stream read/write methods which
  33. // dispatch to the underlying stream interface of an object.
  34. // TODO: should be in mpconfig.h
  35. #define DEFAULT_BUFFER_SIZE 256
  36. STATIC mp_obj_t stream_readall(mp_obj_t self_in);
  37. #define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes)
  38. // Returns error condition in *errcode, if non-zero, return value is number of bytes written
  39. // before error condition occurred. If *errcode == 0, returns total bytes written (which will
  40. // be equal to input size).
  41. mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode, byte flags) {
  42. byte *buf = buf_;
  43. mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
  44. typedef mp_uint_t (*io_func_t)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
  45. io_func_t io_func;
  46. const mp_stream_p_t *stream_p = s->type->protocol;
  47. if (flags & MP_STREAM_RW_WRITE) {
  48. io_func = (io_func_t)stream_p->write;
  49. } else {
  50. io_func = stream_p->read;
  51. }
  52. *errcode = 0;
  53. mp_uint_t done = 0;
  54. while (size > 0) {
  55. mp_uint_t out_sz = io_func(stream, buf, size, errcode);
  56. // For read, out_sz == 0 means EOF. For write, it's unspecified
  57. // what it means, but we don't make any progress, so returning
  58. // is still the best option.
  59. if (out_sz == 0) {
  60. return done;
  61. }
  62. if (out_sz == MP_STREAM_ERROR) {
  63. // If we read something before getting EAGAIN, don't leak it
  64. if (mp_is_nonblocking_error(*errcode) && done != 0) {
  65. *errcode = 0;
  66. }
  67. return done;
  68. }
  69. if (flags & MP_STREAM_RW_ONCE) {
  70. return out_sz;
  71. }
  72. buf += out_sz;
  73. size -= out_sz;
  74. done += out_sz;
  75. }
  76. return done;
  77. }
  78. const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
  79. mp_obj_type_t *type = mp_obj_get_type(self_in);
  80. const mp_stream_p_t *stream_p = type->protocol;
  81. if (stream_p == NULL
  82. || ((flags & MP_STREAM_OP_READ) && stream_p->read == NULL)
  83. || ((flags & MP_STREAM_OP_WRITE) && stream_p->write == NULL)
  84. || ((flags & MP_STREAM_OP_IOCTL) && stream_p->ioctl == NULL)) {
  85. // CPython: io.UnsupportedOperation, OSError subclass
  86. mp_raise_msg(&mp_type_OSError, "stream operation not supported");
  87. }
  88. return stream_p;
  89. }
  90. STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) {
  91. const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
  92. // What to do if sz < -1? Python docs don't specify this case.
  93. // CPython does a readall, but here we silently let negatives through,
  94. // and they will cause a MemoryError.
  95. mp_int_t sz;
  96. if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
  97. return stream_readall(args[0]);
  98. }
  99. #if MICROPY_PY_BUILTINS_STR_UNICODE
  100. if (stream_p->is_text) {
  101. // We need to read sz number of unicode characters. Because we don't have any
  102. // buffering, and because the stream API can only read bytes, we must read here
  103. // in units of bytes and must never over read. If we want sz chars, then reading
  104. // sz bytes will never over-read, so we follow this approach, in a loop to keep
  105. // reading until we have exactly enough chars. This will be 1 read for text
  106. // with ASCII-only chars, and about 2 reads for text with a couple of non-ASCII
  107. // chars. For text with lots of non-ASCII chars, it'll be pretty inefficient
  108. // in time and memory.
  109. vstr_t vstr;
  110. vstr_init(&vstr, sz);
  111. mp_uint_t more_bytes = sz;
  112. mp_uint_t last_buf_offset = 0;
  113. while (more_bytes > 0) {
  114. char *p = vstr_add_len(&vstr, more_bytes);
  115. int error;
  116. mp_uint_t out_sz = mp_stream_read_exactly(args[0], p, more_bytes, &error);
  117. if (error != 0) {
  118. vstr_cut_tail_bytes(&vstr, more_bytes);
  119. if (mp_is_nonblocking_error(error)) {
  120. // With non-blocking streams, we read as much as we can.
  121. // If we read nothing, return None, just like read().
  122. // Otherwise, return data read so far.
  123. // TODO what if we have read only half a non-ASCII char?
  124. if (vstr.len == 0) {
  125. vstr_clear(&vstr);
  126. return mp_const_none;
  127. }
  128. break;
  129. }
  130. mp_raise_OSError(error);
  131. }
  132. if (out_sz < more_bytes) {
  133. // Finish reading.
  134. // TODO what if we have read only half a non-ASCII char?
  135. vstr_cut_tail_bytes(&vstr, more_bytes - out_sz);
  136. if (out_sz == 0) {
  137. break;
  138. }
  139. }
  140. // count chars from bytes just read
  141. for (mp_uint_t off = last_buf_offset;;) {
  142. byte b = vstr.buf[off];
  143. int n;
  144. if (!UTF8_IS_NONASCII(b)) {
  145. // 1-byte ASCII char
  146. n = 1;
  147. } else if ((b & 0xe0) == 0xc0) {
  148. // 2-byte char
  149. n = 2;
  150. } else if ((b & 0xf0) == 0xe0) {
  151. // 3-byte char
  152. n = 3;
  153. } else if ((b & 0xf8) == 0xf0) {
  154. // 4-byte char
  155. n = 4;
  156. } else {
  157. // TODO
  158. n = 5;
  159. }
  160. if (off + n <= vstr.len) {
  161. // got a whole char in n bytes
  162. off += n;
  163. sz -= 1;
  164. last_buf_offset = off;
  165. if (off >= vstr.len) {
  166. more_bytes = sz;
  167. break;
  168. }
  169. } else {
  170. // didn't get a whole char, so work out how many extra bytes are needed for
  171. // this partial char, plus bytes for additional chars that we want
  172. more_bytes = (off + n - vstr.len) + (sz - 1);
  173. break;
  174. }
  175. }
  176. }
  177. return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
  178. }
  179. #endif
  180. vstr_t vstr;
  181. vstr_init_len(&vstr, sz);
  182. int error;
  183. mp_uint_t out_sz = mp_stream_rw(args[0], vstr.buf, sz, &error, flags);
  184. if (error != 0) {
  185. vstr_clear(&vstr);
  186. if (mp_is_nonblocking_error(error)) {
  187. // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read
  188. // "If the object is in non-blocking mode and no bytes are available,
  189. // None is returned."
  190. // This is actually very weird, as naive truth check will treat
  191. // this as EOF.
  192. return mp_const_none;
  193. }
  194. mp_raise_OSError(error);
  195. } else {
  196. vstr.len = out_sz;
  197. return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
  198. }
  199. }
  200. STATIC mp_obj_t stream_read(size_t n_args, const mp_obj_t *args) {
  201. return stream_read_generic(n_args, args, MP_STREAM_RW_READ);
  202. }
  203. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
  204. STATIC mp_obj_t stream_read1(size_t n_args, const mp_obj_t *args) {
  205. return stream_read_generic(n_args, args, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);
  206. }
  207. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read1_obj, 1, 2, stream_read1);
  208. mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len, byte flags) {
  209. mp_get_stream_raise(self_in, MP_STREAM_OP_WRITE);
  210. int error;
  211. mp_uint_t out_sz = mp_stream_rw(self_in, (void*)buf, len, &error, flags);
  212. if (error != 0) {
  213. if (mp_is_nonblocking_error(error)) {
  214. // http://docs.python.org/3/library/io.html#io.RawIOBase.write
  215. // "None is returned if the raw stream is set not to block and
  216. // no single byte could be readily written to it."
  217. return mp_const_none;
  218. }
  219. mp_raise_OSError(error);
  220. } else {
  221. return MP_OBJ_NEW_SMALL_INT(out_sz);
  222. }
  223. }
  224. // XXX hack
  225. void mp_stream_write_adaptor(void *self, const char *buf, size_t len) {
  226. mp_stream_write(MP_OBJ_FROM_PTR(self), buf, len, MP_STREAM_RW_WRITE);
  227. }
  228. STATIC mp_obj_t stream_write_method(size_t n_args, const mp_obj_t *args) {
  229. mp_buffer_info_t bufinfo;
  230. mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
  231. size_t max_len = (size_t)-1;
  232. size_t off = 0;
  233. if (n_args == 3) {
  234. max_len = mp_obj_get_int_truncated(args[2]);
  235. } else if (n_args == 4) {
  236. off = mp_obj_get_int_truncated(args[2]);
  237. max_len = mp_obj_get_int_truncated(args[3]);
  238. if (off > bufinfo.len) {
  239. off = bufinfo.len;
  240. }
  241. }
  242. bufinfo.len -= off;
  243. return mp_stream_write(args[0], (byte*)bufinfo.buf + off, MIN(bufinfo.len, max_len), MP_STREAM_RW_WRITE);
  244. }
  245. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_write_obj, 2, 4, stream_write_method);
  246. STATIC mp_obj_t stream_write1_method(mp_obj_t self_in, mp_obj_t arg) {
  247. mp_buffer_info_t bufinfo;
  248. mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
  249. return mp_stream_write(self_in, bufinfo.buf, bufinfo.len, MP_STREAM_RW_WRITE | MP_STREAM_RW_ONCE);
  250. }
  251. MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write1_obj, stream_write1_method);
  252. STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
  253. mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
  254. mp_buffer_info_t bufinfo;
  255. mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
  256. // CPython extension: if 2nd arg is provided, that's max len to read,
  257. // instead of full buffer. Similar to
  258. // https://docs.python.org/3/library/socket.html#socket.socket.recv_into
  259. mp_uint_t len = bufinfo.len;
  260. if (n_args > 2) {
  261. len = mp_obj_get_int(args[2]);
  262. if (len > bufinfo.len) {
  263. len = bufinfo.len;
  264. }
  265. }
  266. int error;
  267. mp_uint_t out_sz = mp_stream_read_exactly(args[0], bufinfo.buf, len, &error);
  268. if (error != 0) {
  269. if (mp_is_nonblocking_error(error)) {
  270. return mp_const_none;
  271. }
  272. mp_raise_OSError(error);
  273. } else {
  274. return MP_OBJ_NEW_SMALL_INT(out_sz);
  275. }
  276. }
  277. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
  278. STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
  279. const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_READ);
  280. mp_uint_t total_size = 0;
  281. vstr_t vstr;
  282. vstr_init(&vstr, DEFAULT_BUFFER_SIZE);
  283. char *p = vstr.buf;
  284. mp_uint_t current_read = DEFAULT_BUFFER_SIZE;
  285. while (true) {
  286. int error;
  287. mp_uint_t out_sz = stream_p->read(self_in, p, current_read, &error);
  288. if (out_sz == MP_STREAM_ERROR) {
  289. if (mp_is_nonblocking_error(error)) {
  290. // With non-blocking streams, we read as much as we can.
  291. // If we read nothing, return None, just like read().
  292. // Otherwise, return data read so far.
  293. if (total_size == 0) {
  294. return mp_const_none;
  295. }
  296. break;
  297. }
  298. mp_raise_OSError(error);
  299. }
  300. if (out_sz == 0) {
  301. break;
  302. }
  303. total_size += out_sz;
  304. if (out_sz < current_read) {
  305. current_read -= out_sz;
  306. p += out_sz;
  307. } else {
  308. p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE);
  309. current_read = DEFAULT_BUFFER_SIZE;
  310. }
  311. }
  312. vstr.len = total_size;
  313. return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
  314. }
  315. // Unbuffered, inefficient implementation of readline() for raw I/O files.
  316. STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) {
  317. const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
  318. mp_int_t max_size = -1;
  319. if (n_args > 1) {
  320. max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
  321. }
  322. vstr_t vstr;
  323. if (max_size != -1) {
  324. vstr_init(&vstr, max_size);
  325. } else {
  326. vstr_init(&vstr, 16);
  327. }
  328. while (max_size == -1 || max_size-- != 0) {
  329. char *p = vstr_add_len(&vstr, 1);
  330. int error;
  331. mp_uint_t out_sz = stream_p->read(args[0], p, 1, &error);
  332. if (out_sz == MP_STREAM_ERROR) {
  333. if (mp_is_nonblocking_error(error)) {
  334. if (vstr.len == 1) {
  335. // We just incremented it, but otherwise we read nothing
  336. // and immediately got EAGAIN. This case is not well
  337. // specified in
  338. // https://docs.python.org/3/library/io.html#io.IOBase.readline
  339. // unlike similar case for read(). But we follow the latter's
  340. // behavior - return None.
  341. vstr_clear(&vstr);
  342. return mp_const_none;
  343. } else {
  344. goto done;
  345. }
  346. }
  347. mp_raise_OSError(error);
  348. }
  349. if (out_sz == 0) {
  350. done:
  351. // Back out previously added byte
  352. // Consider, what's better - read a char and get OutOfMemory (so read
  353. // char is lost), or allocate first as we do.
  354. vstr_cut_tail_bytes(&vstr, 1);
  355. break;
  356. }
  357. if (*p == '\n') {
  358. break;
  359. }
  360. }
  361. return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(stream_p), &vstr);
  362. }
  363. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
  364. // TODO take an optional extra argument (what does it do exactly?)
  365. STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) {
  366. mp_obj_t lines = mp_obj_new_list(0, NULL);
  367. for (;;) {
  368. mp_obj_t line = stream_unbuffered_readline(1, &self);
  369. if (!mp_obj_is_true(line)) {
  370. break;
  371. }
  372. mp_obj_list_append(lines, line);
  373. }
  374. return lines;
  375. }
  376. MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj, stream_unbuffered_readlines);
  377. mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
  378. mp_obj_t l_in = stream_unbuffered_readline(1, &self);
  379. if (mp_obj_is_true(l_in)) {
  380. return l_in;
  381. }
  382. return MP_OBJ_STOP_ITERATION;
  383. }
  384. mp_obj_t mp_stream_close(mp_obj_t stream) {
  385. const mp_stream_p_t *stream_p = mp_get_stream_raise(stream, MP_STREAM_OP_IOCTL);
  386. int error;
  387. mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_CLOSE, 0, &error);
  388. if (res == MP_STREAM_ERROR) {
  389. mp_raise_OSError(error);
  390. }
  391. return mp_const_none;
  392. }
  393. MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_close_obj, mp_stream_close);
  394. STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
  395. const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
  396. struct mp_stream_seek_t seek_s;
  397. // TODO: Could be uint64
  398. seek_s.offset = mp_obj_get_int(args[1]);
  399. seek_s.whence = SEEK_SET;
  400. if (n_args == 3) {
  401. seek_s.whence = mp_obj_get_int(args[2]);
  402. }
  403. // In POSIX, it's error to seek before end of stream, we enforce it here.
  404. if (seek_s.whence == SEEK_SET && seek_s.offset < 0) {
  405. mp_raise_OSError(MP_EINVAL);
  406. }
  407. int error;
  408. mp_uint_t res = stream_p->ioctl(args[0], MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error);
  409. if (res == MP_STREAM_ERROR) {
  410. mp_raise_OSError(error);
  411. }
  412. // TODO: Could be uint64
  413. return mp_obj_new_int_from_uint(seek_s.offset);
  414. }
  415. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj, 2, 3, stream_seek);
  416. STATIC mp_obj_t stream_tell(mp_obj_t self) {
  417. mp_obj_t offset = MP_OBJ_NEW_SMALL_INT(0);
  418. mp_obj_t whence = MP_OBJ_NEW_SMALL_INT(SEEK_CUR);
  419. const mp_obj_t args[3] = {self, offset, whence};
  420. return stream_seek(3, args);
  421. }
  422. MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
  423. STATIC mp_obj_t stream_flush(mp_obj_t self) {
  424. const mp_stream_p_t *stream_p = mp_get_stream_raise(self, MP_STREAM_OP_IOCTL);
  425. int error;
  426. mp_uint_t res = stream_p->ioctl(self, MP_STREAM_FLUSH, 0, &error);
  427. if (res == MP_STREAM_ERROR) {
  428. mp_raise_OSError(error);
  429. }
  430. return mp_const_none;
  431. }
  432. MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_flush_obj, stream_flush);
  433. STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
  434. const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
  435. mp_buffer_info_t bufinfo;
  436. uintptr_t val = 0;
  437. if (n_args > 2) {
  438. if (mp_get_buffer(args[2], &bufinfo, MP_BUFFER_WRITE)) {
  439. val = (uintptr_t)bufinfo.buf;
  440. } else {
  441. val = mp_obj_get_int_truncated(args[2]);
  442. }
  443. }
  444. int error;
  445. mp_uint_t res = stream_p->ioctl(args[0], mp_obj_get_int(args[1]), val, &error);
  446. if (res == MP_STREAM_ERROR) {
  447. mp_raise_OSError(error);
  448. }
  449. return mp_obj_new_int(res);
  450. }
  451. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);
  452. #if MICROPY_STREAMS_POSIX_API
  453. /*
  454. * POSIX-like functions
  455. *
  456. * These functions have POSIX-compatible signature (except for "void *stream"
  457. * first argument instead of "int fd"). They are useful to port existing
  458. * POSIX-compatible software to work with MicroPython streams.
  459. */
  460. // errno-like variable. If any of the functions below returned with error
  461. // status, this variable will contain error no.
  462. int mp_stream_errno;
  463. ssize_t mp_stream_posix_write(mp_obj_t stream, const void *buf, size_t len) {
  464. mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
  465. const mp_stream_p_t *stream_p = o->type->protocol;
  466. mp_uint_t out_sz = stream_p->write(stream, buf, len, &mp_stream_errno);
  467. if (out_sz == MP_STREAM_ERROR) {
  468. return -1;
  469. } else {
  470. return out_sz;
  471. }
  472. }
  473. ssize_t mp_stream_posix_read(mp_obj_t stream, void *buf, size_t len) {
  474. mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
  475. const mp_stream_p_t *stream_p = o->type->protocol;
  476. mp_uint_t out_sz = stream_p->read(stream, buf, len, &mp_stream_errno);
  477. if (out_sz == MP_STREAM_ERROR) {
  478. return -1;
  479. } else {
  480. return out_sz;
  481. }
  482. }
  483. off_t mp_stream_posix_lseek(mp_obj_t stream, off_t offset, int whence) {
  484. const mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
  485. const mp_stream_p_t *stream_p = o->type->protocol;
  486. struct mp_stream_seek_t seek_s;
  487. seek_s.offset = offset;
  488. seek_s.whence = whence;
  489. mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &mp_stream_errno);
  490. if (res == MP_STREAM_ERROR) {
  491. return -1;
  492. }
  493. return seek_s.offset;
  494. }
  495. int mp_stream_posix_fsync(mp_obj_t stream) {
  496. mp_obj_base_t* o = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
  497. const mp_stream_p_t *stream_p = o->type->protocol;
  498. mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_FLUSH, 0, &mp_stream_errno);
  499. if (res == MP_STREAM_ERROR) {
  500. return -1;
  501. }
  502. return res;
  503. }
  504. #endif