2019-01-19 17:26:41 +01:00
|
|
|
#include "pack.h"
|
|
|
|
|
|
|
|
static char *skipspace(char *str)
|
|
|
|
{
|
|
|
|
while (isspace(*str))
|
|
|
|
++str;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void oom(input_file_t *f)
|
|
|
|
{
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "out of memory");
|
2019-01-19 17:26:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static image_entry_t *filelist_mkentry(input_file_t *f, mode_t filetype)
|
|
|
|
{
|
|
|
|
char *line = f->line;
|
|
|
|
image_entry_t *ent;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
ent = calloc(1, sizeof(*ent));
|
|
|
|
if (ent == NULL) {
|
|
|
|
oom(f);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* name */
|
|
|
|
for (i = 0; !isspace(line[i]) && line[i] != '\0'; ++i)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (!isspace(line[i])) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "expected space after file name");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ent->name = calloc(1, i + 1);
|
|
|
|
if (ent->name == NULL) {
|
|
|
|
oom(f);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ent->name, line, i);
|
|
|
|
if (canonicalize_name(ent->name)) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "invalid file name");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ent->name[0] == '\0') {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "refusing to add entry for '/'");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(ent->name) > 0xFFFF) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "name too long");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = skipspace(line + i);
|
|
|
|
|
|
|
|
/* mode */
|
|
|
|
if (!isdigit(*line)) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "expected numeric mode after file name");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (isdigit(*line)) {
|
|
|
|
if (*line > '7') {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "mode must be octal number");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ent->mode = (ent->mode << 3) | (*(line++) - '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isspace(*line)) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "expected space after file mode");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = skipspace(line);
|
|
|
|
|
|
|
|
ent->mode = (ent->mode & ~S_IFMT) | filetype;
|
|
|
|
|
|
|
|
/* uid */
|
|
|
|
if (!isdigit(*line)) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "expected numeric UID after mode");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (isdigit(*line))
|
|
|
|
ent->uid = (ent->uid * 10) + (*(line++) - '0');
|
|
|
|
|
|
|
|
if (!isspace(*line)) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "expected space after UID");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = skipspace(line);
|
|
|
|
|
|
|
|
/* gid */
|
|
|
|
if (!isdigit(*line)) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "expected numeric GID after UID");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (isdigit(*line))
|
|
|
|
ent->gid = (ent->gid * 10) + (*(line++) - '0');
|
|
|
|
|
|
|
|
line = skipspace(line);
|
|
|
|
|
|
|
|
/* remove processed data */
|
|
|
|
memmove(f->line, line, strlen(line) + 1);
|
|
|
|
return ent;
|
|
|
|
fail:
|
|
|
|
free(ent->name);
|
|
|
|
free(ent);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-31 22:09:38 +01:00
|
|
|
int filelist_mkdir(input_file_t *f, void *obj)
|
2019-01-19 17:26:41 +01:00
|
|
|
{
|
2019-01-31 22:09:38 +01:00
|
|
|
image_entry_t *ent = filelist_mkentry(f, S_IFDIR);
|
|
|
|
image_entry_t **listptr = obj;
|
|
|
|
|
|
|
|
if (ent == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ent->next = *listptr;
|
|
|
|
*listptr = ent;
|
|
|
|
return 0;
|
2019-01-19 17:26:41 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 22:09:38 +01:00
|
|
|
int filelist_mkslink(input_file_t *f, void *obj)
|
2019-01-19 17:26:41 +01:00
|
|
|
{
|
|
|
|
image_entry_t *ent = filelist_mkentry(f, S_IFLNK);
|
2019-01-31 22:09:38 +01:00
|
|
|
image_entry_t **listptr = obj;
|
2019-01-19 17:26:41 +01:00
|
|
|
|
|
|
|
if (ent == NULL)
|
2019-01-31 22:09:38 +01:00
|
|
|
return -1;
|
2019-01-19 17:26:41 +01:00
|
|
|
|
|
|
|
ent->data.symlink.target = strdup(f->line);
|
|
|
|
if (ent->data.symlink.target == NULL) {
|
|
|
|
oom(f);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(ent->data.symlink.target) > 0xFFFF) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "symlink target too long");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-01-31 22:09:38 +01:00
|
|
|
ent->next = *listptr;
|
|
|
|
*listptr = ent;
|
|
|
|
return 0;
|
2019-01-19 17:26:41 +01:00
|
|
|
fail:
|
|
|
|
image_entry_free(ent);
|
2019-01-31 22:09:38 +01:00
|
|
|
return -1;
|
2019-01-19 17:26:41 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 22:09:38 +01:00
|
|
|
int filelist_mkfile(input_file_t *f, void *obj)
|
2019-01-19 17:26:41 +01:00
|
|
|
{
|
|
|
|
image_entry_t *ent = filelist_mkentry(f, S_IFREG);
|
2019-01-31 22:09:38 +01:00
|
|
|
image_entry_t **listptr = obj;
|
2019-01-19 17:26:41 +01:00
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (ent == NULL)
|
2019-01-31 22:09:38 +01:00
|
|
|
return -1;
|
2019-01-19 17:26:41 +01:00
|
|
|
|
|
|
|
ent->data.file.location = strdup(f->line);
|
|
|
|
if (ent->data.file.location == NULL) {
|
|
|
|
oom(f);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(ent->data.file.location, &sb) != 0) {
|
|
|
|
perror(ent->data.file.location);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeof(off_t) > sizeof(uint64_t) &&
|
|
|
|
sb.st_size > (off_t)(~((uint64_t)0))) {
|
2019-01-31 22:09:38 +01:00
|
|
|
input_file_complain(f, "input file is too big");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ent->data.file.size = sb.st_size;
|
2019-01-31 22:09:38 +01:00
|
|
|
|
|
|
|
ent->next = *listptr;
|
|
|
|
*listptr = ent;
|
|
|
|
return 0;
|
2019-01-19 17:26:41 +01:00
|
|
|
fail:
|
|
|
|
image_entry_free(ent);
|
2019-01-31 22:09:38 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const keyword_handler_t line_hooks[] = {
|
|
|
|
{ "file", filelist_mkfile },
|
|
|
|
{ "dir", filelist_mkdir },
|
|
|
|
{ "slink", filelist_mkslink },
|
|
|
|
};
|
|
|
|
|
|
|
|
#define NUM_LINE_HOOKS (sizeof(line_hooks) / sizeof(line_hooks[0]))
|
|
|
|
|
|
|
|
image_entry_t *filelist_read(const char *filename)
|
|
|
|
{
|
|
|
|
image_entry_t *list = NULL;
|
|
|
|
input_file_t f;
|
|
|
|
|
|
|
|
if (open_file(&f, filename))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (process_file(&f, line_hooks, NUM_LINE_HOOKS, &list))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (list == NULL) {
|
|
|
|
fprintf(stderr, "%s: does not contain any entries\n",
|
|
|
|
f.filename);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup_file(&f);
|
|
|
|
return list;
|
|
|
|
fail:
|
|
|
|
cleanup_file(&f);
|
|
|
|
image_entry_free_list(list);
|
2019-01-19 17:26:41 +01:00
|
|
|
return NULL;
|
|
|
|
}
|