stdio_file_implementation.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <AK/Types.h>
  8. #include <LibC/bits/FILE.h>
  9. #include <LibC/bits/pthread_integration.h>
  10. #include <LibC/bits/wchar.h>
  11. #include <sys/types.h>
  12. #pragma once
  13. struct FILE {
  14. public:
  15. FILE(int fd, int mode)
  16. : m_fd(fd)
  17. , m_mode(mode)
  18. {
  19. __pthread_mutex_init(&m_mutex, nullptr);
  20. }
  21. ~FILE();
  22. static FILE* create(int fd, int mode);
  23. void setbuf(u8* data, int mode, size_t size) { m_buffer.setbuf(data, mode, size); }
  24. bool flush();
  25. void purge();
  26. bool close();
  27. int fileno() const { return m_fd; }
  28. bool eof() const { return m_eof; }
  29. int mode() const { return m_mode; }
  30. u8 flags() const { return m_flags; }
  31. int error() const { return m_error; }
  32. void clear_err() { m_error = 0; }
  33. size_t read(u8*, size_t);
  34. size_t write(const u8*, size_t);
  35. template<typename CharType>
  36. bool gets(CharType*, size_t);
  37. bool ungetc(u8 byte) { return m_buffer.enqueue_front(byte); }
  38. int seek(off_t offset, int whence);
  39. off_t tell();
  40. pid_t popen_child() { return m_popen_child; }
  41. void set_popen_child(pid_t child_pid) { m_popen_child = child_pid; }
  42. void reopen(int fd, int mode);
  43. enum Flags : u8 {
  44. None = 0,
  45. LastRead = 1,
  46. LastWrite = 2,
  47. };
  48. private:
  49. struct Buffer {
  50. // A ringbuffer that also transparently implements ungetc().
  51. public:
  52. ~Buffer();
  53. int mode() const { return m_mode; }
  54. void setbuf(u8* data, int mode, size_t size);
  55. // Make sure to call realize() before enqueuing any data.
  56. // Dequeuing can be attempted without it.
  57. void realize(int fd);
  58. void drop();
  59. bool may_use() const;
  60. bool is_not_empty() const { return m_ungotten || !m_empty; }
  61. size_t buffered_size() const;
  62. const u8* begin_dequeue(size_t& available_size) const;
  63. void did_dequeue(size_t actual_size);
  64. u8* begin_enqueue(size_t& available_size) const;
  65. void did_enqueue(size_t actual_size);
  66. bool enqueue_front(u8 byte);
  67. private:
  68. constexpr static auto unget_buffer_size = MB_CUR_MAX;
  69. constexpr static u32 ungotten_mask = ((u32)0xffffffff) >> (sizeof(u32) * 8 - unget_buffer_size);
  70. // Note: the fields here are arranged this way
  71. // to make sizeof(Buffer) smaller.
  72. u8* m_data { nullptr };
  73. size_t m_capacity { BUFSIZ };
  74. size_t m_begin { 0 };
  75. size_t m_end { 0 };
  76. int m_mode { -1 };
  77. Array<u8, unget_buffer_size> m_unget_buffer { 0 };
  78. u32 m_ungotten : unget_buffer_size { 0 };
  79. bool m_data_is_malloced : 1 { false };
  80. // When m_begin == m_end, we want to distinguish whether
  81. // the buffer is full or empty.
  82. bool m_empty : 1 { true };
  83. };
  84. // Read or write using the underlying fd, bypassing the buffer.
  85. ssize_t do_read(u8*, size_t);
  86. ssize_t do_write(const u8*, size_t);
  87. // Read some data into the buffer.
  88. bool read_into_buffer();
  89. // Flush *some* data from the buffer.
  90. bool write_from_buffer();
  91. void lock();
  92. void unlock();
  93. int m_fd { -1 };
  94. int m_mode { 0 };
  95. u8 m_flags { Flags::None };
  96. int m_error { 0 };
  97. bool m_eof { false };
  98. pid_t m_popen_child { -1 };
  99. Buffer m_buffer;
  100. __pthread_mutex_t m_mutex;
  101. friend class ScopedFileLock;
  102. };
  103. class ScopedFileLock {
  104. public:
  105. ScopedFileLock(FILE* file)
  106. : m_file(file)
  107. {
  108. m_file->lock();
  109. }
  110. ~ScopedFileLock()
  111. {
  112. m_file->unlock();
  113. }
  114. private:
  115. FILE* m_file;
  116. };