file::pread

上级 0b6ffac6
......@@ -13,6 +13,7 @@ struct file : public referenced {
file *dup();
int stat(struct stat*);
int read(char *addr, int n);
ssize_t pread(char *addr, size_t n, off_t offset);
int write(char *addr, int n);
enum { FD_NONE, FD_PIPE, FD_INODE, FD_SOCKET } type;
......
......@@ -80,6 +80,21 @@ file::read(char *addr, int n)
panic("fileread");
}
ssize_t
file::pread(char *addr, size_t n, off_t off)
{
if(type == file::FD_INODE){
int r;
ilock(ip, 0);
if(ip->type == 0)
panic("file::pread");
r = readi(ip, addr, off, n);
iunlock(ip);
return r;
}
return -1;
}
int
file::write(char *addr, int n)
{
......
......@@ -57,7 +57,6 @@ sys_pread(int fd, void *ubuf, size_t count, off_t offset)
{
sref<file> f;
uptr i = (uptr)ubuf;
int r;
if (!getfile(fd, &f))
return -1;
......@@ -66,15 +65,7 @@ sys_pread(int fd, void *ubuf, size_t count, off_t offset)
if(pagefault(myproc()->vmap, va, 0) < 0)
return -1;
if(f->type == file::FD_INODE){
ilock(f->ip, 0);
if(f->ip->type == 0)
panic("fileread");
r = readi(f->ip, (char*)ubuf, offset, count);
iunlock(f->ip);
return r;
}
return -1;
return f->pread((char*)ubuf, count, offset);
}
long
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论