]> Devi Nivas Git - cs3210-lab1.git/commitdiff
make mkdir crash-safer, as noticed by many students on midterm
authorkolya <kolya>
Thu, 16 Oct 2008 15:18:49 +0000 (15:18 +0000)
committerkolya <kolya>
Thu, 16 Oct 2008 15:18:49 +0000 (15:18 +0000)
sysfile.c

index a23b48acf9f412abdc05147868cfcdd6a8e67bd8..ce812f516352793245d83f169c1cc3b3966a2b0d 100644 (file)
--- a/sysfile.c
+++ b/sysfile.c
@@ -237,13 +237,6 @@ create(char *path, int canexist, short type, short major, short minor)
   ip->minor = minor;
   ip->nlink = 1;
   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.
     dp->nlink++;  // for ".."
@@ -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)
       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);
   return ip;
 }