Error when fetching tags and direction isn't 1 or -1
[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         if (!(direction == 1 || direction == -1)) {
111                 fprintf(stderr, "Invalid direction: %d\n", direction);
112                 return NULL;
113         }
114         
115         switch(tag[0]) {
116         case '#':
117                 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
118                 break;
119         case '@':
120                 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
121                 break;
122         default:
123                 fprintf(stderr, "Invalid tag type: %s\n", tag);
124                 return NULL;
125         }
126
127         int tag_fd = open(filename, O_RDONLY, 0600);
128         if (tag_fd == -1) {
129                 perror("Could not open tag file");
130                 goto tag_list_open_failed;
131         }
132
133         fstat(tag_fd, &st);
134         if (st.st_size == 0) {
135                 goto tag_list_map_failed;
136         }
137         n_tag_records = st.st_size / sizeof(struct blergref);
138         if (*count > n_tag_records - offset)
139                 *count = n_tag_records - offset;
140         if (offset > n_tag_records) {
141                 fprintf(stderr, "Cannot access tag record beyond end\n");
142                 goto tag_list_map_failed;
143         }
144
145         taglist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0);
146         if (taglist == MAP_FAILED) {
147                 perror("Could not mmap tag file");
148                 goto tag_list_map_failed;
149         }
150         retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count);
151         if (retlist == NULL) {
152                 perror("Could not allocate memory for tag list");
153                 goto tag_list_malloc_failed;
154         }
155         switch(direction) {
156         case 1:
157                 memcpy(retlist, taglist + offset, sizeof(struct blergref) * *count);
158                 break;
159         case -1:
160                 memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct blergref) * *count);
161                 break;
162         }
163
164         munmap(taglist, st.st_size);
165         close(tag_fd);
166         return retlist;
167
168 tag_list_malloc_failed:
169         munmap(taglist, st.st_size);
170 tag_list_map_failed:
171         close(tag_fd);
172 tag_list_open_failed:
173         *count = 0;
174         return NULL;
175 }
176
177 int tag_exists(const char *tag) {
178         char filename[512];
179
180         if (!valid_tag_name(tag + 1))
181                 return 0;
182
183         if (!(tag[0] == '@' || tag[0] == '#')) {
184                 fprintf(stderr, "Invalid tag: %s\n", tag);
185                 return 0;
186         }
187
188         switch(tag[0]) {
189         case '#':
190                 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
191                 break;
192         case '@':
193                 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
194                 break;
195         default:
196                 fprintf(stderr, "Invalid tag type: %s\n", tag);
197                 return 0;
198         }
199         if (access(filename, F_OK) == -1)
200                 return 0;
201         else
202                 return 1;
203 }