fd_* => file_*

This commit is contained in:
rsc 2006-09-06 18:43:45 +00:00
parent 9936bffa45
commit 50e514be98
6 changed files with 41 additions and 41 deletions

16
defs.h
View file

@ -92,14 +92,14 @@ int pipe_read(struct pipe*, char*, int);
// fd.c
struct stat;
void fd_init(void);
int fd_ualloc(void);
struct file* fd_alloc(void);
void fd_close(struct file*);
int fd_read(struct file*, char*, int n);
int fd_write(struct file*, char*, int n);
int fd_stat(struct file*, struct stat*);
void fd_incref(struct file*);
void fileinit(void);
int fdalloc(void);
struct file* filealloc(void);
void fileclose(struct file*);
int fileread(struct file*, char*, int n);
int filewrite(struct file*, char*, int n);
int filestat(struct file*, struct stat*);
void fileincref(struct file*);
// ide.c
void ide_init(void);

26
file.c
View file

@ -17,14 +17,14 @@ struct devsw devsw[NDEV];
struct file file[NFILE];
void
fd_init(void)
fileinit(void)
{
initlock(&fd_table_lock, "fd_table");
}
// Allocate a file descriptor number for curproc.
int
fd_ualloc(void)
fdalloc(void)
{
int fd;
struct proc *p = curproc[cpu()];
@ -36,7 +36,7 @@ fd_ualloc(void)
// Allocate a file descriptor structure
struct file*
fd_alloc(void)
filealloc(void)
{
int i;
@ -56,7 +56,7 @@ fd_alloc(void)
// Write to file descriptor;
// addr is a kernel address, pointing into some process's p->mem.
int
fd_write(struct file *fd, char *addr, int n)
filewrite(struct file *fd, char *addr, int n)
{
if(fd->writable == 0)
return -1;
@ -71,14 +71,14 @@ fd_write(struct file *fd, char *addr, int n)
iunlock(fd->ip);
return r;
} else {
panic("fd_write");
panic("filewrite");
return -1;
}
}
// Read from file descriptor.
int
fd_read(struct file *fd, char *addr, int n)
fileread(struct file *fd, char *addr, int n)
{
if(fd->readable == 0)
return -1;
@ -92,19 +92,19 @@ fd_read(struct file *fd, char *addr, int n)
iunlock(fd->ip);
return cc;
} else {
panic("fd_read");
panic("fileread");
return -1;
}
}
// Close file descriptor.
void
fd_close(struct file *fd)
fileclose(struct file *fd)
{
acquire(&fd_table_lock);
if(fd->ref < 1 || fd->type == FD_CLOSED)
panic("fd_close");
panic("fileclose");
if(--fd->ref == 0){
struct file dummy = *fd;
@ -118,7 +118,7 @@ fd_close(struct file *fd)
} else if(dummy.type == FD_FILE){
idecref(dummy.ip);
} else {
panic("fd_close");
panic("fileclose");
}
} else {
release(&fd_table_lock);
@ -127,7 +127,7 @@ fd_close(struct file *fd)
// Get metadata about file descriptor.
int
fd_stat(struct file *fd, struct stat *st)
filestat(struct file *fd, struct stat *st)
{
if(fd->type == FD_FILE){
ilock(fd->ip);
@ -140,11 +140,11 @@ fd_stat(struct file *fd, struct stat *st)
// Increment file descriptor reference count.
void
fd_incref(struct file *fd)
fileincref(struct file *fd)
{
acquire(&fd_table_lock);
if(fd->ref < 1 || fd->type == FD_CLOSED)
panic("fd_incref");
panic("fileincref");
fd->ref++;
release(&fd_table_lock);
}

2
main.c
View file

@ -49,7 +49,7 @@ main0(void)
kinit(); // physical memory allocator
tvinit(); // trap vectors
idtinit(); // this CPU's interrupt descriptor table
fd_init();
fileinit();
iinit(); // i-node table
// initialize process 0

8
pipe.c
View file

@ -24,9 +24,9 @@ pipe_alloc(struct file **fd1, struct file **fd2)
*fd1 = *fd2 = 0;
struct pipe *p = 0;
if((*fd1 = fd_alloc()) == 0)
if((*fd1 = filealloc()) == 0)
goto oops;
if((*fd2 = fd_alloc()) == 0)
if((*fd2 = filealloc()) == 0)
goto oops;
if((p = (struct pipe*) kalloc(PAGE)) == 0)
goto oops;
@ -49,11 +49,11 @@ pipe_alloc(struct file **fd1, struct file **fd2)
kfree((char*) p, PAGE);
if(*fd1){
(*fd1)->type = FD_NONE;
fd_close(*fd1);
fileclose(*fd1);
}
if(*fd2){
(*fd2)->type = FD_NONE;
fd_close(*fd2);
fileclose(*fd2);
}
return -1;
}

4
proc.c
View file

@ -127,7 +127,7 @@ copyproc(struct proc *p)
for(i = 0; i < NOFILE; i++){
np->ofile[i] = p->ofile[i];
if(np->ofile[i])
fd_incref(np->ofile[i]);
fileincref(np->ofile[i]);
}
np->cwd = p->cwd;
@ -329,7 +329,7 @@ proc_exit(void)
// Close all open files.
for(fd = 0; fd < NOFILE; fd++){
if(cp->ofile[fd]){
fd_close(cp->ofile[fd]);
fileclose(cp->ofile[fd]);
cp->ofile[fd] = 0;
}
}

View file

@ -25,10 +25,10 @@ sys_pipe(void)
if(pipe_alloc(&rfd, &wfd) < 0)
goto oops;
if((f1 = fd_ualloc()) < 0)
if((f1 = fdalloc()) < 0)
goto oops;
p->ofile[f1] = rfd;
if((f2 = fd_ualloc()) < 0)
if((f2 = fdalloc()) < 0)
goto oops;
p->ofile[f2] = wfd;
if(fetcharg(0, &fdp) < 0)
@ -41,9 +41,9 @@ sys_pipe(void)
oops:
if(rfd)
fd_close(rfd);
fileclose(rfd);
if(wfd)
fd_close(wfd);
fileclose(wfd);
if(f1 >= 0)
p->ofile[f1] = 0;
if(f2 >= 0)
@ -67,7 +67,7 @@ sys_write(void)
if(addr + n > p->sz)
return -1;
ret = fd_write(p->ofile[fd], p->mem + addr, n);
ret = filewrite(p->ofile[fd], p->mem + addr, n);
return ret;
}
@ -86,7 +86,7 @@ sys_read(void)
return -1;
if(addr + n > p->sz)
return -1;
ret = fd_read(p->ofile[fd], p->mem + addr, n);
ret = fileread(p->ofile[fd], p->mem + addr, n);
return ret;
}
@ -102,7 +102,7 @@ sys_close(void)
return -1;
if(p->ofile[fd] == 0)
return -1;
fd_close(p->ofile[fd]);
fileclose(p->ofile[fd]);
p->ofile[fd] = 0;
return 0;
}
@ -146,13 +146,13 @@ sys_open(void)
return -1;
}
if((fd = fd_alloc()) == 0){
if((fd = filealloc()) == 0){
iput(ip);
return -1;
}
if((ufd = fd_ualloc()) < 0){
if((ufd = fdalloc()) < 0){
iput(ip);
fd_close(fd);
fileclose(fd);
return -1;
}
@ -310,7 +310,7 @@ sys_fstat(void)
return -1;
if(addr + sizeof(struct stat) > cp->sz)
return -1;
r = fd_stat(cp->ofile[fd], (struct stat*)(cp->mem + addr));
r = filestat(cp->ofile[fd], (struct stat*)(cp->mem + addr));
return r;
}
@ -326,10 +326,10 @@ sys_dup(void)
return -1;
if(cp->ofile[fd] == 0)
return -1;
if((ufd1 = fd_ualloc()) < 0)
if((ufd1 = fdalloc()) < 0)
return -1;
cp->ofile[ufd1] = cp->ofile[fd];
fd_incref(cp->ofile[ufd1]);
fileincref(cp->ofile[ufd1]);
return ufd1;
}