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

36
pipe.c
View file

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