stdio_file_implementation.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(u8 const*, 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. u8 const* readptr(size_t& available_size);
  49. enum Flags : u8 {
  50. None = 0,
  51. LastRead = 1,
  52. LastWrite = 2,
  53. };
  54. private:
  55. struct Buffer {
  56. // A ringbuffer that also transparently implements ungetc().
  57. public:
  58. ~Buffer();
  59. int mode() const { return m_mode; }
  60. void setbuf(u8* data, int mode, size_t size);
  61. // Make sure to call realize() before enqueuing any data.
  62. // Dequeuing can be attempted without it.
  63. void realize(int fd);
  64. void drop();
  65. bool may_use() const;
  66. bool is_not_empty() const { return m_ungotten || !m_empty; }
  67. size_t buffered_size() const;
  68. u8 const* begin_dequeue(size_t& available_size) const;
  69. void did_dequeue(size_t actual_size);
  70. u8* begin_enqueue(size_t& available_size) const;
  71. void did_enqueue(size_t actual_size);
  72. bool enqueue_front(u8 byte);
  73. private:
  74. constexpr static auto unget_buffer_size = MB_CUR_MAX;
  75. constexpr static u32 ungotten_mask = ((u32)0xffffffff) >> (sizeof(u32) * 8 - unget_buffer_size);
  76. // Note: the fields here are arranged this way
  77. // to make sizeof(Buffer) smaller.
  78. u8* m_data { nullptr };
  79. size_t m_capacity { BUFSIZ };
  80. size_t m_begin { 0 };
  81. size_t m_end { 0 };
  82. int m_mode { -1 };
  83. Array<u8, unget_buffer_size> m_unget_buffer { 0 };
  84. u32 m_ungotten : unget_buffer_size { 0 };
  85. bool m_data_is_malloced : 1 { false };
  86. // When m_begin == m_end, we want to distinguish whether
  87. // the buffer is full or empty.
  88. bool m_empty : 1 { true };
  89. };
  90. // Read or write using the underlying fd, bypassing the buffer.
  91. ssize_t do_read(u8*, size_t);
  92. ssize_t do_write(u8 const*, size_t);
  93. // Read some data into the buffer.
  94. bool read_into_buffer();
  95. // Flush *some* data from the buffer.
  96. bool write_from_buffer();
  97. int m_fd { -1 };
  98. int m_mode { 0 };
  99. u8 m_flags { Flags::None };
  100. int m_error { 0 };
  101. bool m_eof { false };
  102. pid_t m_popen_child { -1 };
  103. Buffer m_buffer;
  104. __pthread_mutex_t m_mutex;
  105. IntrusiveListNode<FILE> m_list_node;
  106. public:
  107. using List = IntrusiveList<&FILE::m_list_node>;
  108. };
  109. class ScopedFileLock {
  110. public:
  111. ScopedFileLock(FILE* file)
  112. : m_file(file)
  113. {
  114. m_file->lock();
  115. }
  116. ~ScopedFileLock()
  117. {
  118. m_file->unlock();
  119. }
  120. private:
  121. FILE* m_file;
  122. };