Libraries: Rename LibTar to LibArchive

This is in preparation for a new implementation of zip archive
extraction and creation.
This commit is contained in:
Idan Horowitz 2021-03-18 16:22:05 +02:00 committed by Andreas Kling
parent 40eab55e7d
commit a809db90dd
Notes: sideshowbarker 2024-07-18 21:07:53 +09:00
8 changed files with 36 additions and 36 deletions

View file

@ -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)

View file

@ -0,0 +1,6 @@
set(SOURCES
TarStream.cpp
)
serenity_lib(LibArchive archive)
target_link_libraries(LibArchive LibCore)

View file

@ -32,9 +32,9 @@
#include <string.h>
#include <sys/types.h>
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<char>(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 N>
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)));

View file

@ -25,10 +25,10 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibTar/TarStream.h>
#include <LibArchive/TarStream.h>
#include <string.h>
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);

View file

@ -29,9 +29,9 @@
#include <AK/Span.h>
#include <AK/Stream.h>
#include <LibTar/Tar.h>
#include <LibArchive/Tar.h>
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 };

View file

@ -1,6 +0,0 @@
set(SOURCES
TarStream.cpp
)
serenity_lib(LibTar tar)
target_link_libraries(LibTar LibCore)

View file

@ -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)

View file

@ -27,11 +27,11 @@
#include <AK/LexicalPath.h>
#include <AK/Span.h>
#include <AK/Vector.h>
#include <LibArchive/TarStream.h>
#include <LibCompress/Gzip.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DirIterator.h>
#include <LibCore/FileStream.h>
#include <LibTar/TarStream.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
@ -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);