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.
19 int tag_scan(const char *author, const char *data, int len, uint64_t record) {
20 char *taglist[MAX_TAGS_PER_RECORD];
25 for (i = 0; i < len; i++) {
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");
34 while (i < len && VALID_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) {
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.
44 last_char = data[i-1];
48 taglist[n_tags] = tag;
50 last_char = data[i-1];
51 // We goto here so i doesn't get incremented
57 for (i = 0; i < n_tags; i++) {
58 tag_add(author, taglist[i], record);
63 int tag_add(const char *author, const char *tag, uint64_t record) {
67 memset(t.author, 0, 32);
68 strncpy(t.author, author, 32);
73 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
76 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
79 fprintf(stderr, "Invalid tag type: %s\n", tag);
83 int tag_fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
84 flock(tag_fd, LOCK_EX);
86 perror("Could not open tag file");
89 int n = write(tag_fd, &t, sizeof(struct blergref));
90 if (n < sizeof(struct blergref)) {
91 perror("Could not write new tag");
94 flock(tag_fd, LOCK_UN);
100 struct blergref * tag_list(const char *tag, uint64_t offset, int *count, int direction) {
103 struct blergref *taglist;
104 struct blergref *retlist;
105 uint64_t n_tag_records;
107 if (!valid_tag_name(tag + 1))
110 if (!(direction == 1 || direction == -1)) {
111 fprintf(stderr, "Invalid direction: %d\n", direction);
117 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
120 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
123 fprintf(stderr, "Invalid tag type: %s\n", tag);
127 int tag_fd = open(filename, O_RDONLY, 0600);
129 perror("Could not open tag file");
130 goto tag_list_open_failed;
134 if (st.st_size == 0) {
135 goto tag_list_map_failed;
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;
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;
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;
157 memcpy(retlist, taglist + offset, sizeof(struct blergref) * *count);
160 memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct blergref) * *count);
164 munmap(taglist, st.st_size);
168 tag_list_malloc_failed:
169 munmap(taglist, st.st_size);
172 tag_list_open_failed:
177 int tag_exists(const char *tag) {
180 if (!valid_tag_name(tag + 1))
183 if (!(tag[0] == '@' || tag[0] == '#')) {
184 fprintf(stderr, "Invalid tag: %s\n", tag);
190 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
193 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
196 fprintf(stderr, "Invalid tag type: %s\n", tag);
199 if (access(filename, F_OK) == -1)