Add subscription feed fetching, clean up a bit
[blerg.git] / database / tags.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 <string.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/file.h>
12 #include <sys/mman.h>
13 #include "tags.h"
14 #include "util.h"
15 #include "database.h"
16 #include "config.h"
17
18
19 int tag_scan(const char *author, const char *data, int len, uint64_t record) {
20         char *taglist[MAX_TAGS_PER_RECORD];
21         char last_char = ' ';
22         int n_tags = 0;
23         int i, j;
24
25         for (i = 0; i < len; i++) {
26 tag_scan_start:
27                 if (WHITESPACE(last_char) && (data[i] == '#' || data[i] == '@')) {
28                         if (n_tags == MAX_TAGS_PER_RECORD) {
29                                 fprintf(stderr, "Too many tags in message\n");
30                                 break;
31                         }
32                         int begin = i;
33                         int start = ++i;
34                         while (i < len && VALID_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) {
35                                 i++;
36                         }
37                         if (start - i == 0) continue;
38                         char *tag = calloc(1, MAX_TAG_LENGTH + 2);
39                         memcpy(tag, &data[begin], i - begin);
40                         for (j = 0; j < n_tags; j++) {
41                                 if (!strncmp(tag, taglist[j], MAX_TAG_LENGTH)) {
42                                         // We already have this tag.  Start over.
43                                         free(tag);
44                                         last_char = data[i-1];
45                                         goto tag_scan_start;
46                                 }
47                         }
48                         taglist[n_tags] = tag;
49                         n_tags++;
50                         last_char = data[i-1];
51                         // We goto here so i doesn't get incremented
52                         goto tag_scan_start;
53                 }
54                 last_char = data[i];
55         }
56
57         for (i = 0; i < n_tags; i++) {
58                 tag_add(author, taglist[i], record);
59                 free(taglist[i]);
60         }
61 }
62
63 int tag_add(const char *author, const char *tag, uint64_t record) {
64         char filename[512];
65         struct blergref t;
66
67         memset(t.author, 0, 32);
68         strncpy(t.author, author, 32);
69         t.record = record;
70
71         switch(tag[0]) {
72         case '#':
73                 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
74                 break;
75         case '@':
76                 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
77                 break;
78         default:
79                 fprintf(stderr, "Invalid tag type: %s\n", tag);
80                 return 0;
81         }
82
83         int tag_fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
84         flock(tag_fd, LOCK_EX);
85         if (tag_fd == -1) {
86                 perror("Could not open tag file");
87                 return 0;
88         }
89         int n = write(tag_fd, &t, sizeof(struct blergref));
90         if (n < sizeof(struct blergref)) {
91                 perror("Could not write new tag");
92                 return 0;
93         }
94         flock(tag_fd, LOCK_UN);
95         close(tag_fd);
96
97         return 1;
98 }
99
100 struct blergref * tag_list(const char *tag, uint64_t offset, int *count, int direction) {
101         char filename[512];
102         struct stat st;
103         struct blergref *taglist;
104         struct blergref *retlist;
105         uint64_t n_tag_records;
106
107         if (!valid_tag_name(tag + 1))
108                 return NULL;
109         
110         switch(tag[0]) {
111         case '#':
112                 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
113                 break;
114         case '@':
115                 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
116                 break;
117         default:
118                 fprintf(stderr, "Invalid tag type: %s\n", tag);
119                 return NULL;
120         }
121
122         int tag_fd = open(filename, O_RDONLY, 0600);
123         if (tag_fd == -1) {
124                 perror("Could not open tag file");
125                 goto tag_list_open_failed;
126         }
127
128         fstat(tag_fd, &st);
129         if (st.st_size == 0) {
130                 goto tag_list_map_failed;
131         }
132         n_tag_records = st.st_size / sizeof(struct blergref);
133         if (*count > n_tag_records - offset)
134                 *count = n_tag_records - offset;
135         if (offset > n_tag_records) {
136                 fprintf(stderr, "Cannot access tag record beyond end\n");
137                 goto tag_list_map_failed;
138         }
139
140         taglist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0);
141         if (taglist == MAP_FAILED) {
142                 perror("Could not mmap tag file");
143                 goto tag_list_map_failed;
144         }
145         retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count);
146         if (retlist == NULL) {
147                 perror("Could not allocate memory for tag list");
148                 goto tag_list_malloc_failed;
149         }
150         switch(direction) {
151         case 1:
152                 memcpy(retlist, taglist + offset, sizeof(struct blergref) * *count);
153                 break;
154         case -1:
155                 memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct blergref) * *count);
156                 break;
157         }
158
159         munmap(taglist, st.st_size);
160         close(tag_fd);
161         return retlist;
162
163 tag_list_malloc_failed:
164         munmap(taglist, st.st_size);
165 tag_list_map_failed:
166         close(tag_fd);
167 tag_list_open_failed:
168         *count = 0;
169         return NULL;
170 }
171
172 int tag_exists(const char *tag) {
173         char filename[512];
174
175         if (!valid_tag_name(tag + 1))
176                 return 0;
177
178         if (!(tag[0] == '@' || tag[0] == '#')) {
179                 fprintf(stderr, "Invalid tag: %s\n", tag);
180                 return 0;
181         }
182
183         switch(tag[0]) {
184         case '#':
185                 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
186                 break;
187         case '@':
188                 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
189                 break;
190         default:
191                 fprintf(stderr, "Invalid tag type: %s\n", tag);
192                 return 0;
193         }
194         if (access(filename, F_OK) == -1)
195                 return 0;
196         else
197                 return 1;
198 }