From a809db90ddff280fd523e924789ed907b2b950b9 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 18 Mar 2021 16:22:05 +0200 Subject: [PATCH] Libraries: Rename LibTar to LibArchive This is in preparation for a new implementation of zip archive extraction and creation. --- Userland/Libraries/CMakeLists.txt | 2 +- Userland/Libraries/LibArchive/CMakeLists.txt | 6 ++++++ Userland/Libraries/{LibTar => LibArchive}/Tar.h | 16 ++++++++-------- .../{LibTar => LibArchive}/TarStream.cpp | 16 ++++++++-------- .../Libraries/{LibTar => LibArchive}/TarStream.h | 8 ++++---- Userland/Libraries/LibTar/CMakeLists.txt | 6 ------ Userland/Utilities/CMakeLists.txt | 2 +- Userland/Utilities/tar.cpp | 16 ++++++++-------- 8 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 Userland/Libraries/LibArchive/CMakeLists.txt rename Userland/Libraries/{LibTar => LibArchive}/Tar.h (93%) rename Userland/Libraries/{LibTar => LibArchive}/TarStream.cpp (94%) rename Userland/Libraries/{LibTar => LibArchive}/TarStream.h (94%) delete mode 100644 Userland/Libraries/LibTar/CMakeLists.txt diff --git a/Userland/Libraries/CMakeLists.txt b/Userland/Libraries/CMakeLists.txt index 15b9fb32266..a78e05bee19 100644 --- a/Userland/Libraries/CMakeLists.txt +++ b/Userland/Libraries/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(LibArchive) add_subdirectory(LibAudio) add_subdirectory(LibC) add_subdirectory(LibChess) @@ -29,7 +30,6 @@ add_subdirectory(LibRegex) add_subdirectory(LibSymbolClient) add_subdirectory(LibSyntax) add_subdirectory(LibSystem) -add_subdirectory(LibTar) add_subdirectory(LibTest) add_subdirectory(LibTextCodec) add_subdirectory(LibThread) diff --git a/Userland/Libraries/LibArchive/CMakeLists.txt b/Userland/Libraries/LibArchive/CMakeLists.txt new file mode 100644 index 00000000000..29dce137b0f --- /dev/null +++ b/Userland/Libraries/LibArchive/CMakeLists.txt @@ -0,0 +1,6 @@ +set(SOURCES + TarStream.cpp +) + +serenity_lib(LibArchive archive) +target_link_libraries(LibArchive LibCore) diff --git a/Userland/Libraries/LibTar/Tar.h b/Userland/Libraries/LibArchive/Tar.h similarity index 93% rename from Userland/Libraries/LibTar/Tar.h rename to Userland/Libraries/LibArchive/Tar.h index 38f42bcb03e..cdd001c23ff 100644 --- a/Userland/Libraries/LibTar/Tar.h +++ b/Userland/Libraries/LibArchive/Tar.h @@ -32,9 +32,9 @@ #include #include -namespace Tar { +namespace Archive { -enum FileType { +enum class TarFileType : char { NormalFile = '0', AlternateNormalFile = '\0', HardLink = '1', @@ -54,7 +54,7 @@ constexpr const char* gnu_version = " "; // gnu format version constexpr const char* ustar_magic = "ustar"; // ustar format magic constexpr const char* ustar_version = "00"; // ustar format version -class [[gnu::packed]] Header { +class [[gnu::packed]] TarFileHeader { public: const StringView file_name() const { return m_file_name; } mode_t mode() const { return get_tar_field(m_mode); } @@ -63,7 +63,7 @@ public: // FIXME: support 2001-star size encoding size_t size() const { return get_tar_field(m_size); } time_t timestamp() const { return get_tar_field(m_timestamp); } - FileType type_flag() const { return FileType(m_type_flag); } + TarFileType type_flag() const { return TarFileType(m_type_flag); } const StringView link_name() const { return m_link_name; } const StringView magic() const { return StringView(m_magic, min(__builtin_strlen(m_magic), sizeof(m_magic))); } // in some cases this is a null terminated string, in others its not const StringView version() const { return StringView(m_version, min(__builtin_strlen(m_version), sizeof(m_version))); } // in some cases this is a null terminated string, in others its not @@ -80,7 +80,7 @@ public: void set_gid(gid_t gid) { VERIFY(String::formatted("{:o}", gid).copy_characters_to_buffer(m_gid, sizeof(m_gid))); } void set_size(size_t size) { VERIFY(String::formatted("{:o}", size).copy_characters_to_buffer(m_size, sizeof(m_size))); } void set_timestamp(time_t timestamp) { VERIFY(String::formatted("{:o}", timestamp).copy_characters_to_buffer(m_timestamp, sizeof(m_timestamp))); } - void set_type_flag(FileType type) { m_type_flag = type; } + void set_type_flag(TarFileType type) { m_type_flag = static_cast(type); } void set_link_name(const String& link_name) { VERIFY(link_name.copy_characters_to_buffer(m_link_name, sizeof(m_link_name))); } void set_magic(const char* magic) { memcpy(m_magic, magic, sizeof(m_magic)); } // magic doesnt necessarily include a null byte void set_version(const char* version) { memcpy(m_version, version, sizeof(m_version)); } // version doesnt necessarily include a null byte @@ -115,7 +115,7 @@ private: }; template -size_t Header::get_tar_field(const char (&field)[N]) +size_t TarFileHeader::get_tar_field(const char (&field)[N]) { size_t value = 0; for (size_t i = 0; i < N; ++i) { @@ -128,11 +128,11 @@ size_t Header::get_tar_field(const char (&field)[N]) } return value; } -void Header::calculate_checksum() +void TarFileHeader::calculate_checksum() { memset(m_checksum, ' ', sizeof(m_checksum)); auto checksum = 0u; - for (auto i = 0u; i < sizeof(Header); ++i) { + for (auto i = 0u; i < sizeof(TarFileHeader); ++i) { checksum += ((unsigned char*)this)[i]; } VERIFY(String::formatted("{:o}", checksum).copy_characters_to_buffer(m_checksum, sizeof(m_checksum))); diff --git a/Userland/Libraries/LibTar/TarStream.cpp b/Userland/Libraries/LibArchive/TarStream.cpp similarity index 94% rename from Userland/Libraries/LibTar/TarStream.cpp rename to Userland/Libraries/LibArchive/TarStream.cpp index 2803d2aa5aa..b3763bef17d 100644 --- a/Userland/Libraries/LibTar/TarStream.cpp +++ b/Userland/Libraries/LibArchive/TarStream.cpp @@ -25,10 +25,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include -namespace Tar { +namespace Archive { TarFileStream::TarFileStream(TarInputStream& tar_stream) : m_tar_stream(tar_stream) , m_generation(tar_stream.m_generation) @@ -92,7 +92,7 @@ TarInputStream::TarInputStream(InputStream& stream) m_stream.handle_any_error(); // clear out errors so we dont assert return; } - VERIFY(m_stream.discard_or_error(block_size - sizeof(Header))); + VERIFY(m_stream.discard_or_error(block_size - sizeof(TarFileHeader))); } static constexpr unsigned long block_ceiling(unsigned long offset) @@ -118,7 +118,7 @@ void TarInputStream::advance() return; } - VERIFY(m_stream.discard_or_error(block_size - sizeof(Header))); + VERIFY(m_stream.discard_or_error(block_size - sizeof(TarFileHeader))); } bool TarInputStream::valid() const @@ -142,11 +142,11 @@ TarOutputStream::TarOutputStream(OutputStream& stream) void TarOutputStream::add_directory(const String& path, mode_t mode) { VERIFY(!m_finished); - Header header; + TarFileHeader header; memset(&header, 0, sizeof(header)); header.set_size(0); header.set_file_name(String::formatted("{}/", path)); // Old tar implementations assume directory names end with a / - header.set_type_flag(Directory); + header.set_type_flag(TarFileType::Directory); header.set_mode(mode); header.set_magic(gnu_magic); header.set_version(gnu_version); @@ -159,11 +159,11 @@ void TarOutputStream::add_directory(const String& path, mode_t mode) void TarOutputStream::add_file(const String& path, mode_t mode, const ReadonlyBytes& bytes) { VERIFY(!m_finished); - Header header; + TarFileHeader header; memset(&header, 0, sizeof(header)); header.set_size(bytes.size()); header.set_file_name(path); - header.set_type_flag(NormalFile); + header.set_type_flag(TarFileType::NormalFile); header.set_mode(mode); header.set_magic(gnu_magic); header.set_version(gnu_version); diff --git a/Userland/Libraries/LibTar/TarStream.h b/Userland/Libraries/LibArchive/TarStream.h similarity index 94% rename from Userland/Libraries/LibTar/TarStream.h rename to Userland/Libraries/LibArchive/TarStream.h index a59243ee558..9a486b9efef 100644 --- a/Userland/Libraries/LibTar/TarStream.h +++ b/Userland/Libraries/LibArchive/TarStream.h @@ -29,9 +29,9 @@ #include #include -#include +#include -namespace Tar { +namespace Archive { class TarInputStream; @@ -57,11 +57,11 @@ public: void advance(); bool finished() const { return m_finished; } bool valid() const; - const Header& header() const { return m_header; } + const TarFileHeader& header() const { return m_header; } TarFileStream file_contents(); private: - Header m_header; + TarFileHeader m_header; InputStream& m_stream; unsigned long m_file_offset { 0 }; int m_generation { 0 }; diff --git a/Userland/Libraries/LibTar/CMakeLists.txt b/Userland/Libraries/LibTar/CMakeLists.txt deleted file mode 100644 index 94c04204388..00000000000 --- a/Userland/Libraries/LibTar/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -set(SOURCES - TarStream.cpp -) - -serenity_lib(LibTar tar) -target_link_libraries(LibTar LibCore) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 46bab9ee276..1d5e3c697fa 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -39,7 +39,7 @@ target_link_libraries(passwd LibCrypt) target_link_libraries(paste LibGUI) target_link_libraries(pro LibProtocol) target_link_libraries(su LibCrypt) -target_link_libraries(tar LibTar LibCompress) +target_link_libraries(tar LibArchive LibCompress) target_link_libraries(test-crypto LibCrypto LibTLS LibLine) target_link_libraries(test-compress LibCompress) target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell) diff --git a/Userland/Utilities/tar.cpp b/Userland/Utilities/tar.cpp index c3fb4834c15..322abfc9de4 100644 --- a/Userland/Utilities/tar.cpp +++ b/Userland/Utilities/tar.cpp @@ -27,11 +27,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include @@ -81,7 +81,7 @@ int main(int argc, char** argv) InputStream& file_input_stream = file_stream; InputStream& gzip_input_stream = gzip_stream; - Tar::TarInputStream tar_stream((gzip) ? gzip_input_stream : file_input_stream); + Archive::TarInputStream tar_stream((gzip) ? gzip_input_stream : file_input_stream); if (!tar_stream.valid()) { warnln("the provided file is not a well-formatted ustar file"); return 1; @@ -91,12 +91,12 @@ int main(int argc, char** argv) outln("{}", tar_stream.header().file_name()); if (extract) { - Tar::TarFileStream file_stream = tar_stream.file_contents(); + Archive::TarFileStream file_stream = tar_stream.file_contents(); - const Tar::Header& header = tar_stream.header(); + const Archive::TarFileHeader& header = tar_stream.header(); switch (header.type_flag()) { - case Tar::NormalFile: - case Tar::AlternateNormalFile: { + case Archive::TarFileType::NormalFile: + case Archive::TarFileType::AlternateNormalFile: { int fd = open(String(header.file_name()).characters(), O_CREAT | O_WRONLY, header.mode()); if (fd < 0) { perror("open"); @@ -114,7 +114,7 @@ int main(int argc, char** argv) close(fd); break; } - case Tar::Directory: { + case Archive::TarFileType::Directory: { if (mkdir(String(header.file_name()).characters(), header.mode())) { perror("mkdir"); return 1; @@ -153,7 +153,7 @@ int main(int argc, char** argv) OutputStream& file_output_stream = file_stream; OutputStream& gzip_output_stream = gzip_stream; - Tar::TarOutputStream tar_stream((gzip) ? gzip_output_stream : file_output_stream); + Archive::TarOutputStream tar_stream((gzip) ? gzip_output_stream : file_output_stream); auto add_file = [&](String path) { auto file = Core::File::construct(path);