1
0

cmFileAPI.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmFileAPI_h
  4. #define cmFileAPI_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include "cm_jsoncpp_reader.h"
  7. #include "cm_jsoncpp_value.h"
  8. #include "cm_jsoncpp_writer.h"
  9. #include <map>
  10. #include <memory> // IWYU pragma: keep
  11. #include <string>
  12. #include <unordered_set>
  13. #include <vector>
  14. class cmake;
  15. class cmFileAPI
  16. {
  17. public:
  18. cmFileAPI(cmake* cm);
  19. /** Read fileapi queries from disk. */
  20. void ReadQueries();
  21. /** Write fileapi replies to disk. */
  22. void WriteReplies();
  23. /** Get the "cmake" instance with which this was constructed. */
  24. cmake* GetCMakeInstance() const { return this->CMakeInstance; }
  25. /** Convert a JSON object or array into an object with a single
  26. "jsonFile" member specifying a file named with the given prefix
  27. and holding the original object. Other JSON types are unchanged. */
  28. Json::Value MaybeJsonFile(Json::Value in, std::string const& prefix);
  29. private:
  30. cmake* CMakeInstance;
  31. /** The api/v1 directory location. */
  32. std::string APIv1;
  33. /** The set of files we have just written to the reply directory. */
  34. std::unordered_set<std::string> ReplyFiles;
  35. static std::vector<std::string> LoadDir(std::string const& dir);
  36. void RemoveOldReplyFiles();
  37. // Keep in sync with ObjectKindName.
  38. enum class ObjectKind
  39. {
  40. InternalTest
  41. };
  42. /** Identify one object kind and major version. */
  43. struct Object
  44. {
  45. ObjectKind Kind;
  46. unsigned long Version = 0;
  47. friend bool operator<(Object const& l, Object const& r)
  48. {
  49. if (l.Kind != r.Kind) {
  50. return l.Kind < r.Kind;
  51. }
  52. return l.Version < r.Version;
  53. }
  54. };
  55. /** Represent content of a query directory. */
  56. struct Query
  57. {
  58. /** Known object kind-version pairs. */
  59. std::vector<Object> Known;
  60. /** Unknown object kind names. */
  61. std::vector<std::string> Unknown;
  62. };
  63. /** Represent one request in a client 'query.json'. */
  64. struct ClientRequest : public Object
  65. {
  66. /** Empty if request is valid, else the error string. */
  67. std::string Error;
  68. };
  69. /** Represent the "requests" in a client 'query.json'. */
  70. struct ClientRequests : public std::vector<ClientRequest>
  71. {
  72. /** Empty if requests field is valid, else the error string. */
  73. std::string Error;
  74. };
  75. /** Represent the content of a client query.json file. */
  76. struct ClientQueryJson
  77. {
  78. /** The error string if parsing failed, else empty. */
  79. std::string Error;
  80. /** The 'query.json' object "client" member if it exists, else null. */
  81. Json::Value ClientValue;
  82. /** The 'query.json' object "requests" member if it exists, else null. */
  83. Json::Value RequestsValue;
  84. /** Requests extracted from 'query.json'. */
  85. ClientRequests Requests;
  86. };
  87. /** Represent content of a client query directory. */
  88. struct ClientQuery
  89. {
  90. /** The content of the client query directory except 'query.json'. */
  91. Query DirQuery;
  92. /** True if 'query.json' exists. */
  93. bool HaveQueryJson = false;
  94. /** The 'query.json' content. */
  95. ClientQueryJson QueryJson;
  96. };
  97. /** Whether the top-level query directory exists at all. */
  98. bool QueryExists = false;
  99. /** The content of the top-level query directory. */
  100. Query TopQuery;
  101. /** The content of each "client-$client" query directory. */
  102. std::map<std::string, ClientQuery> ClientQueries;
  103. /** Reply index object generated for object kind/version.
  104. This populates the "objects" field of the reply index. */
  105. std::map<Object, Json::Value> ReplyIndexObjects;
  106. std::unique_ptr<Json::CharReader> JsonReader;
  107. std::unique_ptr<Json::StreamWriter> JsonWriter;
  108. bool ReadJsonFile(std::string const& file, Json::Value& value,
  109. std::string& error);
  110. std::string WriteJsonFile(
  111. Json::Value const& value, std::string const& prefix,
  112. std::string (*computeSuffix)(std::string const&) = ComputeSuffixHash);
  113. static std::string ComputeSuffixHash(std::string const&);
  114. static std::string ComputeSuffixTime(std::string const&);
  115. static bool ReadQuery(std::string const& query,
  116. std::vector<Object>& objects);
  117. void ReadClient(std::string const& client);
  118. void ReadClientQuery(std::string const& client, ClientQueryJson& q);
  119. Json::Value BuildReplyIndex();
  120. Json::Value BuildCMake();
  121. Json::Value BuildReply(Query const& q);
  122. static Json::Value BuildReplyError(std::string const& error);
  123. Json::Value const& AddReplyIndexObject(Object const& o);
  124. static const char* ObjectKindName(ObjectKind kind);
  125. static std::string ObjectName(Object const& o);
  126. Json::Value BuildObject(Object const& object);
  127. ClientRequests BuildClientRequests(Json::Value const& requests);
  128. ClientRequest BuildClientRequest(Json::Value const& request);
  129. Json::Value BuildClientReply(ClientQuery const& q);
  130. Json::Value BuildClientReplyResponses(ClientRequests const& requests);
  131. Json::Value BuildClientReplyResponse(ClientRequest const& request);
  132. struct RequestVersion
  133. {
  134. unsigned int Major = 0;
  135. unsigned int Minor = 0;
  136. };
  137. static bool ReadRequestVersions(Json::Value const& version,
  138. std::vector<RequestVersion>& versions,
  139. std::string& error);
  140. static bool ReadRequestVersion(Json::Value const& version, bool inArray,
  141. std::vector<RequestVersion>& result,
  142. std::string& error);
  143. static std::string NoSupportedVersion(
  144. std::vector<RequestVersion> const& versions);
  145. void BuildClientRequestInternalTest(
  146. ClientRequest& r, std::vector<RequestVersion> const& versions);
  147. Json::Value BuildInternalTest(Object const& object);
  148. };
  149. #endif