formatting, cleanup

This commit is contained in:
rsc 2007-08-14 19:41:56 +00:00
parent cce27ba9fb
commit 2186f88c21

110
file.c
View file

@ -41,75 +41,55 @@ filealloc(void)
return 0; return 0;
} }
// Write to file f. Addr is kernel address. // Increment ref count for file f.
int void
filewrite(struct file *f, char *addr, int n) fileincref(struct file *f)
{ {
if(f->writable == 0) acquire(&file_table_lock);
return -1; if(f->ref < 1 || f->type == FD_CLOSED)
if(f->type == FD_PIPE){ panic("fileincref");
return pipe_write(f->pipe, addr, n); f->ref++;
} else if(f->type == FD_FILE) { release(&file_table_lock);
ilock(f->ip);
int r = writei(f->ip, addr, f->off, n);
if(r > 0) {
f->off += r;
}
iunlock(f->ip);
return r;
} else {
panic("filewrite");
return -1;
}
} }
// Read from file f. Addr is kernel address. // Read from file f. Addr is kernel address.
int int
fileread(struct file *f, char *addr, int n) fileread(struct file *f, char *addr, int n)
{ {
int r;
if(f->readable == 0) if(f->readable == 0)
return -1; return -1;
if(f->type == FD_PIPE){ if(f->type == FD_PIPE)
return pipe_read(f->pipe, addr, n); return pipe_read(f->pipe, addr, n);
} else if(f->type == FD_FILE){ if(f->type == FD_FILE){
ilock(f->ip); ilock(f->ip);
int cc = readi(f->ip, addr, f->off, n); if((r = readi(f->ip, addr, f->off, n)) > 0)
if(cc > 0) f->off += r;
f->off += cc;
iunlock(f->ip); iunlock(f->ip);
return cc; return r;
} else {
panic("fileread");
return -1;
} }
panic("fileread");
} }
// Close file f. (Decrement ref count, close when reaches 0.) // Write to file f. Addr is kernel address.
void int
fileclose(struct file *f) filewrite(struct file *f, char *addr, int n)
{ {
acquire(&file_table_lock); int r;
if(f->ref < 1 || f->type == FD_CLOSED) if(f->writable == 0)
panic("fileclose"); return -1;
if(f->type == FD_PIPE)
if(--f->ref == 0){ return pipe_write(f->pipe, addr, n);
struct file dummy = *f; if(f->type == FD_FILE){
ilock(f->ip);
f->ref = 0; if((r = writei(f->ip, addr, f->off, n)) > 0)
f->type = FD_CLOSED; f->off += r;
release(&file_table_lock); iunlock(f->ip);
return r;
if(dummy.type == FD_PIPE){
pipe_close(dummy.pipe, dummy.writable);
} else if(dummy.type == FD_FILE){
idecref(dummy.ip);
} else {
panic("fileclose");
}
} else {
release(&file_table_lock);
} }
panic("filewrite");
} }
// Get metadata about file f. // Get metadata about file f.
@ -121,17 +101,35 @@ filestat(struct file *f, struct stat *st)
stati(f->ip, st); stati(f->ip, st);
iunlock(f->ip); iunlock(f->ip);
return 0; return 0;
} else }
return -1; return -1;
} }
// Increment ref count for file f. // Close file f. (Decrement ref count, close when reaches 0.)
void void
fileincref(struct file *f) fileclose(struct file *f)
{ {
struct file ff;
acquire(&file_table_lock); acquire(&file_table_lock);
if(f->ref < 1 || f->type == FD_CLOSED) if(f->ref < 1 || f->type == FD_CLOSED)
panic("fileincref"); panic("fileclose");
f->ref++;
if(--f->ref > 0){
release(&file_table_lock); release(&file_table_lock);
return;
}
ff = *f;
f->ref = 0;
f->type = FD_CLOSED;
release(&file_table_lock);
if(ff.type == FD_PIPE)
pipe_close(ff.pipe, ff.writable);
else if(ff.type == FD_FILE)
idecref(ff.ip);
else
panic("fileclose");
} }