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