shithub: gefs

Download patch

ref: 91dbb8d0b0f3b77513bc33334465af796c40aa1e
parent: 5b2fa83efcd77eb4c9c1083da0df5f78ab9a76c2
author: Michael Forney <mforney@mforney.org>
date: Thu Feb 3 05:54:12 EST 2022

fs: fix write to unallocated block within file range

This case occurs with Twrite offset >= 8192 followed by Twrite
offset < 8192.

--- a/fs.c
+++ b/fs.c
@@ -284,7 +284,7 @@
 static int
 writeb(Fid *f, Msg *m, Bptr *ret, char *s, vlong o, vlong n, vlong sz)
 {
-	char buf[Kvmax];
+	char *e, buf[Kvmax];
 	vlong fb, fo;
 	Blk *b, *t;
 	Bptr bp;
@@ -303,14 +303,18 @@
 		return -1;
 	t = nil;
 	if(fb < sz && (fo != 0 || n != Blksz)){
-		if(lookup(f, m, &kv, buf, sizeof(buf), 0) != nil)
+		e = lookup(f, m, &kv, buf, sizeof(buf), 0);
+		if(e == nil){
+			bp = unpackbp(kv.v, kv.nv);
+			if((t = getblk(bp, GBraw)) == nil)
+				return -1;
+			memcpy(b->buf, t->buf, Blksz);
+			freeblk(f->mnt->root, t);
+			putblk(t);
+		}else if(e != Eexist){
+			werrstr("%s", e);
 			return -1;
-		bp = unpackbp(kv.v, kv.nv);
-		if((t = getblk(bp, GBraw)) == nil)
-			return -1;
-		memcpy(b->buf, t->buf, Blksz);
-		freeblk(f->mnt->root, t);
-		putblk(t);
+		}
 	}
 	if(fo+n > Blksz)
 		n = Blksz-fo;