/* Tread - read from a file or directory. */ Npfcall* diod_read (Npfid *fid, u64 offset, u32 count, Npreq *req) { Fid *f = fid->aux; Npfcall *ret = NULL; ssize_t n; if (!f->ioctx) { msg ("diod_read: fid is not open"); np_uerror (EBADF); goto error; } if (!(ret = np_alloc_rread (count))) { np_uerror (ENOMEM); goto error; } n = ioctx_pread (f->ioctx, ret->u.rread.data, count, offset); if (n < 0) { np_uerror (errno); goto error_quiet; } np_set_rread_count (ret, n); return ret; error: errn (np_rerror (), "diod_read %s@%s:%s", fid->user->uname, np_conn_get_client_id (fid->conn), path_s (f->path)); error_quiet: if (ret) free (ret); return NULL; }
/* Tread - read from a file or directory. */ Npfcall* diod_read (Npfid *fid, u64 offset, u32 count, Npreq *req) { Fid *f = fid->aux; Npfcall *ret = NULL; ssize_t n; if (!(ret = np_alloc_rread (count))) { np_uerror (ENOMEM); goto error; } if ((n = pread (f->fd, ret->u.rread.data, count, offset)) < 0) { np_uerror (errno); goto error_quiet; } np_set_rread_count (ret, n); return ret; error: errn (np_rerror (), "diod_read %s@%s:%s", fid->user->uname, np_conn_get_client_id (fid->conn), f->path); error_quiet: if (ret) free (ret); return NULL; }
Npfcall * np_read(Npreq *req, Npfcall *tc) { int n; Npconn *conn = req->conn; Npfid *fid = req->fid; Npfcall *rc = NULL; if (!fid) { np_uerror (EIO); goto done; } if (tc->u.tread.count + P9_IOHDRSZ > conn->msize) { np_uerror(EIO); goto done; } if (fid->type & P9_QTAUTH) { if (conn->srv->auth) { rc = np_alloc_rread(tc->u.tread.count); if (!rc) goto done; n = conn->srv->auth->read(fid, tc->u.tread.offset, tc->u.tread.count, rc->u.rread.data); if (n >= 0) np_set_rread_count(rc, n); else { free(rc); rc = NULL; } } else np_uerror(EIO); goto done; } if ((fid->type & P9_QTDIR)) { np_uerror(EIO); goto done; } if (fid->type & P9_QTTMP) { rc = np_ctl_read(fid, tc->u.tread.offset, tc->u.tread.count, req); } else { if (np_setfsid (req, fid->user, -1) < 0) goto done; if (!conn->srv->read) { np_uerror(ENOSYS); goto done; } rc = (*conn->srv->read)(fid, tc->u.tread.offset, tc->u.tread.count, req); } done: return rc; }
static void test_rread (void) { Npfcall *fc, *fc2; u8 buf[128]; memset (buf, 0xf0, sizeof(buf)); if (!(fc = np_create_rread (sizeof (buf), buf))) msg_exit ("out of memory in %s", __FUNCTION__); np_set_rread_count (fc, sizeof (buf)); fc2 = _rcv_buf (fc, P9_RREAD, __FUNCTION__); assert (fc->u.rread.count == fc2->u.rread.count); assert (memcmp (fc->u.rread.data, fc2->u.rread.data, fc->u.rread.count) == 0); free (fc); free (fc2); }