2019-01-19 17:26:41 +01:00
|
|
|
#include "pack.h"
|
|
|
|
|
|
|
|
static char *skipspace(char *str)
|
|
|
|
{
|
|
|
|
while (isspace(*str))
|
|
|
|
++str;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
static void oom(const char *filename, size_t linenum)
|
2019-01-19 17:26:41 +01:00
|
|
|
{
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum, "out of memory");
|
2019-01-19 17:26:41 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
static image_entry_t *filelist_mkentry(char *line, const char *filename,
|
|
|
|
size_t linenum, mode_t filetype)
|
2019-01-19 17:26:41 +01:00
|
|
|
{
|
|
|
|
image_entry_t *ent;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
ent = calloc(1, sizeof(*ent));
|
|
|
|
if (ent == NULL) {
|
2019-03-08 17:10:47 +01:00
|
|
|
oom(filename, linenum);
|
2019-01-19 17:26:41 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* name */
|
|
|
|
for (i = 0; !isspace(line[i]) && line[i] != '\0'; ++i)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (!isspace(line[i])) {
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"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) {
|
2019-03-08 17:10:47 +01:00
|
|
|
oom(filename, linenum);
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ent->name, line, i);
|
|
|
|
if (canonicalize_name(ent->name)) {
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum, "invalid file name");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ent->name[0] == '\0') {
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"refusing to add entry for '/'");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(ent->name) > 0xFFFF) {
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum, "name too long");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = skipspace(line + i);
|
|
|
|
|
|
|
|
/* mode */
|
|
|
|
if (!isdigit(*line)) {
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"expected numeric mode after file name");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (isdigit(*line)) {
|
|
|
|
if (*line > '7') {
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"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-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"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-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"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-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"expected space after UID");
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = skipspace(line);
|
|
|
|
|
|
|
|
/* gid */
|
|
|
|
if (!isdigit(*line)) {
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"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 */
|
2019-03-08 17:10:47 +01:00
|
|
|
memmove(line, line, strlen(line) + 1);
|
2019-01-19 17:26:41 +01:00
|
|
|
return ent;
|
|
|
|
fail:
|
|
|
|
free(ent->name);
|
|
|
|
free(ent);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
int filelist_mkdir(char *line, const char *filename,
|
|
|
|
size_t linenum, void *obj)
|
2019-01-19 17:26:41 +01:00
|
|
|
{
|
2019-03-08 17:10:47 +01:00
|
|
|
image_entry_t *ent = filelist_mkentry(line,filename,linenum,S_IFDIR);
|
2019-01-31 22:09:38 +01:00
|
|
|
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-03-08 17:10:47 +01:00
|
|
|
int filelist_mkslink(char *line, const char *filename,
|
|
|
|
size_t linenum, void *obj)
|
2019-01-19 17:26:41 +01:00
|
|
|
{
|
2019-03-08 17:10:47 +01:00
|
|
|
image_entry_t *ent = filelist_mkentry(line,filename,linenum,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
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
ent->data.symlink.target = strdup(line);
|
2019-01-19 17:26:41 +01:00
|
|
|
if (ent->data.symlink.target == NULL) {
|
2019-03-08 17:10:47 +01:00
|
|
|
oom(filename, linenum);
|
2019-01-19 17:26:41 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(ent->data.symlink.target) > 0xFFFF) {
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"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-03-08 17:10:47 +01:00
|
|
|
int filelist_mkfile(char *line, const char *filename,
|
|
|
|
size_t linenum, void *obj)
|
2019-01-19 17:26:41 +01:00
|
|
|
{
|
2019-03-08 17:10:47 +01:00
|
|
|
image_entry_t *ent = filelist_mkentry(line,filename,linenum,S_IFREG);
|
2019-01-31 22:09:38 +01:00
|
|
|
image_entry_t **listptr = obj;
|
2019-01-31 23:23:05 +01:00
|
|
|
const char *ptr;
|
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
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
if (line[0] == '\0') {
|
|
|
|
ptr = strrchr(filename, '/');
|
2019-01-31 23:23:05 +01:00
|
|
|
|
|
|
|
if (ptr == NULL) {
|
|
|
|
ent->data.file.location = strdup(ent->name);
|
|
|
|
} else {
|
|
|
|
asprintf(&ent->data.file.location, "%.*s/%s",
|
2019-03-08 17:10:47 +01:00
|
|
|
(int)(ptr - filename), filename,
|
2019-01-31 23:23:05 +01:00
|
|
|
ent->name);
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-08 17:10:47 +01:00
|
|
|
ent->data.file.location = strdup(line);
|
2019-01-31 23:23:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 17:26:41 +01:00
|
|
|
if (ent->data.file.location == NULL) {
|
2019-03-08 17:10:47 +01:00
|
|
|
oom(filename, linenum);
|
2019-01-19 17:26:41 +01:00
|
|
|
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-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"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;
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
static int filelist_mknod(char *line, const char *filename,
|
|
|
|
size_t linenum, void *obj)
|
2019-02-04 17:43:24 +01:00
|
|
|
{
|
|
|
|
image_entry_t **listptr = obj, *ent;
|
|
|
|
unsigned int maj, min;
|
|
|
|
char *ptr;
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
ent = filelist_mkentry(line, filename, linenum, S_IFCHR);
|
2019-02-04 17:43:24 +01:00
|
|
|
if (ent == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
switch (line[0]) {
|
2019-02-04 17:43:24 +01:00
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
case 'B':
|
|
|
|
ent->mode = (ent->mode & (~S_IFMT)) | S_IFBLK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
if (!isspace(line[1]))
|
2019-02-04 17:43:24 +01:00
|
|
|
goto fail;
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
ptr = line + 1;
|
2019-02-04 17:43:24 +01:00
|
|
|
while (isspace(*ptr))
|
|
|
|
++ptr;
|
|
|
|
|
|
|
|
if (sscanf(ptr, "%u %u", &maj, &min) != 2)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ent->data.device.devno = makedev(maj, min);
|
|
|
|
|
|
|
|
ent->next = *listptr;
|
|
|
|
*listptr = ent;
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
image_entry_free(ent);
|
2019-03-08 17:10:47 +01:00
|
|
|
input_file_complain(filename, linenum,
|
|
|
|
"error in device specification");
|
2019-02-04 17:43:24 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-31 22:09:38 +01:00
|
|
|
static const keyword_handler_t line_hooks[] = {
|
|
|
|
{ "file", filelist_mkfile },
|
|
|
|
{ "dir", filelist_mkdir },
|
|
|
|
{ "slink", filelist_mkslink },
|
2019-02-04 17:43:24 +01:00
|
|
|
{ "nod", filelist_mknod },
|
2019-01-31 22:09:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NUM_LINE_HOOKS (sizeof(line_hooks) / sizeof(line_hooks[0]))
|
|
|
|
|
2019-02-07 23:14:45 +01:00
|
|
|
static int alloc_file_ids(image_entry_t *list)
|
|
|
|
{
|
|
|
|
image_entry_t *ent;
|
|
|
|
uint64_t file_id = 0;
|
|
|
|
|
|
|
|
for (ent = list; ent != NULL; ent = ent->next) {
|
|
|
|
if (!S_ISREG(ent->mode))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (file_id > 0x00000000FFFFFFFFUL) {
|
|
|
|
fprintf(stderr, "too many input files\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ent->data.file.id = file_id++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int filelist_read(const char *filename, image_entry_t **out)
|
2019-01-31 22:09:38 +01:00
|
|
|
{
|
|
|
|
image_entry_t *list = NULL;
|
|
|
|
|
2019-03-08 15:22:35 +01:00
|
|
|
if (process_file(filename, line_hooks, NUM_LINE_HOOKS, &list))
|
2019-01-31 22:09:38 +01:00
|
|
|
goto fail;
|
|
|
|
|
2019-02-07 23:14:45 +01:00
|
|
|
if (list != NULL) {
|
|
|
|
list = image_entry_sort(list);
|
|
|
|
|
|
|
|
if (alloc_file_ids(list))
|
|
|
|
goto fail;
|
2019-01-31 22:09:38 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:14:45 +01:00
|
|
|
*out = list;
|
|
|
|
return 0;
|
2019-01-31 22:09:38 +01:00
|
|
|
fail:
|
|
|
|
image_entry_free_list(list);
|
2019-02-07 23:14:45 +01:00
|
|
|
return -1;
|
2019-01-19 17:26:41 +01:00
|
|
|
}
|