TarStream.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Array.h>
  9. #include <LibArchive/TarStream.h>
  10. #include <string.h>
  11. namespace Archive {
  12. TarFileStream::TarFileStream(TarInputStream& tar_stream)
  13. : m_tar_stream(tar_stream)
  14. , m_generation(tar_stream.m_generation)
  15. {
  16. }
  17. ErrorOr<Bytes> TarFileStream::read(Bytes bytes)
  18. {
  19. // Verify that the stream has not advanced.
  20. VERIFY(m_tar_stream.m_generation == m_generation);
  21. auto header_size = TRY(m_tar_stream.header().size());
  22. auto to_read = min(bytes.size(), header_size - m_tar_stream.m_file_offset);
  23. auto slice = TRY(m_tar_stream.m_stream->read(bytes.trim(to_read)));
  24. m_tar_stream.m_file_offset += slice.size();
  25. return slice;
  26. }
  27. bool TarFileStream::is_eof() const
  28. {
  29. // Verify that the stream has not advanced.
  30. VERIFY(m_tar_stream.m_generation == m_generation);
  31. auto header_size_or_error = m_tar_stream.header().size();
  32. if (header_size_or_error.is_error())
  33. return true;
  34. auto header_size = header_size_or_error.release_value();
  35. return m_tar_stream.m_stream->is_eof()
  36. || m_tar_stream.m_file_offset >= header_size;
  37. }
  38. ErrorOr<size_t> TarFileStream::write(ReadonlyBytes)
  39. {
  40. // This is purely for wrapping and representing file contents in an archive.
  41. VERIFY_NOT_REACHED();
  42. }
  43. ErrorOr<NonnullOwnPtr<TarInputStream>> TarInputStream::construct(NonnullOwnPtr<Core::Stream::Stream> stream)
  44. {
  45. auto tar_stream = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TarInputStream(move(stream))));
  46. // Try and read the header.
  47. auto header_span = TRY(tar_stream->m_stream->read(Bytes(&tar_stream->m_header, sizeof(m_header))));
  48. if (header_span.size() != sizeof(m_header))
  49. return Error::from_string_literal("Failed to read the entire header");
  50. // Discard the rest of the block.
  51. TRY(tar_stream->m_stream->discard(block_size - sizeof(TarFileHeader)));
  52. return tar_stream;
  53. }
  54. TarInputStream::TarInputStream(NonnullOwnPtr<Core::Stream::Stream> stream)
  55. : m_stream(move(stream))
  56. {
  57. }
  58. static constexpr unsigned long block_ceiling(unsigned long offset)
  59. {
  60. return block_size * (1 + ((offset - 1) / block_size));
  61. }
  62. ErrorOr<void> TarInputStream::advance()
  63. {
  64. if (finished())
  65. return Error::from_string_literal("Attempted to advance a finished stream");
  66. m_generation++;
  67. // Discard the pending bytes of the current entry.
  68. auto file_size = TRY(m_header.size());
  69. TRY(m_stream->discard(block_ceiling(file_size) - m_file_offset));
  70. m_file_offset = 0;
  71. // FIXME: This is not unlike the initial initialization. Maybe we should merge those two.
  72. auto header_span = TRY(m_stream->read(Bytes(&m_header, sizeof(m_header))));
  73. if (header_span.size() != sizeof(m_header))
  74. return Error::from_string_literal("Failed to read the entire header");
  75. if (!valid())
  76. return Error::from_string_literal("Header is not valid");
  77. // Discard the rest of the header block.
  78. TRY(m_stream->discard(block_size - sizeof(TarFileHeader)));
  79. return {};
  80. }
  81. bool TarInputStream::valid() const
  82. {
  83. auto const header_magic = header().magic();
  84. auto const header_version = header().version();
  85. if (!((header_magic == gnu_magic && header_version == gnu_version)
  86. || (header_magic == ustar_magic && header_version == ustar_version)
  87. || (header_magic == posix1_tar_magic && header_version == posix1_tar_version)))
  88. return false;
  89. // POSIX.1-1988 tar does not have magic numbers, so we also need to verify the header checksum.
  90. if (header().checksum().is_error())
  91. return false;
  92. return header().checksum().release_value() == header().expected_checksum();
  93. }
  94. TarFileStream TarInputStream::file_contents()
  95. {
  96. VERIFY(!finished());
  97. return TarFileStream(*this);
  98. }
  99. TarOutputStream::TarOutputStream(OutputStream& stream)
  100. : m_stream(stream)
  101. {
  102. }
  103. void TarOutputStream::add_directory(String const& path, mode_t mode)
  104. {
  105. VERIFY(!m_finished);
  106. TarFileHeader header {};
  107. header.set_size(0);
  108. header.set_filename_and_prefix(String::formatted("{}/", path)); // Old tar implementations assume directory names end with a /
  109. header.set_type_flag(TarFileType::Directory);
  110. header.set_mode(mode);
  111. header.set_magic(gnu_magic);
  112. header.set_version(gnu_version);
  113. header.calculate_checksum();
  114. VERIFY(m_stream.write_or_error(Bytes { &header, sizeof(header) }));
  115. u8 padding[block_size] = { 0 };
  116. VERIFY(m_stream.write_or_error(Bytes { &padding, block_size - sizeof(header) }));
  117. }
  118. void TarOutputStream::add_file(String const& path, mode_t mode, ReadonlyBytes bytes)
  119. {
  120. VERIFY(!m_finished);
  121. TarFileHeader header {};
  122. header.set_size(bytes.size());
  123. header.set_filename_and_prefix(path);
  124. header.set_type_flag(TarFileType::NormalFile);
  125. header.set_mode(mode);
  126. header.set_magic(gnu_magic);
  127. header.set_version(gnu_version);
  128. header.calculate_checksum();
  129. VERIFY(m_stream.write_or_error(ReadonlyBytes { &header, sizeof(header) }));
  130. constexpr Array<u8, block_size> padding { 0 };
  131. VERIFY(m_stream.write_or_error(ReadonlyBytes { &padding, block_size - sizeof(header) }));
  132. size_t n_written = 0;
  133. while (n_written < bytes.size()) {
  134. n_written += m_stream.write(bytes.slice(n_written, min(bytes.size() - n_written, block_size)));
  135. }
  136. VERIFY(m_stream.write_or_error(ReadonlyBytes { &padding, block_size - (n_written % block_size) }));
  137. }
  138. void TarOutputStream::add_link(String const& path, mode_t mode, StringView link_name)
  139. {
  140. VERIFY(!m_finished);
  141. TarFileHeader header {};
  142. header.set_size(0);
  143. header.set_filename_and_prefix(path);
  144. header.set_type_flag(TarFileType::SymLink);
  145. header.set_mode(mode);
  146. header.set_magic(gnu_magic);
  147. header.set_version(gnu_version);
  148. header.set_link_name(link_name);
  149. header.calculate_checksum();
  150. VERIFY(m_stream.write_or_error(Bytes { &header, sizeof(header) }));
  151. u8 padding[block_size] = { 0 };
  152. VERIFY(m_stream.write_or_error(Bytes { &padding, block_size - sizeof(header) }));
  153. }
  154. void TarOutputStream::finish()
  155. {
  156. VERIFY(!m_finished);
  157. constexpr Array<u8, block_size> padding { 0 };
  158. m_stream.write_or_error(ReadonlyBytes { &padding, block_size }); // 2 empty records that are used to signify the end of the archive
  159. m_stream.write_or_error(ReadonlyBytes { &padding, block_size });
  160. m_finished = true;
  161. }
  162. }