You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.8 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2023, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "fs_wait_for_mount.hpp"
  16. #include "syslog.hpp"
  17. #include <functional>
  18. #include <thread>
  19. #include <unordered_set>
  20. namespace fs
  21. {
  22. typedef std::unordered_set<fs::Path> PathSet;
  23. }
  24. constexpr std::chrono::milliseconds SLEEP_DURATION = std::chrono::milliseconds(333);
  25. namespace std
  26. {
  27. template<>
  28. struct hash<fs::Path>
  29. {
  30. std::size_t
  31. operator()(fs::Path const &path_) const noexcept
  32. {
  33. return std::hash<std::string>{}(path_.string());
  34. }
  35. };
  36. }
  37. static
  38. void
  39. _check_mounted(const struct stat &src_st_,
  40. const fs::PathSet &tgt_paths_,
  41. fs::PathVector *successes_,
  42. fs::PathVector *failures_)
  43. {
  44. fs::PathVector &successes = *successes_;
  45. fs::PathVector &failures = *failures_;
  46. for(auto const &tgt_path : tgt_paths_)
  47. {
  48. int rv;
  49. struct stat tgt_st;
  50. rv = fs::stat(tgt_path,&tgt_st);
  51. if(rv == 0)
  52. {
  53. if(tgt_st.st_dev != src_st_.st_dev)
  54. successes.push_back(tgt_path);
  55. else
  56. failures.push_back(tgt_path);
  57. }
  58. else
  59. {
  60. failures.push_back(tgt_path);
  61. }
  62. }
  63. }
  64. static
  65. void
  66. _wait_for_mount(const struct stat &src_st_,
  67. const fs::PathVector &tgt_paths_,
  68. const std::chrono::milliseconds &timeout_)
  69. {
  70. fs::PathVector successes;
  71. fs::PathVector failures;
  72. std::unordered_set<fs::Path> tgt_paths;
  73. std::chrono::time_point<std::chrono::steady_clock> now;
  74. std::chrono::time_point<std::chrono::steady_clock> deadline;
  75. tgt_paths.insert(tgt_paths_.begin(),tgt_paths_.end());
  76. now = std::chrono::steady_clock::now();
  77. deadline = now + timeout_;
  78. while(true)
  79. {
  80. if(tgt_paths.empty())
  81. break;
  82. if(now >= deadline)
  83. break;
  84. successes.clear();
  85. failures.clear();
  86. ::_check_mounted(src_st_,tgt_paths,&successes,&failures);
  87. for(auto const &path : successes)
  88. {
  89. tgt_paths.erase(path);
  90. syslog_info("%s is mounted",path.string().c_str());
  91. }
  92. std::this_thread::sleep_for(SLEEP_DURATION);
  93. now = std::chrono::steady_clock::now();
  94. }
  95. for(auto const &path : failures)
  96. syslog_notice("%s not mounted within timeout",path.string().c_str());
  97. if(!failures.empty())
  98. syslog_warning("Continuing to mount mergerfs despite %u branches not "
  99. "being different from the mountpoint filesystem",
  100. failures.size());
  101. }
  102. void
  103. fs::wait_for_mount(const fs::Path &src_path_,
  104. const fs::PathVector &tgt_paths_,
  105. const std::chrono::milliseconds &timeout_)
  106. {
  107. int rv;
  108. struct stat src_st;
  109. rv = fs::stat(src_path_,&src_st);
  110. if(rv == -1)
  111. return syslog_error("Error stat'ing mount path: %s (%s)",
  112. src_path_.c_str(),
  113. strerror(errno));
  114. ::_wait_for_mount(src_st,tgt_paths_,timeout_);
  115. }