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.

189 lines
4.8 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.hpp"
  16. #include "fs_exists.hpp"
  17. #include "fs_info.hpp"
  18. #include "fs_path.hpp"
  19. #include "fs_statvfs_cache.hpp"
  20. #include "policy.hpp"
  21. #include "policy_error.hpp"
  22. #include "rwlock.hpp"
  23. #include <string>
  24. #include <vector>
  25. #include <limits>
  26. #include <sys/stat.h>
  27. using std::string;
  28. using std::vector;
  29. namespace newest
  30. {
  31. static
  32. int
  33. create(const Branches &branches_,
  34. const char *fusepath,
  35. const uint64_t minfreespace,
  36. vector<string> *paths)
  37. {
  38. rwlock::ReadGuard guard(&branches_.lock);
  39. int rv;
  40. int error;
  41. time_t newest;
  42. struct stat st;
  43. fs::info_t info;
  44. const Branch *branch;
  45. const string *newestbasepath;
  46. error = ENOENT;
  47. newest = std::numeric_limits<time_t>::min();
  48. newestbasepath = NULL;
  49. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  50. {
  51. branch = &branches_[i];
  52. if(!fs::exists(branch->path,fusepath,&st))
  53. error_and_continue(error,ENOENT);
  54. if(branch->ro_or_nc())
  55. error_and_continue(error,EROFS);
  56. if(st.st_mtime < newest)
  57. continue;
  58. rv = fs::info(&branch->path,&info);
  59. if(rv == -1)
  60. error_and_continue(error,ENOENT);
  61. if(info.readonly)
  62. error_and_continue(error,EROFS);
  63. if(info.spaceavail < minfreespace)
  64. error_and_continue(error,ENOSPC);
  65. newest = st.st_mtime;
  66. newestbasepath = &branch->path;
  67. }
  68. if(newestbasepath == NULL)
  69. return (errno=error,-1);
  70. paths->push_back(*newestbasepath);
  71. return 0;
  72. }
  73. static
  74. int
  75. action(const Branches &branches_,
  76. const char *fusepath,
  77. vector<string> *paths)
  78. {
  79. rwlock::ReadGuard guard(&branches_.lock);
  80. int rv;
  81. int error;
  82. bool readonly;
  83. time_t newest;
  84. struct stat st;
  85. const Branch *branch;
  86. const string *newestbasepath;
  87. error = ENOENT;
  88. newest = std::numeric_limits<time_t>::min();
  89. newestbasepath = NULL;
  90. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  91. {
  92. branch = &branches_[i];
  93. if(!fs::exists(branch->path,fusepath,&st))
  94. error_and_continue(error,ENOENT);
  95. if(branch->ro())
  96. error_and_continue(error,EROFS);
  97. if(st.st_mtime < newest)
  98. continue;
  99. rv = fs::statvfs_cache_readonly(branch->path,&readonly);
  100. if(rv == -1)
  101. error_and_continue(error,ENOENT);
  102. if(readonly)
  103. error_and_continue(error,EROFS);
  104. newest = st.st_mtime;
  105. newestbasepath = &branch->path;
  106. }
  107. if(newestbasepath == NULL)
  108. return (errno=error,-1);
  109. paths->push_back(*newestbasepath);
  110. return 0;
  111. }
  112. static
  113. int
  114. search(const Branches &branches_,
  115. const char *fusepath,
  116. vector<string> *paths)
  117. {
  118. rwlock::ReadGuard guard(&branches_.lock);
  119. time_t newest;
  120. struct stat st;
  121. const Branch *branch;
  122. const string *newestbasepath;
  123. newest = std::numeric_limits<time_t>::min();
  124. newestbasepath = NULL;
  125. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  126. {
  127. branch = &branches_[i];
  128. if(!fs::exists(branch->path,fusepath,&st))
  129. continue;
  130. if(st.st_mtime < newest)
  131. continue;
  132. newest = st.st_mtime;
  133. newestbasepath = &branch->path;
  134. }
  135. if(newestbasepath == NULL)
  136. return (errno=ENOENT,-1);
  137. paths->push_back(*newestbasepath);
  138. return 0;
  139. }
  140. }
  141. int
  142. Policy::Func::newest(const Category::Enum::Type type,
  143. const Branches &branches_,
  144. const char *fusepath,
  145. const uint64_t minfreespace,
  146. vector<string> *paths)
  147. {
  148. switch(type)
  149. {
  150. case Category::Enum::create:
  151. return newest::create(branches_,fusepath,minfreespace,paths);
  152. case Category::Enum::action:
  153. return newest::action(branches_,fusepath,paths);
  154. case Category::Enum::search:
  155. default:
  156. return newest::search(branches_,fusepath,paths);
  157. }
  158. }