razor.c
changeset 229 cae6308aa5b1
parent 228 8b9849d1e5b0
     1.1 --- a/razor.c	Sun Jun 15 11:21:24 2008 -0400
     1.2 +++ b/razor.c	Sun Jun 15 18:16:20 2008 -0400
     1.3 @@ -49,18 +49,22 @@
     1.4  	struct razor_set_section sections[0];
     1.5  };
     1.6  
     1.7 -#define RAZOR_MAGIC 0x7a7a7a7a
     1.8 +#define RAZOR_MAGIC 		0x7a7a7a7a
     1.9 +#define RAZOR_DETAILS_MAGIC 	0x7a7a7a7b
    1.10 +#define RAZOR_FILES_MAGIC 	0x7a7a7a7c
    1.11  #define RAZOR_VERSION 1
    1.12  
    1.13  #define RAZOR_STRING_POOL		0
    1.14  #define RAZOR_PACKAGES			1
    1.15  #define RAZOR_PROPERTIES		2
    1.16 -#define RAZOR_FILES			3
    1.17 -#define RAZOR_PACKAGE_POOL		4
    1.18 -#define RAZOR_PROPERTY_POOL		5
    1.19 -#define RAZOR_FILE_POOL			6
    1.20 -#define RAZOR_FILE_STRING_POOL		7
    1.21 -#define RAZOR_DETAILS_STRING_POOL	8
    1.22 +#define RAZOR_PACKAGE_POOL		3
    1.23 +#define RAZOR_PROPERTY_POOL		4
    1.24 +
    1.25 +#define RAZOR_DETAILS_STRING_POOL	0
    1.26 +
    1.27 +#define RAZOR_FILES			0
    1.28 +#define RAZOR_FILE_POOL			1
    1.29 +#define RAZOR_FILE_STRING_POOL		2
    1.30  
    1.31  struct razor_package {
    1.32  	uint name  : 24;
    1.33 @@ -104,6 +108,8 @@
    1.34  	struct array file_string_pool;
    1.35  	struct array details_string_pool;
    1.36  	struct razor_set_header *header;
    1.37 +	struct razor_set_header *details_header;
    1.38 +	struct razor_set_header *files_header;
    1.39  };
    1.40  
    1.41  struct import_entry {
    1.42 @@ -144,11 +150,17 @@
    1.43  	{ RAZOR_STRING_POOL,		offsetof(struct razor_set, string_pool) },
    1.44  	{ RAZOR_PACKAGES,		offsetof(struct razor_set, packages) },
    1.45  	{ RAZOR_PROPERTIES,		offsetof(struct razor_set, properties) },
    1.46 -	{ RAZOR_FILES,			offsetof(struct razor_set, files) },
    1.47  	{ RAZOR_PACKAGE_POOL,		offsetof(struct razor_set, package_pool) },
    1.48  	{ RAZOR_PROPERTY_POOL,		offsetof(struct razor_set, property_pool) },
    1.49 +};
    1.50 +
    1.51 +struct razor_set_section razor_files_sections[] = {
    1.52 +	{ RAZOR_FILES,			offsetof(struct razor_set, files) },
    1.53  	{ RAZOR_FILE_POOL,		offsetof(struct razor_set, file_pool) },
    1.54  	{ RAZOR_FILE_STRING_POOL,	offsetof(struct razor_set, file_string_pool) },
    1.55 +};
    1.56 +
    1.57 +struct razor_set_section razor_details_sections[] = {
    1.58  	{ RAZOR_DETAILS_STRING_POOL,	offsetof(struct razor_set, details_string_pool) },
    1.59  };
    1.60  
    1.61 @@ -229,11 +241,93 @@
    1.62  		}
    1.63  	}
    1.64  
    1.65 +	if (set->details_header) {
    1.66 +		for (i = 0; set->details_header->sections[i].type; i++)
    1.67 +			;
    1.68 +		size = set->details_header->sections[i].type;
    1.69 +		munmap(set->details_header, size);
    1.70 +	} else {
    1.71 +		for (i = 0; i < ARRAY_SIZE(razor_details_sections); i++) {
    1.72 +			a = (void *) set + razor_details_sections[i].offset;
    1.73 +			free(a->data);
    1.74 +		}
    1.75 +	}
    1.76 +
    1.77 +	if (set->files_header) {
    1.78 +		for (i = 0; set->files_header->sections[i].type; i++)
    1.79 +			;
    1.80 +		size = set->files_header->sections[i].type;
    1.81 +		munmap(set->files_header, size);
    1.82 +	} else {
    1.83 +		for (i = 0; i < ARRAY_SIZE(razor_files_sections); i++) {
    1.84 +			a = (void *) set + razor_files_sections[i].offset;
    1.85 +			free(a->data);
    1.86 +		}
    1.87 +	}
    1.88 +
    1.89  	free(set);
    1.90  }
    1.91  
    1.92 -int
    1.93 -razor_set_write_to_fd(struct razor_set *set, int fd)
    1.94 +void
    1.95 +razor_set_open_details(struct razor_set *set, const char *filename)
    1.96 +{
    1.97 +	struct razor_set_section *s;
    1.98 +	struct stat stat;
    1.99 +	struct array *array;
   1.100 +	int fd;
   1.101 +
   1.102 +	fd = open(filename, O_RDONLY);
   1.103 +	if (fstat(fd, &stat) < 0)
   1.104 +		return;
   1.105 +	set->details_header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   1.106 +	if (set->details_header == MAP_FAILED)
   1.107 +		return;
   1.108 +
   1.109 +	for (s = set->details_header->sections; ~s->type; s++) {
   1.110 +		if (s->type >= ARRAY_SIZE(razor_details_sections))
   1.111 +			continue;
   1.112 +		if (s->type != razor_details_sections[s->type].type)
   1.113 +			continue;
   1.114 +		array = (void *) set + razor_details_sections[s->type].offset;
   1.115 +		array->data = (void *) set->details_header + s->offset;
   1.116 +		array->size = s->size;
   1.117 +		array->alloc = s->size;
   1.118 +	}
   1.119 +	close(fd);
   1.120 +}
   1.121 +
   1.122 +void
   1.123 +razor_set_open_files(struct razor_set *set, const char *filename)
   1.124 +{
   1.125 +	struct razor_set_section *s;
   1.126 +	struct stat stat;
   1.127 +	struct array *array;
   1.128 +	int fd;
   1.129 +
   1.130 +	fd = open(filename, O_RDONLY);
   1.131 +	if (fstat(fd, &stat) < 0)
   1.132 +		return;
   1.133 +	set->files_header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   1.134 +	if (set->files_header == MAP_FAILED)
   1.135 +		return;
   1.136 +
   1.137 +	for (s = set->files_header->sections; ~s->type; s++) {
   1.138 +		if (s->type >= ARRAY_SIZE(razor_files_sections))
   1.139 +			continue;
   1.140 +		if (s->type != razor_files_sections[s->type].type)
   1.141 +			continue;
   1.142 +		array = (void *) set + razor_files_sections[s->type].offset;
   1.143 +		array->data = (void *) set->files_header + s->offset;
   1.144 +		array->size = s->size;
   1.145 +		array->alloc = s->size;
   1.146 +	}
   1.147 +	close(fd);
   1.148 +}
   1.149 +
   1.150 +static int
   1.151 +razor_set_write_sections_to_fd(struct razor_set *set, int fd, int magic,
   1.152 +			       struct razor_set_section *sections,
   1.153 +			       size_t array_size)
   1.154  {
   1.155  	char data[4096];
   1.156  	struct razor_set_header *header = (struct razor_set_header *) data;
   1.157 @@ -242,14 +336,14 @@
   1.158  	int i;
   1.159  
   1.160  	memset(data, 0, sizeof data);
   1.161 -	header->magic = RAZOR_MAGIC;
   1.162 +	header->magic = magic;
   1.163  	header->version = RAZOR_VERSION;
   1.164  	offset = sizeof data;
   1.165  
   1.166 -	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   1.167 -		if (razor_sections[i].type != i)
   1.168 +	for (i = 0; i < array_size; i++) {
   1.169 +		if (sections[i].type != i)
   1.170  			continue;
   1.171 -		a = (void *) set + razor_sections[i].offset;
   1.172 +		a = (void *) set + sections[i].offset;
   1.173  		header->sections[i].type = i;
   1.174  		header->sections[i].offset = offset;
   1.175  		header->sections[i].size = a->size;
   1.176 @@ -262,10 +356,10 @@
   1.177  
   1.178  	razor_write(fd, data, sizeof data);
   1.179  	memset(data, 0, sizeof data);
   1.180 -	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   1.181 -		if (razor_sections[i].type != i)
   1.182 +	for (i = 0; i < array_size; i++) {
   1.183 +		if (sections[i].type != i)
   1.184  			continue;
   1.185 -		a = (void *) set + razor_sections[i].offset;
   1.186 +		a = (void *) set + sections[i].offset;
   1.187  		razor_write(fd, a->data, a->size);
   1.188  		razor_write(fd, data, ALIGN(a->size, 4096) - a->size);
   1.189  	}
   1.190 @@ -274,7 +368,31 @@
   1.191  }
   1.192  
   1.193  int
   1.194 -razor_set_write(struct razor_set *set, const char *filename)
   1.195 +razor_set_write_to_fd(struct razor_set *set, int fd,
   1.196 +		      enum razor_repo_file_type type)
   1.197 +{
   1.198 +	switch (type) {
   1.199 +	case RAZOR_REPO_FILE_MAIN:
   1.200 +		return razor_set_write_sections_to_fd(set, fd, RAZOR_MAGIC,
   1.201 +						      razor_sections,
   1.202 +						      ARRAY_SIZE(razor_sections));
   1.203 +
   1.204 +	case RAZOR_REPO_FILE_DETAILS:
   1.205 +		return razor_set_write_sections_to_fd(set, fd, RAZOR_DETAILS_MAGIC,
   1.206 +						      razor_details_sections,
   1.207 +						      ARRAY_SIZE(razor_details_sections));
   1.208 +	case RAZOR_REPO_FILE_FILES:
   1.209 +		return razor_set_write_sections_to_fd(set, fd, RAZOR_FILES_MAGIC,
   1.210 +						      razor_files_sections,
   1.211 +						      ARRAY_SIZE(razor_files_sections));
   1.212 +	default:
   1.213 +		return -1;
   1.214 +	}
   1.215 +}
   1.216 +
   1.217 +int
   1.218 +razor_set_write(struct razor_set *set, const char *filename,
   1.219 +		enum razor_repo_file_type type)
   1.220  {
   1.221  	int fd, status;
   1.222  
   1.223 @@ -282,7 +400,7 @@
   1.224  	if (fd < 0)
   1.225  		return -1;
   1.226  
   1.227 -	status = razor_set_write_to_fd(set, fd);
   1.228 +	status = razor_set_write_to_fd(set, fd, type);
   1.229  	if (status) {
   1.230  	    close(fd);
   1.231  	    return status;