shithub: vcardfs

Download patch

ref: f4a258fdbe0f2b82de6de38f491e31a4bb1a4c3f
parent: ad8274ef26c101c9d03d8c4ca6376e3a90a67734
author: sirjofri <sirjofri@sirjofri.de>
date: Sat Oct 19 11:59:34 EDT 2024

adds support for adding lines

--- a/vcardfs.c
+++ b/vcardfs.c
@@ -442,6 +442,53 @@
 	return vp;
 }
 
+static File*
+addline(Req *r, Vfile *vf)
+{
+	File *dirf, *paramf, *dataf;
+	Vline *l, *nl;
+	
+	if (!vf->card) {
+		respond(r, "invalid card file");
+		return nil;
+	}
+	
+	dirf = createfile(r->fid->file, r->ifcall.name, user, DMDIR|0777, nil);
+	if (!dirf) {
+		responderror(r);
+		return nil;
+	}
+	nl = mallocz(sizeof(Vline), 1);
+	nl->name = estrdup(r->ifcall.name);
+	nl->value = estrdup("");
+	
+	if (!vf->card->content) {
+		vf->card->content = nl;
+	} else {
+		for (l = vf->card->content; l->next; l = l->next)
+			continue;
+		l->next = nl;
+	}
+	
+	dirf->aux = emkvfile(Qline, vf->card, nl, nil, vf->cardfile);
+	
+	paramf = createfile(dirf, "params", user, DMDIR|0777, nil);
+	if (!paramf) {
+		responderror(r);
+		return dirf;
+	}
+	paramf->aux = emkvfile(Qparams, vf->card, nl, nil, vf->cardfile);
+	
+	dataf = createfile(dirf, "data", user, 0666, nil);
+	if (!dataf) {
+		responderror(r);
+		return dirf;
+	}
+	dataf->aux = emkvfile(Qdata, vf->card, nl, nil, vf->cardfile);
+	respond(r, nil);
+	return dirf;
+}
+
 static void
 fscreate(Req *r)
 {
@@ -453,8 +500,12 @@
 	USED(nf);
 	f = r->fid->file;
 	vf = f->aux;
-	if (r->ifcall.perm&DMDIR)
-		goto Nil;
+	if (r->ifcall.perm&DMDIR) {
+		if (vf->level != Qcard)
+			goto Nil;
+		nf = addline(r, vf);
+		goto Next;
+	}
 	switch (vf->level) {
 	case Qline:
 		if (strcmp(r->ifcall.name, "group") != 0)
@@ -480,16 +531,20 @@
 		goto Nil;
 	}
 	
-	if (nf) {
-		r->fid->file = nf;
-		r->ofcall.qid = nf->qid;
+Next:
+	if (!nf) {
+		/* assume already responded */
+		return;
 	}
+	r->fid->file = nf;
+	r->ofcall.qid = nf->qid;
 	
 	if (vf->cardfile->serialized)
 		free(vf->cardfile->serialized);
 	vf->cardfile->serialized = nil;
 	
-	respond(r, nil);
+	if (!r->responded)
+		respond(r, nil);
 	return;
 	
 Nil:
@@ -637,6 +692,7 @@
 			continue;
 		cf = emkvfile(Qcard, c, nil, nil, nil);
 		fc = createfile(fs.tree->root, s, user, DMDIR|0777, cf);
+		cf->cardfile = cf;
 		cf->file = fc;
 		free(s);
 		if (!fc)