shithub: oai

Download patch

ref: dff0176ccca8f8ad2a917b864a805ee57f548d27
parent: 3d4c4ee5d639d10ae577561ce0f932cf5b2df7d8
author: sirjofri <sirjofri@sirjofri.de>
date: Wed Feb 25 06:13:13 EST 2026

read_file and list_files, error out if wrong type

--- a/oai.c
+++ b/oai.c
@@ -76,6 +76,14 @@
 		return strdup("list_files: invalid folder");
 	}
 	
+	dirbuf = dirstat(s);
+	if (!(dirbuf->mode&DMDIR)) {
+		s = smprint("%s is a file, not a folder", s);
+		free(dirbuf);
+		jsonfree(j);
+		return s;
+	}
+	
 	fd = open(s, OREAD);
 	if (fd < 0)
 		return strdup("");
@@ -90,8 +98,11 @@
 	str = s_new();
 	for (i = 0; i < n; i++) {
 		str = s_append(str, dirbuf[i].name);
+		if (dirbuf[i].mode&DMDIR)
+			str = s_append(str, "/");
 		str = s_append(str, "\n");
 	}
+	free(dirbuf);
 	s = strdup(s_to_c(str));
 	s_free(str);
 	return s;
@@ -116,6 +127,7 @@
 	char *file;
 	Biobuf *io;
 	char *s;
+	Dir *dir;
 	
 	if (!allowed(toolcall))
 		return abortcall(toolcall);
@@ -133,6 +145,16 @@
 		fprint(2, "invalid file in read_file request!\n");
 		jsonfree(j);
 		return strdup("bad request in read_file: no file!\n");
+	}
+	
+	dir = dirstat(file);
+	if (!dir)
+		return smprint("error opening file: %r\n");
+	
+	if (dir->mode&DMDIR) {
+		s = smprint("error: file is a directory\n");
+		free(dir);
+		return s;
 	}
 	
 	io = Bopen(file, OREAD);
--