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