FileStream.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. if (has_any_error())
  56. return 0;
  57. auto nread = m_buffered.bytes().copy_trimmed_to(bytes);
  58. m_buffered.bytes().slice(nread, m_buffered.size() - nread).copy_to(m_buffered);
  59. m_buffered.trim(m_buffered.size() - nread);
  60. while (nread < bytes.size()) {
  61. if (m_file->eof())
  62. return nread;
  63. if (m_file->has_error()) {
  64. set_fatal_error();
  65. return 0;
  66. }
  67. const auto buffer = m_file->read(bytes.size() - nread);
  68. nread += buffer.bytes().copy_to(bytes.slice(nread));
  69. }
  70. return nread;
  71. }
  72. bool read_or_error(Bytes bytes) override
  73. {
  74. if (read(bytes) < bytes.size()) {
  75. set_fatal_error();
  76. return false;
  77. }
  78. return true;
  79. }
  80. bool discard_or_error(size_t count) override
  81. {
  82. u8 buffer[4096];
  83. size_t ndiscarded = 0;
  84. while (ndiscarded < count && !eof())
  85. ndiscarded += read({ buffer, min<size_t>(count - ndiscarded, sizeof(buffer)) });
  86. if (eof()) {
  87. set_fatal_error();
  88. return false;
  89. }
  90. return true;
  91. }
  92. bool eof() const override
  93. {
  94. if (m_buffered.size() > 0)
  95. return false;
  96. if (m_file->eof())
  97. return true;
  98. m_buffered = m_file->read(4096);
  99. return m_buffered.size() == 0;
  100. }
  101. void close()
  102. {
  103. if (!m_file->close())
  104. set_fatal_error();
  105. }
  106. private:
  107. InputFileStream() = default;
  108. mutable NonnullRefPtr<File> m_file;
  109. mutable ByteBuffer m_buffered;
  110. };
  111. class OutputFileStream : public OutputStream {
  112. public:
  113. explicit OutputFileStream(NonnullRefPtr<File> file)
  114. : m_file(file)
  115. {
  116. }
  117. static Result<OutputFileStream, String> open(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644)
  118. {
  119. ASSERT((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  120. auto file_result = File::open(filename, mode, permissions);
  121. if (file_result.is_error())
  122. return file_result.error();
  123. return OutputFileStream { file_result.value() };
  124. }
  125. static Result<Buffered<OutputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644)
  126. {
  127. ASSERT((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
  128. auto file_result = File::open(filename, mode, permissions);
  129. if (file_result.is_error())
  130. return file_result.error();
  131. return Buffered<OutputFileStream> { file_result.value() };
  132. }
  133. static OutputFileStream stdout()
  134. {
  135. return OutputFileStream { Core::File::stdout() };
  136. }
  137. static Buffered<OutputFileStream> stdout_buffered()
  138. {
  139. return Buffered<OutputFileStream> { Core::File::stdout() };
  140. }
  141. size_t write(ReadonlyBytes bytes) override
  142. {
  143. if (!m_file->write(bytes.data(), bytes.size())) {
  144. set_fatal_error();
  145. return 0;
  146. }
  147. return bytes.size();
  148. }
  149. bool write_or_error(ReadonlyBytes bytes) override
  150. {
  151. if (write(bytes) < bytes.size()) {
  152. set_fatal_error();
  153. return false;
  154. }
  155. return true;
  156. }
  157. void close()
  158. {
  159. if (!m_file->close())
  160. set_fatal_error();
  161. }
  162. private:
  163. NonnullRefPtr<File> m_file;
  164. };
  165. }