file::pread

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