File.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Badge.h>
  9. #include <AK/BufferedStream.h>
  10. #include <AK/Noncopyable.h>
  11. #include <AK/NonnullOwnPtr.h>
  12. #include <AK/Stream.h>
  13. #include <LibCore/Forward.h>
  14. #include <LibIPC/Forward.h>
  15. namespace Core {
  16. class File final : public SeekableStream {
  17. AK_MAKE_NONCOPYABLE(File);
  18. public:
  19. enum class OpenMode : unsigned {
  20. NotOpen = 0,
  21. Read = 1,
  22. Write = 2,
  23. ReadWrite = 3,
  24. Append = 4,
  25. Truncate = 8,
  26. MustBeNew = 16,
  27. KeepOnExec = 32,
  28. Nonblocking = 64,
  29. DontCreate = 128,
  30. };
  31. enum class ShouldCloseFileDescriptor {
  32. Yes,
  33. No,
  34. };
  35. static ErrorOr<NonnullOwnPtr<File>> open(StringView filename, OpenMode, mode_t = 0644);
  36. static ErrorOr<NonnullOwnPtr<File>> adopt_fd(int fd, OpenMode, ShouldCloseFileDescriptor = ShouldCloseFileDescriptor::Yes);
  37. static ErrorOr<NonnullOwnPtr<File>> standard_input();
  38. static ErrorOr<NonnullOwnPtr<File>> standard_output();
  39. static ErrorOr<NonnullOwnPtr<File>> standard_error();
  40. static ErrorOr<NonnullOwnPtr<File>> open_file_or_standard_stream(StringView filename, OpenMode mode);
  41. File(File&& other) { operator=(move(other)); }
  42. File& operator=(File&& other)
  43. {
  44. if (&other == this)
  45. return *this;
  46. m_mode = exchange(other.m_mode, OpenMode::NotOpen);
  47. m_fd = exchange(other.m_fd, -1);
  48. m_last_read_was_eof = exchange(other.m_last_read_was_eof, false);
  49. return *this;
  50. }
  51. virtual ErrorOr<Bytes> read_some(Bytes) override;
  52. virtual ErrorOr<ByteBuffer> read_until_eof(size_t block_size = 4096) override;
  53. virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
  54. virtual bool is_eof() const override;
  55. virtual bool is_open() const override;
  56. virtual void close() override;
  57. virtual ErrorOr<size_t> seek(i64 offset, SeekMode) override;
  58. virtual ErrorOr<size_t> tell() const override;
  59. virtual ErrorOr<void> truncate(size_t length) override;
  60. // Sets the blocking mode of the file. If blocking mode is disabled, reads
  61. // will fail with EAGAIN when there's no data available to read, and writes
  62. // will fail with EAGAIN when the data cannot be written without blocking
  63. // (due to the send buffer being full, for example).
  64. // See also Socket::set_blocking.
  65. ErrorOr<void> set_blocking(bool enabled);
  66. template<OneOf<::IPC::File, ::Core::MappedFile> VIP>
  67. int leak_fd(Badge<VIP>)
  68. {
  69. m_should_close_file_descriptor = ShouldCloseFileDescriptor::No;
  70. return m_fd;
  71. }
  72. int fd() const
  73. {
  74. return m_fd;
  75. }
  76. virtual ~File() override
  77. {
  78. if (m_should_close_file_descriptor == ShouldCloseFileDescriptor::Yes)
  79. close();
  80. }
  81. static int open_mode_to_options(OpenMode mode);
  82. private:
  83. File(OpenMode mode, ShouldCloseFileDescriptor should_close = ShouldCloseFileDescriptor::Yes)
  84. : m_mode(mode)
  85. , m_should_close_file_descriptor(should_close)
  86. {
  87. }
  88. ErrorOr<void> open_path(StringView filename, mode_t);
  89. OpenMode m_mode { OpenMode::NotOpen };
  90. int m_fd { -1 };
  91. bool m_last_read_was_eof { false };
  92. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  93. size_t m_file_offset { 0 };
  94. };
  95. AK_ENUM_BITWISE_OPERATORS(File::OpenMode)
  96. using InputBufferedFile = InputBufferedSeekable<File>;
  97. using OutputBufferedFile = OutputBufferedSeekable<File>;
  98. }