076-scan_actor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // SPDX-FileCopyrightText: 2019-2023 Ivan Baidakou
  3. #include "test-utils.h"
  4. #include "access.h"
  5. #include "test_supervisor.h"
  6. #include "diff-builder.h"
  7. #include "model/cluster.h"
  8. #include "model/diff/aggregate.h"
  9. #include "model/diff/modify/clone_file.h"
  10. #include "hasher/hasher_proxy_actor.h"
  11. #include "hasher/hasher_actor.h"
  12. #include "fs/scan_actor.h"
  13. #include "net/names.h"
  14. #include "utils/error_code.h"
  15. using namespace syncspirit;
  16. using namespace syncspirit::test;
  17. using namespace syncspirit::model;
  18. using namespace syncspirit::net;
  19. using namespace syncspirit::hasher;
  20. struct fixture_t {
  21. using target_ptr_t = r::intrusive_ptr_t<fs::scan_actor_t>;
  22. using error_msg_t = model::message::io_error_t;
  23. using error_msg_ptr_t = r::intrusive_ptr_t<error_msg_t>;
  24. using completion_msg_t = fs::message::scan_completed_t;
  25. using errors_container_t = std::vector<error_msg_ptr_t>;
  26. fixture_t() noexcept : root_path{bfs::unique_path()}, path_guard{root_path} {
  27. utils::set_default("trace");
  28. bfs::create_directory(root_path);
  29. scan_completions = 0;
  30. }
  31. void run() noexcept {
  32. auto my_id =
  33. device_id_t::from_string("KHQNO2S-5QSILRK-YX4JZZ4-7L77APM-QNVGZJT-EKU7IFI-PNEPBMY-4MXFMQD").value();
  34. my_device = device_t::create(my_id, "my-device").value();
  35. auto peer_id =
  36. device_id_t::from_string("VUV42CZ-IQD5A37-RPEBPM4-VVQK6E4-6WSKC7B-PVJQHHD-4PZD44V-ENC6WAZ").value();
  37. peer_device = device_t::create(peer_id, "peer-device").value();
  38. cluster = new cluster_t(my_device, 1, 1);
  39. cluster->get_devices().put(my_device);
  40. cluster->get_devices().put(peer_device);
  41. r::system_context_t ctx;
  42. sup = ctx.create_supervisor<supervisor_t>().timeout(timeout).create_registry().finish();
  43. sup->cluster = cluster;
  44. auto folder_id = "1234-5678";
  45. sup->configure_callback = [&](r::plugin::plugin_base_t &plugin) {
  46. plugin.template with_casted<r::plugin::starter_plugin_t>([&](auto &p) {
  47. p.subscribe_actor(r::lambda<error_msg_t>([&](error_msg_t &msg) { errors.push_back(&msg); }));
  48. p.subscribe_actor(r::lambda<completion_msg_t>([&](completion_msg_t &) { ++scan_completions; }));
  49. });
  50. };
  51. sup->start();
  52. sup->do_process();
  53. auto builder = diff_builder_t(*cluster);
  54. builder.create_folder(folder_id, root_path.string()).share_folder(peer_id.get_sha256(), folder_id).apply(*sup);
  55. folder = cluster->get_folders().by_id(folder_id);
  56. folder_info = folder->get_folder_infos().by_device(*my_device);
  57. files = &folder_info->get_file_infos();
  58. folder_info_peer = folder->get_folder_infos().by_device(*peer_device);
  59. files_peer = &folder_info_peer->get_file_infos();
  60. CHECK(static_cast<r::actor_base_t *>(sup.get())->access<to::state>() == r::state_t::OPERATIONAL);
  61. sup->create_actor<hasher_actor_t>().index(1).timeout(timeout).finish();
  62. auto proxy_addr = sup->create_actor<hasher::hasher_proxy_actor_t>()
  63. .timeout(timeout)
  64. .hasher_threads(1)
  65. .name(net::names::hasher_proxy)
  66. .finish()
  67. ->get_address();
  68. sup->do_process();
  69. auto fs_config = config::fs_config_t{3600, 10};
  70. target = sup->create_actor<fs::scan_actor_t>()
  71. .timeout(timeout)
  72. .cluster(cluster)
  73. .fs_config(fs_config)
  74. .requested_hashes_limit(2ul)
  75. .finish();
  76. sup->do_process();
  77. sup->send<fs::payload::scan_folder_t>(target->get_address(), folder_id);
  78. main();
  79. sup->do_process();
  80. sup->shutdown();
  81. sup->do_process();
  82. CHECK(static_cast<r::actor_base_t *>(sup.get())->access<to::state>() == r::state_t::SHUT_DOWN);
  83. }
  84. virtual void main() noexcept {}
  85. r::pt::time_duration timeout = r::pt::millisec{10};
  86. r::intrusive_ptr_t<supervisor_t> sup;
  87. cluster_ptr_t cluster;
  88. device_ptr_t my_device;
  89. bfs::path root_path;
  90. path_guard_t path_guard;
  91. target_ptr_t target;
  92. model::folder_ptr_t folder;
  93. model::folder_info_ptr_t folder_info;
  94. model::folder_info_ptr_t folder_info_peer;
  95. model::file_infos_map_t *files;
  96. model::file_infos_map_t *files_peer;
  97. errors_container_t errors;
  98. std::uint32_t scan_completions;
  99. model::device_ptr_t peer_device;
  100. };
  101. void test_meta_changes() {
  102. struct F : fixture_t {
  103. void main() noexcept override {
  104. sys::error_code ec;
  105. SECTION("trivial") {
  106. SECTION("no files") {
  107. sup->do_process();
  108. CHECK(folder_info->get_file_infos().size() == 0);
  109. }
  110. SECTION("just 1 dir") {
  111. CHECK(bfs::create_directories(root_path / "abc"));
  112. sup->do_process();
  113. CHECK(folder_info->get_file_infos().size() == 0);
  114. }
  115. #ifndef SYNCSPIRIT_WIN
  116. SECTION("just 1 subdir, which cannot be read") {
  117. auto subdir = root_path / "abc";
  118. CHECK(bfs::create_directories(subdir / "def", ec));
  119. auto guard = test::path_guard_t(subdir);
  120. bfs::permissions(subdir, bfs::perms::no_perms);
  121. bfs::permissions(subdir, bfs::perms::owner_read, ec);
  122. if (!ec) {
  123. sup->do_process();
  124. CHECK(folder_info->get_file_infos().size() == 0);
  125. bfs::permissions(subdir, bfs::perms::all_all);
  126. REQUIRE(errors.size() == 1);
  127. auto &errs = errors.at(0)->payload.errors;
  128. REQUIRE(errs.size() == 1);
  129. REQUIRE(errs.at(0).path == (subdir / "def"));
  130. REQUIRE(errs.at(0).ec);
  131. }
  132. }
  133. #endif
  134. REQUIRE(scan_completions == 1);
  135. }
  136. proto::FileInfo pr_fi;
  137. std::int64_t modified = 1641828421;
  138. pr_fi.set_name("q.txt");
  139. pr_fi.set_modified_s(modified);
  140. pr_fi.set_block_size(5ul);
  141. pr_fi.set_size(5ul);
  142. auto version = pr_fi.mutable_version();
  143. auto counter = version->add_counters();
  144. counter->set_id(1);
  145. counter->set_value(peer_device->as_uint());
  146. auto bi = proto::BlockInfo();
  147. bi.set_size(5);
  148. bi.set_weak_hash(12);
  149. bi.set_hash(utils::sha256_digest("12345").value());
  150. bi.set_offset(0);
  151. auto b = block_info_t::create(bi).value();
  152. SECTION("a file does not physically exist") {
  153. auto file_peer = file_info_t::create(cluster->next_uuid(), pr_fi, folder_info_peer).value();
  154. file_peer->assign_block(b, 0);
  155. folder_info_peer->add(file_peer, false);
  156. auto diff = diff::cluster_diff_ptr_t(new diff::modify::clone_file_t(*file_peer));
  157. REQUIRE(diff->apply(*cluster));
  158. auto file = files->by_name(pr_fi.name());
  159. sup->do_process();
  160. CHECK(files->size() == 1);
  161. CHECK(!file->is_locally_available());
  162. REQUIRE(scan_completions == 1);
  163. }
  164. SECTION("complete file exists") {
  165. auto file_peer = file_info_t::create(cluster->next_uuid(), pr_fi, folder_info_peer).value();
  166. file_peer->assign_block(b, 0);
  167. folder_info_peer->add(file_peer, false);
  168. auto diff = diff::cluster_diff_ptr_t(new diff::modify::clone_file_t(*file_peer));
  169. REQUIRE(diff->apply(*cluster));
  170. auto file = files->by_name(pr_fi.name());
  171. file->set_source(nullptr);
  172. auto path = file->get_path();
  173. SECTION("meta is not changed") {
  174. write_file(path, "12345");
  175. bfs::last_write_time(path, modified);
  176. sup->do_process();
  177. CHECK(files->size() == 1);
  178. CHECK(file->is_locally_available());
  179. }
  180. SECTION("meta is changed (modification)") {
  181. write_file(path, "12345");
  182. sup->do_process();
  183. CHECK(files->size() == 1);
  184. auto new_file = files->by_name(pr_fi.name());
  185. REQUIRE(new_file);
  186. CHECK(file != new_file);
  187. CHECK(new_file->is_locally_available());
  188. CHECK(new_file->get_size() == 5);
  189. REQUIRE(new_file->get_blocks().size() == 1);
  190. CHECK(new_file->get_blocks()[0]->get_size() == 5);
  191. }
  192. SECTION("meta is changed (size)") {
  193. write_file(path, "123456");
  194. bfs::last_write_time(path, modified);
  195. sup->do_process();
  196. CHECK(files->size() == 1);
  197. auto new_file = files->by_name(pr_fi.name());
  198. REQUIRE(new_file);
  199. CHECK(file != new_file);
  200. CHECK(new_file->is_locally_available());
  201. CHECK(new_file->get_size() == 5);
  202. REQUIRE(new_file->get_blocks().size() == 1);
  203. CHECK(new_file->get_blocks()[0]->get_size() == 5);
  204. }
  205. SECTION("meta is changed (content)") {
  206. write_file(path, "67890");
  207. sup->do_process();
  208. CHECK(files->size() == 1);
  209. auto new_file = files->by_name(pr_fi.name());
  210. REQUIRE(new_file);
  211. CHECK(file != new_file);
  212. CHECK(new_file->is_locally_available());
  213. CHECK(new_file->get_size() == 5);
  214. REQUIRE(new_file->get_blocks().size() == 1);
  215. CHECK(new_file->get_blocks()[0]->get_size() == 5);
  216. }
  217. REQUIRE(scan_completions == 1);
  218. }
  219. SECTION("incomplete file exists") {
  220. pr_fi.set_size(10ul);
  221. pr_fi.set_block_size(5ul);
  222. auto bi_2 = proto::BlockInfo();
  223. bi_2.set_size(5);
  224. bi_2.set_weak_hash(12);
  225. bi_2.set_hash(utils::sha256_digest("67890").value());
  226. bi_2.set_offset(5);
  227. auto b2 = block_info_t::create(bi_2).value();
  228. auto file_peer = file_info_t::create(cluster->next_uuid(), pr_fi, folder_info_peer).value();
  229. file_peer->assign_block(b, 0);
  230. file_peer->assign_block(b2, 1);
  231. folder_info_peer->add(file_peer, false);
  232. auto diff = diff::cluster_diff_ptr_t(new diff::modify::clone_file_t(*file_peer));
  233. REQUIRE(diff->apply(*cluster));
  234. auto file = files->by_name(pr_fi.name());
  235. auto path = file->get_path().string() + ".syncspirit-tmp";
  236. file->lock(); // should be locked on db, as there is a source
  237. auto content = "12345\0\0\0\0\0";
  238. write_file(path, std::string(content, 10));
  239. SECTION("outdated -> just remove") {
  240. bfs::last_write_time(path, modified - 24 * 3600);
  241. sup->do_process();
  242. CHECK(!file->is_locally_available());
  243. CHECK(!file->is_locked());
  244. CHECK(!bfs::exists(path));
  245. }
  246. SECTION("just 1st block is valid, tmp is kept") {
  247. sup->do_process();
  248. CHECK(!file->is_locally_available());
  249. CHECK(!file->is_locally_available(0));
  250. CHECK(!file->is_locally_available(1));
  251. CHECK(!file->is_locked());
  252. CHECK(!file_peer->is_locally_available());
  253. CHECK(file_peer->is_locally_available(0));
  254. CHECK(!file_peer->is_locally_available(1));
  255. CHECK(bfs::exists(path));
  256. }
  257. SECTION("source is missing -> tmp is removed") {
  258. file->set_source({});
  259. file->unlock();
  260. sup->do_process();
  261. CHECK(!file->is_locally_available());
  262. CHECK(!file->is_locked());
  263. CHECK(!file_peer->is_locally_available());
  264. CHECK(!file_peer->is_locally_available(0));
  265. CHECK(!file_peer->is_locally_available(1));
  266. CHECK(!bfs::exists(path));
  267. }
  268. SECTION("corrupted content") {
  269. SECTION("1st block") { write_file(path, "2234567890"); }
  270. SECTION("2nd block") { write_file(path, "1234567899"); }
  271. SECTION("missing source file") { file->set_source(nullptr); }
  272. sup->do_process();
  273. CHECK(!file->is_locally_available(0));
  274. CHECK(!file->is_locally_available(1));
  275. CHECK(!file->is_locked());
  276. CHECK(!file_peer->is_locally_available(0));
  277. CHECK(!file_peer->is_locally_available(1));
  278. CHECK(!bfs::exists(path));
  279. }
  280. #ifndef SYNCSPIRIT_WIN
  281. SECTION("error on reading -> remove") {
  282. bfs::permissions(path, bfs::perms::no_perms);
  283. if (!ec) {
  284. sup->do_process();
  285. CHECK(!file->is_locally_available());
  286. CHECK(!file->is_locked());
  287. CHECK(!bfs::exists(path));
  288. REQUIRE(errors.size() == 0);
  289. }
  290. }
  291. #endif
  292. REQUIRE(scan_completions == 1);
  293. }
  294. SECTION("local (previous) file exists") {
  295. pr_fi.set_size(15ul);
  296. pr_fi.set_block_size(5ul);
  297. auto bi_2 = proto::BlockInfo();
  298. bi_2.set_size(5);
  299. bi_2.set_weak_hash(12);
  300. bi_2.set_hash(utils::sha256_digest("67890").value());
  301. bi_2.set_offset(5);
  302. auto b2 = block_info_t::create(bi_2).value();
  303. auto bi_3 = proto::BlockInfo();
  304. bi_3.set_size(5);
  305. bi_3.set_weak_hash(12);
  306. bi_3.set_hash(utils::sha256_digest("abcde").value());
  307. bi_3.set_offset(10);
  308. auto b3 = block_info_t::create(bi_3).value();
  309. pr_fi.set_size(5ul);
  310. auto file_my = file_info_t::create(cluster->next_uuid(), pr_fi, folder_info).value();
  311. file_my->assign_block(b, 0);
  312. file_my->lock();
  313. folder_info->add(file_my, false);
  314. pr_fi.set_size(15ul);
  315. counter->set_id(2);
  316. auto file_peer = file_info_t::create(cluster->next_uuid(), pr_fi, folder_info_peer).value();
  317. file_peer->assign_block(b, 0);
  318. file_peer->assign_block(b2, 1);
  319. file_peer->assign_block(b3, 2);
  320. folder_info_peer->add(file_peer, false);
  321. auto diff = diff::cluster_diff_ptr_t(new diff::modify::clone_file_t(*file_peer));
  322. REQUIRE(diff->apply(*cluster));
  323. auto file = files->by_name(pr_fi.name());
  324. auto path_my = file->get_path().string();
  325. auto path_peer = file->get_path().string() + ".syncspirit-tmp";
  326. write_file(path_my, "12345");
  327. bfs::last_write_time(path_my, modified);
  328. auto content = "1234567890\0\0\0\0\0";
  329. write_file(path_peer, std::string(content, 15));
  330. sup->do_process();
  331. CHECK(file_my->is_locally_available());
  332. CHECK(file_my->get_source() == file_peer);
  333. CHECK(!file_peer->is_locally_available());
  334. CHECK(file_peer->is_locally_available(0));
  335. CHECK(file_peer->is_locally_available(1));
  336. CHECK(!file_peer->is_locally_available(2));
  337. REQUIRE(scan_completions == 1);
  338. }
  339. }
  340. };
  341. F().run();
  342. }
  343. void test_new_files() {
  344. struct F : fixture_t {
  345. void main() noexcept override {
  346. sys::error_code ec;
  347. auto &blocks = cluster->get_blocks();
  348. SECTION("new symlink") {
  349. auto file_path = root_path / "symlink";
  350. bfs::create_symlink(bfs::path("/some/where"), file_path, ec);
  351. REQUIRE(!ec);
  352. sup->do_process();
  353. auto file = files->by_name("symlink");
  354. REQUIRE(file);
  355. CHECK(file->is_locally_available());
  356. CHECK(!file->is_file());
  357. CHECK(file->is_link());
  358. CHECK(file->get_block_size() == 0);
  359. CHECK(file->get_size() == 0);
  360. CHECK(blocks.size() == 0);
  361. REQUIRE(scan_completions == 1);
  362. }
  363. SECTION("empty file") {
  364. CHECK(bfs::create_directories(root_path / "abc"));
  365. auto file_path = root_path / "abc" / "empty.file";
  366. write_file(file_path, "");
  367. sup->do_process();
  368. auto file = files->by_name("abc/empty.file");
  369. REQUIRE(file);
  370. CHECK(file->is_locally_available());
  371. CHECK(!file->is_link());
  372. CHECK(file->is_file());
  373. CHECK(file->get_block_size() == 0);
  374. CHECK(file->get_size() == 0);
  375. CHECK(blocks.size() == 0);
  376. REQUIRE(scan_completions == 1);
  377. }
  378. SECTION("non-empty file (1 block)") {
  379. auto file_path = root_path / "file.ext";
  380. write_file(file_path, "12345");
  381. sup->do_process();
  382. auto file = files->by_name("file.ext");
  383. REQUIRE(file);
  384. CHECK(file->is_locally_available());
  385. CHECK(!file->is_link());
  386. CHECK(file->is_file());
  387. CHECK(file->get_block_size() == 5);
  388. CHECK(file->get_size() == 5);
  389. CHECK(blocks.size() == 1);
  390. REQUIRE(scan_completions == 1);
  391. }
  392. SECTION("non-empty file (2 blocks)") {
  393. auto file_path = root_path / "file.ext";
  394. auto sz = size_t{128 * 1024 * 2};
  395. std::string data(sz, 'x');
  396. write_file(file_path, data);
  397. sup->do_process();
  398. auto file = files->by_name("file.ext");
  399. REQUIRE(file);
  400. CHECK(file->is_locally_available());
  401. CHECK(!file->is_link());
  402. CHECK(file->is_file());
  403. CHECK(file->get_size() == sz);
  404. CHECK(file->get_blocks().size() == 2);
  405. CHECK(blocks.size() == 1);
  406. REQUIRE(scan_completions == 1);
  407. }
  408. SECTION("non-empty file (3 blocks)") {
  409. auto file_path = root_path / "file.ext";
  410. auto sz = size_t{128 * 1024 * 3};
  411. std::string data(sz, 'x');
  412. write_file(file_path, data);
  413. sup->do_process();
  414. auto file = files->by_name("file.ext");
  415. REQUIRE(file);
  416. CHECK(file->is_locally_available());
  417. CHECK(!file->is_link());
  418. CHECK(file->is_file());
  419. CHECK(file->get_size() == sz);
  420. CHECK(file->get_blocks().size() == 3);
  421. CHECK(blocks.size() == 1);
  422. REQUIRE(scan_completions == 1);
  423. }
  424. SECTION("two files, different content") {
  425. auto file1_path = root_path / "file1.ext";
  426. write_file(file1_path, "12345");
  427. auto file2_path = root_path / "file2.ext";
  428. write_file(file2_path, "67890");
  429. sup->do_process();
  430. auto file1 = files->by_name("file1.ext");
  431. REQUIRE(file1);
  432. CHECK(file1->is_locally_available());
  433. CHECK(!file1->is_link());
  434. CHECK(file1->is_file());
  435. CHECK(file1->get_block_size() == 5);
  436. CHECK(file1->get_size() == 5);
  437. auto file2 = files->by_name("file2.ext");
  438. REQUIRE(file2);
  439. CHECK(file2->is_locally_available());
  440. CHECK(!file2->is_link());
  441. CHECK(file2->is_file());
  442. CHECK(file2->get_block_size() == 5);
  443. CHECK(file2->get_size() == 5);
  444. CHECK(blocks.size() == 2);
  445. REQUIRE(scan_completions == 1);
  446. }
  447. SECTION("two files, same content") {
  448. auto file1_path = root_path / "file1.ext";
  449. write_file(file1_path, "12345");
  450. auto file2_path = root_path / "file2.ext";
  451. write_file(file2_path, "12345");
  452. sup->do_process();
  453. auto file1 = files->by_name("file1.ext");
  454. REQUIRE(file1);
  455. CHECK(file1->is_locally_available());
  456. CHECK(!file1->is_link());
  457. CHECK(file1->is_file());
  458. CHECK(file1->get_block_size() == 5);
  459. CHECK(file1->get_size() == 5);
  460. auto file2 = files->by_name("file2.ext");
  461. REQUIRE(file2);
  462. CHECK(file2->is_locally_available());
  463. CHECK(!file2->is_link());
  464. CHECK(file2->is_file());
  465. CHECK(file2->get_block_size() == 5);
  466. CHECK(file2->get_size() == 5);
  467. CHECK(blocks.size() == 1);
  468. REQUIRE(scan_completions == 1);
  469. }
  470. }
  471. };
  472. F().run();
  473. }
  474. void test_remove_file() {
  475. struct F : fixture_t {
  476. void main() noexcept override {
  477. sys::error_code ec;
  478. auto &blocks = cluster->get_blocks();
  479. SECTION("non-empty file") {
  480. auto file_path = root_path / "file.ext";
  481. write_file(file_path, "12345");
  482. sup->do_process();
  483. REQUIRE(scan_completions == 1);
  484. auto file = files->by_name("file.ext");
  485. REQUIRE(file);
  486. REQUIRE(blocks.size() == 1);
  487. bfs::remove(file_path);
  488. auto &addr = target->get_address();
  489. sup->send<fs::payload::scan_folder_t>(addr, std::string(folder->get_id()));
  490. sup->do_process();
  491. file = files->by_name("file.ext");
  492. CHECK(file->is_deleted() == 1);
  493. CHECK(blocks.size() == 0);
  494. REQUIRE(scan_completions == 2);
  495. }
  496. }
  497. };
  498. F().run();
  499. };
  500. int _init() {
  501. REGISTER_TEST_CASE(test_meta_changes, "test_meta_changes", "[fs]");
  502. REGISTER_TEST_CASE(test_new_files, "test_new_files", "[fs]");
  503. REGISTER_TEST_CASE(test_remove_file, "test_remove_file", "[fs]");
  504. return 1;
  505. }
  506. static int v = _init();