stdio_file_implementation.h 3.8 KB

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