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 #define MAX_TAG_LENGTH 64
20 #define TAG_CHAR(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_')
22 int tag_scan(const char *author, const char *data, int len, uint64_t record) {
23 char *taglist[MAX_TAGS];
27 for (i = 0; i < len; i++) {
29 if (data[i] == '#' || data[i] == '@') {
30 if (n_tags == MAX_TAGS) {
31 fprintf(stderr, "Too many tags in message\n");
36 while (i < len && TAG_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) {
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.
49 taglist[n_tags] = tag;
51 // We goto here so i doesn't get incremented
56 for (i = 0; i < n_tags; i++) {
57 tag_add(author, taglist[i], record);
62 int tag_add(const char *author, const char *tag, uint64_t record) {
66 memset(t.author, 0, 32);
67 strncpy(t.author, author, 32);
72 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
75 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
78 fprintf(stderr, "Invalid tag type: %s\n", tag);
82 int tag_fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
83 flock(tag_fd, LOCK_EX);
85 perror("Could not open tag file");
88 int n = write(tag_fd, &t, sizeof(struct tag));
89 if (n < sizeof(struct tag)) {
90 perror("Could not write new tag");
93 flock(tag_fd, LOCK_UN);
99 struct tag * tag_list(const char *tag, uint64_t offset, int *count, int direction) {
104 uint64_t n_tag_records;
106 if (!valid_name(tag + 1))
111 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
114 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
117 fprintf(stderr, "Invalid tag type: %s\n", tag);
121 int tag_fd = open(filename, O_RDONLY, 0600);
123 perror("Could not open tag file");
129 if (st.st_size == 0) {
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");
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;
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;
154 memcpy(retlist, taglist + offset, sizeof(struct tag) * *count);
157 memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct tag) * *count);
161 munmap(taglist, st.st_size);
165 tag_list_malloc_failed:
166 munmap(taglist, st.st_size);
169 tag_list_open_failed:
173 int tag_exists(const char *tag) {
176 if (!valid_name(tag + 1))
179 if (!(tag[0] == '@' || tag[0] == '#')) {
180 fprintf(stderr, "Invalid tag: %s\n", tag);
186 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
189 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
192 fprintf(stderr, "Invalid tag type: %s\n", tag);
195 if (access(filename, F_OK) == -1)