From 9a97a9a4fe224bcf53ad23af31bca67bbb71a824 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 15 May 2023 20:11:56 +0200 Subject: libtar: replace tar_xattr_t with dir_entry_xattr_t struct Signed-off-by: David Oberhollenzer --- bin/sqfs2tar/src/sqfs2tar.h | 2 +- bin/sqfs2tar/src/write_tree.c | 4 ++-- bin/sqfs2tar/src/xattr.c | 23 ++++++++------------- bin/tar2sqfs/src/process_tarball.c | 2 +- include/io/xattr.h | 17 +++++++++++++++- include/tar/tar.h | 15 +++----------- lib/io/Makemodule.am | 3 ++- lib/io/src/xattr.c | 40 +++++++++++++++++++++++++++++++++++++ lib/tar/src/cleanup.c | 13 +----------- lib/tar/src/pax_header.c | 36 ++++++++------------------------- lib/tar/src/write_header.c | 6 +++--- lib/tar/test/tar_write_simple.c | 41 ++++++++++---------------------------- 12 files changed, 96 insertions(+), 106 deletions(-) create mode 100644 lib/io/src/xattr.c diff --git a/bin/sqfs2tar/src/sqfs2tar.h b/bin/sqfs2tar/src/sqfs2tar.h index 4bf5428..88185ee 100644 --- a/bin/sqfs2tar/src/sqfs2tar.h +++ b/bin/sqfs2tar/src/sqfs2tar.h @@ -48,7 +48,7 @@ char *assemble_tar_path(char *name, bool is_dir); /* xattr.c */ int get_xattrs(const char *name, const sqfs_inode_generic_t *inode, - tar_xattr_t **out); + dir_entry_xattr_t **out); /* write_tree.c */ int write_tree(const sqfs_tree_node_t *n); diff --git a/bin/sqfs2tar/src/write_tree.c b/bin/sqfs2tar/src/write_tree.c index 354ec21..150bac1 100644 --- a/bin/sqfs2tar/src/write_tree.c +++ b/bin/sqfs2tar/src/write_tree.c @@ -68,7 +68,7 @@ static void inode_stat(const sqfs_tree_node_t *node, struct stat *sb) static int write_tree_dfs(const sqfs_tree_node_t *n) { sqfs_hard_link_t *lnk = NULL; - tar_xattr_t *xattr = NULL; + dir_entry_xattr_t *xattr = NULL; char *name, *target; struct stat sb; size_t len; @@ -134,7 +134,7 @@ static int write_tree_dfs(const sqfs_tree_node_t *n) target = S_ISLNK(sb.st_mode) ? (char *)n->inode->extra : NULL; ret = write_tar_header(out_file, &sb, name, target, xattr, record_counter++); - free_xattr_list(xattr); + dir_entry_xattr_list_free(xattr); if (ret > 0) goto out_skip; diff --git a/bin/sqfs2tar/src/xattr.c b/bin/sqfs2tar/src/xattr.c index abec4fb..8d21cd8 100644 --- a/bin/sqfs2tar/src/xattr.c +++ b/bin/sqfs2tar/src/xattr.c @@ -6,32 +6,25 @@ */ #include "sqfs2tar.h" -static tar_xattr_t *mkxattr(const sqfs_xattr_entry_t *key, - const sqfs_xattr_value_t *value) +static dir_entry_xattr_t *mkxattr(const sqfs_xattr_entry_t *key, + const sqfs_xattr_value_t *value) { - tar_xattr_t *ent; - - ent = calloc(1, sizeof(*ent) + strlen((const char *)key->key) + - value->size + 2); + dir_entry_xattr_t *ent; + ent = dir_entry_xattr_create((const char *)key->key, + value->value, value->size); if (ent == NULL) { perror("creating xattr entry"); return NULL; } - ent->key = ent->data; - ent->value = (sqfs_u8 *)ent->key + strlen((const char *)key->key) + 1; - ent->value_len = value->size; - - strcpy(ent->key, (const char *)key->key); - memcpy(ent->value, value->value, value->size + 1); return ent; } int get_xattrs(const char *name, const sqfs_inode_generic_t *inode, - tar_xattr_t **out) + dir_entry_xattr_t **out) { - tar_xattr_t *list = NULL, *ent; + dir_entry_xattr_t *list = NULL, *ent; sqfs_xattr_value_t *value; sqfs_xattr_entry_t *key; sqfs_xattr_id_t desc; @@ -86,6 +79,6 @@ int get_xattrs(const char *name, const sqfs_inode_generic_t *inode, *out = list; return 0; fail: - free_xattr_list(list); + dir_entry_xattr_list_free(list); return -1; } diff --git a/bin/tar2sqfs/src/process_tarball.c b/bin/tar2sqfs/src/process_tarball.c index 2ba018d..bcc60db 100644 --- a/bin/tar2sqfs/src/process_tarball.c +++ b/bin/tar2sqfs/src/process_tarball.c @@ -41,7 +41,7 @@ static int write_file(istream_t *input_file, sqfs_writer_t *sqfs, static int copy_xattr(sqfs_writer_t *sqfs, tree_node_t *node, const tar_header_decoded_t *hdr) { - tar_xattr_t *xattr; + dir_entry_xattr_t *xattr; int ret; ret = sqfs_xattr_writer_begin(sqfs->xwr, 0); diff --git a/include/io/xattr.h b/include/io/xattr.h index d912fad..cf35bca 100644 --- a/include/io/xattr.h +++ b/include/io/xattr.h @@ -1,4 +1,4 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* SPDX-License-Identifier: GPL-3.0-or-later */ /* * xattr.h * @@ -7,6 +7,8 @@ #ifndef IO_XATTR_H #define IO_XATTR_H +#include "sqfs/predef.h" + typedef struct dir_entry_xattr_t { struct dir_entry_xattr_t *next; char *key; @@ -15,4 +17,17 @@ typedef struct dir_entry_xattr_t { char data[]; } dir_entry_xattr_t; +#ifdef __cplusplus +extern "C" { +#endif + +dir_entry_xattr_t *dir_entry_xattr_create(const char *key, const sqfs_u8 *value, + size_t value_len); + +void dir_entry_xattr_list_free(dir_entry_xattr_t *list); + +#ifdef __cplusplus +} +#endif + #endif /* IO_XATTR_H */ diff --git a/include/tar/tar.h b/include/tar/tar.h index 7c4faa7..21ded7d 100644 --- a/include/tar/tar.h +++ b/include/tar/tar.h @@ -11,6 +11,7 @@ #include "compat.h" #include "io/istream.h" #include "io/ostream.h" +#include "io/dir_iterator.h" #include #include @@ -22,14 +23,6 @@ typedef struct sparse_map_t { sqfs_u64 count; } sparse_map_t; -typedef struct tar_xattr_t { - struct tar_xattr_t *next; - char *key; - sqfs_u8 *value; - size_t value_len; - char data[]; -} tar_xattr_t; - typedef struct { char *name; char *link_target; @@ -38,7 +31,7 @@ typedef struct { sqfs_u64 record_size; bool unknown_record; bool is_hard_link; - tar_xattr_t *xattr; + dir_entry_xattr_t *xattr; sqfs_u16 mode; sqfs_u64 uid; @@ -59,7 +52,7 @@ extern "C" { headers need to be generated. */ 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); int write_hard_link(ostream_t *fp, const struct stat *sb, const char *name, @@ -83,8 +76,6 @@ int padd_file(ostream_t *fp, sqfs_u64 size); void free_sparse_list(sparse_map_t *sparse); -void free_xattr_list(tar_xattr_t *list); - #ifdef __cplusplus } #endif 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 + */ +#include "io/xattr.h" + +#include +#include + +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); -- cgit v1.2.3