File.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. };
  30. enum class ShouldCloseFileDescriptor {
  31. Yes,
  32. No,
  33. };
  34. static ErrorOr<NonnullOwnPtr<File>> open(StringView filename, OpenMode, mode_t = 0644);
  35. static ErrorOr<NonnullOwnPtr<File>> adopt_fd(int fd, OpenMode, ShouldCloseFileDescriptor = ShouldCloseFileDescriptor::Yes);
  36. static ErrorOr<NonnullOwnPtr<File>> standard_input();
  37. static ErrorOr<NonnullOwnPtr<File>> standard_output();
  38. static ErrorOr<NonnullOwnPtr<File>> standard_error();
  39. static ErrorOr<NonnullOwnPtr<File>> open_file_or_standard_stream(StringView filename, OpenMode mode);
  40. File(File&& other) { operator=(move(other)); }
  41. File& operator=(File&& other)
  42. {
  43. if (&other == this)
  44. return *this;
  45. m_mode = exchange(other.m_mode, OpenMode::NotOpen);
  46. m_fd = exchange(other.m_fd, -1);
  47. m_last_read_was_eof = exchange(other.m_last_read_was_eof, false);
  48. return *this;
  49. }
  50. virtual ErrorOr<Bytes> read_some(Bytes) override;
  51. virtual ErrorOr<ByteBuffer> read_until_eof(size_t block_size = 4096) override;
  52. virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
  53. virtual bool is_eof() const override;
  54. virtual bool is_open() const override;
  55. virtual void close() override;
  56. virtual ErrorOr<size_t> seek(i64 offset, SeekMode) override;
  57. virtual ErrorOr<void> truncate(size_t length) override;
  58. template<OneOf<::IPC::File, ::Core::MappedFile> VIP>
  59. int leak_fd(Badge<VIP>)
  60. {
  61. m_should_close_file_descriptor = ShouldCloseFileDescriptor::No;
  62. return m_fd;
  63. }
  64. int fd() const
  65. {
  66. return m_fd;
  67. }
  68. virtual ~File() override
  69. {
  70. if (m_should_close_file_descriptor == ShouldCloseFileDescriptor::Yes)
  71. close();
  72. }
  73. static int open_mode_to_options(OpenMode mode);
  74. private:
  75. File(OpenMode mode, ShouldCloseFileDescriptor should_close = ShouldCloseFileDescriptor::Yes)
  76. : m_mode(mode)
  77. , m_should_close_file_descriptor(should_close)
  78. {
  79. }
  80. ErrorOr<void> open_path(StringView filename, mode_t);
  81. OpenMode m_mode { OpenMode::NotOpen };
  82. int m_fd { -1 };
  83. bool m_last_read_was_eof { false };
  84. ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes };
  85. };
  86. AK_ENUM_BITWISE_OPERATORS(File::OpenMode)
  87. using InputBufferedFile = InputBufferedSeekable<File>;
  88. using OutputBufferedFile = OutputBufferedStream<File>;
  89. }