Message.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Types.h>
  8. #include <Kernel/FileSystem/Plan9FS/Definitions.h>
  9. #include <Kernel/KBuffer.h>
  10. #include <Kernel/KBufferBuilder.h>
  11. namespace Kernel {
  12. class Plan9FS;
  13. class Plan9FSInode;
  14. class Plan9FSMessage {
  15. friend class Plan9FS;
  16. friend class Plan9FSInode;
  17. public:
  18. enum class Type : u8 {
  19. // 9P2000.L
  20. Tlerror = 6,
  21. Rlerror = 7,
  22. Tstatfs = 8,
  23. Rstatfs = 9,
  24. Tlopen = 12,
  25. Rlopen = 13,
  26. Tlcreate = 14,
  27. Rlcreate = 15,
  28. Tsymlink = 16,
  29. Rsymlink = 17,
  30. Tmknod = 18,
  31. Rmknod = 19,
  32. Trename = 20,
  33. Rrename = 21,
  34. Treadlink = 22,
  35. Rreadlink = 23,
  36. Tgetattr = 24,
  37. Rgetattr = 25,
  38. Tsetattr = 26,
  39. Rsetattr = 27,
  40. Txattrwalk = 30,
  41. Rxattrwalk = 31,
  42. Txattrcreate = 32,
  43. Rxattrcreate = 33,
  44. Treaddir = 40,
  45. Rreaddir = 41,
  46. Tfsync = 50,
  47. Rfsync = 51,
  48. Tlock = 52,
  49. Rlock = 53,
  50. Tgetlock = 54,
  51. Rgetlock = 55,
  52. Tlink = 70,
  53. Rlink = 71,
  54. Tmkdir = 72,
  55. Rmkdir = 73,
  56. Trenameat = 74,
  57. Rrenameat = 75,
  58. Tunlinkat = 76,
  59. Runlinkat = 77,
  60. // 9P2000
  61. Tversion = 100,
  62. Rversion = 101,
  63. Tauth = 102,
  64. Rauth = 103,
  65. Tattach = 104,
  66. Rattach = 105,
  67. Terror = 106,
  68. Rerror = 107,
  69. Tflush = 108,
  70. Rflush = 109,
  71. Twalk = 110,
  72. Rwalk = 111,
  73. Topen = 112,
  74. Ropen = 113,
  75. Tcreate = 114,
  76. Rcreate = 115,
  77. Tread = 116,
  78. Rread = 117,
  79. Twrite = 118,
  80. Rwrite = 119,
  81. Tclunk = 120,
  82. Rclunk = 121,
  83. Tremove = 122,
  84. Rremove = 123,
  85. Tstat = 124,
  86. Rstat = 125,
  87. Twstat = 126,
  88. Rwstat = 127
  89. };
  90. class Decoder {
  91. public:
  92. explicit Decoder(StringView data)
  93. : m_data(data)
  94. {
  95. }
  96. Decoder& operator>>(u8&);
  97. Decoder& operator>>(u16&);
  98. Decoder& operator>>(u32&);
  99. Decoder& operator>>(u64&);
  100. Decoder& operator>>(StringView&);
  101. Decoder& operator>>(Plan9FSQIdentifier&);
  102. StringView read_data();
  103. bool has_more_data() const { return !m_data.is_empty(); }
  104. private:
  105. StringView m_data;
  106. template<typename N>
  107. Decoder& read_number(N& number)
  108. {
  109. VERIFY(sizeof(number) <= m_data.length());
  110. memcpy(&number, m_data.characters_without_null_termination(), sizeof(number));
  111. m_data = m_data.substring_view(sizeof(number), m_data.length() - sizeof(number));
  112. return *this;
  113. }
  114. };
  115. Plan9FSMessage& operator<<(u8);
  116. Plan9FSMessage& operator<<(u16);
  117. Plan9FSMessage& operator<<(u32);
  118. Plan9FSMessage& operator<<(u64);
  119. Plan9FSMessage& operator<<(StringView);
  120. ErrorOr<void> append_data(StringView);
  121. template<typename T>
  122. Plan9FSMessage& operator>>(T& t)
  123. {
  124. VERIFY(m_have_been_built);
  125. m_built.decoder >> t;
  126. return *this;
  127. }
  128. StringView read_data()
  129. {
  130. VERIFY(m_have_been_built);
  131. return m_built.decoder.read_data();
  132. }
  133. Type type() const { return m_type; }
  134. u16 tag() const { return m_tag; }
  135. Plan9FSMessage(Plan9FS&, Type);
  136. Plan9FSMessage(NonnullOwnPtr<KBuffer>&&);
  137. ~Plan9FSMessage();
  138. Plan9FSMessage& operator=(Plan9FSMessage&&);
  139. KBuffer const& build();
  140. static constexpr size_t max_header_size = 24;
  141. private:
  142. template<typename N>
  143. Plan9FSMessage& append_number(N number)
  144. {
  145. VERIFY(!m_have_been_built);
  146. // FIXME: Handle append failure.
  147. (void)m_builder.append(reinterpret_cast<char const*>(&number), sizeof(number));
  148. return *this;
  149. }
  150. union {
  151. KBufferBuilder m_builder;
  152. struct {
  153. NonnullOwnPtr<KBuffer> buffer;
  154. Decoder decoder;
  155. } m_built;
  156. };
  157. u16 m_tag { 0 };
  158. Type m_type { 0 };
  159. bool m_have_been_built { false };
  160. };
  161. }