shithub: gefs

Download patch

ref: 17b3e44bd031c2d1fbbab32ced6ef91190891402
parent: 51527a131e0f5e287cfd752a3d87f2acf3fbe042
author: Ori Bernstein <ori@eigenstate.org>
date: Sun May 7 12:52:27 EDT 2023

fs: create should error if the file exists

also, adjust error names

--- a/dat.h
+++ b/dat.h
@@ -125,6 +125,7 @@
 extern char Eimpl[];
 extern char Ebotch[];
 extern char Eio[];
+extern char Enofid[];
 extern char Efid[];
 extern char Etype[];
 extern char Edscan[];
--- a/error.c
+++ b/error.c
@@ -7,11 +7,12 @@
 char Eimpl[]	= "not implemented";
 char Ebotch[]	= "protocol botch";
 char Eio[]	= "i/o error";
-char Efid[]	= "unknown fid";
+char Enofid[]	= "unknown fid";
+char Efid[]	= "fid in use";
 char Etype[]	= "invalid fid type";
 char Edscan[]	= "invalid dir scan offset";
-char Eexist[]	= "directory entry not found";
-char Emode[]	= "unknown mode";
+char Eexist[]	= "create/wstat -- file exists";
+char Emode[]	= "open/create -- unknown mode";
 char Efull[]	= "file system full";
 char Eauth[]	= "authentication failed";
 char Elength[]	= "name too long";
@@ -54,11 +55,9 @@
 //char Eqid[]		= "phase error -- qid does not match";
 //char Eaccess[]	= "access permission denied";
 //char Eentry[]		= "directory entry not found";
-//char Emode[]		= "open/create -- unknown mode";
 //char Edir1[]		= "walk -- in a non-directory";
 //char Edir2[]		= "create -- in a non-directory";
 //char Ephase[]		= "phase error -- cannot happen";
-//char Eexist[]		= "create/wstat -- file exists";
 //char Edot[]		= "create/wstat -- . and .. illegal names";
 //char Ewalk[]		= "walk -- too many (system wide)";
 //char Eoffset[]	= "read/write -- offset negative";
--- a/fs.c
+++ b/fs.c
@@ -761,7 +761,7 @@
 	f.dmode = 0600;
 	f.auth = authnew();
 	if(dupfid(m->conn, m->afid, &f) == nil){
-		rerror(m, Enomem);
+		rerror(m, Efid);
 		free(de);
 		return;
 	}
@@ -892,7 +892,7 @@
 
 	if(m->afid != NOFID){
 		if((af = getfid(m->conn, m->afid)) == nil){
-			rerror(m, Efid);
+			rerror(m, Enofid);
 			goto Out;
 		}
 		if(af->uid != uid){
@@ -943,7 +943,7 @@
 	f.dgid = d.gid;
 	f.dmode = d.mode;
 	if(dupfid(m->conn, m->fid, &f) == nil){
-		rerror(m, Enomem);
+		rerror(m, Efid);
 		goto Out;
 	}
 
@@ -990,7 +990,7 @@
 	int i;
 
 	if((o = getfid(m->conn, m->fid)) == nil){
-		rerror(m, Efid);
+		rerror(m, Enofid);
 		return;
 	}
 	if(o->mode != -1){
@@ -1072,7 +1072,7 @@
 	f = o;
 	if(m->fid != m->newfid && i == m->nwname){
 		if((f = dupfid(m->conn, m->newfid, o)) == nil){
-			rerror(m, Enomem);
+			rerror(m, Efid);
 			putfid(o);
 			return;
 		}
@@ -1118,7 +1118,7 @@
 	int n;
 
 	if((f = getfid(m->conn, m->fid)) == nil){
-		rerror(m, Efid);
+		rerror(m, Enofid);
 		return;
 	}
 	rlock(f->dent);
@@ -1153,7 +1153,7 @@
 
 	rename = 0;
 	if((f = getfid(m->conn, m->fid)) == nil){
-		rerror(m, Efid);
+		rerror(m, Enofid);
 		return;
 	}
 	de = f->dent;
@@ -1365,7 +1365,7 @@
 	Fid *f;
 
 	if((f = getfid(m->conn, m->fid)) == nil){
-		rerror(m, Efid);
+		rerror(m, Enofid);
 		return;
 	}
 	lock(f);
@@ -1392,7 +1392,7 @@
 	Msg mb[2];
 	Fid *f;
 	Xdir d;
-	int nm, clobber;
+	int nm;
 
 	if(okname(m->name) == -1){
 		rerror(m, Ename);
@@ -1399,7 +1399,7 @@
 		return;
 	}
 	if((f = getfid(m->conn, m->fid)) == nil){
-		rerror(m, Efid);
+		rerror(m, Enofid);
 		return;
 	}
 	if(m->perm & (DMMOUNT|DMAUTH)){
@@ -1409,17 +1409,20 @@
 	}
 	de = f->dent;
 
-	clobber = 0;
-	if(walk1(f->mnt->root, f->qpath, m->name, &old, &oldlen) == 0)
-		clobber = 1;
+	if(walk1(f->mnt->root, f->qpath, m->name, &old, &oldlen) == 0){
+		rerror(m, Emode);
+		putfid(f);
+		return;
+	}
 
 	rlock(de);
 	if(fsaccess(f, de->mode, de->uid, de->gid, DMWRITE) == -1){
 		runlock(de);
-		rerror(m, Eperm);
+		rerror(m, Eexist);
 		putfid(f);
 		return;
 	}
+
 	d.gid = de->gid;
 	runlock(de);
 
@@ -1472,26 +1475,15 @@
 		putfid(f);
 		return;
 	}
+
+	lock(f);
 	de = getdent(f->qpath, &d);
 	if(de == nil){
-		if(m->fid != m->newfid)
-			clunkfid(m->conn, f);
 		rerror(m, Enomem);
 		putfid(f);
 		return;
 	}
 
-	wlock(de);
-	if(clobber && (e = clearb(f->mnt, old.path, 0, oldlen)) != nil){
-		rerror(m, e);
-		wunlock(de);
-		putfid(f);
-		return;
-	}
-	de->length = 0;
-	wunlock(de);
-
-	lock(f);
 	if(f->mode != -1){
 		unlock(f);
 		clunkdent(de);
@@ -1543,7 +1535,7 @@
 	char *e;
 
 	if((f = getfid(m->conn, m->fid)) == nil){
-		rerror(m, Efid);
+		rerror(m, Enofid);
 		return;
 	}
 	clunkfid(m->conn, f);
@@ -1604,7 +1596,7 @@
 
 	mbits = mode2bits(m->mode);
 	if((f = getfid(m->conn, m->fid)) == nil){
-		rerror(m, Efid);
+		rerror(m, Enofid);
 		return;
 	}
 
@@ -1887,7 +1879,7 @@
 	Fid *f;
 
 	if((f = getfid(m->conn, m->fid)) == nil){
-		rerror(m, Efid);
+		rerror(m, Enofid);
 		return;
 	}
 	r.type = Rread;
@@ -1942,7 +1934,7 @@
 	Fid *f;
 
 	if((f = getfid(m->conn, m->fid)) == nil){
-		rerror(m, Efid);
+		rerror(m, Enofid);
 		return;
 	}
 	if(!(f->mode & DMWRITE)){