2019-01-28 20:01:36 +01:00
|
|
|
#include "pack.h"
|
|
|
|
|
2019-01-31 22:09:38 +01:00
|
|
|
static int handle_requires(input_file_t *f, void *obj)
|
2019-01-28 20:01:36 +01:00
|
|
|
{
|
|
|
|
char *ptr = f->line, *end;
|
2019-01-31 22:09:38 +01:00
|
|
|
pkg_desc_t *desc = obj;
|
2019-01-28 20:01:36 +01:00
|
|
|
dependency_t *dep;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
while (*ptr != '\0') {
|
|
|
|
while (isspace(*ptr))
|
|
|
|
++ptr;
|
|
|
|
end = ptr;
|
|
|
|
while (*end != '\0' && !isspace(*end))
|
|
|
|
++end;
|
|
|
|
|
|
|
|
len = end - ptr;
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (len > 0xFF) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "dependency name too long");
|
2019-01-28 20:01:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dep = calloc(1, sizeof(*dep) + len + 1);
|
|
|
|
if (dep == NULL) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "out of memory");
|
2019-01-28 20:01:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dep->name, ptr, len);
|
|
|
|
dep->type = PKG_DEPENDENCY_REQUIRES;
|
|
|
|
dep->next = desc->deps;
|
|
|
|
desc->deps = dep;
|
|
|
|
|
|
|
|
ptr = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:31:05 +01:00
|
|
|
static int handle_toc_compressor(input_file_t *f, void *obj)
|
|
|
|
{
|
|
|
|
pkg_desc_t *desc = obj;
|
|
|
|
|
|
|
|
desc->toccmp = compressor_by_name(f->line);
|
|
|
|
|
|
|
|
if (desc->toccmp == NULL) {
|
|
|
|
input_file_complain(f, "unkown compressor");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_data_compressor(input_file_t *f, void *obj)
|
|
|
|
{
|
|
|
|
pkg_desc_t *desc = obj;
|
|
|
|
|
|
|
|
desc->datacmp = compressor_by_name(f->line);
|
|
|
|
|
|
|
|
if (desc->datacmp == NULL) {
|
|
|
|
input_file_complain(f, "unkown compressor");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-31 22:09:38 +01:00
|
|
|
static const keyword_handler_t line_hooks[] = {
|
2019-02-07 23:31:05 +01:00
|
|
|
{ "toc-compressor", handle_toc_compressor },
|
|
|
|
{ "data-compressor", handle_data_compressor },
|
2019-01-28 20:01:36 +01:00
|
|
|
{ "requires", handle_requires },
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NUM_LINE_HOOKS (sizeof(line_hooks) / sizeof(line_hooks[0]))
|
|
|
|
|
2019-02-07 23:31:05 +01:00
|
|
|
static compressor_t *get_default_compressor(void)
|
|
|
|
{
|
|
|
|
compressor_t *cmp;
|
|
|
|
|
|
|
|
cmp = compressor_by_id(PKG_COMPRESSION_LZMA);
|
|
|
|
if (cmp != NULL)
|
|
|
|
return cmp;
|
|
|
|
|
|
|
|
cmp = compressor_by_id(PKG_COMPRESSION_ZLIB);
|
|
|
|
if (cmp != NULL)
|
|
|
|
return cmp;
|
|
|
|
|
|
|
|
cmp = compressor_by_id(PKG_COMPRESSION_NONE);
|
|
|
|
if (cmp != NULL)
|
|
|
|
return cmp;
|
|
|
|
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
2019-01-28 20:01:36 +01:00
|
|
|
int desc_read(const char *path, pkg_desc_t *desc)
|
|
|
|
{
|
|
|
|
input_file_t f;
|
2019-03-07 15:12:59 +01:00
|
|
|
char *ptr;
|
2019-01-28 20:01:36 +01:00
|
|
|
|
|
|
|
memset(desc, 0, sizeof(*desc));
|
|
|
|
|
|
|
|
if (open_file(&f, path))
|
|
|
|
return -1;
|
|
|
|
|
2019-01-31 22:09:38 +01:00
|
|
|
if (process_file(&f, line_hooks, NUM_LINE_HOOKS, desc)) {
|
|
|
|
cleanup_file(&f);
|
|
|
|
return -1;
|
2019-01-30 10:40:02 +01:00
|
|
|
}
|
|
|
|
|
2019-01-30 10:39:12 +01:00
|
|
|
cleanup_file(&f);
|
2019-02-07 23:31:05 +01:00
|
|
|
|
|
|
|
if (desc->datacmp == NULL)
|
|
|
|
desc->datacmp = get_default_compressor();
|
|
|
|
|
|
|
|
if (desc->toccmp == NULL)
|
|
|
|
desc->toccmp = get_default_compressor();
|
|
|
|
|
|
|
|
if (desc->datacmp == NULL || desc->toccmp == NULL) {
|
|
|
|
fputs("no compressor implementations available\n", stderr);
|
|
|
|
desc_free(desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:12:59 +01:00
|
|
|
ptr = strrchr(path, '/');
|
|
|
|
|
|
|
|
desc->name = strdup((ptr == NULL) ? path : (ptr + 1));
|
|
|
|
if (desc->name == NULL) {
|
|
|
|
fputs("out of memory\n", stderr);
|
|
|
|
desc_free(desc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = strrchr(desc->name, '.');
|
|
|
|
if (ptr != NULL)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2019-01-28 20:01:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void desc_free(pkg_desc_t *desc)
|
|
|
|
{
|
|
|
|
dependency_t *dep;
|
|
|
|
|
|
|
|
while (desc->deps != NULL) {
|
|
|
|
dep = desc->deps;
|
|
|
|
desc->deps = dep->next;
|
|
|
|
|
|
|
|
free(dep);
|
|
|
|
}
|
2019-01-30 10:40:02 +01:00
|
|
|
|
|
|
|
free(desc->name);
|
2019-01-28 20:01:36 +01:00
|
|
|
}
|