shithub: vcardfs

Download patch

ref: ad8274ef26c101c9d03d8c4ca6376e3a90a67734
parent: e23fd2fce14cd0ea17a924503c334f9d63b8470a
author: sirjofri <sirjofri@sirjofri.de>
date: Sat Oct 19 11:33:55 EDT 2024

adds first two commands: write and new

--- a/vcardfs.c
+++ b/vcardfs.c
@@ -24,7 +24,7 @@
 	- "write" (save file)
 /import
 	- write new vcf file to import
-/ export
+/export
 	- read all vcards as vcf
 
 */
@@ -312,6 +312,93 @@
 }
 
 static void
+savedata(Req *r)
+{
+	char *s;
+	int fd;
+	
+	fd = open(file, OTRUNC|OWRITE);
+	if (fd < 0) {
+		responderror(r);
+		return;
+	}
+	
+	s = vcmserialize(cards);
+	if (!s) {
+		responderror(r);
+		return;
+	}
+	
+	fprint(fd, "%s", s);
+	free(s);
+	respond(r, nil);
+}
+
+static void
+addnew(Req *r, char *fn)
+{
+	Vcard *c, *cc;
+	
+	c = mallocz(sizeof(Vcard), 1);
+	if (!c) {
+		responderror(r);
+		return;
+	}
+	c->content = mallocz(sizeof(Vline), 1);
+	if (!c->content) {
+		responderror(r);
+		return;
+	}
+	c->content->name = estrdup("fn");
+	c->content->value = estrdup(fn);
+	
+	initcardfiles(c);
+	
+	if (!cards) {
+		cards = c;
+		respond(r, nil);
+		return;
+	}
+	
+	for (cc = cards; cc->next; cc = cc->next)
+		continue;
+	
+	cc->next = c;
+	respond(r, nil);
+}
+
+static void
+ctlcmd(Req *r)
+{
+	char *s, *t;
+	
+	s = mallocz(r->ifcall.count + 1, 1);
+	if (!s)
+		sysfatal("%r");
+	memcpy(s, r->ifcall.data, r->ifcall.count);
+	
+	t = strchr(s, '\n');
+	if (t)
+		*t = 0;
+	
+	if (strcmp(s, "write") == 0) {
+		savedata(r);
+		goto Out;
+	}
+	
+	if (strncmp(s, "new ", 4) == 0) {
+		addnew(r, s+4);
+		goto Out;
+	}
+	free(s);
+	respond(r, "not implemented");
+	return;
+Out:
+	free(s);
+	r->ofcall.count = r->ofcall.count;
+}
+
+static void
 fswrite(Req *r)
 {
 	Vfile *f;
@@ -318,7 +405,8 @@
 	f = r->fid->file->aux;
 	switch (f->level) {
 	case Qctl:
-		break;
+		ctlcmd(r);
+		return;
 	case Qimport:
 		readimportdata(r, f);
 		return;
@@ -442,6 +530,11 @@
 	
 	initcardfiles(ncards);
 	
+	if (!cards) {
+		cards = ncards;
+		return;
+	}
+	
 	for (nc = cards; nc->next; nc = nc->next)
 		continue;
 	
@@ -609,7 +702,7 @@
 		sysfatal("%r");
 	
 	fs.tree = alloctree("vcf", "vcf", DMDIR|0555, nil);
-	createfile(fs.tree->root, "ctl", user, 0666,
+	createfile(fs.tree->root, "ctl", user, 0222,
 		emkvfile(Qctl, nil, nil, nil, nil));
 	createfile(fs.tree->root, "import", user, 0222,
 		emkvfile(Qimport, nil, nil, nil, nil));