diff options
Diffstat (limited to 'librazor/razor.c')
-rw-r--r-- | librazor/razor.c | 153 |
1 files changed, 139 insertions, 14 deletions
diff --git a/librazor/razor.c b/librazor/razor.c index bb05700..53187a6 100644 --- a/librazor/razor.c +++ b/librazor/razor.c @@ -51,12 +51,19 @@ struct razor_set_section razor_sections[] = { { RAZOR_STRING_POOL, offsetof(struct razor_set, string_pool) }, { RAZOR_PACKAGES, offsetof(struct razor_set, packages) }, { RAZOR_PROPERTIES, offsetof(struct razor_set, properties) }, - { RAZOR_FILES, offsetof(struct razor_set, files) }, { RAZOR_PACKAGE_POOL, offsetof(struct razor_set, package_pool) }, { RAZOR_PROPERTY_POOL, offsetof(struct razor_set, property_pool) }, - { RAZOR_FILE_POOL, offsetof(struct razor_set, file_pool) }, }; +struct razor_set_section razor_files_sections[] = { + { RAZOR_FILES, offsetof(struct razor_set, files) }, + { RAZOR_FILE_POOL, offsetof(struct razor_set, file_pool) }, + { RAZOR_FILE_STRING_POOL, offsetof(struct razor_set, file_string_pool) }, +}; + +struct razor_set_section razor_details_sections[] = { + { RAZOR_DETAILS_STRING_POOL, offsetof(struct razor_set, details_string_pool) }, +}; struct razor_set * razor_set_create(void) { @@ -112,6 +119,62 @@ razor_set_open(const char *filename) } void +razor_set_open_details(struct razor_set *set, const char *filename) +{ + struct razor_set_section *s; + struct stat stat; + struct array *array; + int fd; + + fd = open(filename, O_RDONLY); + if (fstat(fd, &stat) < 0) + return; + set->details_header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (set->details_header == MAP_FAILED) + return; + + for (s = set->details_header->sections; ~s->type; s++) { + if (s->type >= ARRAY_SIZE(razor_details_sections)) + continue; + if (s->type != razor_details_sections[s->type].type) + continue; + array = (void *) set + razor_details_sections[s->type].offset; + array->data = (void *) set->details_header + s->offset; + array->size = s->size; + array->alloc = s->size; + } + close(fd); +} + +void +razor_set_open_files(struct razor_set *set, const char *filename) +{ + struct razor_set_section *s; + struct stat stat; + struct array *array; + int fd; + + fd = open(filename, O_RDONLY); + if (fstat(fd, &stat) < 0) + return; + set->files_header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (set->files_header == MAP_FAILED) + return; + + for (s = set->files_header->sections; ~s->type; s++) { + if (s->type >= ARRAY_SIZE(razor_files_sections)) + continue; + if (s->type != razor_files_sections[s->type].type) + continue; + array = (void *) set + razor_files_sections[s->type].offset; + array->data = (void *) set->files_header + s->offset; + array->size = s->size; + array->alloc = s->size; + } + close(fd); +} + +void razor_set_destroy(struct razor_set *set) { unsigned int size; @@ -130,11 +193,37 @@ razor_set_destroy(struct razor_set *set) } } + if (set->details_header) { + for (i = 0; set->details_header->sections[i].type; i++) + ; + size = set->details_header->sections[i].type; + munmap(set->details_header, size); + } else { + for (i = 0; i < ARRAY_SIZE(razor_details_sections); i++) { + a = (void *) set + razor_details_sections[i].offset; + free(a->data); + } + } + + if (set->files_header) { + for (i = 0; set->files_header->sections[i].type; i++) + ; + size = set->files_header->sections[i].type; + munmap(set->files_header, size); + } else { + for (i = 0; i < ARRAY_SIZE(razor_files_sections); i++) { + a = (void *) set + razor_files_sections[i].offset; + free(a->data); + } + } + free(set); } -int -razor_set_write_to_fd(struct razor_set *set, int fd) +static int +razor_set_write_sections_to_fd(struct razor_set *set, int fd, int magic, + struct razor_set_section *sections, + size_t array_size) { char data[4096]; struct razor_set_header *header = (struct razor_set_header *) data; @@ -143,14 +232,14 @@ razor_set_write_to_fd(struct razor_set *set, int fd) int i; memset(data, 0, sizeof data); - header->magic = RAZOR_MAGIC; + header->magic = magic; header->version = RAZOR_VERSION; offset = sizeof data; - for (i = 0; i < ARRAY_SIZE(razor_sections); i++) { - if (razor_sections[i].type != i) + for (i = 0; i < array_size; i++) { + if (sections[i].type != i) continue; - a = (void *) set + razor_sections[i].offset; + a = (void *) set + sections[i].offset; header->sections[i].type = i; header->sections[i].offset = offset; header->sections[i].size = a->size; @@ -163,10 +252,10 @@ razor_set_write_to_fd(struct razor_set *set, int fd) razor_write(fd, data, sizeof data); memset(data, 0, sizeof data); - for (i = 0; i < ARRAY_SIZE(razor_sections); i++) { - if (razor_sections[i].type != i) + for (i = 0; i < array_size; i++) { + if (sections[i].type != i) continue; - a = (void *) set + razor_sections[i].offset; + a = (void *) set + sections[i].offset; razor_write(fd, a->data, a->size); razor_write(fd, data, ALIGN(a->size, 4096) - a->size); } @@ -175,7 +264,31 @@ razor_set_write_to_fd(struct razor_set *set, int fd) } int -razor_set_write(struct razor_set *set, const char *filename) +razor_set_write_to_fd(struct razor_set *set, int fd, + enum razor_repo_file_type type) +{ + switch (type) { + case RAZOR_REPO_FILE_MAIN: + return razor_set_write_sections_to_fd(set, fd, RAZOR_MAGIC, + razor_sections, + ARRAY_SIZE(razor_sections)); + + case RAZOR_REPO_FILE_DETAILS: + return razor_set_write_sections_to_fd(set, fd, RAZOR_DETAILS_MAGIC, + razor_details_sections, + ARRAY_SIZE(razor_details_sections)); + case RAZOR_REPO_FILE_FILES: + return razor_set_write_sections_to_fd(set, fd, RAZOR_FILES_MAGIC, + razor_files_sections, + ARRAY_SIZE(razor_files_sections)); + default: + return -1; + } +} + +int +razor_set_write(struct razor_set *set, const char *filename, + enum razor_repo_file_type type) { int fd, status; @@ -183,7 +296,7 @@ razor_set_write(struct razor_set *set, const char *filename) if (fd < 0) return -1; - status = razor_set_write_to_fd(set, fd); + status = razor_set_write_to_fd(set, fd, type); if (status) { close(fd); return status; @@ -191,7 +304,6 @@ razor_set_write(struct razor_set *set, const char *filename) return close(fd); } - void razor_build_evr(char *evr_buf, int size, const char *epoch, const char *version, const char *release) @@ -269,6 +381,19 @@ razor_set_get_package(struct razor_set *set, const char *package) return p; } +void +razor_package_get_details(struct razor_set *set, struct razor_package *package, + const char **summary, const char **description, + const char **url, const char **license) +{ + const char *pool = set->details_string_pool.data; + + *summary = &pool[package->summary]; + *description = &pool[package->description]; + *url = &pool[package->url]; + *license = &pool[package->license]; +} + struct razor_entry * razor_set_find_entry(struct razor_set *set, struct razor_entry *dir, const char *pattern) |