diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/io/Makemodule.am | 3 | ||||
-rw-r--r-- | lib/io/src/xattr.c | 40 | ||||
-rw-r--r-- | lib/tar/src/cleanup.c | 13 | ||||
-rw-r--r-- | lib/tar/src/pax_header.c | 36 | ||||
-rw-r--r-- | lib/tar/src/write_header.c | 6 | ||||
-rw-r--r-- | lib/tar/test/tar_write_simple.c | 41 |
6 files changed, 65 insertions, 74 deletions
diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am index 841febf..3be208f 100644 --- a/lib/io/Makemodule.am +++ b/lib/io/Makemodule.am @@ -3,7 +3,8 @@ libio_a_SOURCES = include/io/istream.h include/io/ostream.h include/io/xfrm.h \ include/io/dir_iterator.h include/io/xattr.h \ lib/io/src/internal.h lib/io/src/ostream.c \ lib/io/src/istream.c lib/io/src/get_line.c lib/io/src/xfrm/ostream.c \ - lib/io/src/xfrm/istream.c lib/io/src/dir_tree_iterator.c + lib/io/src/xfrm/istream.c lib/io/src/dir_tree_iterator.c \ + lib/io/src/xattr.c libio_a_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS) $(XZ_CFLAGS) libio_a_CFLAGS += $(ZSTD_CFLAGS) $(BZIP2_CFLAGS) diff --git a/lib/io/src/xattr.c b/lib/io/src/xattr.c new file mode 100644 index 0000000..dd9a338 --- /dev/null +++ b/lib/io/src/xattr.c @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * xattr.c + * + * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at> + */ +#include "io/xattr.h" + +#include <stdlib.h> +#include <string.h> + +dir_entry_xattr_t *dir_entry_xattr_create(const char *key, const sqfs_u8 *value, + size_t value_len) +{ + size_t key_len = strlen(key); + dir_entry_xattr_t *out = calloc(1, sizeof(*out) + key_len + 1 + + value_len + 1); + + if (out != NULL) { + out->key = out->data; + out->value = (sqfs_u8 *)(out->data + key_len + 1); + + memcpy(out->key, key, key_len); + memcpy(out->value, value, value_len); + out->value_len = value_len; + } + + return out; +} + +void dir_entry_xattr_list_free(dir_entry_xattr_t *list) +{ + dir_entry_xattr_t *old; + + while (list != NULL) { + old = list; + list = list->next; + free(old); + } +} diff --git a/lib/tar/src/cleanup.c b/lib/tar/src/cleanup.c index 9f33336..3ba8019 100644 --- a/lib/tar/src/cleanup.c +++ b/lib/tar/src/cleanup.c @@ -21,20 +21,9 @@ void free_sparse_list(sparse_map_t *sparse) } } -void free_xattr_list(tar_xattr_t *list) -{ - tar_xattr_t *old; - - while (list != NULL) { - old = list; - list = list->next; - free(old); - } -} - void clear_header(tar_header_decoded_t *hdr) { - free_xattr_list(hdr->xattr); + dir_entry_xattr_list_free(hdr->xattr); free_sparse_list(hdr->sparse); free(hdr->name); free(hdr->link_target); diff --git a/lib/tar/src/pax_header.c b/lib/tar/src/pax_header.c index b61aab6..81f6ad4 100644 --- a/lib/tar/src/pax_header.c +++ b/lib/tar/src/pax_header.c @@ -141,7 +141,7 @@ fail: } static int pax_xattr_schily(tar_header_decoded_t *out, - tar_xattr_t *xattr) + dir_entry_xattr_t *xattr) { xattr->next = out->xattr; out->xattr = xattr; @@ -149,7 +149,7 @@ static int pax_xattr_schily(tar_header_decoded_t *out, } static int pax_xattr_libarchive(tar_header_decoded_t *out, - tar_xattr_t *xattr) + dir_entry_xattr_t *xattr) { int ret; @@ -184,7 +184,8 @@ static const struct pax_handler_t { int (*uint)(tar_header_decoded_t *out, sqfs_u64 uval); int (*str)(tar_header_decoded_t *out, char *str); int (*cstr)(tar_header_decoded_t *out, const char *str); - int (*xattr)(tar_header_decoded_t *out, tar_xattr_t *xattr); + int (*xattr)(tar_header_decoded_t *out, + dir_entry_xattr_t *xattr); } cb; } pax_fields[] = { { "uid", PAX_UID, PAX_TYPE_UINT, { .uint = pax_uid } }, @@ -234,33 +235,11 @@ static const struct pax_handler_t *find_handler(const char *key) return NULL; } -static tar_xattr_t *mkxattr(const char *key, - const char *value, size_t valuelen) -{ - size_t keylen = strlen(key); - tar_xattr_t *xattr; - - xattr = calloc(1, sizeof(*xattr) + keylen + 1 + valuelen + 1); - if (xattr == NULL) - return NULL; - - xattr->key = xattr->data; - memcpy(xattr->key, key, keylen); - xattr->key[keylen] = '\0'; - - xattr->value = (sqfs_u8 *)xattr->key + keylen + 1; - memcpy(xattr->value, value, valuelen); - xattr->value[valuelen] = '\0'; - - xattr->value_len = valuelen; - return xattr; -} - static int apply_handler(tar_header_decoded_t *out, const struct pax_handler_t *field, const char *key, const char *value, size_t valuelen) { - tar_xattr_t *xattr; + dir_entry_xattr_t *xattr; sqfs_s64 s64val; sqfs_u64 uval; char *copy; @@ -295,8 +274,9 @@ static int apply_handler(tar_header_decoded_t *out, } break; case PAX_TYPE_PREFIXED_XATTR: - xattr = mkxattr(key + strlen(field->name) + 1, - value, valuelen); + xattr = dir_entry_xattr_create(key + strlen(field->name) + 1, + (const sqfs_u8 *)value, + valuelen); if (xattr == NULL) { perror("reading pax xattr field"); return -1; diff --git a/lib/tar/src/write_header.c b/lib/tar/src/write_header.c index 6876c38..f5473ca 100644 --- a/lib/tar/src/write_header.c +++ b/lib/tar/src/write_header.c @@ -144,11 +144,11 @@ static size_t prefix_digit_len(size_t len) } static int write_schily_xattr(ostream_t *fp, const struct stat *orig, - const char *name, const tar_xattr_t *xattr) + const char *name, const dir_entry_xattr_t *xattr) { static const char *prefix = "SCHILY.xattr."; size_t len, total_size = 0; - const tar_xattr_t *it; + const dir_entry_xattr_t *it; char *buffer, *ptr; int ret; @@ -184,7 +184,7 @@ static int write_schily_xattr(ostream_t *fp, const struct stat *orig, } int write_tar_header(ostream_t *fp, const struct stat *sb, const char *name, - const char *slink_target, const tar_xattr_t *xattr, + const char *slink_target, const dir_entry_xattr_t *xattr, unsigned int counter) { const char *reason; diff --git a/lib/tar/test/tar_write_simple.c b/lib/tar/test/tar_write_simple.c index c78565b..738b469 100644 --- a/lib/tar/test/tar_write_simple.c +++ b/lib/tar/test/tar_write_simple.c @@ -52,29 +52,7 @@ static const char *buffer_get_filename(ostream_t *strm) #define TIME_STAMP (1057296600) -static tar_xattr_t *mkxattr(const char *key, const sqfs_u8 *value, - size_t value_len) -{ - size_t key_len = strlen(key); - tar_xattr_t *out = malloc(sizeof(*out) + key_len + 1 + value_len + 1); - - TEST_NOT_NULL(out); - - out->next = NULL; - out->key = out->data; - out->value = (sqfs_u8 *)(out->data + key_len + 1); - out->value_len = value_len; - - memcpy(out->data, key, key_len); - out->data[key_len] = '\0'; - - memcpy(out->data + key_len + 1, value, value_len); - out->data[key_len + 1 + value_len] = '\0'; - - return out; -} - -static tar_xattr_t *mkxattr_chain(void) +static dir_entry_xattr_t *mkxattr_chain(void) { static const uint8_t value[] = { 0x00, 0x00, 0x00, 0x02, @@ -83,17 +61,20 @@ static tar_xattr_t *mkxattr_chain(void) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; - tar_xattr_t *list; - - list = mkxattr("user.mime_type", (const sqfs_u8 *)"blob/magic", 10); - list->next = mkxattr("security.capability", value, sizeof(value)); - + dir_entry_xattr_t *list; + + list = dir_entry_xattr_create("user.mime_type", + (const sqfs_u8 *)"blob/magic", 10); + TEST_NOT_NULL(list); + list->next = dir_entry_xattr_create("security.capability", + value, sizeof(value)); + TEST_NOT_NULL(list->next); return list; } int main(int argc, char **argv) { - tar_xattr_t *xattr; + dir_entry_xattr_t *xattr; struct stat sb; istream_t *fp; int ret; @@ -173,7 +154,7 @@ int main(int argc, char **argv) ret = write_tar_header(&mem_stream, &sb, "home/goliath/test.exe", NULL, xattr, 11); TEST_EQUAL_I(ret, 0); - free_xattr_list(xattr); + dir_entry_xattr_list_free(xattr); ret = ostream_append(&mem_stream, ":-)\n", 4); TEST_EQUAL_I(ret, 0); |