From 6351872732fce77186f401050eee92c7c3aa3461 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 20 May 2023 17:04:15 +0200 Subject: libtar: add a dir_iterator_t implementation for tar files The existing istream_t wrapper is mered into this one as well, we can open the files via the iterators open_file_ro function. Unit tests and tar2sqfs are modified accordingly. Signed-off-by: David Oberhollenzer --- lib/tar/test/tar_iterator.c | 139 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 lib/tar/test/tar_iterator.c (limited to 'lib/tar/test/tar_iterator.c') diff --git a/lib/tar/test/tar_iterator.c b/lib/tar/test/tar_iterator.c new file mode 100644 index 0000000..21bd3e6 --- /dev/null +++ b/lib/tar/test/tar_iterator.c @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * tar_iterator.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" +#include "io/file.h" +#include "tar/tar.h" +#include "util/test.h" +#include "sqfs/error.h" + +#ifndef TESTUID +#define TESTUID 1000 +#endif + +#ifndef TESTGID +#define TESTGID TESTUID +#endif + +#ifndef TESTFNAME +#define TESTFNAME input.txt +#endif + +#ifndef TESTTS +#define TESTTS 1542905892 +#endif + +static const char *fname = STRVALUE(TESTFNAME); + +int main(int argc, char **argv) +{ + istream_t *fp, *ti, *ti2; + dir_iterator_t *it; + dir_entry_t *ent; + char buffer[100]; + sqfs_s32 ret; + sqfs_s64 ts; + (void)argc; (void)argv; + + /* Open the file, create an iterator */ + fp = istream_open_file(STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + TEST_NOT_NULL(fp); + TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1); + it = tar_open_stream(fp); + TEST_NOT_NULL(it); + TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2); + TEST_EQUAL_UI(((sqfs_object_t *)it)->refcount, 1); + + /* read entry */ + ret = it->next(it, &ent); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(ent); + TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2); + TEST_EQUAL_UI(((sqfs_object_t *)it)->refcount, 1); + + ts = TESTTS; + TEST_EQUAL_UI(ent->mode, S_IFREG | 0644); + TEST_EQUAL_UI(ent->uid, TESTUID); + TEST_EQUAL_UI(ent->gid, TESTGID); + TEST_EQUAL_UI(ent->mtime, ts); + TEST_STR_EQUAL(ent->name, fname); + free(ent); + ent = NULL; + + /* Open file stream */ + ret = it->open_file_ro(it, &ti); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(ti); + TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2); + TEST_EQUAL_UI(((sqfs_object_t *)it)->refcount, 2); + TEST_EQUAL_UI(((sqfs_object_t *)ti)->refcount, 1); + + /* test that the iterator is now "locked" */ + ret = it->open_file_ro(it, &ti2); + TEST_EQUAL_I(ret, SQFS_ERROR_SEQUENCE); + TEST_NULL(ti2); + ret = it->next(it, &ent); + TEST_EQUAL_I(ret, SQFS_ERROR_SEQUENCE); + TEST_NULL(ent); + TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2); + TEST_EQUAL_UI(((sqfs_object_t *)it)->refcount, 2); + TEST_EQUAL_UI(((sqfs_object_t *)ti)->refcount, 1); + + /* read the data from the stream */ + ret = istream_read(ti, buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 5); + buffer[5] = '\0'; + TEST_STR_EQUAL(buffer, "test\n"); + + ret = istream_read(ti, buffer, sizeof(buffer)); + TEST_EQUAL_I(ret, 0); + + sqfs_drop(ti); + TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2); + TEST_EQUAL_UI(((sqfs_object_t *)it)->refcount, 1); + + /* read past EOF on the iterator */ + ret = it->next(it, &ent); + TEST_ASSERT(ret > 0); + TEST_NULL(ent); + + /* cleanup */ + sqfs_drop(it); + TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1); + sqfs_drop(fp); + + /* re-open the tar iterator */ + fp = istream_open_file(STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + TEST_NOT_NULL(fp); + it = tar_open_stream(fp); + TEST_NOT_NULL(it); + + /* read entry */ + ret = it->next(it, &ent); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(ent); + + ts = TESTTS; + TEST_EQUAL_UI(ent->mode, S_IFREG | 0644); + TEST_EQUAL_UI(ent->uid, TESTUID); + TEST_EQUAL_UI(ent->gid, TESTGID); + TEST_EQUAL_UI(ent->mtime, ts); + TEST_STR_EQUAL(ent->name, fname); + free(ent); + ent = NULL; + + /* read next entry, mus treturn EOF */ + ret = it->next(it, &ent); + TEST_ASSERT(ret > 0); + TEST_NULL(ent); + + /* cleanup */ + sqfs_drop(it); + TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1); + sqfs_drop(fp); + + return EXIT_SUCCESS; +} -- cgit v1.2.3