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);
32 int subscription_remove(const char *from, const char *to) {
34 struct stringbucket * sb;
36 snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
37 sb = stringbucket_open(filename);
38 stringbucket_delete(sb, to);
39 stringbucket_close(sb);
41 snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to);
42 sb = stringbucket_open(filename);
43 stringbucket_delete(sb, from);
44 stringbucket_close(sb);
49 void subscription_notify_add_item(char *to, void *stuff) {
52 snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, to);
53 int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
54 write(fd, stuff, sizeof(struct blergref));
58 int subscription_notify(const char *author, uint64_t record) {
62 strncpy(r.author, author, 32);
65 snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, author);
66 struct stringbucket * sb = stringbucket_open(filename);
67 stringbucket_iterate(sb, subscription_notify_add_item, &r);
68 stringbucket_close(sb);
73 struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) {
76 struct blergref * slist;
77 struct blergref * retlist;
78 uint64_t n_subscription_records;
80 if (!valid_name(author))
83 snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, author);
85 int feed_fd = open(filename, O_RDONLY);
87 perror("Could not open subscription feed");
88 goto subscription_list_map_failed;
92 if (st.st_size == 0) {
94 goto subscription_list_map_failed;
96 n_subscription_records = st.st_size / sizeof(struct blergref);
97 if (*count > n_subscription_records - offset)
98 *count = n_subscription_records - offset;
99 if (offset > n_subscription_records) {
100 fprintf(stderr, "Cannot access subscription record beyond end\n");
101 goto subscription_list_map_failed;
106 slist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, feed_fd, 0);
107 if (slist == MAP_FAILED) {
108 perror("Could not mmap tag_file");
109 goto subscription_list_map_failed;
111 retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count);
112 if (retlist == NULL) {
113 perror("Could not allocate memory for subscription feed list");
114 goto subscription_list_malloc_failed;
119 memcpy(retlist, slist + offset, sizeof(struct blergref) * *count);
122 memcpy(retlist, slist + (n_subscription_records - *count - offset), sizeof(struct blergref) * *count);
125 munmap(slist, st.st_size);
129 subscription_list_malloc_failed:
130 munmap(slist, st.st_size);
131 subscription_list_map_failed:
133 subscription_list_open_failed:
138 int is_subscribed(const char *from, const char *to) {
140 struct stringbucket * sb;
143 snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
144 sb = stringbucket_open(filename);
145 if (stringbucket_find(sb, to) != -1)
147 stringbucket_close(sb);
152 int subscription_count_items(const char *user) {
156 if (!valid_name(user))
159 snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, user);
161 if (access(filename, R_OK) != 0)
164 return st.st_size / sizeof(struct blergref);