Skip to content
Snippets Groups Projects
Commit 2186f88c authored by rsc's avatar rsc
Browse files

formatting, cleanup

parent cce27ba9
No related branches found
No related tags found
No related merge requests found
...@@ -41,75 +41,55 @@ filealloc(void) ...@@ -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)
panic("fileclose");
if(--f->ref == 0){ if(f->writable == 0)
struct file dummy = *f; return -1;
if(f->type == FD_PIPE)
f->ref = 0; return pipe_write(f->pipe, addr, n);
f->type = FD_CLOSED; if(f->type == FD_FILE){
release(&file_table_lock); ilock(f->ip);
if((r = writei(f->ip, addr, f->off, n)) > 0)
if(dummy.type == FD_PIPE){ f->off += r;
pipe_close(dummy.pipe, dummy.writable); iunlock(f->ip);
} else if(dummy.type == FD_FILE){ return r;
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) ...@@ -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);
return;
}
ff = *f;
f->ref = 0;
f->type = FD_CLOSED;
release(&file_table_lock); 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");
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment