aboutsummaryrefslogtreecommitdiff
path: root/bin/gensquashfs/filemap_xattr.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-11-16 10:29:19 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-11-18 16:21:12 +0100
commit007ff40bf440d72d2ca4ffed26b6b2ea752b1562 (patch)
tree705472f3938e5a1f1e7b988d66c1f4d7e884e999 /bin/gensquashfs/filemap_xattr.c
parentfefcbb9710c87dcadab7dba5af52cb94b06625ac (diff)
filemap xattr: use file istream from libio instead of stdio FILE
The line-by-line reading function in the io library was specifically added for this kind of use case, so we don't have to handle trimming of lines, don't have to touch getline() and it's convoluted error handling (return value -1 could mean either EOF or error). The code that searches for '\n' and replaces it with '\0' can be removed as well and a memory leak involving "line" is removed along the way. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'bin/gensquashfs/filemap_xattr.c')
-rw-r--r--bin/gensquashfs/filemap_xattr.c50
1 files changed, 24 insertions, 26 deletions
diff --git a/bin/gensquashfs/filemap_xattr.c b/bin/gensquashfs/filemap_xattr.c
index 4c35c2e..e5a16be 100644
--- a/bin/gensquashfs/filemap_xattr.c
+++ b/bin/gensquashfs/filemap_xattr.c
@@ -86,15 +86,9 @@ decode(const char *value, size_t *size) {
static int
parse_file_name(char *line, struct XattrMap *map) {
- char *p;
struct XattrMapPattern *current_file;
- char *file_name = &line[strlen(NEW_FILE_START)];
+ char *file_name = strdup(line + strlen(NEW_FILE_START));
- p = strchr(file_name, '\n');
- if (p != NULL) {
- *p = '\0';
- }
- file_name = strdup(file_name);
if (file_name == NULL) {
return -1;
}
@@ -114,18 +108,10 @@ parse_file_name(char *line, struct XattrMap *map) {
static int
parse_xattr(char *key_start, char *value_start, struct XattrMap *map) {
- char *p;
size_t len;
struct XattrMapPattern *current_pattern = map->patterns;
struct XattrMapEntry *current_entry;
- *value_start = '\0';
- value_start += 1;
- p = strchr(value_start, '\n');
- if (p != NULL) {
- *p = '\0';
- }
-
current_entry = calloc(1, sizeof(struct XattrMapEntry));
if (current_entry == NULL) {
return -1;
@@ -144,10 +130,9 @@ parse_xattr(char *key_start, char *value_start, struct XattrMap *map) {
void *
xattr_open_map_file(const char *path) {
struct XattrMap *map;
- char *line = NULL;
- size_t line_size;
+ size_t line_num = 1;
char *p = NULL;
- FILE *file = fopen(path, "r");
+ istream_t *file = istream_open_file(path);
if (file == NULL) {
return NULL;
}
@@ -156,23 +141,36 @@ xattr_open_map_file(const char *path) {
if (map == NULL)
goto fail_close;
- while (getline(&line, &line_size, file) != -1) {
+ for (;;) {
+ char *line = NULL;
+ int ret = istream_get_line(file, &line, &line_num,
+ ISTREAM_LINE_LTRIM |
+ ISTREAM_LINE_RTRIM |
+ ISTREAM_LINE_SKIP_EMPTY);
+ if (ret < 0)
+ goto fail;
+ if (ret > 0)
+ break;
+
if (strncmp(NEW_FILE_START, line, strlen(NEW_FILE_START)) == 0) {
- if (parse_file_name(line, map) < 0)
- goto fail;
+ ret = parse_file_name(line, map);
} else if ((p = strchr(line, '=')) && map->patterns) {
- if (parse_xattr(line, p, map) < 0)
- goto fail;
+ *(p++) = '\0';
+ ret = parse_xattr(line, p, map);
}
+
+ ++line_num;
+ free(line);
+ if (ret < 0)
+ goto fail;
}
- free(line);
- fclose(file);
+ sqfs_destroy(file);
return map;
fail:
xattr_close_map_file(map);
fail_close:
- fclose(file);
+ sqfs_destroy(file);
return NULL;
}