fd_* => file_*
This commit is contained in:
parent
9936bffa45
commit
50e514be98
6 changed files with 41 additions and 41 deletions
16
defs.h
16
defs.h
|
@ -92,14 +92,14 @@ int pipe_read(struct pipe*, char*, int);
|
||||||
|
|
||||||
// fd.c
|
// fd.c
|
||||||
struct stat;
|
struct stat;
|
||||||
void fd_init(void);
|
void fileinit(void);
|
||||||
int fd_ualloc(void);
|
int fdalloc(void);
|
||||||
struct file* fd_alloc(void);
|
struct file* filealloc(void);
|
||||||
void fd_close(struct file*);
|
void fileclose(struct file*);
|
||||||
int fd_read(struct file*, char*, int n);
|
int fileread(struct file*, char*, int n);
|
||||||
int fd_write(struct file*, char*, int n);
|
int filewrite(struct file*, char*, int n);
|
||||||
int fd_stat(struct file*, struct stat*);
|
int filestat(struct file*, struct stat*);
|
||||||
void fd_incref(struct file*);
|
void fileincref(struct file*);
|
||||||
|
|
||||||
// ide.c
|
// ide.c
|
||||||
void ide_init(void);
|
void ide_init(void);
|
||||||
|
|
26
file.c
26
file.c
|
@ -17,14 +17,14 @@ struct devsw devsw[NDEV];
|
||||||
struct file file[NFILE];
|
struct file file[NFILE];
|
||||||
|
|
||||||
void
|
void
|
||||||
fd_init(void)
|
fileinit(void)
|
||||||
{
|
{
|
||||||
initlock(&fd_table_lock, "fd_table");
|
initlock(&fd_table_lock, "fd_table");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate a file descriptor number for curproc.
|
// Allocate a file descriptor number for curproc.
|
||||||
int
|
int
|
||||||
fd_ualloc(void)
|
fdalloc(void)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
struct proc *p = curproc[cpu()];
|
struct proc *p = curproc[cpu()];
|
||||||
|
@ -36,7 +36,7 @@ fd_ualloc(void)
|
||||||
|
|
||||||
// Allocate a file descriptor structure
|
// Allocate a file descriptor structure
|
||||||
struct file*
|
struct file*
|
||||||
fd_alloc(void)
|
filealloc(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ fd_alloc(void)
|
||||||
// Write to file descriptor;
|
// Write to file descriptor;
|
||||||
// addr is a kernel address, pointing into some process's p->mem.
|
// addr is a kernel address, pointing into some process's p->mem.
|
||||||
int
|
int
|
||||||
fd_write(struct file *fd, char *addr, int n)
|
filewrite(struct file *fd, char *addr, int n)
|
||||||
{
|
{
|
||||||
if(fd->writable == 0)
|
if(fd->writable == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -71,14 +71,14 @@ fd_write(struct file *fd, char *addr, int n)
|
||||||
iunlock(fd->ip);
|
iunlock(fd->ip);
|
||||||
return r;
|
return r;
|
||||||
} else {
|
} else {
|
||||||
panic("fd_write");
|
panic("filewrite");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read from file descriptor.
|
// Read from file descriptor.
|
||||||
int
|
int
|
||||||
fd_read(struct file *fd, char *addr, int n)
|
fileread(struct file *fd, char *addr, int n)
|
||||||
{
|
{
|
||||||
if(fd->readable == 0)
|
if(fd->readable == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -92,19 +92,19 @@ fd_read(struct file *fd, char *addr, int n)
|
||||||
iunlock(fd->ip);
|
iunlock(fd->ip);
|
||||||
return cc;
|
return cc;
|
||||||
} else {
|
} else {
|
||||||
panic("fd_read");
|
panic("fileread");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close file descriptor.
|
// Close file descriptor.
|
||||||
void
|
void
|
||||||
fd_close(struct file *fd)
|
fileclose(struct file *fd)
|
||||||
{
|
{
|
||||||
acquire(&fd_table_lock);
|
acquire(&fd_table_lock);
|
||||||
|
|
||||||
if(fd->ref < 1 || fd->type == FD_CLOSED)
|
if(fd->ref < 1 || fd->type == FD_CLOSED)
|
||||||
panic("fd_close");
|
panic("fileclose");
|
||||||
|
|
||||||
if(--fd->ref == 0){
|
if(--fd->ref == 0){
|
||||||
struct file dummy = *fd;
|
struct file dummy = *fd;
|
||||||
|
@ -118,7 +118,7 @@ fd_close(struct file *fd)
|
||||||
} else if(dummy.type == FD_FILE){
|
} else if(dummy.type == FD_FILE){
|
||||||
idecref(dummy.ip);
|
idecref(dummy.ip);
|
||||||
} else {
|
} else {
|
||||||
panic("fd_close");
|
panic("fileclose");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
release(&fd_table_lock);
|
release(&fd_table_lock);
|
||||||
|
@ -127,7 +127,7 @@ fd_close(struct file *fd)
|
||||||
|
|
||||||
// Get metadata about file descriptor.
|
// Get metadata about file descriptor.
|
||||||
int
|
int
|
||||||
fd_stat(struct file *fd, struct stat *st)
|
filestat(struct file *fd, struct stat *st)
|
||||||
{
|
{
|
||||||
if(fd->type == FD_FILE){
|
if(fd->type == FD_FILE){
|
||||||
ilock(fd->ip);
|
ilock(fd->ip);
|
||||||
|
@ -140,11 +140,11 @@ fd_stat(struct file *fd, struct stat *st)
|
||||||
|
|
||||||
// Increment file descriptor reference count.
|
// Increment file descriptor reference count.
|
||||||
void
|
void
|
||||||
fd_incref(struct file *fd)
|
fileincref(struct file *fd)
|
||||||
{
|
{
|
||||||
acquire(&fd_table_lock);
|
acquire(&fd_table_lock);
|
||||||
if(fd->ref < 1 || fd->type == FD_CLOSED)
|
if(fd->ref < 1 || fd->type == FD_CLOSED)
|
||||||
panic("fd_incref");
|
panic("fileincref");
|
||||||
fd->ref++;
|
fd->ref++;
|
||||||
release(&fd_table_lock);
|
release(&fd_table_lock);
|
||||||
}
|
}
|
||||||
|
|
2
main.c
2
main.c
|
@ -49,7 +49,7 @@ main0(void)
|
||||||
kinit(); // physical memory allocator
|
kinit(); // physical memory allocator
|
||||||
tvinit(); // trap vectors
|
tvinit(); // trap vectors
|
||||||
idtinit(); // this CPU's interrupt descriptor table
|
idtinit(); // this CPU's interrupt descriptor table
|
||||||
fd_init();
|
fileinit();
|
||||||
iinit(); // i-node table
|
iinit(); // i-node table
|
||||||
|
|
||||||
// initialize process 0
|
// initialize process 0
|
||||||
|
|
8
pipe.c
8
pipe.c
|
@ -24,9 +24,9 @@ pipe_alloc(struct file **fd1, struct file **fd2)
|
||||||
*fd1 = *fd2 = 0;
|
*fd1 = *fd2 = 0;
|
||||||
struct pipe *p = 0;
|
struct pipe *p = 0;
|
||||||
|
|
||||||
if((*fd1 = fd_alloc()) == 0)
|
if((*fd1 = filealloc()) == 0)
|
||||||
goto oops;
|
goto oops;
|
||||||
if((*fd2 = fd_alloc()) == 0)
|
if((*fd2 = filealloc()) == 0)
|
||||||
goto oops;
|
goto oops;
|
||||||
if((p = (struct pipe*) kalloc(PAGE)) == 0)
|
if((p = (struct pipe*) kalloc(PAGE)) == 0)
|
||||||
goto oops;
|
goto oops;
|
||||||
|
@ -49,11 +49,11 @@ pipe_alloc(struct file **fd1, struct file **fd2)
|
||||||
kfree((char*) p, PAGE);
|
kfree((char*) p, PAGE);
|
||||||
if(*fd1){
|
if(*fd1){
|
||||||
(*fd1)->type = FD_NONE;
|
(*fd1)->type = FD_NONE;
|
||||||
fd_close(*fd1);
|
fileclose(*fd1);
|
||||||
}
|
}
|
||||||
if(*fd2){
|
if(*fd2){
|
||||||
(*fd2)->type = FD_NONE;
|
(*fd2)->type = FD_NONE;
|
||||||
fd_close(*fd2);
|
fileclose(*fd2);
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
4
proc.c
4
proc.c
|
@ -127,7 +127,7 @@ copyproc(struct proc *p)
|
||||||
for(i = 0; i < NOFILE; i++){
|
for(i = 0; i < NOFILE; i++){
|
||||||
np->ofile[i] = p->ofile[i];
|
np->ofile[i] = p->ofile[i];
|
||||||
if(np->ofile[i])
|
if(np->ofile[i])
|
||||||
fd_incref(np->ofile[i]);
|
fileincref(np->ofile[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
np->cwd = p->cwd;
|
np->cwd = p->cwd;
|
||||||
|
@ -329,7 +329,7 @@ proc_exit(void)
|
||||||
// Close all open files.
|
// Close all open files.
|
||||||
for(fd = 0; fd < NOFILE; fd++){
|
for(fd = 0; fd < NOFILE; fd++){
|
||||||
if(cp->ofile[fd]){
|
if(cp->ofile[fd]){
|
||||||
fd_close(cp->ofile[fd]);
|
fileclose(cp->ofile[fd]);
|
||||||
cp->ofile[fd] = 0;
|
cp->ofile[fd] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
26
sysfile.c
26
sysfile.c
|
@ -25,10 +25,10 @@ sys_pipe(void)
|
||||||
|
|
||||||
if(pipe_alloc(&rfd, &wfd) < 0)
|
if(pipe_alloc(&rfd, &wfd) < 0)
|
||||||
goto oops;
|
goto oops;
|
||||||
if((f1 = fd_ualloc()) < 0)
|
if((f1 = fdalloc()) < 0)
|
||||||
goto oops;
|
goto oops;
|
||||||
p->ofile[f1] = rfd;
|
p->ofile[f1] = rfd;
|
||||||
if((f2 = fd_ualloc()) < 0)
|
if((f2 = fdalloc()) < 0)
|
||||||
goto oops;
|
goto oops;
|
||||||
p->ofile[f2] = wfd;
|
p->ofile[f2] = wfd;
|
||||||
if(fetcharg(0, &fdp) < 0)
|
if(fetcharg(0, &fdp) < 0)
|
||||||
|
@ -41,9 +41,9 @@ sys_pipe(void)
|
||||||
|
|
||||||
oops:
|
oops:
|
||||||
if(rfd)
|
if(rfd)
|
||||||
fd_close(rfd);
|
fileclose(rfd);
|
||||||
if(wfd)
|
if(wfd)
|
||||||
fd_close(wfd);
|
fileclose(wfd);
|
||||||
if(f1 >= 0)
|
if(f1 >= 0)
|
||||||
p->ofile[f1] = 0;
|
p->ofile[f1] = 0;
|
||||||
if(f2 >= 0)
|
if(f2 >= 0)
|
||||||
|
@ -67,7 +67,7 @@ sys_write(void)
|
||||||
if(addr + n > p->sz)
|
if(addr + n > p->sz)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
ret = fd_write(p->ofile[fd], p->mem + addr, n);
|
ret = filewrite(p->ofile[fd], p->mem + addr, n);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +86,7 @@ sys_read(void)
|
||||||
return -1;
|
return -1;
|
||||||
if(addr + n > p->sz)
|
if(addr + n > p->sz)
|
||||||
return -1;
|
return -1;
|
||||||
ret = fd_read(p->ofile[fd], p->mem + addr, n);
|
ret = fileread(p->ofile[fd], p->mem + addr, n);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ sys_close(void)
|
||||||
return -1;
|
return -1;
|
||||||
if(p->ofile[fd] == 0)
|
if(p->ofile[fd] == 0)
|
||||||
return -1;
|
return -1;
|
||||||
fd_close(p->ofile[fd]);
|
fileclose(p->ofile[fd]);
|
||||||
p->ofile[fd] = 0;
|
p->ofile[fd] = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -146,13 +146,13 @@ sys_open(void)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((fd = fd_alloc()) == 0){
|
if((fd = filealloc()) == 0){
|
||||||
iput(ip);
|
iput(ip);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if((ufd = fd_ualloc()) < 0){
|
if((ufd = fdalloc()) < 0){
|
||||||
iput(ip);
|
iput(ip);
|
||||||
fd_close(fd);
|
fileclose(fd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -310,7 +310,7 @@ sys_fstat(void)
|
||||||
return -1;
|
return -1;
|
||||||
if(addr + sizeof(struct stat) > cp->sz)
|
if(addr + sizeof(struct stat) > cp->sz)
|
||||||
return -1;
|
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;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,10 +326,10 @@ sys_dup(void)
|
||||||
return -1;
|
return -1;
|
||||||
if(cp->ofile[fd] == 0)
|
if(cp->ofile[fd] == 0)
|
||||||
return -1;
|
return -1;
|
||||||
if((ufd1 = fd_ualloc()) < 0)
|
if((ufd1 = fdalloc()) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
cp->ofile[ufd1] = cp->ofile[fd];
|
cp->ofile[ufd1] = cp->ofile[fd];
|
||||||
fd_incref(cp->ofile[ufd1]);
|
fileincref(cp->ofile[ufd1]);
|
||||||
return ufd1;
|
return ufd1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue