1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:57:45 +00:00

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
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 size_t block_size = 512;
constexpr const char* gnu_magic = "ustar "; // gnu format magic constexpr StringView gnu_magic = "ustar "; // gnu format magic
constexpr const char* gnu_version = " "; // gnu format version constexpr StringView gnu_version = " "; // gnu format version
constexpr const char* ustar_magic = "ustar"; // ustar format magic constexpr StringView ustar_magic = "ustar"; // ustar format magic
constexpr const char* ustar_version = "00"; // ustar format version constexpr StringView ustar_version = "00"; // ustar format version
constexpr const char* posix1_tar_magic = ""; // POSIX.1-1988 format magic constexpr StringView posix1_tar_magic = ""; // POSIX.1-1988 format magic
constexpr const char* posix1_tar_version = ""; // POSIX.1-1988 format version constexpr StringView posix1_tar_version = ""; // POSIX.1-1988 format version
template<size_t N> template<size_t N>
static size_t get_field_as_integral(const char (&field)[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) 2020, Peter Elliott <pelliott@serenityos.org>
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Array.h>
#include <LibArchive/TarStream.h> #include <LibArchive/TarStream.h>
#include <string.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_magic(gnu_magic);
header.set_version(gnu_version); header.set_version(gnu_version);
header.calculate_checksum(); header.calculate_checksum();
VERIFY(m_stream.write_or_error(Bytes { &header, sizeof(header) })); VERIFY(m_stream.write_or_error(ReadonlyBytes { &header, sizeof(header) }));
u8 padding[block_size] = { 0 }; constexpr Array<u8, block_size> padding { 0 };
VERIFY(m_stream.write_or_error(Bytes { &padding, block_size - sizeof(header) })); VERIFY(m_stream.write_or_error(ReadonlyBytes { &padding, block_size - sizeof(header) }));
size_t n_written = 0; size_t n_written = 0;
while (n_written < bytes.size()) { while (n_written < bytes.size()) {
n_written += m_stream.write(bytes.slice(n_written, min(bytes.size() - n_written, block_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() void TarOutputStream::finish()
{ {
VERIFY(!m_finished); VERIFY(!m_finished);
u8 padding[block_size] = { 0 }; constexpr Array<u8, block_size> padding { 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(ReadonlyBytes { &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 }); m_stream.write_or_error(ReadonlyBytes { &padding, block_size });
m_finished = true; m_finished = true;
} }