소스 검색

LibArchive: Use constexpr variables where possible

Lenny Maiorani 3 년 전
부모
커밋
847efd8aca
2개의 변경된 파일15개의 추가작업 그리고 13개의 파일을 삭제
  1. 6 6
      Userland/Libraries/LibArchive/Tar.h
  2. 9 7
      Userland/Libraries/LibArchive/TarStream.cpp

+ 6 - 6
Userland/Libraries/LibArchive/Tar.h

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

+ 9 - 7
Userland/Libraries/LibArchive/TarStream.cpp

@@ -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;
 }