Misc file clean up

上级 b9682048
......@@ -9,12 +9,11 @@
u64 namehash(const strbuf<DIRSIZ>&);
struct file : public referenced {
file() : type(file::FD_NONE), readable(0), writable(0),
socket(0), pipe(nullptr), ip(nullptr), off(0) {
inc();
}
static file *alloc();
file *dup();
void close();
int stat(struct stat*);
int read(char *addr, int n);
int write(char *addr, int n);
enum { FD_NONE, FD_PIPE, FD_INODE, FD_SOCKET } type;
......@@ -27,6 +26,9 @@ struct file : public referenced {
u32 off;
NEW_DELETE_OPS(file);
private:
file();
protected:
virtual void onzero() const;
};
......
......@@ -21,7 +21,7 @@ public:
~filetable() {
for(int fd = 0; fd < NOFILE; fd++){
if (ofile_[fd]){
ofile_[fd]->close();
ofile_[fd]->dec();
ofile_[fd] = 0;
}
}
......@@ -63,7 +63,7 @@ public:
acquire(&lock_);
ofile_[fd] = nullptr;
release(&lock_);
f->close();
f->dec();
}
void decref() {
......
......@@ -86,15 +86,6 @@ void e1000hwaddr(u8 *hwaddr);
// exec.c
int exec(const char*, char**);
// file.c
struct file* filealloc(void);
void fileclose(struct file*);
struct file* filedup(struct file*);
void fileinit(void);
int fileread(struct file*, char*, int n);
int filestat(struct file*, struct stat*);
int filewrite(struct file*, char*, int n);
// fs.c
int namecmp(const char*, const char*);
struct inode* dirlookup(struct inode*, char*);
......
......@@ -9,8 +9,21 @@
struct devsw __mpalign__ devsw[NDEV];
file*
file::alloc(void)
{
return new file();
}
file::file(void)
: type(file::FD_NONE), readable(0), writable(0),
socket(0), pipe(nullptr), ip(nullptr), off(0)
{
inc();
}
void
file::onzero() const
file::onzero(void) const
{
if(type == file::FD_PIPE)
pipeclose(pipe, writable);
......@@ -23,12 +36,6 @@ file::onzero() const
delete this;
}
void
file::close(void)
{
dec();
}
file*
file::dup(void)
{
......@@ -36,65 +43,62 @@ file::dup(void)
return this;
}
// Get metadata about file f.
int
filestat(struct file *f, struct stat *st)
file::stat(struct stat *st)
{
if(f->type == file::FD_INODE){
ilock(f->ip, 0);
if(f->ip->type == 0)
if(type == file::FD_INODE){
ilock(ip, 0);
if(ip->type == 0)
panic("filestat");
stati(f->ip, st);
iunlock(f->ip);
stati(ip, st);
iunlock(ip);
return 0;
}
return -1;
}
// Read from file f. Addr is kernel address.
int
fileread(struct file *f, char *addr, int n)
file::read(char *addr, int n)
{
int r;
if(f->readable == 0)
if(readable == 0)
return -1;
if(f->type == file::FD_PIPE)
return piperead(f->pipe, addr, n);
if(f->type == file::FD_INODE){
ilock(f->ip, 0);
if(f->ip->type == 0)
if(type == file::FD_PIPE)
return piperead(pipe, addr, n);
if(type == file::FD_INODE){
ilock(ip, 0);
if(ip->type == 0)
panic("fileread");
if((r = readi(f->ip, addr, f->off, n)) > 0)
f->off += r;
iunlock(f->ip);
if((r = readi(ip, addr, off, n)) > 0)
off += r;
iunlock(ip);
return r;
}
if(f->type == file::FD_SOCKET)
return netread(f->socket, addr, n);
if(type == file::FD_SOCKET)
return netread(socket, addr, n);
panic("fileread");
}
// Write to file f. Addr is kernel address.
int
filewrite(struct file *f, char *addr, int n)
file::write(char *addr, int n)
{
int r;
if(f->writable == 0)
if(writable == 0)
return -1;
if(f->type == file::FD_PIPE)
return pipewrite(f->pipe, addr, n);
if(f->type == file::FD_INODE){
ilock(f->ip, 1);
if(f->ip->type == 0 || f->ip->type == T_DIR)
if(type == file::FD_PIPE)
return pipewrite(pipe, addr, n);
if(type == file::FD_INODE){
ilock(ip, 1);
if(ip->type == 0 || ip->type == T_DIR)
panic("filewrite but 0 or T_DIR");
if((r = writei(f->ip, addr, f->off, n)) > 0)
f->off += r;
iunlock(f->ip);
if((r = writei(ip, addr, off, n)) > 0)
off += r;
iunlock(ip);
return r;
}
if(f->type == file::FD_SOCKET)
return netwrite(f->socket, addr, n);
if(type == file::FD_SOCKET)
return netwrite(socket, addr, n);
panic("filewrite");
}
......@@ -28,7 +28,7 @@ pipealloc(struct file **f0, struct file **f1)
p = 0;
*f0 = *f1 = 0;
if((*f0 = new file()) == 0 || (*f1 = new file()) == 0)
if((*f0 = file::alloc()) == 0 || (*f1 = file::alloc()) == 0)
goto bad;
if((p = (pipe*)kmalloc(sizeof(*p))) == 0)
goto bad;
......@@ -55,9 +55,9 @@ pipealloc(struct file **f0, struct file **f1)
kmfree((char*)p, sizeof(*p));
}
if(*f0)
(*f0)->close();
(*f0)->dec();
if(*f1)
(*f1)->close();
(*f1)->dec();
return -1;
}
......
......@@ -49,7 +49,7 @@ sys_read(int fd, char *p, int n)
if(!getfile(fd, &f) || argcheckptr(p, n) < 0)
return -1;
return fileread(f.ptr(), p, n);
return f->read(p, n);
}
ssize_t
......@@ -84,7 +84,7 @@ sys_write(int fd, char *p, int n)
if (!getfile(fd, &f) || argcheckptr(p, n) < 0)
return -1;
return filewrite(f.ptr(), p, n);
return f->write(p, n);
}
long
......@@ -105,7 +105,7 @@ sys_fstat(int fd, struct stat *st)
if (!getfile(fd, &f) || argcheckptr(st, sizeof(*st)) < 0)
return -1;
return filestat(f.ptr(), st);
return f->stat(st);
}
// Create the path new as a link to the same inode as old.
......@@ -311,9 +311,9 @@ sys_openat(int dirfd, const char *path, int omode)
}
}
if((f = new file()) == 0 || (fd = fdalloc(f)) < 0){
if((f = file::alloc()) == 0 || (fd = fdalloc(f)) < 0){
if(f)
f->close();
f->dec();
iunlockput(ip);
return -1;
}
......@@ -410,7 +410,7 @@ sys_pipe(int *fd)
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
if(fd0 >= 0)
myproc()->ftable->close(fd0);
wf->close();
wf->dec();
return -1;
}
fd[0] = fd0;
......@@ -442,13 +442,13 @@ allocsocket(struct file **rf, int *rfd)
struct file *f;
int fd;
f = new file();
f = file::alloc();
if (f == nullptr)
return -1;
fd = fdalloc(f);
if (fd < 0) {
f->close();
f->dec();
return fd;
}
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论