From 007ff40bf440d72d2ca4ffed26b6b2ea752b1562 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 16 Nov 2022 10:29:19 +0100 Subject: 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 --- bin/gensquashfs/filemap_xattr.c | 50 ++++++++++++++++++++--------------------- bin/gensquashfs/mkfs.h | 1 + 2 files changed, 25 insertions(+), 26 deletions(-) (limited to 'bin/gensquashfs') 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; } diff --git a/bin/gensquashfs/mkfs.h b/bin/gensquashfs/mkfs.h index bbecdbb..960a31c 100644 --- a/bin/gensquashfs/mkfs.h +++ b/bin/gensquashfs/mkfs.h @@ -13,6 +13,7 @@ #include "common.h" #include "fstree.h" #include "util/util.h" +#include "io/file.h" #ifdef HAVE_SYS_XATTR_H #include -- cgit v1.2.3