changeset 982:9fefaaa6dcba

post: abstract away file reading & filename recording Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Tue, 13 Nov 2018 20:51:12 -0500
parents 272b84ebb926
children 74e981842ba9
files listing.c post.c post.h
diffstat 3 files changed, 27 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/listing.c	Wed Nov 14 00:54:16 2018 +0000
+++ b/listing.c	Tue Nov 13 20:51:12 2018 -0500
@@ -33,30 +33,19 @@
 struct str *listing(struct post *post, const char *fname)
 {
 	char path[FILENAME_MAX];
-	uint64_t file_rev;
 	struct str *in;
-	int ret;
 
 	snprintf(path, FILENAME_MAX, "%s/posts/%d/%s",
 		 str_cstr(config.data_dir), post->id, fname);
 
-	in = file_cache_get(path, &file_rev);
-	if (IS_ERR(in)) {
-		ret = PTR_ERR(in);
+	in = post_get_cached_file(post, path);
+	if (IS_ERR(in))
 		goto err;
-	}
-
-	ret = post_add_filename(post, path, file_rev);
-	if (ret)
-		goto err_free;
 
 	return listing_str(in);
 
-err_free:
-	str_putref(in);
-
 err:
 	snprintf(path, FILENAME_MAX, "Failed to read in listing '%d/%s': %s",
-		 post->id, fname, xstrerror(ret));
+		 post->id, fname, xstrerror(PTR_ERR(in)));
 	return STR_DUP(path);
 }
--- a/post.c	Wed Nov 14 00:54:16 2018 +0000
+++ b/post.c	Tue Nov 13 20:51:12 2018 -0500
@@ -81,9 +81,23 @@
 	init_post_index();
 }
 
-int post_add_filename(struct post *post, const char *path, uint64_t cache_rev)
+struct str *post_get_cached_file(struct post *post, const char *path)
 {
-	return nvl_set_int(post->files, path, cache_rev);
+	struct str *out;
+	uint64_t rev;
+	int err;
+
+	out = file_cache_get(path, &rev);
+	if (IS_ERR(out))
+		return out;
+
+	err = nvl_set_int(post->files, path, rev);
+	if (err) {
+		str_putref(out);
+		out = ERR_PTR(err);
+	}
+
+	return out;
 }
 
 static void post_remove_all_filenames(struct post *post)
@@ -145,24 +159,14 @@
 static struct str *load_comment(struct post *post, int commid)
 {
 	char path[FILENAME_MAX];
-	uint64_t file_rev;
 	struct str *out;
 
 	snprintf(path, FILENAME_MAX, "%s/posts/%d/comments/%d/text.txt",
 		 str_cstr(config.data_dir), post->id, commid);
 
-	out = file_cache_get(path, &file_rev);
-	if (IS_ERR(out)) {
+	out = post_get_cached_file(post, path);
+	if (IS_ERR(out))
 		out = STATIC_STR("Error: could not load comment text.");
-	} else {
-		int ret;
-
-		ret = post_add_filename(post, path, file_rev);
-		if (ret) {
-			str_putref(out);
-			out = STATIC_STR("Error: internal error has occured.");
-		}
-	}
 
 	return out;
 }
@@ -171,21 +175,16 @@
 {
 	char path[FILENAME_MAX];
 	struct comment *comm;
-	uint64_t file_rev;
 	struct str *meta;
 	struct val *lv;
 	struct val *v;
-	int ret;
 
 	snprintf(path, FILENAME_MAX, "%s/posts/%d/comments/%d/meta.lisp",
 		 str_cstr(config.data_dir), post->id, commid);
 
-	meta = file_cache_get(path, &file_rev);
+	meta = post_get_cached_file(post, path);
 	ASSERT(!IS_ERR(meta));
 
-	ret = post_add_filename(post, path, file_rev);
-	ASSERT0(ret);
-
 	lv = sexpr_parse_str(meta);
 	ASSERT(!IS_ERR(lv));
 
@@ -303,7 +302,6 @@
 	};
 
 	char path[FILENAME_MAX];
-	uint64_t file_rev;
 	struct str *raw;
 	int ret;
 
@@ -312,17 +310,12 @@
 	snprintf(path, FILENAME_MAX, "%s/posts/%d/post.%s",
 		 str_cstr(config.data_dir), post->id, exts[post->fmt]);
 
-	raw = file_cache_get(path, &file_rev);
+	raw = post_get_cached_file(post, path);
 	if (IS_ERR(raw))
 		return PTR_ERR(raw);
 
-	ret = post_add_filename(post, path, file_rev);
-	if (ret)
-		goto out;
-
 	ret = __do_load_post_body_fmt3(post, raw);
 
-out:
 	str_putref(raw);
 
 	return ret;
@@ -346,26 +339,20 @@
 static int __refresh_published(struct post *post)
 {
 	char path[FILENAME_MAX];
-	uint64_t file_rev;
 	struct str *meta;
 	struct val *lv;
-	int ret;
 
 	snprintf(path, FILENAME_MAX, "%s/posts/%d/post.lisp",
 		 str_cstr(config.data_dir), post->id);
 
-	meta = file_cache_get(path, &file_rev);
+	meta = post_get_cached_file(post, path);
 	if (IS_ERR(meta))
 		return PTR_ERR(meta);
 
-	ret = post_add_filename(post, path, file_rev);
-	if (ret)
-		goto err;
-
 	lv = sexpr_parse_str(meta);
 	if (IS_ERR(lv)) {
-		ret = PTR_ERR(lv);
-		goto err;
+		str_putref(meta);
+		return PTR_ERR(lv);
 	}
 
 	__refresh_published_prop(post, lv);
@@ -384,12 +371,6 @@
 	str_putref(meta);
 
 	return 0;
-
-err:
-	str_putref(meta);
-
-	return ret;
-
 }
 
 static bool must_refresh(struct post *post)
--- a/post.h	Wed Nov 14 00:54:16 2018 +0000
+++ b/post.h	Tue Nov 13 20:51:12 2018 -0500
@@ -84,7 +84,7 @@
 struct req;
 
 extern void init_post_subsys(void);
-extern int post_add_filename(struct post *post, const char *path, uint64_t cache_rev);
+extern struct str *post_get_cached_file(struct post *post, const char *path);
 extern struct post *load_post(int postid, bool preview);
 extern void post_refresh(struct post *post);
 extern void post_destroy(struct post *post);