1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2 * BSD-style license. Please see the COPYING file for details.
11 #include "subscription.h"
12 #include "stringbucket.h"
15 int subscription_add(const char *from, const char *to) {
17 struct stringbucket * sb;
19 snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
20 sb = stringbucket_open(filename);
21 stringbucket_add(sb, to);
22 stringbucket_close(sb);
24 snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to);
25 sb = stringbucket_open(filename);
26 stringbucket_add(sb, from);
27 stringbucket_close(sb);
30 int subscription_remove(const char *from, const char *to) {
32 struct stringbucket * sb;
34 snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
35 sb = stringbucket_open(filename);
36 stringbucket_delete(sb, to);
37 stringbucket_close(sb);
39 snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to);
40 sb = stringbucket_open(filename);
41 stringbucket_delete(sb, from);
42 stringbucket_close(sb);
45 void subscription_notify_add_item(char *to, void *stuff) {
48 snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, to);
49 int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
50 write(fd, stuff, sizeof(struct blergref));
54 int subscription_notify(const char *author, uint64_t record) {
58 strncpy(r.author, author, 32);
61 snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, author);
62 struct stringbucket * sb = stringbucket_open(filename);
63 stringbucket_iterate(sb, subscription_notify_add_item, &r);
64 stringbucket_close(sb);
67 struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) {
70 struct blergref * slist;
71 struct blergref * retlist;
72 uint64_t n_subscription_records;
74 if (!valid_name(author))
77 snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, author);
79 int feed_fd = open(filename, O_RDONLY);
81 perror("Could not open subscription feed");
82 goto subscription_list_map_failed;
86 if (st.st_size == 0) {
88 goto subscription_list_map_failed;
90 n_subscription_records = st.st_size / sizeof(struct blergref);
91 if (*count > n_subscription_records - offset)
92 *count = n_subscription_records - offset;
93 if (offset > n_subscription_records) {
94 fprintf(stderr, "Cannot access subscription record beyond end\n");
95 goto subscription_list_map_failed;
100 slist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, feed_fd, 0);
101 if (slist == MAP_FAILED) {
102 perror("Could not mmap tag_file");
103 goto subscription_list_map_failed;
105 retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count);
106 if (retlist == NULL) {
107 perror("Could not allocate memory for subscription feed list");
108 goto subscription_list_malloc_failed;
113 memcpy(retlist, slist + offset, sizeof(struct blergref) * *count);
116 memcpy(retlist, slist + (n_subscription_records - *count - offset), sizeof(struct blergref) * *count);
119 munmap(slist, st.st_size);
123 subscription_list_malloc_failed:
124 munmap(slist, st.st_size);
125 subscription_list_map_failed:
127 subscription_list_open_failed:
132 int is_subscribed(const char *from, const char *to) {
134 struct stringbucket * sb;
137 snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
138 sb = stringbucket_open(filename);
139 if (stringbucket_find(sb, to) != -1)
141 stringbucket_close(sb);