-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
55 lines (45 loc) · 974 Bytes
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// implementation of file descriptor
#include "spinlock.h"
#include "common.h"
#include "param.h"
#include "file.h"
#include "defs.h"
struct {
struct spinlock lock;
struct file file[NFILE];
} ftable;
// Locate and return an empty slot in the file table
int filealloc(void)
{
struct file *fp;
struct inode *ip;
fp = &ftable.file;
while(fp->ref > 0 && fp < &ftable.file[NFILE])
fp++;
if (fp->ref > 0)
PANIC("fdalloc: cannot locate free file slot");
fp->ref = 1;
return fp;
}
int filewrite(struct file *f, char *addr, int n)
{
if (f->type = FD_INODE) {
}
PANIC("filewrite: writing to unknown type");
}
int fileread(struct file *f, char *addr, int n)
{
int r;
//kprintf("fileread: type: %d", f->type);
if(f->readable == 0)
return -1;
if(f->type == FD_PIPE)
PANIC("fileread: pipe?!");
if(f->type == FD_INODE){
ilock(f->ip);
if((r = readi(f->ip, addr, f->off, n)) > 0)
f->off += r;
return r;
}
PANIC("fileread");
}