summary refs log tree commit diff
diff options
context:
space:
mode:
authorwrmr2025-06-23 20:11:09 -0400
committerwrmr2025-06-23 20:11:09 -0400
commit894e9a34a7657f3223f6ea385e66222361d89fc7 (patch)
tree0410f8922082bd5c1d8e6b043eb541385cc396c6
parentd66e948d4a5bb03ea09479ee7a326c95c1618871 (diff)
simplify and improve posts_gather() + posts_gather_from()
no more chdir()! we weren't even reading those files then anyway, that
happens after pruning the feed length to 200
-rw-r--r--main.c51
1 files changed, 19 insertions, 32 deletions
diff --git a/main.c b/main.c
index c315085..bd599d8 100644
--- a/main.c
+++ b/main.c
@@ -148,48 +148,35 @@ int post_cmp(const void *a, const void *b) {
 	return -ts_cmp(&pa->timestamp, &pb->timestamp);
 }
 
+/* @path must be both
+ * (a) absolute; and
+ * (b) allocated either statically or within arena @a
+ */
 void posts_gather_from(PostList *posts, Str username, const char *path, Arena *a) {
-	if (chdir(path)) return;
-
-	DIR *bink = opendir(".bink");
-	if (!bink) return;
-
-	if (chdir(".bink")) {
-		log_warn("couldn't cd %s/.bink", path);
-		return;
-	}
-
-	char cwd[PATH_MAX];
-	getcwd(cwd, PATH_MAX);
-
-	struct dirent *de_bink;
-	while ((de_bink = readdir(bink))) {
-		if (!strcmp(de_bink->d_name, ".") || !strcmp(de_bink->d_name, "..")) continue;
-
+	DIR *d = opendir(path);
+	if (!d) return;
+	for (struct dirent *de; (de = readdir(d)); ) {
+		if (*de->d_name == '.') continue;
 		Post p = { 0 };
-
-		if (str_to_timespec(str_from_cstr(de_bink->d_name), &p.timestamp)) continue;
+		if (str_to_timespec(str_from_cstr(de->d_name), &p.timestamp)) continue;
 		if (timestamp_invalid(&p.timestamp)) continue;
-
 		p.user = str_dup(username, a);
-		p.path = cstr_fmt(a, "%s/%s", cwd, de_bink->d_name);
-
+		p.path = cstr_fmt(a, "%s/%s", path, de->d_name);
 		DA_PUSH(posts, p);
 	}
-	closedir(bink);
+	closedir(d);
 }
 
 void posts_gather(PostList *posts, Arena *a) {
-	struct dirent *de_homedir;
-	DIR *home = opendir("/home/");
-	if (!home) err(1, "couldn't open /home/");
-	while ((de_homedir = readdir(home))) {
-		if (!strcmp(de_homedir->d_name, ".") || !strcmp(de_homedir->d_name, "..")) continue;
-		if (chdir("/home/")) err(1, "failed to change directory");
-		posts_gather_from(posts, str_from_cstr(de_homedir->d_name), de_homedir->d_name, a);
+	DIR *d = opendir("/home/");
+	if (!d) return;
+	for (struct dirent *de; (de = readdir(d)); ) {
+		if (*de->d_name == '.') continue;
+		posts_gather_from(posts, str_from_cstr(de->d_name),
+			cstr_fmt(a, "/home/%s/.bink", de->d_name), a);
 	}
-	closedir(home);
-	posts_gather_from(posts, S("our"), "/town/our/data", a);
+	closedir(d);
+	posts_gather_from(posts, S("our"), "/town/our/data/.bink", a);
 }
 
 void posts_load(PostList *posts, Arena *a) {