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.
17 #include "configuration.h"
20 int tag_scan(const char *author, const char *data, int len, uint64_t record) {
21 char *taglist[MAX_TAGS_PER_RECORD];
26 for (i = 0; i < len; i++) {
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");
35 while (i < len && VALID_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) {
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.
45 last_char = data[i-1];
49 taglist[n_tags] = tag;
51 last_char = data[i-1];
52 // We goto here so i doesn't get incremented
58 for (i = 0; i < n_tags; i++) {
59 tag_add(author, taglist[i], record);
65 int tag_add(const char *author, const char *tag, uint64_t record) {
66 char filename[FILENAME_MAX];
69 memset(t.author, 0, 32);
70 strncpy(t.author, author, 32);
75 snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.hash_tags_path, tag + 1);
78 snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.ref_tags_path, tag + 1);
81 fprintf(stderr, "Invalid tag type: %s\n", tag);
85 int tag_fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
86 flock(tag_fd, LOCK_EX);
88 perror("Could not open tag file");
91 int n = write(tag_fd, &t, sizeof(struct blergref));
92 if (n < sizeof(struct blergref)) {
93 perror("Could not write new tag");
96 flock(tag_fd, LOCK_UN);
102 struct blergref * tag_list(const char *tag, uint64_t offset, int *count, int direction) {
103 char filename[FILENAME_MAX];
105 struct blergref *taglist;
106 struct blergref *retlist;
107 uint64_t n_tag_records;
109 if (!valid_tag_name(tag + 1))
112 if (!(direction == 1 || direction == -1)) {
113 fprintf(stderr, "Invalid direction: %d\n", direction);
119 snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.hash_tags_path, tag + 1);
122 snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.ref_tags_path, tag + 1);
125 fprintf(stderr, "Invalid tag type: %s\n", tag);
129 int tag_fd = open(filename, O_RDONLY, 0600);
131 perror("Could not open tag file");
132 goto tag_list_open_failed;
136 if (st.st_size == 0) {
137 goto tag_list_map_failed;
139 n_tag_records = st.st_size / sizeof(struct blergref);
140 if (*count > n_tag_records - offset)
141 *count = n_tag_records - offset;
142 if (offset > n_tag_records) {
143 fprintf(stderr, "Cannot access tag record beyond end\n");
144 goto tag_list_map_failed;
147 taglist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0);
148 if (taglist == MAP_FAILED) {
149 perror("Could not mmap tag file");
150 goto tag_list_map_failed;
152 retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count);
153 if (retlist == NULL) {
154 perror("Could not allocate memory for tag list");
155 goto tag_list_malloc_failed;
159 memcpy(retlist, taglist + offset, sizeof(struct blergref) * *count);
162 memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct blergref) * *count);
166 munmap(taglist, st.st_size);
170 tag_list_malloc_failed:
171 munmap(taglist, st.st_size);
174 tag_list_open_failed:
179 int tag_exists(const char *tag) {
180 char filename[FILENAME_MAX];
182 if (!valid_tag_name(tag + 1))
185 if (!(tag[0] == '@' || tag[0] == '#')) {
186 fprintf(stderr, "Invalid tag: %s\n", tag);
192 snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.hash_tags_path, tag + 1);
195 snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.ref_tags_path, tag + 1);
198 fprintf(stderr, "Invalid tag type: %s\n", tag);
201 if (access(filename, F_OK) == -1)