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))
112 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
115 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
118 fprintf(stderr, "Invalid tag type: %s\n", tag);
122 int tag_fd = open(filename, O_RDONLY, 0600);
124 perror("Could not open tag file");
125 goto tag_list_open_failed;
129 if (st.st_size == 0) {
130 goto tag_list_map_failed;
132 n_tag_records = st.st_size / sizeof(struct blergref);
133 if (*count > n_tag_records - offset)
134 *count = n_tag_records - offset;
135 if (offset > n_tag_records) {
136 fprintf(stderr, "Cannot access tag record beyond end\n");
137 goto tag_list_map_failed;
140 taglist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0);
141 if (taglist == MAP_FAILED) {
142 perror("Could not mmap tag file");
143 goto tag_list_map_failed;
145 retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count);
146 if (retlist == NULL) {
147 perror("Could not allocate memory for tag list");
148 goto tag_list_malloc_failed;
152 memcpy(retlist, taglist + offset, sizeof(struct blergref) * *count);
155 memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct blergref) * *count);
159 munmap(taglist, st.st_size);
163 tag_list_malloc_failed:
164 munmap(taglist, st.st_size);
167 tag_list_open_failed:
172 int tag_exists(const char *tag) {
175 if (!valid_tag_name(tag + 1))
178 if (!(tag[0] == '@' || tag[0] == '#')) {
179 fprintf(stderr, "Invalid tag: %s\n", tag);
185 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
188 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
191 fprintf(stderr, "Invalid tag type: %s\n", tag);
194 if (access(filename, F_OK) == -1)