STATIC mp_uint_t fdfile_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) { mp_obj_fdfile_t *o = MP_OBJ_TO_PTR(o_in); check_fd_is_open(o); switch (request) { case MP_STREAM_SEEK: { struct mp_stream_seek_t *s = (struct mp_stream_seek_t*)arg; off_t off = lseek(o->fd, s->offset, s->whence); if (off == (off_t)-1) { *errcode = errno; return MP_STREAM_ERROR; } s->offset = off; return 0; } case MP_STREAM_FLUSH: if (fsync(o->fd) < 0) { *errcode = errno; return MP_STREAM_ERROR; } return 0; default: *errcode = EINVAL; return MP_STREAM_ERROR; } }
STATIC machine_int_t fdfile_write(mp_obj_t o_in, const void *buf, machine_uint_t size, int *errcode) { mp_obj_fdfile_t *o = o_in; check_fd_is_open(o); machine_int_t r = write(o->fd, buf, size); if (r == -1) { *errcode = errno; } return r; }
STATIC mp_uint_t fdfile_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) { mp_obj_fdfile_t *o = o_in; check_fd_is_open(o); mp_int_t r = write(o->fd, buf, size); if (r == -1) { *errcode = errno; return MP_STREAM_ERROR; } return r; }
STATIC mp_uint_t fdfile_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { mp_obj_fdfile_t *o = MP_OBJ_TO_PTR(o_in); check_fd_is_open(o); mp_int_t r = read(o->fd, buf, size); if (r == -1) { *errcode = errno; return MP_STREAM_ERROR; } return r; }
STATIC mp_uint_t fdfile_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) { mp_obj_fdfile_t *o = MP_OBJ_TO_PTR(o_in); check_fd_is_open(o); #if MICROPY_PY_OS_DUPTERM if (o->fd <= STDERR_FILENO) { mp_hal_stdout_tx_strn(buf, size); return size; } #endif mp_int_t r = write(o->fd, buf, size); while (r == -1 && errno == EINTR) { if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) { mp_obj_t obj = MP_STATE_VM(mp_pending_exception); MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL; nlr_raise(obj); } r = write(o->fd, buf, size); } if (r == -1) { *errcode = errno; return MP_STREAM_ERROR; } return r; }
STATIC mp_obj_t fdfile_flush(mp_obj_t self_in) { mp_obj_fdfile_t *self = self_in; check_fd_is_open(self); fsync(self->fd); return mp_const_none; }
STATIC mp_obj_t fdfile_fileno(mp_obj_t self_in) { mp_obj_fdfile_t *self = self_in; check_fd_is_open(self); return MP_OBJ_NEW_SMALL_INT(self->fd); }