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.

176 lines
4.5 KiB

6 years ago
6 years ago
  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "errno.hpp"
  15. #include "fs_exists.hpp"
  16. #include "fs_info.hpp"
  17. #include "fs_path.hpp"
  18. #include "fs_statvfs_cache.hpp"
  19. #include "policy.hpp"
  20. #include "policy_error.hpp"
  21. #include "policy_newest.hpp"
  22. #include "rwlock.hpp"
  23. #include <string>
  24. #include <limits>
  25. #include <sys/stat.h>
  26. using std::string;
  27. namespace newest
  28. {
  29. static
  30. int
  31. create(const Branches::CPtr &branches_,
  32. const char *fusepath_,
  33. StrVec *paths_)
  34. {
  35. int rv;
  36. int error;
  37. time_t newest;
  38. struct stat st;
  39. fs::info_t info;
  40. const string *basepath;
  41. error = ENOENT;
  42. newest = std::numeric_limits<time_t>::min();
  43. basepath = NULL;
  44. for(auto &branch : *branches_)
  45. {
  46. if(branch.ro_or_nc())
  47. error_and_continue(error,EROFS);
  48. if(!fs::exists(branch.path,fusepath_,&st))
  49. error_and_continue(error,ENOENT);
  50. if(st.st_mtime < newest)
  51. continue;
  52. rv = fs::info(branch.path,&info);
  53. if(rv == -1)
  54. error_and_continue(error,ENOENT);
  55. if(info.readonly)
  56. error_and_continue(error,EROFS);
  57. if(info.spaceavail < branch.minfreespace())
  58. error_and_continue(error,ENOSPC);
  59. newest = st.st_mtime;
  60. basepath = &branch.path;
  61. }
  62. if(basepath == NULL)
  63. return (errno=error,-1);
  64. paths_->push_back(*basepath);
  65. return 0;
  66. }
  67. static
  68. int
  69. action(const Branches::CPtr &branches_,
  70. const char *fusepath_,
  71. StrVec *paths_)
  72. {
  73. int rv;
  74. int error;
  75. bool readonly;
  76. time_t newest;
  77. struct stat st;
  78. const string *basepath;
  79. error = ENOENT;
  80. newest = std::numeric_limits<time_t>::min();
  81. basepath = NULL;
  82. for(auto &branch : *branches_)
  83. {
  84. if(branch.ro())
  85. error_and_continue(error,EROFS);
  86. if(!fs::exists(branch.path,fusepath_,&st))
  87. error_and_continue(error,ENOENT);
  88. if(st.st_mtime < newest)
  89. continue;
  90. rv = fs::statvfs_cache_readonly(branch.path,&readonly);
  91. if(rv == -1)
  92. error_and_continue(error,ENOENT);
  93. if(readonly)
  94. error_and_continue(error,EROFS);
  95. newest = st.st_mtime;
  96. basepath = &branch.path;
  97. }
  98. if(basepath == NULL)
  99. return (errno=error,-1);
  100. paths_->push_back(*basepath);
  101. return 0;
  102. }
  103. static
  104. int
  105. search(const Branches::CPtr &branches_,
  106. const char *fusepath_,
  107. StrVec *paths_)
  108. {
  109. time_t newest;
  110. struct stat st;
  111. const string *basepath;
  112. newest = std::numeric_limits<time_t>::min();
  113. basepath = NULL;
  114. for(auto &branch : *branches_)
  115. {
  116. if(!fs::exists(branch.path,fusepath_,&st))
  117. continue;
  118. if(st.st_mtime < newest)
  119. continue;
  120. newest = st.st_mtime;
  121. basepath = &branch.path;
  122. }
  123. if(basepath == NULL)
  124. return (errno=ENOENT,-1);
  125. paths_->push_back(*basepath);
  126. return 0;
  127. }
  128. }
  129. int
  130. Policy::Newest::Action::operator()(const Branches::CPtr &branches_,
  131. const char *fusepath_,
  132. StrVec *paths_) const
  133. {
  134. return ::newest::action(branches_,fusepath_,paths_);
  135. }
  136. int
  137. Policy::Newest::Create::operator()(const Branches::CPtr &branches_,
  138. const char *fusepath_,
  139. StrVec *paths_) const
  140. {
  141. return ::newest::create(branches_,fusepath_,paths_);
  142. }
  143. int
  144. Policy::Newest::Search::operator()(const Branches::CPtr &branches_,
  145. const char *fusepath_,
  146. StrVec *paths_) const
  147. {
  148. return ::newest::search(branches_,fusepath_,paths_);
  149. }