TarStream.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Span.h>
  9. #include <AK/Stream.h>
  10. #include <LibArchive/Tar.h>
  11. namespace Archive {
  12. class TarInputStream;
  13. class TarFileStream : public InputStream {
  14. public:
  15. size_t read(Bytes) override;
  16. bool unreliable_eof() const override;
  17. bool read_or_error(Bytes) override;
  18. bool discard_or_error(size_t count) override;
  19. private:
  20. TarFileStream(TarInputStream& stream);
  21. TarInputStream& m_tar_stream;
  22. int m_generation;
  23. friend class TarInputStream;
  24. };
  25. class TarInputStream {
  26. public:
  27. TarInputStream(InputStream&);
  28. void advance();
  29. bool finished() const { return m_finished; }
  30. bool valid() const;
  31. const TarFileHeader& header() const { return m_header; }
  32. TarFileStream file_contents();
  33. private:
  34. TarFileHeader m_header;
  35. InputStream& m_stream;
  36. unsigned long m_file_offset { 0 };
  37. int m_generation { 0 };
  38. bool m_finished { false };
  39. friend class TarFileStream;
  40. };
  41. class TarOutputStream {
  42. public:
  43. TarOutputStream(OutputStream&);
  44. void add_file(const String& path, mode_t, const ReadonlyBytes&);
  45. void add_directory(const String& path, mode_t);
  46. void finish();
  47. private:
  48. OutputStream& m_stream;
  49. bool m_finished { false };
  50. friend class TarFileStream;
  51. };
  52. }