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