count is an in/out, not just an out var
[blerg.git] / database / subscription.c
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.
3  */
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/mman.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include "subscription.h"
12 #include "stringbucket.h"
13 #include "config.h"
14
15 int subscription_add(const char *from, const char *to) {
16         char filename[512];
17         struct stringbucket * sb;
18
19         snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
20         sb = stringbucket_open(filename);
21         stringbucket_add(sb, to);
22         stringbucket_close(sb);
23
24         snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to);
25         sb = stringbucket_open(filename);
26         stringbucket_add(sb, from);
27         stringbucket_close(sb);
28
29         return 1;
30 }
31
32 int subscription_remove(const char *from, const char *to) {
33         char filename[512];
34         struct stringbucket * sb;
35
36         snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
37         sb = stringbucket_open(filename);
38         stringbucket_delete(sb, to);
39         stringbucket_close(sb);
40
41         snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to);
42         sb = stringbucket_open(filename);
43         stringbucket_delete(sb, from);
44         stringbucket_close(sb);
45
46         return 1;
47 }
48
49 void subscription_notify_add_item(char *to, void *stuff) {
50         char filename[512];
51
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));
55         close(fd);
56 }
57
58 int subscription_notify(const char *author, uint64_t record) {
59         char filename[512];
60         struct blergref r;
61
62         strncpy(r.author, author, 32);
63         r.record = record;
64
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);
69
70         return 1;
71 }
72
73 struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) {
74         char filename[512];
75         struct stat st;
76         struct blergref * slist;
77         struct blergref * retlist;
78         uint64_t n_subscription_records;
79
80         if (!valid_name(author))
81                 return NULL;
82
83         snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, author);
84
85         int feed_fd = open(filename, O_RDONLY);
86         if (feed_fd == -1) {
87                 perror("Could not open subscription feed");
88                 goto subscription_list_map_failed;
89         }
90
91         fstat(feed_fd, &st);
92         if (st.st_size == 0) {
93                 close(feed_fd);
94                 goto subscription_list_map_failed;
95         }
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;
102                 close(feed_fd);
103                 return NULL;
104         }
105
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;
110         }
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;
115         }
116         
117         switch(direction) {
118         case 1:
119                 memcpy(retlist, slist + offset, sizeof(struct blergref) * *count);
120                 break;
121         case -1:
122                 memcpy(retlist, slist + (n_subscription_records - *count - offset), sizeof(struct blergref) * *count);
123         }
124
125         munmap(slist, st.st_size);
126         close(feed_fd);
127         return retlist;
128
129 subscription_list_malloc_failed:
130         munmap(slist, st.st_size);
131 subscription_list_map_failed:
132         close(feed_fd);
133 subscription_list_open_failed:
134         *count = 0;
135         return NULL;
136 }
137
138 int is_subscribed(const char *from, const char *to) {
139         char filename[512];
140         struct stringbucket * sb;
141         int ret = 0;
142
143         snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
144         sb = stringbucket_open(filename);
145         if (stringbucket_find(sb, to) != -1)
146                 ret = 1;
147         stringbucket_close(sb);
148
149         return ret;
150 }
151
152 int subscription_count_items(const char *user) {
153         char filename[512];
154         struct stat st;
155
156         if (!valid_name(user))
157                 return -1;
158
159         snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, user);
160
161         if (access(filename, R_OK) != 0)
162                 return 0;
163         stat(filename, &st);
164         return st.st_size / sizeof(struct blergref);
165 }