TarStream.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibArchive/TarStream.h>
  8. #include <string.h>
  9. namespace Archive {
  10. TarFileStream::TarFileStream(TarInputStream& tar_stream)
  11. : m_tar_stream(tar_stream)
  12. , m_generation(tar_stream.m_generation)
  13. {
  14. }
  15. size_t TarFileStream::read(Bytes bytes)
  16. {
  17. // verify that the stream has not advanced
  18. VERIFY(m_tar_stream.m_generation == m_generation);
  19. if (has_any_error())
  20. return 0;
  21. auto to_read = min(bytes.size(), m_tar_stream.header().size() - m_tar_stream.m_file_offset);
  22. auto nread = m_tar_stream.m_stream.read(bytes.trim(to_read));
  23. m_tar_stream.m_file_offset += nread;
  24. return nread;
  25. }
  26. bool TarFileStream::unreliable_eof() const
  27. {
  28. // verify that the stream has not advanced
  29. VERIFY(m_tar_stream.m_generation == m_generation);
  30. return m_tar_stream.m_stream.unreliable_eof()
  31. || m_tar_stream.m_file_offset >= m_tar_stream.header().size();
  32. }
  33. bool TarFileStream::read_or_error(Bytes bytes)
  34. {
  35. // verify that the stream has not advanced
  36. VERIFY(m_tar_stream.m_generation == m_generation);
  37. if (read(bytes) < bytes.size()) {
  38. set_fatal_error();
  39. return false;
  40. }
  41. return true;
  42. }
  43. bool TarFileStream::discard_or_error(size_t count)
  44. {
  45. // verify that the stream has not advanced
  46. VERIFY(m_tar_stream.m_generation == m_generation);
  47. if (count > m_tar_stream.header().size() - m_tar_stream.m_file_offset) {
  48. return false;
  49. }
  50. m_tar_stream.m_file_offset += count;
  51. return m_tar_stream.m_stream.discard_or_error(count);
  52. }
  53. TarInputStream::TarInputStream(InputStream& stream)
  54. : m_stream(stream)
  55. {
  56. if (!m_stream.read_or_error(Bytes(&m_header, sizeof(m_header)))) {
  57. m_finished = true;
  58. m_stream.handle_any_error(); // clear out errors so we dont assert
  59. return;
  60. }
  61. VERIFY(m_stream.discard_or_error(block_size - sizeof(TarFileHeader)));
  62. }
  63. static constexpr unsigned long block_ceiling(unsigned long offset)
  64. {
  65. return block_size * (1 + ((offset - 1) / block_size));
  66. }
  67. void TarInputStream::advance()
  68. {
  69. if (m_finished)
  70. return;
  71. m_generation++;
  72. VERIFY(m_stream.discard_or_error(block_ceiling(m_header.size()) - m_file_offset));
  73. m_file_offset = 0;
  74. if (!m_stream.read_or_error(Bytes(&m_header, sizeof(m_header)))) {
  75. m_finished = true;
  76. return;
  77. }
  78. if (!valid()) {
  79. m_finished = true;
  80. return;
  81. }
  82. VERIFY(m_stream.discard_or_error(block_size - sizeof(TarFileHeader)));
  83. }
  84. bool TarInputStream::valid() const
  85. {
  86. auto const header_magic = header().magic();
  87. auto const header_version = header().version();
  88. if (!((header_magic == gnu_magic && header_version == gnu_version)
  89. || (header_magic == ustar_magic && header_version == ustar_version)
  90. || (header_magic == posix1_tar_magic && header_version == posix1_tar_version)))
  91. return false;
  92. // POSIX.1-1988 tar does not have magic numbers, so we also need to verify the header checksum.
  93. return header().checksum() == header().expected_checksum();
  94. }
  95. TarFileStream TarInputStream::file_contents()
  96. {
  97. VERIFY(!m_finished);
  98. return TarFileStream(*this);
  99. }
  100. TarOutputStream::TarOutputStream(OutputStream& stream)
  101. : m_stream(stream)
  102. {
  103. }
  104. void TarOutputStream::add_directory(const String& path, mode_t mode)
  105. {
  106. VERIFY(!m_finished);
  107. TarFileHeader header;
  108. memset(&header, 0, sizeof(header));
  109. header.set_size(0);
  110. header.set_filename(String::formatted("{}/", path)); // Old tar implementations assume directory names end with a /
  111. header.set_type_flag(TarFileType::Directory);
  112. header.set_mode(mode);
  113. header.set_magic(gnu_magic);
  114. header.set_version(gnu_version);
  115. header.calculate_checksum();
  116. VERIFY(m_stream.write_or_error(Bytes { &header, sizeof(header) }));
  117. u8 padding[block_size] = { 0 };
  118. VERIFY(m_stream.write_or_error(Bytes { &padding, block_size - sizeof(header) }));
  119. }
  120. void TarOutputStream::add_file(const String& path, mode_t mode, ReadonlyBytes bytes)
  121. {
  122. VERIFY(!m_finished);
  123. TarFileHeader header;
  124. memset(&header, 0, sizeof(header));
  125. header.set_size(bytes.size());
  126. header.set_filename(path);
  127. header.set_type_flag(TarFileType::NormalFile);
  128. header.set_mode(mode);
  129. header.set_magic(gnu_magic);
  130. header.set_version(gnu_version);
  131. header.calculate_checksum();
  132. VERIFY(m_stream.write_or_error(Bytes { &header, sizeof(header) }));
  133. u8 padding[block_size] = { 0 };
  134. VERIFY(m_stream.write_or_error(Bytes { &padding, block_size - sizeof(header) }));
  135. size_t n_written = 0;
  136. while (n_written < bytes.size()) {
  137. n_written += m_stream.write(bytes.slice(n_written, min(bytes.size() - n_written, block_size)));
  138. }
  139. VERIFY(m_stream.write_or_error(Bytes { &padding, block_size - (n_written % block_size) }));
  140. }
  141. void TarOutputStream::finish()
  142. {
  143. VERIFY(!m_finished);
  144. u8 padding[block_size] = { 0 };
  145. m_stream.write_or_error(Bytes { &padding, block_size }); // 2 empty records that are used to signify the end of the archive
  146. m_stream.write_or_error(Bytes { &padding, block_size });
  147. m_finished = true;
  148. }
  149. }