diff options
Diffstat (limited to 'common/file-list.c')
-rw-r--r-- | common/file-list.c | 72 |
1 files changed, 58 insertions, 14 deletions
diff --git a/common/file-list.c b/common/file-list.c index 90c0f429c5..8d61b76cbb 100644 --- a/common/file-list.c +++ b/common/file-list.c @@ -1,3 +1,16 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + */ + +#define pr_fmt(fmt) "file_list: " fmt + #include <common.h> #include <malloc.h> #include <fs.h> @@ -8,12 +21,44 @@ #define PARSE_NAME 1 #define PARSE_FLAGS 2 +struct file_list_entry *file_list_entry_by_name(struct file_list *files, const char *name) +{ + struct file_list_entry *entry; + + file_list_for_each_entry(files, entry) { + if (!strcmp(entry->name, name)) + return entry; + } + + return NULL; +} + +int file_list_add_entry(struct file_list *files, const char *name, const char *filename, + unsigned long flags) +{ + struct file_list_entry *entry; + + entry = file_list_entry_by_name(files, name); + if (entry) + return -EEXIST; + + entry = xzalloc(sizeof(*entry)); + + entry->name = xstrdup(name); + entry->filename = xstrdup(filename); + entry->flags = flags; + + list_add_tail(&entry->list, &files->list); + + return 0; +} + static int file_list_parse_one(struct file_list *files, const char *partstr, const char **endstr) { int i = 0, state = PARSE_DEVICE; char filename[PATH_MAX]; char name[PATH_MAX]; - struct file_list_entry *entry = xzalloc(sizeof(*entry)); + unsigned long flags = 0; memset(filename, 0, sizeof(filename)); memset(name, 0, sizeof(name)); @@ -39,15 +84,16 @@ static int file_list_parse_one(struct file_list *files, const char *partstr, con case PARSE_FLAGS: switch (*partstr) { case 's': - entry->flags |= FILE_LIST_FLAG_SAFE; + flags |= FILE_LIST_FLAG_SAFE; break; case 'r': - entry->flags |= FILE_LIST_FLAG_READBACK; + flags |= FILE_LIST_FLAG_READBACK; break; case 'c': - entry->flags |= FILE_LIST_FLAG_CREATE; + flags |= FILE_LIST_FLAG_CREATE; break; default: + pr_err("Unknown flag '%c'\n", *partstr); return -EINVAL; } break; @@ -57,18 +103,16 @@ static int file_list_parse_one(struct file_list *files, const char *partstr, con partstr++; } - if (state != PARSE_FLAGS) + if (state != PARSE_FLAGS) { + pr_err("Missing ')'\n"); return -EINVAL; + } - entry->name = xstrdup(name); - entry->filename = xstrdup(filename); if (*partstr == ',') partstr++; *endstr = partstr; - list_add_tail(&entry->list, &files->list); - - return 0; + return file_list_add_entry(files, name, filename, flags); } struct file_list *file_list_parse(const char *str) @@ -82,9 +126,9 @@ struct file_list *file_list_parse(const char *str) INIT_LIST_HEAD(&files->list); while (*str) { - if (file_list_parse_one(files, str, &endptr)) { - printf("parse error\n"); - ret = -EINVAL; + ret = file_list_parse_one(files, str, &endptr); + if (ret) { + pr_err("parse error\n"); goto out; } str = endptr; @@ -94,7 +138,7 @@ struct file_list *file_list_parse(const char *str) return files; out: - free(files); + file_list_free(files); return ERR_PTR(ret); } |