Misc file clean up

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