Use strings to identify section types in the on-disk repo format.
Previously, a given razor file type had a fixed number of sections in a
fixed order, identified by an integer type. Now, sections are identified
by a named string (stored in a string pool after the section lists).
This will allow for razor files to contain arbitrary sections.
For bonus points, also drop the 4k section alignment and change the
magic byte string to "RZDB".
committer: Kristian H?gsberg <krh@redhat.com>
1.1 --- a/TODO Tue Jul 08 22:57:34 2008 -0400
1.2 +++ b/TODO Wed Jul 09 10:11:13 2008 -0400
1.3 @@ -118,14 +118,7 @@
1.4
1.5 Package set file format items:
1.6
1.7 -- drop the 4k section alignment
1.8 -
1.9 -- just use strings for header identifiers, make the string pool
1.10 - section have a fixed string (maybe make "strings" always the first
1.11 - string so its index is 0), or maybe just require that it's the first
1.12 - section in the file.
1.13 -
1.14 -- nail down byte-order of rzdb file.
1.15 +- nail down byte-order of repo file.
1.16
1.17 - version the sections in the file, put the element size in the header
1.18 so we can add stuff to elements in a backwards compatible way.
2.1 --- a/librazor/razor-internal.h Tue Jul 08 22:57:34 2008 -0400
2.2 +++ b/librazor/razor-internal.h Wed Jul 09 10:11:13 2008 -0400
2.3 @@ -82,7 +82,7 @@
2.4
2.5
2.6 struct razor_set_section {
2.7 - uint32_t type;
2.8 + uint32_t name;
2.9 uint32_t offset;
2.10 uint32_t size;
2.11 };
2.12 @@ -90,25 +90,23 @@
2.13 struct razor_set_header {
2.14 uint32_t magic;
2.15 uint32_t version;
2.16 - struct razor_set_section sections[0];
2.17 + uint32_t num_sections;
2.18 };
2.19
2.20 -#define RAZOR_MAGIC 0x7a7a7a7a
2.21 -#define RAZOR_DETAILS_MAGIC 0x7a7a7a7b
2.22 -#define RAZOR_FILES_MAGIC 0x7a7a7a7c
2.23 -#define RAZOR_VERSION 1
2.24 +#define RAZOR_MAGIC 0x525a4442
2.25 +#define RAZOR_VERSION 1
2.26
2.27 -#define RAZOR_STRING_POOL 0
2.28 -#define RAZOR_PACKAGES 1
2.29 -#define RAZOR_PROPERTIES 2
2.30 -#define RAZOR_PACKAGE_POOL 3
2.31 -#define RAZOR_PROPERTY_POOL 4
2.32 +#define RAZOR_STRING_POOL "string_pool"
2.33 +#define RAZOR_PACKAGES "packages"
2.34 +#define RAZOR_PROPERTIES "properties"
2.35 +#define RAZOR_PACKAGE_POOL "package_pool"
2.36 +#define RAZOR_PROPERTY_POOL "property_pool"
2.37
2.38 -#define RAZOR_DETAILS_STRING_POOL 0
2.39 +#define RAZOR_DETAILS_STRING_POOL "details_string_pool"
2.40
2.41 -#define RAZOR_FILES 0
2.42 -#define RAZOR_FILE_POOL 1
2.43 -#define RAZOR_FILE_STRING_POOL 2
2.44 +#define RAZOR_FILES "files"
2.45 +#define RAZOR_FILE_POOL "file_pool"
2.46 +#define RAZOR_FILE_STRING_POOL "file_string_pool"
2.47
2.48 struct razor_package {
2.49 uint name : 24;
2.50 @@ -150,9 +148,15 @@
2.51 struct array file_pool;
2.52 struct array file_string_pool;
2.53 struct array details_string_pool;
2.54 +
2.55 struct razor_set_header *header;
2.56 + size_t header_size;
2.57 +
2.58 struct razor_set_header *details_header;
2.59 + size_t details_header_size;
2.60 +
2.61 struct razor_set_header *files_header;
2.62 + size_t files_header_size;
2.63 };
2.64
2.65 struct import_entry {
3.1 --- a/librazor/razor.c Tue Jul 08 22:57:34 2008 -0400
3.2 +++ b/librazor/razor.c Wed Jul 09 10:11:13 2008 -0400
3.3 @@ -49,7 +49,12 @@
3.4 return p;
3.5 }
3.6
3.7 -struct razor_set_section razor_sections[] = {
3.8 +struct razor_set_section_index {
3.9 + const char *name;
3.10 + uint32_t offset;
3.11 +};
3.12 +
3.13 +struct razor_set_section_index razor_sections[] = {
3.14 { RAZOR_STRING_POOL, offsetof(struct razor_set, string_pool) },
3.15 { RAZOR_PACKAGES, offsetof(struct razor_set, packages) },
3.16 { RAZOR_PROPERTIES, offsetof(struct razor_set, properties) },
3.17 @@ -57,13 +62,13 @@
3.18 { RAZOR_PROPERTY_POOL, offsetof(struct razor_set, property_pool) },
3.19 };
3.20
3.21 -struct razor_set_section razor_files_sections[] = {
3.22 +struct razor_set_section_index razor_files_sections[] = {
3.23 { RAZOR_FILES, offsetof(struct razor_set, files) },
3.24 { RAZOR_FILE_POOL, offsetof(struct razor_set, file_pool) },
3.25 { RAZOR_FILE_STRING_POOL, offsetof(struct razor_set, file_string_pool) },
3.26 };
3.27
3.28 -struct razor_set_section razor_details_sections[] = {
3.29 +struct razor_set_section_index razor_details_sections[] = {
3.30 { RAZOR_DETAILS_STRING_POOL, offsetof(struct razor_set, details_string_pool) },
3.31 };
3.32
3.33 @@ -87,65 +92,42 @@
3.34 return set;
3.35 }
3.36
3.37 -RAZOR_EXPORT struct razor_set *
3.38 -razor_set_open(const char *filename)
3.39 +static int
3.40 +razor_set_bind_sections(struct razor_set *set,
3.41 + struct razor_set_header **header,
3.42 + size_t *header_size,
3.43 + struct razor_set_section_index section_index[],
3.44 + int section_index_size,
3.45 + const char *filename)
3.46 {
3.47 - struct razor_set *set;
3.48 - struct razor_set_section *s;
3.49 + struct razor_set_section *s, *sections;
3.50 struct stat stat;
3.51 struct array *array;
3.52 - int fd;
3.53 -
3.54 - set = zalloc(sizeof *set);
3.55 - fd = open(filename, O_RDONLY);
3.56 - if (fstat(fd, &stat) < 0)
3.57 - return NULL;
3.58 - set->header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3.59 - if (set->header == MAP_FAILED) {
3.60 - free(set);
3.61 - return NULL;
3.62 - }
3.63 -
3.64 - for (s = set->header->sections; ~s->type; s++) {
3.65 - if (s->type >= ARRAY_SIZE(razor_sections))
3.66 - continue;
3.67 - if (s->type != razor_sections[s->type].type)
3.68 - continue;
3.69 - array = (void *) set + razor_sections[s->type].offset;
3.70 - array->data = (void *) set->header + s->offset;
3.71 - array->size = s->size;
3.72 - array->alloc = s->size;
3.73 - }
3.74 - close(fd);
3.75 -
3.76 - return set;
3.77 -}
3.78 -
3.79 -RAZOR_EXPORT int
3.80 -razor_set_open_details(struct razor_set *set, const char *filename)
3.81 -{
3.82 - struct razor_set_section *s;
3.83 - struct stat stat;
3.84 - struct array *array;
3.85 - int fd;
3.86 -
3.87 - assert (set != NULL);
3.88 - assert (filename != NULL);
3.89 + const char *pool;
3.90 + int fd, i;
3.91
3.92 fd = open(filename, O_RDONLY);
3.93 if (fstat(fd, &stat) < 0)
3.94 return -1;
3.95 - set->details_header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3.96 - if (set->details_header == MAP_FAILED)
3.97 + *header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3.98 + if (*header == MAP_FAILED)
3.99 return -1;
3.100 + *header_size = stat.st_size;
3.101
3.102 - for (s = set->details_header->sections; ~s->type; s++) {
3.103 - if (s->type >= ARRAY_SIZE(razor_details_sections))
3.104 + sections = (void *) *header + sizeof **header;
3.105 + pool = (void *) sections + (*header)->num_sections * sizeof *sections;
3.106 +
3.107 + for (i = 0; i < (*header)->num_sections; i++) {
3.108 + int j;
3.109 + s = sections + i;
3.110 + for (j = 0; j < section_index_size; j++)
3.111 + if (!strcmp(section_index[j].name,
3.112 + &pool[s->name]))
3.113 + break;
3.114 + if (j == section_index_size)
3.115 continue;
3.116 - if (s->type != razor_details_sections[s->type].type)
3.117 - continue;
3.118 - array = (void *) set + razor_details_sections[s->type].offset;
3.119 - array->data = (void *) set->details_header + s->offset;
3.120 + array = (void *) set + section_index[j].offset;
3.121 + array->data = (void *) *header + s->offset;
3.122 array->size = s->size;
3.123 array->alloc = s->size;
3.124 }
3.125 @@ -154,53 +136,51 @@
3.126 return 0;
3.127 }
3.128
3.129 +RAZOR_EXPORT struct razor_set *
3.130 +razor_set_open(const char *filename)
3.131 +{
3.132 + struct razor_set *set;
3.133 +
3.134 + set = zalloc(sizeof *set);
3.135 + if (razor_set_bind_sections(set, &set->header, &set->header_size,
3.136 + razor_sections, ARRAY_SIZE(razor_sections),
3.137 + filename)){
3.138 + free(set);
3.139 + return NULL;
3.140 + }
3.141 + return set;
3.142 +}
3.143 +
3.144 +RAZOR_EXPORT int
3.145 +razor_set_open_details(struct razor_set *set, const char *filename)
3.146 +{
3.147 + return razor_set_bind_sections(set, &set->details_header,
3.148 + &set->details_header_size,
3.149 + razor_details_sections,
3.150 + ARRAY_SIZE(razor_details_sections),
3.151 + filename);
3.152 +}
3.153 +
3.154 RAZOR_EXPORT int
3.155 razor_set_open_files(struct razor_set *set, const char *filename)
3.156 {
3.157 - struct razor_set_section *s;
3.158 - struct stat stat;
3.159 - struct array *array;
3.160 - int fd;
3.161 -
3.162 - assert (set != NULL);
3.163 - assert (filename != NULL);
3.164 -
3.165 - fd = open(filename, O_RDONLY);
3.166 - if (fstat(fd, &stat) < 0)
3.167 - return -1;
3.168 - set->files_header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3.169 - if (set->files_header == MAP_FAILED)
3.170 - return -1;
3.171 -
3.172 - for (s = set->files_header->sections; ~s->type; s++) {
3.173 - if (s->type >= ARRAY_SIZE(razor_files_sections))
3.174 - continue;
3.175 - if (s->type != razor_files_sections[s->type].type)
3.176 - continue;
3.177 - array = (void *) set + razor_files_sections[s->type].offset;
3.178 - array->data = (void *) set->files_header + s->offset;
3.179 - array->size = s->size;
3.180 - array->alloc = s->size;
3.181 - }
3.182 - close(fd);
3.183 -
3.184 - return 0;
3.185 + return razor_set_bind_sections(set, &set->files_header,
3.186 + &set->files_header_size,
3.187 + razor_files_sections,
3.188 + ARRAY_SIZE(razor_files_sections),
3.189 + filename);
3.190 }
3.191
3.192 RAZOR_EXPORT void
3.193 razor_set_destroy(struct razor_set *set)
3.194 {
3.195 - unsigned int size;
3.196 struct array *a;
3.197 int i;
3.198
3.199 assert (set != NULL);
3.200
3.201 if (set->header) {
3.202 - for (i = 0; set->header->sections[i].type; i++)
3.203 - ;
3.204 - size = set->header->sections[i].type;
3.205 - munmap(set->header, size);
3.206 + munmap(set->header, set->header_size);
3.207 } else {
3.208 for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
3.209 a = (void *) set + razor_sections[i].offset;
3.210 @@ -209,10 +189,7 @@
3.211 }
3.212
3.213 if (set->details_header) {
3.214 - for (i = 0; set->details_header->sections[i].type; i++)
3.215 - ;
3.216 - size = set->details_header->sections[i].type;
3.217 - munmap(set->details_header, size);
3.218 + munmap(set->details_header, set->details_header_size);
3.219 } else {
3.220 for (i = 0; i < ARRAY_SIZE(razor_details_sections); i++) {
3.221 a = (void *) set + razor_details_sections[i].offset;
3.222 @@ -221,10 +198,7 @@
3.223 }
3.224
3.225 if (set->files_header) {
3.226 - for (i = 0; set->files_header->sections[i].type; i++)
3.227 - ;
3.228 - size = set->files_header->sections[i].type;
3.229 - munmap(set->files_header, size);
3.230 + munmap(set->files_header, set->files_header_size);
3.231 } else {
3.232 for (i = 0; i < ARRAY_SIZE(razor_files_sections); i++) {
3.233 a = (void *) set + razor_files_sections[i].offset;
3.234 @@ -236,45 +210,50 @@
3.235 }
3.236
3.237 static int
3.238 -razor_set_write_sections_to_fd(struct razor_set *set, int fd, int magic,
3.239 - struct razor_set_section *sections,
3.240 +razor_set_write_sections_to_fd(struct razor_set *set, int fd,
3.241 + struct razor_set_section_index *sections,
3.242 size_t array_size)
3.243 {
3.244 - char data[4096];
3.245 - struct razor_set_header *header = (struct razor_set_header *) data;
3.246 - struct array *a;
3.247 + struct razor_set_header header;
3.248 + struct razor_set_section *out_sections =
3.249 + malloc(array_size * sizeof *out_sections);
3.250 + struct hashtable table;
3.251 + struct array *a, pool;
3.252 uint32_t offset;
3.253 int i;
3.254
3.255 - memset(data, 0, sizeof data);
3.256 - header->magic = magic;
3.257 - header->version = RAZOR_VERSION;
3.258 - offset = sizeof data;
3.259 + header.magic = RAZOR_MAGIC;
3.260 + header.version = RAZOR_VERSION;
3.261 + header.num_sections = array_size;
3.262 + offset = sizeof header + array_size * sizeof *out_sections;
3.263 +
3.264 + array_init(&pool);
3.265 + hashtable_init(&table, &pool);
3.266 +
3.267 + for (i = 0; i < array_size; i++)
3.268 + out_sections[i].name =
3.269 + hashtable_tokenize(&table, sections[i].name);
3.270 +
3.271 + offset += pool.size;
3.272
3.273 for (i = 0; i < array_size; i++) {
3.274 - if (sections[i].type != i)
3.275 - continue;
3.276 a = (void *) set + sections[i].offset;
3.277 - header->sections[i].type = i;
3.278 - header->sections[i].offset = offset;
3.279 - header->sections[i].size = a->size;
3.280 - offset += ALIGN(a->size, 4096);
3.281 + out_sections[i].offset = offset;
3.282 + out_sections[i].size = a->size;
3.283 + offset += a->size;
3.284 }
3.285
3.286 - header->sections[i].type = ~0;
3.287 - header->sections[i].offset = 0;
3.288 - header->sections[i].size = 0;
3.289 + razor_write(fd, &header, sizeof header);
3.290 + razor_write(fd, out_sections, array_size * sizeof *out_sections);
3.291 + razor_write(fd, pool.data, pool.size);
3.292
3.293 - razor_write(fd, data, sizeof data);
3.294 - memset(data, 0, sizeof data);
3.295 for (i = 0; i < array_size; i++) {
3.296 - if (sections[i].type != i)
3.297 - continue;
3.298 a = (void *) set + sections[i].offset;
3.299 razor_write(fd, a->data, a->size);
3.300 - razor_write(fd, data, ALIGN(a->size, 4096) - a->size);
3.301 }
3.302
3.303 + free(out_sections);
3.304 +
3.305 return 0;
3.306 }
3.307
3.308 @@ -284,16 +263,16 @@
3.309 {
3.310 switch (type) {
3.311 case RAZOR_REPO_FILE_MAIN:
3.312 - return razor_set_write_sections_to_fd(set, fd, RAZOR_MAGIC,
3.313 + return razor_set_write_sections_to_fd(set, fd,
3.314 razor_sections,
3.315 ARRAY_SIZE(razor_sections));
3.316
3.317 case RAZOR_REPO_FILE_DETAILS:
3.318 - return razor_set_write_sections_to_fd(set, fd, RAZOR_DETAILS_MAGIC,
3.319 + return razor_set_write_sections_to_fd(set, fd,
3.320 razor_details_sections,
3.321 ARRAY_SIZE(razor_details_sections));
3.322 case RAZOR_REPO_FILE_FILES:
3.323 - return razor_set_write_sections_to_fd(set, fd, RAZOR_FILES_MAGIC,
3.324 + return razor_set_write_sections_to_fd(set, fd,
3.325 razor_files_sections,
3.326 ARRAY_SIZE(razor_files_sections));
3.327 default: