iread for T_DEV
O_RDWR, etc. create file
This commit is contained in:
parent
2601de0032
commit
939f9edeac
5 changed files with 41 additions and 12 deletions
8
fs.c
8
fs.c
|
@ -239,6 +239,12 @@ readi(struct inode *ip, void *xdst, uint off, uint n)
|
||||||
uint target = n, n1;
|
uint target = n, n1;
|
||||||
struct buf *bp;
|
struct buf *bp;
|
||||||
|
|
||||||
|
if (ip->type == T_DEV) {
|
||||||
|
if (ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].d_read)
|
||||||
|
return -1;
|
||||||
|
return devsw[ip->major].d_read (ip->minor, xdst, n);
|
||||||
|
}
|
||||||
|
|
||||||
while(n > 0 && off < ip->size){
|
while(n > 0 && off < ip->size){
|
||||||
bp = bread(ip->dev, bmap(ip, off / BSIZE));
|
bp = bread(ip->dev, bmap(ip, off / BSIZE));
|
||||||
n1 = min(n, ip->size - off);
|
n1 = min(n, ip->size - off);
|
||||||
|
@ -257,6 +263,8 @@ int
|
||||||
writei(struct inode *ip, void *addr, uint n)
|
writei(struct inode *ip, void *addr, uint n)
|
||||||
{
|
{
|
||||||
if (ip->type == T_DEV) {
|
if (ip->type == T_DEV) {
|
||||||
|
if (ip->major < 0 || ip->major >= NDEV || !devsw[ip->major].d_write)
|
||||||
|
return -1;
|
||||||
return devsw[ip->major].d_write (ip->minor, addr, n);
|
return devsw[ip->major].d_write (ip->minor, addr, n);
|
||||||
} else {
|
} else {
|
||||||
panic ("writei: unknown type\n");
|
panic ("writei: unknown type\n");
|
||||||
|
|
4
fs.h
4
fs.h
|
@ -37,3 +37,7 @@ struct dirent {
|
||||||
char name[DIRSIZ];
|
char name[DIRSIZ];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define O_CREATE 0x200
|
||||||
|
#define O_RDONLY 0x000
|
||||||
|
#define O_WRONLY 0x001
|
||||||
|
#define O_RDWR 0x002
|
||||||
|
|
2
ide.c
2
ide.c
|
@ -58,7 +58,7 @@ void
|
||||||
ide_intr(void)
|
ide_intr(void)
|
||||||
{
|
{
|
||||||
acquire(&ide_lock);
|
acquire(&ide_lock);
|
||||||
cprintf("cpu%d: ide_intr\n", cpu());
|
// cprintf("cpu%d: ide_intr\n", cpu());
|
||||||
wakeup(&request[tail]);
|
wakeup(&request[tail]);
|
||||||
release(&ide_lock);
|
release(&ide_lock);
|
||||||
lapic_eoi();
|
lapic_eoi();
|
||||||
|
|
29
syscall.c
29
syscall.c
|
@ -252,18 +252,28 @@ sys_open(void)
|
||||||
uint arg0, arg1;
|
uint arg0, arg1;
|
||||||
int ufd;
|
int ufd;
|
||||||
struct fd *fd;
|
struct fd *fd;
|
||||||
|
struct inode *dp;
|
||||||
|
int l;
|
||||||
|
|
||||||
if(fetcharg(0, &arg0) < 0 || fetcharg(1, &arg1) < 0)
|
if(fetcharg(0, &arg0) < 0 || fetcharg(1, &arg1) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
if(checkstring(arg0) < 0)
|
if((l = checkstring(arg0)) < 0)
|
||||||
return -1;
|
|
||||||
if((ip = namei(cp->mem + arg0)) == 0)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
if((ip = namei(cp->mem + arg0)) == 0) {
|
||||||
|
if (arg1 & O_CREATE) {
|
||||||
|
if (l >= DIRSIZ)
|
||||||
|
return -1;
|
||||||
|
dp = iget(rootdev, 1); // XXX should parse name
|
||||||
|
if (dp->type != T_DIR)
|
||||||
|
return -1;
|
||||||
|
if ((ip = mknod (dp, cp->mem + arg0, T_FILE, 0, 0)) == 0)
|
||||||
|
return -1;
|
||||||
|
} else return -1;
|
||||||
|
}
|
||||||
if((fd = fd_alloc()) == 0){
|
if((fd = fd_alloc()) == 0){
|
||||||
iput(ip);
|
iput(ip);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((ufd = fd_ualloc()) < 0){
|
if((ufd = fd_ualloc()) < 0){
|
||||||
iput(ip);
|
iput(ip);
|
||||||
fd_close(fd);
|
fd_close(fd);
|
||||||
|
@ -272,9 +282,12 @@ sys_open(void)
|
||||||
|
|
||||||
iunlock(ip);
|
iunlock(ip);
|
||||||
fd->type = FD_FILE;
|
fd->type = FD_FILE;
|
||||||
if (arg1) {
|
if (arg1 & O_RDWR) {
|
||||||
fd->readable = 1;
|
fd->readable = 1;
|
||||||
fd->writeable = 1;
|
fd->writeable = 1;
|
||||||
|
} else if (arg1 & O_WRONLY) {
|
||||||
|
fd->readable = 0;
|
||||||
|
fd->writeable = 1;
|
||||||
} else {
|
} else {
|
||||||
fd->readable = 1;
|
fd->readable = 1;
|
||||||
fd->writeable = 0;
|
fd->writeable = 0;
|
||||||
|
@ -304,13 +317,9 @@ sys_mknod(void)
|
||||||
if(l >= DIRSIZ)
|
if(l >= DIRSIZ)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
dp = iget(rootdev, 1);
|
dp = iget(rootdev, 1); // XXX should parse name
|
||||||
|
|
||||||
cprintf("root inode type: %d\n", dp->type);
|
|
||||||
|
|
||||||
if (dp->type != T_DIR)
|
if (dp->type != T_DIR)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
nip = mknod (dp, cp->mem + arg0, (short) arg1, (short) arg2,
|
nip = mknod (dp, cp->mem + arg0, (short) arg1, (short) arg2,
|
||||||
(short) arg3);
|
(short) arg3);
|
||||||
|
|
||||||
|
|
10
userfs.c
10
userfs.c
|
@ -20,7 +20,7 @@ main(void)
|
||||||
puts ("mknod failed\n");
|
puts ("mknod failed\n");
|
||||||
else
|
else
|
||||||
puts ("made a node\n");
|
puts ("made a node\n");
|
||||||
fd = open("console", 1);
|
fd = open("console", O_WRONLY);
|
||||||
if(fd >= 0){
|
if(fd >= 0){
|
||||||
puts("open console ok\n");
|
puts("open console ok\n");
|
||||||
} else {
|
} else {
|
||||||
|
@ -45,6 +45,14 @@ main(void)
|
||||||
} else {
|
} else {
|
||||||
puts("open doesnotexist failed\n");
|
puts("open doesnotexist failed\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fd = open("doesnotexist", O_CREATE|O_RDWR);
|
||||||
|
if(fd >= 0){
|
||||||
|
puts("creat doesnotexist succeeded\n");
|
||||||
|
} else {
|
||||||
|
puts("error: creat doesnotexist failed!\n");
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
//exec("echo", echo_args);
|
//exec("echo", echo_args);
|
||||||
exec("cat", cat_args);
|
exec("cat", cat_args);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue