+/* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
+ * BSD-style license. Please see the COPYING file for details.
+ */
#include <sys/types.h>
#include <sys/stat.h>
-#include <fcntl.h>
+#include <sys/mman.h>
+#include <stdlib.h>
#include <stdio.h>
+#include <fcntl.h>
#include <string.h>
#include "subscription.h"
#include "stringbucket.h"
char filename[512];
snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, to);
- int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT);
- write(fd, stuff, sizeof(struct subscription_record));
+ int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
+ write(fd, stuff, sizeof(struct blergref));
close(fd);
}
int subscription_notify(const char *author, uint64_t record) {
char filename[512];
- struct subscription_record r;
+ struct blergref r;
strncpy(r.author, author, 32);
r.record = record;
stringbucket_iterate(sb, subscription_notify_add_item, &r);
stringbucket_close(sb);
}
+
+struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) {
+ char filename[512];
+ struct stat st;
+ struct blergref * slist;
+ struct blergref * retlist;
+ uint64_t n_subscription_records;
+
+ if (!valid_name(author))
+ return NULL;
+
+ snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, author);
+
+ int feed_fd = open(filename, O_RDONLY);
+ if (feed_fd == -1) {
+ perror("Could not open subscription feed");
+ goto subscription_list_map_failed;
+ }
+
+ fstat(feed_fd, &st);
+ if (st.st_size == 0) {
+ close(feed_fd);
+ goto subscription_list_map_failed;
+ }
+ n_subscription_records = st.st_size / sizeof(struct blergref);
+ if (*count > n_subscription_records - offset)
+ *count = n_subscription_records - offset;
+ if (offset > n_subscription_records) {
+ fprintf(stderr, "Cannot access subscription record beyond end\n");
+ goto subscription_list_map_failed;
+ close(feed_fd);
+ return NULL;
+ }
+
+ slist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, feed_fd, 0);
+ if (slist == MAP_FAILED) {
+ perror("Could not mmap tag_file");
+ goto subscription_list_map_failed;
+ }
+ retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count);
+ if (retlist == NULL) {
+ perror("Could not allocate memory for subscription feed list");
+ goto subscription_list_malloc_failed;
+ }
+
+ switch(direction) {
+ case 1:
+ memcpy(retlist, slist + offset, sizeof(struct blergref) * *count);
+ break;
+ case -1:
+ memcpy(retlist, slist + (n_subscription_records - *count - offset), sizeof(struct blergref) * *count);
+ }
+
+ munmap(slist, st.st_size);
+ close(feed_fd);
+ return retlist;
+
+subscription_list_malloc_failed:
+ munmap(slist, st.st_size);
+subscription_list_map_failed:
+ close(feed_fd);
+subscription_list_open_failed:
+ *count = 0;
+ return NULL;
+}
+
+int is_subscribed(const char *from, const char *to) {
+ char filename[512];
+ struct stringbucket * sb;
+ int ret = 0;
+
+ snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
+ sb = stringbucket_open(filename);
+ if (stringbucket_find(sb, to) != -1)
+ ret = 1;
+ stringbucket_close(sb);
+
+ return ret;
+}
+
+int subscription_count_items(const char *user) {
+ char filename[512];
+ struct stat st;
+
+ if (!valid_name(user))
+ return -1;
+
+ snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, user);
+
+ if (access(filename, R_OK) != 0)
+ return 0;
+ stat(filename, &st);
+ return st.st_size / sizeof(struct blergref);
+}