Skip to content

Commit

Permalink
stop using fd to name files
Browse files Browse the repository at this point in the history
  • Loading branch information
rsc committed Sep 8, 2006
1 parent 5692823 commit 2cbb4b1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 60 deletions.
84 changes: 42 additions & 42 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
#include "fs.h"
#include "fsvar.h"

struct spinlock fd_table_lock;
struct spinlock file_table_lock;
struct devsw devsw[NDEV];

struct file file[NFILE];

void
fileinit(void)
{
initlock(&fd_table_lock, "fd_table");
initlock(&file_table_lock, "file_table");
}

// Allocate a file structure
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -108,30 +108,30 @@ 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;
}

// 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 changes: 18 additions & 18 deletions pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 2cbb4b1

Please sign in to comment.