diff-builder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2024 Ivan Baidakou
  3. #include "diff-builder.h"
  4. #include "model/messages.h"
  5. #include "model/diff/local/update.h"
  6. #include "model/diff/local/scan_finish.h"
  7. #include "model/diff/local/scan_request.h"
  8. #include "model/diff/local/scan_start.h"
  9. #include "model/diff/local/synchronization_finish.h"
  10. #include "model/diff/local/synchronization_start.h"
  11. #include "model/diff/modify/add_ignored_device.h"
  12. #include "model/diff/modify/add_pending_device.h"
  13. #include "model/diff/modify/append_block.h"
  14. #include "model/diff/modify/block_ack.h"
  15. #include "model/diff/modify/clone_block.h"
  16. #include "model/diff/modify/clone_file.h"
  17. #include "model/diff/modify/finish_file.h"
  18. #include "model/diff/modify/finish_file_ack.h"
  19. #include "model/diff/modify/share_folder.h"
  20. #include "model/diff/modify/unshare_folder.h"
  21. #include "model/diff/modify/update_peer.h"
  22. #include "model/diff/modify/remove_folder.h"
  23. #include "model/diff/modify/remove_peer.h"
  24. #include "model/diff/modify/remove_ignored_device.h"
  25. #include "model/diff/modify/remove_pending_device.h"
  26. #include "model/diff/modify/upsert_folder.h"
  27. #include "model/diff/contact/update_contact.h"
  28. #include "model/diff/contact/peer_state.h"
  29. #include "model/diff/peer/cluster_update.h"
  30. #include "model/diff/peer/update_folder.h"
  31. using namespace syncspirit::test;
  32. using namespace syncspirit::model;
  33. cluster_configurer_t::cluster_configurer_t(diff_builder_t &builder_, std::string_view peer_sha256_) noexcept
  34. : builder{builder_}, peer_sha256{peer_sha256_}, folder{nullptr} {}
  35. cluster_configurer_t &&cluster_configurer_t::add(std::string_view sha256, std::string_view folder_id, uint64_t index,
  36. int64_t max_sequence) noexcept {
  37. folder = cc.add_folders();
  38. folder->set_id(std::string(folder_id));
  39. auto device = folder->add_devices();
  40. device->set_id(std::string(sha256));
  41. device->set_index_id(index);
  42. device->set_max_sequence(max_sequence);
  43. return std::move(*this);
  44. }
  45. diff_builder_t &cluster_configurer_t::finish() noexcept {
  46. auto &cluster = builder.cluster;
  47. auto peer = builder.cluster.get_devices().by_sha256(peer_sha256);
  48. auto diff = diff::peer::cluster_update_t::create(cluster, *builder.sequencer, *peer, cc);
  49. assert(diff.has_value());
  50. builder.assign(diff.value().get());
  51. return builder;
  52. }
  53. index_maker_t::index_maker_t(diff_builder_t &builder_, std::string_view sha256, std::string_view folder_id) noexcept
  54. : builder{builder_}, peer_sha256{sha256} {
  55. index.set_folder(std::string(folder_id));
  56. }
  57. index_maker_t &&index_maker_t::add(const proto::FileInfo &file, const model::device_ptr_t &peer) noexcept {
  58. auto f = index.add_files();
  59. *f = file;
  60. if (f->version().counters_size() == 0) {
  61. auto v = f->mutable_version();
  62. auto c = v->add_counters();
  63. c->set_id(peer->as_uint());
  64. c->set_value(1);
  65. }
  66. return std::move(*this);
  67. }
  68. std::error_code index_maker_t::fail() noexcept {
  69. auto &cluster = builder.cluster;
  70. auto peer = builder.cluster.get_devices().by_sha256(peer_sha256);
  71. auto opt = diff::peer::update_folder_t::create(cluster, *builder.sequencer, *peer, index);
  72. return opt.error();
  73. }
  74. diff_builder_t &index_maker_t::finish() noexcept {
  75. auto &cluster = builder.cluster;
  76. auto peer = builder.cluster.get_devices().by_sha256(peer_sha256);
  77. auto diff = diff::peer::update_folder_t::create(cluster, *builder.sequencer, *peer, index);
  78. assert(diff.has_value());
  79. builder.assign(diff.value().get());
  80. return builder;
  81. }
  82. diff_builder_t::diff_builder_t(model::cluster_t &cluster_) noexcept : cluster{cluster_} {
  83. sequencer = model::make_sequencer(0);
  84. }
  85. diff_builder_t &diff_builder_t::apply(rotor::supervisor_t &sup) noexcept {
  86. auto has_diffs = [&]() -> bool { return (bool)cluster_diff; };
  87. assert(has_diffs());
  88. auto &addr = sup.get_address();
  89. bool do_try = true;
  90. while (do_try) {
  91. do_try = false;
  92. if (cluster_diff) {
  93. sup.send<model::payload::model_update_t>(addr, std::move(cluster_diff), nullptr);
  94. do_try = true;
  95. }
  96. sup.do_process();
  97. }
  98. return *this;
  99. }
  100. auto diff_builder_t::apply() noexcept -> outcome::result<void> {
  101. auto r = outcome::result<void>(outcome::success());
  102. bool do_try = true;
  103. while (do_try) {
  104. do_try = false;
  105. if (r && cluster_diff) {
  106. r = cluster_diff->apply(cluster);
  107. cluster_diff.reset();
  108. do_try = true;
  109. }
  110. }
  111. return r;
  112. }
  113. diff_builder_t &diff_builder_t::upsert_folder(std::string_view id, std::string_view path,
  114. std::string_view label) noexcept {
  115. db::Folder db_folder;
  116. db_folder.set_id(std::string(id));
  117. db_folder.set_label(std::string(label));
  118. db_folder.set_path(std::string(path));
  119. auto opt = diff::modify::upsert_folder_t::create(cluster, *sequencer, db_folder);
  120. return assign(opt.value().get());
  121. }
  122. diff_builder_t &diff_builder_t::upsert_folder(const db::Folder &data) noexcept {
  123. auto opt = diff::modify::upsert_folder_t::create(cluster, *sequencer, data);
  124. return assign(opt.value().get());
  125. }
  126. diff_builder_t &diff_builder_t::update_peer(const model::device_id_t &device, std::string_view name,
  127. std::string_view cert_name, bool auto_accept) noexcept {
  128. db::Device db_device;
  129. db_device.set_name(std::string(name));
  130. db_device.set_cert_name(std::string(cert_name));
  131. db_device.set_auto_accept(auto_accept);
  132. return assign(new diff::modify::update_peer_t(db_device, device, cluster));
  133. }
  134. cluster_configurer_t diff_builder_t::configure_cluster(std::string_view sha256) noexcept {
  135. return cluster_configurer_t(*this, sha256);
  136. }
  137. index_maker_t diff_builder_t::make_index(std::string_view sha256, std::string_view folder_id) noexcept {
  138. return index_maker_t(*this, sha256, folder_id);
  139. }
  140. diff_builder_t &diff_builder_t::share_folder(std::string_view sha256, std::string_view folder_id) noexcept {
  141. auto device = cluster.get_devices().by_sha256(sha256);
  142. auto folder = cluster.get_folders().by_id(folder_id);
  143. auto opt = diff::modify::share_folder_t::create(cluster, *sequencer, *device, *folder);
  144. if (!opt) {
  145. spdlog::error("cannot share: {}", opt.assume_error().message());
  146. return *this;
  147. }
  148. return assign(opt.assume_value().get());
  149. }
  150. diff_builder_t &diff_builder_t::unshare_folder(model::folder_info_t &fi) noexcept {
  151. return assign(new diff::modify::unshare_folder_t(cluster, fi));
  152. }
  153. diff_builder_t &diff_builder_t::clone_file(const model::file_info_t &source) noexcept {
  154. return assign(new diff::modify::clone_file_t(source, *sequencer));
  155. }
  156. diff_builder_t &diff_builder_t::finish_file(const model::file_info_t &source) noexcept {
  157. return assign(new diff::modify::finish_file_t(source));
  158. }
  159. diff_builder_t &diff_builder_t::finish_file_ack(const model::file_info_t &source) noexcept {
  160. return assign(new diff::modify::finish_file_ack_t(source));
  161. }
  162. diff_builder_t &diff_builder_t::local_update(std::string_view folder_id, const proto::FileInfo &file_) noexcept {
  163. return assign(new diff::local::update_t(cluster, *sequencer, folder_id, file_));
  164. }
  165. diff_builder_t &diff_builder_t::remove_peer(const model::device_t &peer) noexcept {
  166. return assign(new diff::modify::remove_peer_t(cluster, peer));
  167. return *this;
  168. }
  169. diff_builder_t &diff_builder_t::remove_folder(const model::folder_t &folder) noexcept {
  170. return assign(new diff::modify::remove_folder_t(cluster, folder));
  171. return *this;
  172. }
  173. diff_builder_t &diff_builder_t::update_state(const model::device_t &peer, const r::address_ptr_t &peer_addr,
  174. model::device_state_t state) noexcept {
  175. return assign(new model::diff::contact::peer_state_t(cluster, peer.device_id().get_sha256(), peer_addr, state));
  176. }
  177. diff_builder_t &diff_builder_t::update_contact(const model::device_id_t &device,
  178. const utils::uri_container_t &uris) noexcept {
  179. return assign(new model::diff::contact::update_contact_t(cluster, device, uris));
  180. }
  181. diff_builder_t &diff_builder_t::append_block(const model::file_info_t &target, size_t block_index, std::string data,
  182. dispose_callback_t callback) noexcept {
  183. return assign(new diff::modify::append_block_t(target, block_index, std::move(data), std::move(callback)));
  184. }
  185. diff_builder_t &diff_builder_t::clone_block(const model::file_block_t &file_block,
  186. dispose_callback_t callback) noexcept {
  187. return assign(new diff::modify::clone_block_t(file_block, std::move(callback)));
  188. }
  189. diff_builder_t &diff_builder_t::ack_block(const model::diff::modify::block_transaction_t &diff) noexcept {
  190. return assign(new diff::modify::block_ack_t(diff));
  191. return *this;
  192. }
  193. diff_builder_t &diff_builder_t::add_ignored_device(const model::device_id_t &device,
  194. db::SomeDevice db_device) noexcept {
  195. return assign(new diff::modify::add_ignored_device_t(cluster, device, db_device));
  196. }
  197. diff_builder_t &diff_builder_t::add_unknown_device(const model::device_id_t &device,
  198. db::SomeDevice db_device) noexcept {
  199. return assign(new diff::modify::add_pending_device_t(device, db_device));
  200. }
  201. diff_builder_t &diff_builder_t::remove_ignored_device(const model::ignored_device_t &device) noexcept {
  202. return assign(new diff::modify::remove_ignored_device_t(device));
  203. }
  204. diff_builder_t &diff_builder_t::remove_unknown_device(const model::pending_device_t &device) noexcept {
  205. return assign(new diff::modify::remove_pending_device_t(device));
  206. }
  207. diff_builder_t &diff_builder_t::scan_start(std::string_view id, const r::pt::ptime &at) noexcept {
  208. auto final_at = at.is_not_a_date_time() ? r::pt::microsec_clock::local_time() : at;
  209. return assign(new model::diff::local::scan_start_t(std::string(id), final_at));
  210. }
  211. diff_builder_t &diff_builder_t::scan_finish(std::string_view id, const r::pt::ptime &at) noexcept {
  212. auto final_at = at.is_not_a_date_time() ? r::pt::microsec_clock::local_time() : at;
  213. return assign(new model::diff::local::scan_finish_t(std::string(id), final_at));
  214. }
  215. diff_builder_t &diff_builder_t::scan_request(std::string_view id) noexcept {
  216. return assign(new model::diff::local::scan_request_t(std::string(id)));
  217. }
  218. diff_builder_t &diff_builder_t::synchronization_start(std::string_view id) noexcept {
  219. return assign(new model::diff::local::synchronization_start_t(std::string(id)));
  220. }
  221. diff_builder_t &diff_builder_t::synchronization_finish(std::string_view id) noexcept {
  222. return assign(new model::diff::local::synchronization_finish_t(std::string(id)));
  223. }
  224. template <typename Holder, typename Diff> static void generic_assign(Holder *holder, Diff *diff) noexcept {
  225. if (!(*holder)) {
  226. holder->reset(diff);
  227. } else {
  228. auto h = *holder;
  229. while (h && h->sibling) {
  230. h = h->sibling;
  231. }
  232. h->assign_sibling(diff);
  233. }
  234. }
  235. diff_builder_t &diff_builder_t::assign(model::diff::cluster_diff_t *diff) noexcept {
  236. generic_assign(&cluster_diff, diff);
  237. return *this;
  238. }
  239. auto diff_builder_t::get_sequencer() noexcept -> model::sequencer_t & { return *sequencer; }