stdio_file_implementation.h 3.6 KB

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