Skip to content

Commit f3685aa

Browse files
author
rsc
committed
simplify
1 parent 7f399cc commit f3685aa

File tree

3 files changed

+16
-29
lines changed

3 files changed

+16
-29
lines changed

file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
struct file {
2-
enum { FD_CLOSED, FD_NONE, FD_PIPE, FD_INODE } type;
2+
enum { FD_NONE, FD_PIPE, FD_INODE } type;
33
int ref; // reference count
44
char readable;
55
char writable;

pipe.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,10 @@ pipealloc(struct file **f0, struct file **f1)
4747
bad:
4848
if(p)
4949
kfree((char*)p, PAGE);
50-
if(*f0){
51-
(*f0)->type = FD_NONE;
50+
if(*f0)
5251
fileclose(*f0);
53-
}
54-
if(*f1){
55-
(*f1)->type = FD_NONE;
52+
if(*f1)
5653
fileclose(*f1);
57-
}
5854
return -1;
5955
}
6056

sysfile.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,17 @@ sys_link(void)
127127
iunlock(ip);
128128

129129
if((dp = nameiparent(new, name)) == 0)
130-
goto bad;
130+
goto bad;
131131
ilock(dp);
132-
if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0)
132+
if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){
133+
iunlockput(dp);
133134
goto bad;
135+
}
134136
iunlockput(dp);
135137
iput(ip);
136138
return 0;
137139

138140
bad:
139-
if(dp)
140-
iunlockput(dp);
141141
ilock(ip);
142142
ip->nlink--;
143143
iupdate(ip);
@@ -212,7 +212,7 @@ sys_unlink(void)
212212
}
213213

214214
static struct inode*
215-
create(char *path, int canexist, short type, short major, short minor)
215+
create(char *path, short type, short major, short minor)
216216
{
217217
uint off;
218218
struct inode *ip, *dp;
@@ -222,10 +222,10 @@ create(char *path, int canexist, short type, short major, short minor)
222222
return 0;
223223
ilock(dp);
224224

225-
if(canexist && (ip = dirlookup(dp, name, &off)) != 0){
225+
if((ip = dirlookup(dp, name, &off)) != 0){
226226
iunlockput(dp);
227227
ilock(ip);
228-
if(ip->type != type || ip->major != major || ip->minor != minor){
228+
if(ip->type != type || type != T_FILE){
229229
iunlockput(ip);
230230
return 0;
231231
}
@@ -250,17 +250,8 @@ create(char *path, int canexist, short type, short major, short minor)
250250
panic("create dots");
251251
}
252252

253-
if(dirlink(dp, name, ip->inum) < 0){
254-
if(type == T_DIR){
255-
dp->nlink--;
256-
iupdate(dp);
257-
}
258-
iunlockput(dp);
259-
260-
ip->nlink = 0;
261-
iunlockput(ip);
262-
return 0;
263-
}
253+
if(dirlink(dp, name, ip->inum) < 0)
254+
panic("create: dirlink");
264255

265256
iunlockput(dp);
266257
return ip;
@@ -278,13 +269,13 @@ sys_open(void)
278269
return -1;
279270

280271
if(omode & O_CREATE){
281-
if((ip = create(path, 1, T_FILE, 0, 0)) == 0)
272+
if((ip = create(path, T_FILE, 0, 0)) == 0)
282273
return -1;
283274
} else {
284275
if((ip = namei(path)) == 0)
285276
return -1;
286277
ilock(ip);
287-
if(ip->type == T_DIR && (omode & (O_RDWR|O_WRONLY))){
278+
if(ip->type == T_DIR && omode != O_RDONLY){
288279
iunlockput(ip);
289280
return -1;
290281
}
@@ -318,7 +309,7 @@ sys_mknod(void)
318309
if((len=argstr(0, &path)) < 0 ||
319310
argint(1, &major) < 0 ||
320311
argint(2, &minor) < 0 ||
321-
(ip = create(path, 0, T_DEV, major, minor)) == 0)
312+
(ip = create(path, T_DEV, major, minor)) == 0)
322313
return -1;
323314
iunlockput(ip);
324315
return 0;
@@ -330,7 +321,7 @@ sys_mkdir(void)
330321
char *path;
331322
struct inode *ip;
332323

333-
if(argstr(0, &path) < 0 || (ip = create(path, 0, T_DIR, 0, 0)) == 0)
324+
if(argstr(0, &path) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0)
334325
return -1;
335326
iunlockput(ip);
336327
return 0;

0 commit comments

Comments
 (0)