LibArchive: Use constexpr variables where possible

This commit is contained in:
Lenny Maiorani 2022-02-15 10:14:31 -07:00 committed by Idan Horowitz
parent 583e197897
commit 847efd8aca
Notes: sideshowbarker 2024-07-17 18:44:56 +09:00
2 changed files with 15 additions and 13 deletions

View File

@ -30,12 +30,12 @@ enum class TarFileType : char {
};
constexpr size_t block_size = 512;
constexpr const char* gnu_magic = "ustar "; // gnu format magic
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
constexpr const char* posix1_tar_magic = ""; // POSIX.1-1988 format magic
constexpr const char* posix1_tar_version = ""; // POSIX.1-1988 format version
constexpr StringView gnu_magic = "ustar "; // gnu format magic
constexpr StringView gnu_version = " "; // gnu format version
constexpr StringView ustar_magic = "ustar"; // ustar format magic
constexpr StringView ustar_version = "00"; // ustar format version
constexpr StringView posix1_tar_magic = ""; // POSIX.1-1988 format magic
constexpr StringView posix1_tar_version = ""; // POSIX.1-1988 format version
template<size_t N>
static size_t get_field_as_integral(const char (&field)[N])

View File

@ -1,10 +1,12 @@
/*
* Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Array.h>
#include <LibArchive/TarStream.h>
#include <string.h>
@ -155,22 +157,22 @@ void TarOutputStream::add_file(const String& path, mode_t mode, ReadonlyBytes by
header.set_magic(gnu_magic);
header.set_version(gnu_version);
header.calculate_checksum();
VERIFY(m_stream.write_or_error(Bytes { &header, sizeof(header) }));
u8 padding[block_size] = { 0 };
VERIFY(m_stream.write_or_error(Bytes { &padding, block_size - sizeof(header) }));
VERIFY(m_stream.write_or_error(ReadonlyBytes { &header, sizeof(header) }));
constexpr Array<u8, block_size> padding { 0 };
VERIFY(m_stream.write_or_error(ReadonlyBytes { &padding, block_size - sizeof(header) }));
size_t n_written = 0;
while (n_written < bytes.size()) {
n_written += m_stream.write(bytes.slice(n_written, min(bytes.size() - n_written, block_size)));
}
VERIFY(m_stream.write_or_error(Bytes { &padding, block_size - (n_written % block_size) }));
VERIFY(m_stream.write_or_error(ReadonlyBytes { &padding, block_size - (n_written % block_size) }));
}
void TarOutputStream::finish()
{
VERIFY(!m_finished);
u8 padding[block_size] = { 0 };
m_stream.write_or_error(Bytes { &padding, block_size }); // 2 empty records that are used to signify the end of the archive
m_stream.write_or_error(Bytes { &padding, block_size });
constexpr Array<u8, block_size> padding { 0 };
m_stream.write_or_error(ReadonlyBytes { &padding, block_size }); // 2 empty records that are used to signify the end of the archive
m_stream.write_or_error(ReadonlyBytes { &padding, block_size });
m_finished = true;
}