stdio_file_implementation.h 3.9 KB

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