13 #define MAX_TAG_LENGTH 64
16 #define TAG_CHAR(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_')
18 int tag_scan(const char *author, const char *data, int len, uint64_t record) {
19 char *taglist[MAX_TAGS];
23 for (i = 0; i < len; i++) {
25 if (data[i] == '#' || data[i] == '@') {
26 if (n_tags == MAX_TAGS) {
27 fprintf(stderr, "Too many tags in message\n");
32 while (i < len && TAG_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) {
35 if (start - i == 0) continue;
36 char *tag = calloc(1, MAX_TAG_LENGTH + 2);
37 memcpy(tag, &data[begin], i - begin);
38 for (j = 0; j < n_tags; j++) {
39 if (!strncmp(tag, taglist[j], MAX_TAG_LENGTH)) {
40 // We already have this tag. Start over.
45 taglist[n_tags] = tag;
47 // We goto here so i doesn't get incremented
52 for (i = 0; i < n_tags; i++) {
53 tag_add(author, taglist[i], record);
58 int tag_add(const char *author, const char *tag, uint64_t record) {
62 memset(t.author, 0, 32);
63 strncpy(t.author, author, 32);
68 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
71 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
74 fprintf(stderr, "Invalid tag type: %s\n", tag);
78 int tag_fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
79 flock(tag_fd, LOCK_EX);
81 perror("Could not open tag file");
84 int n = write(tag_fd, &t, sizeof(struct tag));
85 if (n < sizeof(struct tag)) {
86 perror("Could not write new tag");
89 flock(tag_fd, LOCK_UN);
95 struct tag * tag_list(const char *tag, uint64_t offset, uint64_t *count, int direction) {
100 uint64_t n_tag_records;
104 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
107 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
110 fprintf(stderr, "Invalid tag type: %s\n", tag);
114 int tag_fd = open(filename, O_RDONLY, 0600);
116 perror("Could not open tag file");
122 if (st.st_size == 0) {
127 n_tag_records = st.st_size / sizeof(struct tag);
128 if (*count > n_tag_records)
129 *count = n_tag_records;
130 if (offset > n_tag_records) {
131 fprintf(stderr, "Cannot access tag record beyond end\n");
135 taglist = (struct tag *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0);
136 if (taglist == MAP_FAILED) {
137 perror("Could not mmap tag file");
138 goto tag_list_map_failed;
140 retlist = (struct tag *) malloc(sizeof(struct tag) * *count);
141 if (retlist == NULL) {
142 perror("Could not allocate memory for tag list");
143 goto tag_list_malloc_failed;
147 memcpy(retlist, taglist + offset, sizeof(struct tag) * *count);
150 memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct tag) * *count);
154 munmap(taglist, st.st_size);
158 tag_list_malloc_failed:
159 munmap(taglist, st.st_size);
162 tag_list_open_failed:
166 int tag_exists(const char *tag) {
167 int taglen = strlen(tag);
171 fprintf(stderr, "Tag too short\n");
173 } else if (taglen > 33) {
174 fprintf(stderr, "Tag too long\n");
177 if (!(tag[0] == '@' || tag[0] == '#')) {
178 fprintf(stderr, "Invalid tag: %s\n", tag);
184 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
187 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
190 fprintf(stderr, "Invalid tag type: %s\n", tag);
193 if (access(filename, F_OK) == -1)