stop using fd to name files

This commit is contained in:
rsc 2006-09-08 13:44:42 +00:00
parent 5692823b1f
commit 2cbb4b1842
2 changed files with 60 additions and 60 deletions

84
file.c
View file

@ -11,7 +11,7 @@
#include "fs.h" #include "fs.h"
#include "fsvar.h" #include "fsvar.h"
struct spinlock fd_table_lock; struct spinlock file_table_lock;
struct devsw devsw[NDEV]; struct devsw devsw[NDEV];
struct file file[NFILE]; struct file file[NFILE];
@ -19,7 +19,7 @@ struct file file[NFILE];
void void
fileinit(void) fileinit(void)
{ {
initlock(&fd_table_lock, "fd_table"); initlock(&file_table_lock, "file_table");
} }
// Allocate a file structure // Allocate a file structure
@ -28,34 +28,34 @@ filealloc(void)
{ {
int i; int i;
acquire(&fd_table_lock); acquire(&file_table_lock);
for(i = 0; i < NFILE; i++){ for(i = 0; i < NFILE; i++){
if(file[i].type == FD_CLOSED){ if(file[i].type == FD_CLOSED){
file[i].type = FD_NONE; file[i].type = FD_NONE;
file[i].ref = 1; file[i].ref = 1;
release(&fd_table_lock); release(&file_table_lock);
return file + i; return file + i;
} }
} }
release(&fd_table_lock); release(&file_table_lock);
return 0; return 0;
} }
// Write to file f. Addr is kernel address. // Write to file f. Addr is kernel address.
int int
filewrite(struct file *fd, char *addr, int n) filewrite(struct file *f, char *addr, int n)
{ {
if(fd->writable == 0) if(f->writable == 0)
return -1; return -1;
if(fd->type == FD_PIPE){ if(f->type == FD_PIPE){
return pipe_write(fd->pipe, addr, n); return pipe_write(f->pipe, addr, n);
} else if(fd->type == FD_FILE) { } else if(f->type == FD_FILE) {
ilock(fd->ip); ilock(f->ip);
int r = writei(fd->ip, addr, fd->off, n); int r = writei(f->ip, addr, f->off, n);
if(r > 0) { if(r > 0) {
fd->off += r; f->off += r;
} }
iunlock(fd->ip); iunlock(f->ip);
return r; return r;
} else { } else {
panic("filewrite"); panic("filewrite");
@ -65,18 +65,18 @@ filewrite(struct file *fd, char *addr, int n)
// Read from file f. Addr is kernel address. // Read from file f. Addr is kernel address.
int int
fileread(struct file *fd, char *addr, int n) fileread(struct file *f, char *addr, int n)
{ {
if(fd->readable == 0) if(f->readable == 0)
return -1; return -1;
if(fd->type == FD_PIPE){ if(f->type == FD_PIPE){
return pipe_read(fd->pipe, addr, n); return pipe_read(f->pipe, addr, n);
} else if(fd->type == FD_FILE){ } else if(f->type == FD_FILE){
ilock(fd->ip); ilock(f->ip);
int cc = readi(fd->ip, addr, fd->off, n); int cc = readi(f->ip, addr, f->off, n);
if(cc > 0) if(cc > 0)
fd->off += cc; f->off += cc;
iunlock(fd->ip); iunlock(f->ip);
return cc; return cc;
} else { } else {
panic("fileread"); panic("fileread");
@ -86,19 +86,19 @@ fileread(struct file *fd, char *addr, int n)
// Close file f. (Decrement ref count, close when reaches 0.) // Close file f. (Decrement ref count, close when reaches 0.)
void void
fileclose(struct file *fd) fileclose(struct file *f)
{ {
acquire(&fd_table_lock); acquire(&file_table_lock);
if(fd->ref < 1 || fd->type == FD_CLOSED) if(f->ref < 1 || f->type == FD_CLOSED)
panic("fileclose"); panic("fileclose");
if(--fd->ref == 0){ if(--f->ref == 0){
struct file dummy = *fd; struct file dummy = *f;
fd->ref = 0; f->ref = 0;
fd->type = FD_CLOSED; f->type = FD_CLOSED;
release(&fd_table_lock); release(&file_table_lock);
if(dummy.type == FD_PIPE){ if(dummy.type == FD_PIPE){
pipe_close(dummy.pipe, dummy.writable); pipe_close(dummy.pipe, dummy.writable);
@ -108,18 +108,18 @@ fileclose(struct file *fd)
panic("fileclose"); panic("fileclose");
} }
} else { } else {
release(&fd_table_lock); release(&file_table_lock);
} }
} }
// Get metadata about file f. // Get metadata about file f.
int int
filestat(struct file *fd, struct stat *st) filestat(struct file *f, struct stat *st)
{ {
if(fd->type == FD_FILE){ if(f->type == FD_FILE){
ilock(fd->ip); ilock(f->ip);
stati(fd->ip, st); stati(f->ip, st);
iunlock(fd->ip); iunlock(f->ip);
return 0; return 0;
} else } else
return -1; return -1;
@ -127,11 +127,11 @@ filestat(struct file *fd, struct stat *st)
// Increment ref count for file f. // Increment ref count for file f.
void void
fileincref(struct file *fd) fileincref(struct file *f)
{ {
acquire(&fd_table_lock); acquire(&file_table_lock);
if(fd->ref < 1 || fd->type == FD_CLOSED) if(f->ref < 1 || f->type == FD_CLOSED)
panic("fileincref"); panic("fileincref");
fd->ref++; f->ref++;
release(&fd_table_lock); release(&file_table_lock);
} }

36
pipe.c
View file

@ -19,14 +19,14 @@ struct pipe {
}; };
int int
pipe_alloc(struct file **fd1, struct file **fd2) pipe_alloc(struct file **f0, struct file **f1)
{ {
*fd1 = *fd2 = 0; *f0 = *f1 = 0;
struct pipe *p = 0; struct pipe *p = 0;
if((*fd1 = filealloc()) == 0) if((*f0 = filealloc()) == 0)
goto oops; goto oops;
if((*fd2 = filealloc()) == 0) if((*f1 = filealloc()) == 0)
goto oops; goto oops;
if((p = (struct pipe*) kalloc(PAGE)) == 0) if((p = (struct pipe*) kalloc(PAGE)) == 0)
goto oops; goto oops;
@ -35,25 +35,25 @@ pipe_alloc(struct file **fd1, struct file **fd2)
p->writep = 0; p->writep = 0;
p->readp = 0; p->readp = 0;
initlock(&p->lock, "pipe"); initlock(&p->lock, "pipe");
(*fd1)->type = FD_PIPE; (*f0)->type = FD_PIPE;
(*fd1)->readable = 1; (*f0)->readable = 1;
(*fd1)->writable = 0; (*f0)->writable = 0;
(*fd1)->pipe = p; (*f0)->pipe = p;
(*fd2)->type = FD_PIPE; (*f1)->type = FD_PIPE;
(*fd2)->readable = 0; (*f1)->readable = 0;
(*fd2)->writable = 1; (*f1)->writable = 1;
(*fd2)->pipe = p; (*f1)->pipe = p;
return 0; return 0;
oops: oops:
if(p) if(p)
kfree((char*) p, PAGE); kfree((char*) p, PAGE);
if(*fd1){ if(*f0){
(*fd1)->type = FD_NONE; (*f0)->type = FD_NONE;
fileclose(*fd1); fileclose(*f0);
} }
if(*fd2){ if(*f1){
(*fd2)->type = FD_NONE; (*f1)->type = FD_NONE;
fileclose(*fd2); fileclose(*f1);
} }
return -1; return -1;
} }