stream.c 20 KB

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