From cd2a35b7f0180a6e6b3220838d90b906ae90dcf1 Mon Sep 17 00:00:00 2001 From: Dmitrii Date: Sun, 10 Nov 2024 14:13:27 +0300 Subject: [PATCH 1/2] remove file after test --- tests/tar/tar_tests.cpp | 46 +++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/tests/tar/tar_tests.cpp b/tests/tar/tar_tests.cpp index f79504f..84dfc3a 100644 --- a/tests/tar/tar_tests.cpp +++ b/tests/tar/tar_tests.cpp @@ -1,32 +1,38 @@ +#include #include #include "include/structarus/tar_creator.hpp" using namespace tar; +namespace fs = std::filesystem; TEST(TarHeader, BasicConstruction) { - detail::Header header; - EXPECT_EQ(header.name.size(), 100u); - EXPECT_EQ(header.mode.size(), 8u); - EXPECT_EQ(header.uid.size(), 8u); - EXPECT_EQ(header.gid.size(), 8u); - EXPECT_EQ(header.size.size(), 12u); - EXPECT_EQ(header.mtime.size(), 12u); - EXPECT_EQ(header.checksum.size(), 8u); - EXPECT_EQ(header.typeflag, '0'); - EXPECT_EQ(header.linkname.size(), 100u); - EXPECT_EQ(header.magic.size(), 6u); - EXPECT_EQ(header.version.size(), 2u); - EXPECT_EQ(header.uname.size(), 32u); - EXPECT_EQ(header.gname.size(), 32u); - EXPECT_EQ(header.devmajor.size(), 8u); - EXPECT_EQ(header.devminor.size(), 8u); - EXPECT_EQ(header.prefix.size(), 155u); - EXPECT_EQ(header.padding.size(), 12u); // Adjusted size to match the actual padding size + detail::Header header; + EXPECT_EQ(header.name.size(), 100u); + EXPECT_EQ(header.mode.size(), 8u); + EXPECT_EQ(header.uid.size(), 8u); + EXPECT_EQ(header.gid.size(), 8u); + EXPECT_EQ(header.size.size(), 12u); + EXPECT_EQ(header.mtime.size(), 12u); + EXPECT_EQ(header.checksum.size(), 8u); + EXPECT_EQ(header.typeflag, '0'); + EXPECT_EQ(header.linkname.size(), 100u); + EXPECT_EQ(header.magic.size(), 6u); + EXPECT_EQ(header.version.size(), 2u); + EXPECT_EQ(header.uname.size(), 32u); + EXPECT_EQ(header.gname.size(), 32u); + EXPECT_EQ(header.devmajor.size(), 8u); + EXPECT_EQ(header.devminor.size(), 8u); + EXPECT_EQ(header.prefix.size(), 155u); + EXPECT_EQ(header.padding.size(), + 12u); // Adjusted size to match the actual padding size } TEST(Tar, Dummy) { - std::string result; - tar::Creator creator(result); + std::string filename{"altushka"}; + { + tar::Creator creator(filename); creator.addFile({"sqwoz", "bab"}); + } + fs::remove(filename); } From 507206656feb48b6a22981390c9a426533064cba Mon Sep 17 00:00:00 2001 From: Dmitrii Date: Sun, 10 Nov 2024 14:50:32 +0300 Subject: [PATCH 2/2] add test and rename --- CMakeLists.txt | 2 +- README.md | 7 +++++- include/structarus/detail/tar_to_stream.hpp | 2 +- include/structarus/tar_info.hpp | 4 +-- tests/tar/tar_tests.cpp | 27 +++++++++++++++++++-- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 67ff3f2..af1ba00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.14) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 23) project( structarus diff --git a/README.md b/README.md index 229dcc1..2b25e4c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # Structarus -Creates structure of directories and files inside tar archive. \ No newline at end of file +Creates structure of directories and files inside tar archive. This library doesn't compress anything. It is just take files with their containing and produces tape archive with them inside it. + +Structarus can create archive in memory and on the disk. + +## How to use +See in tests. diff --git a/include/structarus/detail/tar_to_stream.hpp b/include/structarus/detail/tar_to_stream.hpp index fccf12e..0ee96e4 100644 --- a/include/structarus/detail/tar_to_stream.hpp +++ b/include/structarus/detail/tar_to_stream.hpp @@ -24,7 +24,7 @@ void tar_to_stream(T & stream, /// stream to write to, e.g. ostream auto const strFilemode = detail::getStringFilemode(info.filemode); detail::Header header; - detail::toArrayFromString(info.archiveName, header.name); + detail::toArrayFromString(info.filename, header.name); detail::toArrayFromString(strFilemode, header.mode); detail::toArrayFromString(info.uname, header.uname); detail::toArrayFromString(info.gname, header.gname); diff --git a/include/structarus/tar_info.hpp b/include/structarus/tar_info.hpp index edec553..89a32a6 100644 --- a/include/structarus/tar_info.hpp +++ b/include/structarus/tar_info.hpp @@ -18,8 +18,8 @@ enum class FileType { }; struct FileInfo { - std::string const & archiveName; /// name of the file to write in tar - std::string const & data; /// data for writing + std::string const & filename; /// name of the file to write in tar + std::string const & data; /// data for writing FileType fileType = FileType::RegularFile; Filemode filemode = Filemode::ReadWriteExecute; uint64_t mtime = 0; /// file modification time, in seconds since epoch diff --git a/tests/tar/tar_tests.cpp b/tests/tar/tar_tests.cpp index 84dfc3a..76a0fbb 100644 --- a/tests/tar/tar_tests.cpp +++ b/tests/tar/tar_tests.cpp @@ -1,7 +1,9 @@ #include #include +#include #include "include/structarus/tar_creator.hpp" +#include "include/structarus/tar_info.hpp" using namespace tar; namespace fs = std::filesystem; @@ -29,10 +31,31 @@ TEST(TarHeader, BasicConstruction) { } TEST(Tar, Dummy) { - std::string filename{"altushka"}; + std::string filename{"altushka.tar"}; { + // Note: creator destructor is needed tar::Creator creator(filename); - creator.addFile({"sqwoz", "bab"}); + creator.addFile({"sqwoz.txt", "bab"}); } fs::remove(filename); } + +TEST(Tar, Example) { + + // filename must contain absolute path indise tar + // e.g. my/awesome/file.txt + using FilenameAndData = std::pair; + std::vector files{{"first/file.cpp", "#include "}, + {"second/file.py", "import os"}}; + + std::string inMemoryTar; + { + // Note: creator destructor is needed + tar::Creator creator(inMemoryTar); + for (auto const &file : files) { + creator.addFile({file.first, file.second, tar::FileType::RegularFile}); + } + } + + ASSERT_FALSE(inMemoryTar.empty()); +}