make mkdir crash-safer, as noticed by many students on midterm
This commit is contained in:
parent
e1626709d6
commit
f3e87bc838
1 changed files with 11 additions and 7 deletions
18
sysfile.c
18
sysfile.c
|
@ -238,13 +238,6 @@ create(char *path, int canexist, short type, short major, short minor)
|
||||||
ip->nlink = 1;
|
ip->nlink = 1;
|
||||||
iupdate(ip);
|
iupdate(ip);
|
||||||
|
|
||||||
if(dirlink(dp, name, ip->inum) < 0){
|
|
||||||
ip->nlink = 0;
|
|
||||||
iunlockput(ip);
|
|
||||||
iunlockput(dp);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(type == T_DIR){ // Create . and .. entries.
|
if(type == T_DIR){ // Create . and .. entries.
|
||||||
dp->nlink++; // for ".."
|
dp->nlink++; // for ".."
|
||||||
iupdate(dp);
|
iupdate(dp);
|
||||||
|
@ -252,6 +245,17 @@ create(char *path, int canexist, short type, short major, short minor)
|
||||||
if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
|
if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
|
||||||
panic("create dots");
|
panic("create dots");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(dirlink(dp, name, ip->inum) < 0){
|
||||||
|
dp->nlink--;
|
||||||
|
iupdate(dp);
|
||||||
|
iunlockput(dp);
|
||||||
|
|
||||||
|
ip->nlink = 0;
|
||||||
|
iunlockput(ip);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
iunlockput(dp);
|
iunlockput(dp);
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue