FileStream.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Buffered.h>
  28. #include <AK/Stream.h>
  29. #include <LibCore/File.h>
  30. namespace Core {
  31. class InputFileStream final : public InputStream {
  32. public:
  33. explicit InputFileStream(NonnullRefPtr<File> file)
  34. : m_file(file)
  35. {
  36. }
  37. static Result<InputFileStream, String> open(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::ReadOnly, mode_t permissions = 0644)
  38. {
  39. ASSERT((mode & 0xf) == IODevice::OpenMode::ReadOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  40. auto file_result = File::open(filename, mode, permissions);
  41. if (file_result.is_error())
  42. return file_result.error();
  43. return InputFileStream { file_result.value() };
  44. }
  45. static Result<Buffered<InputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::ReadOnly, mode_t permissions = 0644)
  46. {
  47. ASSERT((mode & 0xf) == IODevice::OpenMode::ReadOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  48. auto file_result = File::open(filename, mode, permissions);
  49. if (file_result.is_error())
  50. return file_result.error();
  51. return Buffered<InputFileStream> { file_result.value() };
  52. }
  53. size_t read(Bytes bytes) override
  54. {
  55. size_t nread = 0;
  56. if (!m_buffered.is_empty()) {
  57. nread += m_buffered.bytes().copy_trimmed_to(bytes);
  58. m_buffered.bytes().slice(nread).copy_to(m_buffered);
  59. m_buffered.trim(m_buffered.size() - nread);
  60. }
  61. while (nread < bytes.size() && !eof()) {
  62. if (m_file->has_error()) {
  63. set_fatal_error();
  64. return 0;
  65. }
  66. const auto buffer = m_file->read(bytes.size() - nread);
  67. nread += buffer.bytes().copy_to(bytes.slice(nread));
  68. }
  69. return nread;
  70. }
  71. bool read_or_error(Bytes bytes) override
  72. {
  73. if (read(bytes) < bytes.size()) {
  74. set_fatal_error();
  75. return false;
  76. }
  77. return true;
  78. }
  79. bool discard_or_error(size_t count) override
  80. {
  81. u8 buffer[4096];
  82. size_t ndiscarded = 0;
  83. while (ndiscarded < count && !eof())
  84. ndiscarded += read({ buffer, min<size_t>(count - ndiscarded, sizeof(buffer)) });
  85. if (eof()) {
  86. set_fatal_error();
  87. return false;
  88. }
  89. return true;
  90. }
  91. bool eof() const override
  92. {
  93. if (m_buffered.size() > 0)
  94. return false;
  95. if (m_file->eof())
  96. return true;
  97. m_buffered = m_file->read(4096);
  98. return m_buffered.size() == 0;
  99. }
  100. void close()
  101. {
  102. if (!m_file->close())
  103. set_fatal_error();
  104. }
  105. private:
  106. InputFileStream() = default;
  107. mutable NonnullRefPtr<File> m_file;
  108. mutable ByteBuffer m_buffered;
  109. };
  110. class OutputFileStream : public OutputStream {
  111. public:
  112. explicit OutputFileStream(NonnullRefPtr<File> file)
  113. : m_file(file)
  114. {
  115. }
  116. static Result<OutputFileStream, String> open(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644)
  117. {
  118. ASSERT((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  119. auto file_result = File::open(filename, mode, permissions);
  120. if (file_result.is_error())
  121. return file_result.error();
  122. return OutputFileStream { file_result.value() };
  123. }
  124. static Result<Buffered<OutputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644)
  125. {
  126. ASSERT((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  127. auto file_result = File::open(filename, mode, permissions);
  128. if (file_result.is_error())
  129. return file_result.error();
  130. return Buffered<OutputFileStream> { file_result.value() };
  131. }
  132. static OutputFileStream stdout()
  133. {
  134. return OutputFileStream { Core::File::stdout() };
  135. }
  136. static Buffered<OutputFileStream> stdout_buffered()
  137. {
  138. return Buffered<OutputFileStream> { Core::File::stdout() };
  139. }
  140. size_t write(ReadonlyBytes bytes) override
  141. {
  142. if (!m_file->write(bytes.data(), bytes.size())) {
  143. set_fatal_error();
  144. return 0;
  145. }
  146. return bytes.size();
  147. }
  148. bool write_or_error(ReadonlyBytes bytes) override
  149. {
  150. if (write(bytes) < bytes.size()) {
  151. set_fatal_error();
  152. return false;
  153. }
  154. return true;
  155. }
  156. void close()
  157. {
  158. if (!m_file->close())
  159. set_fatal_error();
  160. }
  161. private:
  162. NonnullRefPtr<File> m_file;
  163. };
  164. }