14 #define MAX_TAG_LENGTH 64
17 #define TAG_CHAR(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_')
19 int tag_scan(const char *author, const char *data, int len, uint64_t record) {
20 char *taglist[MAX_TAGS];
24 for (i = 0; i < len; i++) {
26 if (data[i] == '#' || data[i] == '@') {
27 if (n_tags == MAX_TAGS) {
28 fprintf(stderr, "Too many tags in message\n");
33 while (i < len && TAG_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) {
36 if (start - i == 0) continue;
37 char *tag = calloc(1, MAX_TAG_LENGTH + 2);
38 memcpy(tag, &data[begin], i - begin);
39 for (j = 0; j < n_tags; j++) {
40 if (!strncmp(tag, taglist[j], MAX_TAG_LENGTH)) {
41 // We already have this tag. Start over.
46 taglist[n_tags] = tag;
48 // We goto here so i doesn't get incremented
53 for (i = 0; i < n_tags; i++) {
54 tag_add(author, taglist[i], record);
59 int tag_add(const char *author, const char *tag, uint64_t record) {
63 memset(t.author, 0, 32);
64 strncpy(t.author, author, 32);
69 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
72 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
75 fprintf(stderr, "Invalid tag type: %s\n", tag);
79 int tag_fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
80 flock(tag_fd, LOCK_EX);
82 perror("Could not open tag file");
85 int n = write(tag_fd, &t, sizeof(struct tag));
86 if (n < sizeof(struct tag)) {
87 perror("Could not write new tag");
90 flock(tag_fd, LOCK_UN);
96 struct tag * tag_list(const char *tag, uint64_t offset, int *count, int direction) {
101 uint64_t n_tag_records;
103 if (!valid_name(tag + 1))
108 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
111 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
114 fprintf(stderr, "Invalid tag type: %s\n", tag);
118 int tag_fd = open(filename, O_RDONLY, 0600);
120 perror("Could not open tag file");
126 if (st.st_size == 0) {
131 n_tag_records = st.st_size / sizeof(struct tag);
132 if (*count > n_tag_records)
133 *count = n_tag_records;
134 if (offset > n_tag_records) {
135 fprintf(stderr, "Cannot access tag record beyond end\n");
139 taglist = (struct tag *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0);
140 if (taglist == MAP_FAILED) {
141 perror("Could not mmap tag file");
142 goto tag_list_map_failed;
144 retlist = (struct tag *) malloc(sizeof(struct tag) * *count);
145 if (retlist == NULL) {
146 perror("Could not allocate memory for tag list");
147 goto tag_list_malloc_failed;
151 memcpy(retlist, taglist + offset, sizeof(struct tag) * *count);
154 memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct tag) * *count);
158 munmap(taglist, st.st_size);
162 tag_list_malloc_failed:
163 munmap(taglist, st.st_size);
166 tag_list_open_failed:
170 int tag_exists(const char *tag) {
173 if (!valid_name(tag + 1))
176 if (!(tag[0] == '@' || tag[0] == '#')) {
177 fprintf(stderr, "Invalid tag: %s\n", tag);
183 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
186 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
189 fprintf(stderr, "Invalid tag type: %s\n", tag);
192 if (access(filename, F_OK) == -1)