Further error handling refinement
[blerg.git] / database / tags.c
index 51d8698..5ec12fa 100644 (file)
@@ -104,7 +104,7 @@ struct tag * tag_list(const char *tag, uint64_t offset, int *count, int directio
        struct tag *retlist;
        uint64_t n_tag_records;
 
-       if (!valid_name(tag + 1))
+       if (!valid_tag_name(tag + 1))
                return NULL;
        
        switch(tag[0]) {
@@ -122,22 +122,19 @@ struct tag * tag_list(const char *tag, uint64_t offset, int *count, int directio
        int tag_fd = open(filename, O_RDONLY, 0600);
        if (tag_fd == -1) {
                perror("Could not open tag file");
-               *count = 0;
-               return NULL;
+               goto tag_list_open_failed;
        }
 
        fstat(tag_fd, &st);
        if (st.st_size == 0) {
-               close(tag_fd);
-               *count = 0;
-               return NULL;
+               goto tag_list_map_failed;
        }
        n_tag_records = st.st_size / sizeof(struct tag);
-       if (*count > n_tag_records)
-               *count = n_tag_records;
+       if (*count > n_tag_records - offset)
+               *count = n_tag_records - offset;
        if (offset > n_tag_records) {
                fprintf(stderr, "Cannot access tag record beyond end\n");
-               return NULL;
+               goto tag_list_map_failed;
        }
 
        taglist = (struct tag *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0);
@@ -168,13 +165,14 @@ tag_list_malloc_failed:
 tag_list_map_failed:
        close(tag_fd);
 tag_list_open_failed:
+       *count = 0;
        return NULL;
 }
 
 int tag_exists(const char *tag) {
        char filename[512];
 
-       if (!valid_name(tag + 1))
+       if (!valid_tag_name(tag + 1))
                return 0;
 
        if (!(tag[0] == '@' || tag[0] == '#')) {