TarStream.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <AK/OwnPtr.h>
  10. #include <LibArchive/TarStream.h>
  11. #include <string.h>
  12. namespace Archive {
  13. TarFileStream::TarFileStream(TarInputStream& tar_stream)
  14. : m_tar_stream(tar_stream)
  15. , m_generation(tar_stream.m_generation)
  16. {
  17. }
  18. ErrorOr<Bytes> TarFileStream::read_some(Bytes bytes)
  19. {
  20. // Verify that the stream has not advanced.
  21. VERIFY(m_tar_stream.m_generation == m_generation);
  22. auto header_size = TRY(m_tar_stream.header().size());
  23. auto to_read = min(bytes.size(), header_size - m_tar_stream.m_file_offset);
  24. auto slice = TRY(m_tar_stream.m_stream->read_some(bytes.trim(to_read)));
  25. m_tar_stream.m_file_offset += slice.size();
  26. return slice;
  27. }
  28. bool TarFileStream::is_eof() const
  29. {
  30. // Verify that the stream has not advanced.
  31. VERIFY(m_tar_stream.m_generation == m_generation);
  32. auto header_size_or_error = m_tar_stream.header().size();
  33. if (header_size_or_error.is_error())
  34. return true;
  35. auto header_size = header_size_or_error.release_value();
  36. return m_tar_stream.m_stream->is_eof()
  37. || m_tar_stream.m_file_offset >= header_size;
  38. }
  39. ErrorOr<size_t> TarFileStream::write_some(ReadonlyBytes)
  40. {
  41. return Error::from_errno(EBADF);
  42. }
  43. ErrorOr<NonnullOwnPtr<TarInputStream>> TarInputStream::construct(NonnullOwnPtr<Stream> stream)
  44. {
  45. auto tar_stream = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TarInputStream(move(stream))));
  46. TRY(tar_stream->load_next_header());
  47. return tar_stream;
  48. }
  49. TarInputStream::TarInputStream(NonnullOwnPtr<Stream> stream)
  50. : m_stream(move(stream))
  51. {
  52. }
  53. static constexpr unsigned long block_ceiling(unsigned long offset)
  54. {
  55. return block_size * (1 + ((offset - 1) / block_size));
  56. }
  57. ErrorOr<void> TarInputStream::advance()
  58. {
  59. if (finished())
  60. return Error::from_string_literal("Attempted to advance a finished stream");
  61. m_generation++;
  62. // Discard the pending bytes of the current entry.
  63. auto file_size = TRY(m_header.size());
  64. TRY(m_stream->discard(block_ceiling(file_size) - m_file_offset));
  65. m_file_offset = 0;
  66. TRY(load_next_header());
  67. return {};
  68. }
  69. ErrorOr<void> TarInputStream::load_next_header()
  70. {
  71. size_t number_of_consecutive_zero_blocks = 0;
  72. while (true) {
  73. // FIXME: This should read the entire span.
  74. auto header_span = TRY(m_stream->read_some(Bytes(&m_header, sizeof(m_header))));
  75. if (header_span.size() != sizeof(m_header))
  76. return Error::from_string_literal("Failed to read the entire header");
  77. // Discard the rest of the header block.
  78. TRY(m_stream->discard(block_size - sizeof(TarFileHeader)));
  79. if (!header().is_zero_block())
  80. break;
  81. number_of_consecutive_zero_blocks++;
  82. // Two zero blocks in a row marks the end of the archive.
  83. if (number_of_consecutive_zero_blocks >= 2) {
  84. m_found_end_of_archive = true;
  85. return {};
  86. }
  87. }
  88. if (!TRY(valid()))
  89. return Error::from_string_literal("Header has an invalid magic or checksum");
  90. return {};
  91. }
  92. ErrorOr<bool> TarInputStream::valid() const
  93. {
  94. auto const header_magic = header().magic();
  95. auto const header_version = header().version();
  96. if (!((header_magic == gnu_magic && header_version == gnu_version)
  97. || (header_magic == ustar_magic && header_version == ustar_version)
  98. || (header_magic == posix1_tar_magic && header_version == posix1_tar_version)))
  99. return false;
  100. // POSIX.1-1988 tar does not have magic numbers, so we also need to verify the header checksum.
  101. return TRY(header().checksum()) == header().expected_checksum();
  102. }
  103. TarFileStream TarInputStream::file_contents()
  104. {
  105. VERIFY(!finished());
  106. return TarFileStream(*this);
  107. }
  108. TarOutputStream::TarOutputStream(MaybeOwned<Stream> stream)
  109. : m_stream(move(stream))
  110. {
  111. }
  112. ErrorOr<void> TarOutputStream::add_directory(StringView path, mode_t mode)
  113. {
  114. VERIFY(!m_finished);
  115. TarFileHeader header {};
  116. TRY(header.set_size(0));
  117. header.set_filename_and_prefix(TRY(String::formatted("{}/", path))); // Old tar implementations assume directory names end with a /
  118. header.set_type_flag(TarFileType::Directory);
  119. TRY(header.set_mode(mode));
  120. header.set_magic(gnu_magic);
  121. header.set_version(gnu_version);
  122. TRY(header.calculate_checksum());
  123. TRY(m_stream->write_until_depleted(Bytes { &header, sizeof(header) }));
  124. u8 padding[block_size] = { 0 };
  125. TRY(m_stream->write_until_depleted(Bytes { &padding, block_size - sizeof(header) }));
  126. return {};
  127. }
  128. ErrorOr<void> TarOutputStream::add_file(StringView path, mode_t mode, ReadonlyBytes bytes)
  129. {
  130. VERIFY(!m_finished);
  131. TarFileHeader header {};
  132. TRY(header.set_size(bytes.size()));
  133. header.set_filename_and_prefix(path);
  134. header.set_type_flag(TarFileType::NormalFile);
  135. TRY(header.set_mode(mode));
  136. header.set_magic(gnu_magic);
  137. header.set_version(gnu_version);
  138. TRY(header.calculate_checksum());
  139. TRY(m_stream->write_until_depleted(ReadonlyBytes { &header, sizeof(header) }));
  140. constexpr Array<u8, block_size> padding { 0 };
  141. TRY(m_stream->write_until_depleted(ReadonlyBytes { &padding, block_size - sizeof(header) }));
  142. size_t n_written = 0;
  143. while (n_written < bytes.size()) {
  144. n_written += MUST(m_stream->write_some(bytes.slice(n_written, min(bytes.size() - n_written, block_size))));
  145. }
  146. TRY(m_stream->write_until_depleted(ReadonlyBytes { &padding, block_size - (n_written % block_size) }));
  147. return {};
  148. }
  149. ErrorOr<void> TarOutputStream::add_link(StringView path, mode_t mode, StringView link_name)
  150. {
  151. VERIFY(!m_finished);
  152. TarFileHeader header {};
  153. TRY(header.set_size(0));
  154. header.set_filename_and_prefix(path);
  155. header.set_type_flag(TarFileType::SymLink);
  156. TRY(header.set_mode(mode));
  157. header.set_magic(gnu_magic);
  158. header.set_version(gnu_version);
  159. header.set_link_name(link_name);
  160. TRY(header.calculate_checksum());
  161. TRY(m_stream->write_until_depleted(Bytes { &header, sizeof(header) }));
  162. u8 padding[block_size] = { 0 };
  163. TRY(m_stream->write_until_depleted(Bytes { &padding, block_size - sizeof(header) }));
  164. return {};
  165. }
  166. ErrorOr<void> TarOutputStream::finish()
  167. {
  168. VERIFY(!m_finished);
  169. constexpr Array<u8, block_size> padding { 0 };
  170. // 2 empty records that are used to signify the end of the archive.
  171. TRY(m_stream->write_until_depleted(ReadonlyBytes { &padding, block_size }));
  172. TRY(m_stream->write_until_depleted(ReadonlyBytes { &padding, block_size }));
  173. m_finished = true;
  174. return {};
  175. }
  176. }