librazor/razor.c
changeset 258 29d5002bd17f
parent 248 057933050c42
child 259 5b0601d184ed
     1.1 --- a/librazor/razor.c	Fri Jun 20 15:10:34 2008 -0400
     1.2 +++ b/librazor/razor.c	Fri Jun 20 19:04:47 2008 -0400
     1.3 @@ -51,12 +51,19 @@
     1.4  	{ RAZOR_STRING_POOL,	offsetof(struct razor_set, string_pool) },
     1.5  	{ RAZOR_PACKAGES,	offsetof(struct razor_set, packages) },
     1.6  	{ RAZOR_PROPERTIES,	offsetof(struct razor_set, properties) },
     1.7 -	{ RAZOR_FILES,		offsetof(struct razor_set, files) },
     1.8  	{ RAZOR_PACKAGE_POOL,	offsetof(struct razor_set, package_pool) },
     1.9  	{ RAZOR_PROPERTY_POOL,	offsetof(struct razor_set, property_pool) },
    1.10 -	{ RAZOR_FILE_POOL,	offsetof(struct razor_set, file_pool) },
    1.11  };
    1.12  
    1.13 +struct razor_set_section razor_files_sections[] = {
    1.14 +	{ RAZOR_FILES,			offsetof(struct razor_set, files) },
    1.15 +	{ RAZOR_FILE_POOL,		offsetof(struct razor_set, file_pool) },
    1.16 +	{ RAZOR_FILE_STRING_POOL,	offsetof(struct razor_set, file_string_pool) },
    1.17 +};
    1.18 +
    1.19 +struct razor_set_section razor_details_sections[] = {
    1.20 +	{ RAZOR_DETAILS_STRING_POOL,	offsetof(struct razor_set, details_string_pool) },
    1.21 +};
    1.22  struct razor_set *
    1.23  razor_set_create(void)
    1.24  {
    1.25 @@ -112,6 +119,62 @@
    1.26  }
    1.27  
    1.28  void
    1.29 +razor_set_open_details(struct razor_set *set, const char *filename)
    1.30 +{
    1.31 +	struct razor_set_section *s;
    1.32 +	struct stat stat;
    1.33 +	struct array *array;
    1.34 +	int fd;
    1.35 +
    1.36 +	fd = open(filename, O_RDONLY);
    1.37 +	if (fstat(fd, &stat) < 0)
    1.38 +		return;
    1.39 +	set->details_header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    1.40 +	if (set->details_header == MAP_FAILED)
    1.41 +		return;
    1.42 +
    1.43 +	for (s = set->details_header->sections; ~s->type; s++) {
    1.44 +		if (s->type >= ARRAY_SIZE(razor_details_sections))
    1.45 +			continue;
    1.46 +		if (s->type != razor_details_sections[s->type].type)
    1.47 +			continue;
    1.48 +		array = (void *) set + razor_details_sections[s->type].offset;
    1.49 +		array->data = (void *) set->details_header + s->offset;
    1.50 +		array->size = s->size;
    1.51 +		array->alloc = s->size;
    1.52 +	}
    1.53 +	close(fd);
    1.54 +}
    1.55 +
    1.56 +void
    1.57 +razor_set_open_files(struct razor_set *set, const char *filename)
    1.58 +{
    1.59 +	struct razor_set_section *s;
    1.60 +	struct stat stat;
    1.61 +	struct array *array;
    1.62 +	int fd;
    1.63 +
    1.64 +	fd = open(filename, O_RDONLY);
    1.65 +	if (fstat(fd, &stat) < 0)
    1.66 +		return;
    1.67 +	set->files_header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    1.68 +	if (set->files_header == MAP_FAILED)
    1.69 +		return;
    1.70 +
    1.71 +	for (s = set->files_header->sections; ~s->type; s++) {
    1.72 +		if (s->type >= ARRAY_SIZE(razor_files_sections))
    1.73 +			continue;
    1.74 +		if (s->type != razor_files_sections[s->type].type)
    1.75 +			continue;
    1.76 +		array = (void *) set + razor_files_sections[s->type].offset;
    1.77 +		array->data = (void *) set->files_header + s->offset;
    1.78 +		array->size = s->size;
    1.79 +		array->alloc = s->size;
    1.80 +	}
    1.81 +	close(fd);
    1.82 +}
    1.83 +
    1.84 +void
    1.85  razor_set_destroy(struct razor_set *set)
    1.86  {
    1.87  	unsigned int size;
    1.88 @@ -130,11 +193,37 @@
    1.89  		}
    1.90  	}
    1.91  
    1.92 +	if (set->details_header) {
    1.93 +		for (i = 0; set->details_header->sections[i].type; i++)
    1.94 +			;
    1.95 +		size = set->details_header->sections[i].type;
    1.96 +		munmap(set->details_header, size);
    1.97 +	} else {
    1.98 +		for (i = 0; i < ARRAY_SIZE(razor_details_sections); i++) {
    1.99 +			a = (void *) set + razor_details_sections[i].offset;
   1.100 +			free(a->data);
   1.101 +		}
   1.102 +	}
   1.103 +
   1.104 +	if (set->files_header) {
   1.105 +		for (i = 0; set->files_header->sections[i].type; i++)
   1.106 +			;
   1.107 +		size = set->files_header->sections[i].type;
   1.108 +		munmap(set->files_header, size);
   1.109 +	} else {
   1.110 +		for (i = 0; i < ARRAY_SIZE(razor_files_sections); i++) {
   1.111 +			a = (void *) set + razor_files_sections[i].offset;
   1.112 +			free(a->data);
   1.113 +		}
   1.114 +	}
   1.115 +
   1.116  	free(set);
   1.117  }
   1.118  
   1.119 -int
   1.120 -razor_set_write_to_fd(struct razor_set *set, int fd)
   1.121 +static int
   1.122 +razor_set_write_sections_to_fd(struct razor_set *set, int fd, int magic,
   1.123 +			       struct razor_set_section *sections,
   1.124 +			       size_t array_size)
   1.125  {
   1.126  	char data[4096];
   1.127  	struct razor_set_header *header = (struct razor_set_header *) data;
   1.128 @@ -143,14 +232,14 @@
   1.129  	int i;
   1.130  
   1.131  	memset(data, 0, sizeof data);
   1.132 -	header->magic = RAZOR_MAGIC;
   1.133 +	header->magic = magic;
   1.134  	header->version = RAZOR_VERSION;
   1.135  	offset = sizeof data;
   1.136  
   1.137 -	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   1.138 -		if (razor_sections[i].type != i)
   1.139 +	for (i = 0; i < array_size; i++) {
   1.140 +		if (sections[i].type != i)
   1.141  			continue;
   1.142 -		a = (void *) set + razor_sections[i].offset;
   1.143 +		a = (void *) set + sections[i].offset;
   1.144  		header->sections[i].type = i;
   1.145  		header->sections[i].offset = offset;
   1.146  		header->sections[i].size = a->size;
   1.147 @@ -163,10 +252,10 @@
   1.148  
   1.149  	razor_write(fd, data, sizeof data);
   1.150  	memset(data, 0, sizeof data);
   1.151 -	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   1.152 -		if (razor_sections[i].type != i)
   1.153 +	for (i = 0; i < array_size; i++) {
   1.154 +		if (sections[i].type != i)
   1.155  			continue;
   1.156 -		a = (void *) set + razor_sections[i].offset;
   1.157 +		a = (void *) set + sections[i].offset;
   1.158  		razor_write(fd, a->data, a->size);
   1.159  		razor_write(fd, data, ALIGN(a->size, 4096) - a->size);
   1.160  	}
   1.161 @@ -175,7 +264,31 @@
   1.162  }
   1.163  
   1.164  int
   1.165 -razor_set_write(struct razor_set *set, const char *filename)
   1.166 +razor_set_write_to_fd(struct razor_set *set, int fd,
   1.167 +		      enum razor_repo_file_type type)
   1.168 +{
   1.169 +	switch (type) {
   1.170 +	case RAZOR_REPO_FILE_MAIN:
   1.171 +		return razor_set_write_sections_to_fd(set, fd, RAZOR_MAGIC,
   1.172 +						      razor_sections,
   1.173 +						      ARRAY_SIZE(razor_sections));
   1.174 +
   1.175 +	case RAZOR_REPO_FILE_DETAILS:
   1.176 +		return razor_set_write_sections_to_fd(set, fd, RAZOR_DETAILS_MAGIC,
   1.177 +						      razor_details_sections,
   1.178 +						      ARRAY_SIZE(razor_details_sections));
   1.179 +	case RAZOR_REPO_FILE_FILES:
   1.180 +		return razor_set_write_sections_to_fd(set, fd, RAZOR_FILES_MAGIC,
   1.181 +						      razor_files_sections,
   1.182 +						      ARRAY_SIZE(razor_files_sections));
   1.183 +	default:
   1.184 +		return -1;
   1.185 +	}
   1.186 +}
   1.187 +
   1.188 +int
   1.189 +razor_set_write(struct razor_set *set, const char *filename,
   1.190 +		enum razor_repo_file_type type)
   1.191  {
   1.192  	int fd, status;
   1.193  
   1.194 @@ -183,7 +296,7 @@
   1.195  	if (fd < 0)
   1.196  		return -1;
   1.197  
   1.198 -	status = razor_set_write_to_fd(set, fd);
   1.199 +	status = razor_set_write_to_fd(set, fd, type);
   1.200  	if (status) {
   1.201  	    close(fd);
   1.202  	    return status;
   1.203 @@ -191,7 +304,6 @@
   1.204  
   1.205  	return close(fd);
   1.206  }
   1.207 -
   1.208  void
   1.209  razor_build_evr(char *evr_buf, int size, const char *epoch,
   1.210  		const char *version, const char *release)
   1.211 @@ -269,6 +381,19 @@
   1.212  	return p;
   1.213  }
   1.214  
   1.215 +void
   1.216 +razor_package_get_details(struct razor_set *set, struct razor_package *package,
   1.217 +			  const char **summary, const char **description,
   1.218 +			  const char **url, const char **license)
   1.219 +{
   1.220 +	const char *pool = set->details_string_pool.data;
   1.221 +
   1.222 +	*summary = &pool[package->summary];
   1.223 +	*description = &pool[package->description];
   1.224 +	*url = &pool[package->url];
   1.225 +	*license = &pool[package->license];
   1.226 +}
   1.227 +
   1.228  struct razor_entry *
   1.229  razor_set_find_entry(struct razor_set *set,
   1.230  		     struct razor_entry *dir, const char *pattern)