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.

262 lines
5.2 KiB

  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 <string>
  15. #include <vector>
  16. #include <fcntl.h>
  17. #include <glob.h>
  18. #include <limits.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include "errno.hpp"
  22. #include "fs_attr.hpp"
  23. #include "fs_base_realpath.hpp"
  24. #include "fs_base_stat.hpp"
  25. #include "fs_base_statvfs.hpp"
  26. #include "fs_path.hpp"
  27. #include "fs_xattr.hpp"
  28. #include "statvfs_util.hpp"
  29. #include "str.hpp"
  30. #include "success_fail.hpp"
  31. using std::string;
  32. using std::vector;
  33. namespace fs
  34. {
  35. bool
  36. exists(const string &path,
  37. struct stat &st)
  38. {
  39. int rv;
  40. rv = fs::lstat(path,st);
  41. return LSTAT_SUCCEEDED(rv);
  42. }
  43. bool
  44. exists(const string &path)
  45. {
  46. struct stat st;
  47. return exists(path,st);
  48. }
  49. bool
  50. info(const string &path,
  51. bool &readonly,
  52. uint64_t &spaceavail,
  53. uint64_t &spaceused)
  54. {
  55. int rv;
  56. struct statvfs st;
  57. rv = fs::statvfs(path,st);
  58. if(STATVFS_SUCCEEDED(rv))
  59. {
  60. readonly = StatVFS::readonly(st);
  61. spaceavail = StatVFS::spaceavail(st);
  62. spaceused = StatVFS::spaceused(st);
  63. }
  64. return STATVFS_SUCCEEDED(rv);
  65. }
  66. bool
  67. readonly(const string &path)
  68. {
  69. int rv;
  70. struct statvfs st;
  71. rv = fs::statvfs(path,st);
  72. return (STATVFS_SUCCEEDED(rv) && StatVFS::readonly(st));
  73. }
  74. bool
  75. spaceavail(const string &path,
  76. uint64_t &spaceavail)
  77. {
  78. int rv;
  79. struct statvfs st;
  80. rv = fs::statvfs(path,st);
  81. if(STATVFS_SUCCEEDED(rv))
  82. spaceavail = StatVFS::spaceavail(st);
  83. return STATVFS_SUCCEEDED(rv);
  84. }
  85. bool
  86. spaceused(const string &path,
  87. uint64_t &spaceused)
  88. {
  89. int rv;
  90. struct statvfs st;
  91. rv = fs::statvfs(path,st);
  92. if(STATVFS_SUCCEEDED(rv))
  93. spaceused = StatVFS::spaceused(st);
  94. return STATVFS_SUCCEEDED(rv);
  95. }
  96. void
  97. findallfiles(const vector<string> &srcmounts,
  98. const char *fusepath,
  99. vector<string> &paths)
  100. {
  101. string fullpath;
  102. for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
  103. {
  104. fs::path::make(&srcmounts[i],fusepath,fullpath);
  105. if(!fs::exists(fullpath))
  106. continue;
  107. paths.push_back(fullpath);
  108. }
  109. }
  110. int
  111. findonfs(const vector<string> &srcmounts,
  112. const string &fusepath,
  113. const int fd,
  114. string &basepath)
  115. {
  116. int rv;
  117. dev_t dev;
  118. string fullpath;
  119. struct stat st;
  120. rv = fs::fstat(fd,st);
  121. if(FSTAT_FAILED(rv))
  122. return -1;
  123. dev = st.st_dev;
  124. for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
  125. {
  126. fs::path::make(&srcmounts[i],fusepath,fullpath);
  127. rv = fs::lstat(fullpath,st);
  128. if(FSTAT_FAILED(rv))
  129. continue;
  130. if(st.st_dev != dev)
  131. continue;
  132. basepath = srcmounts[i];
  133. return 0;
  134. }
  135. return (errno=ENOENT,-1);
  136. }
  137. void
  138. glob(const vector<string> &patterns,
  139. vector<string> &strs)
  140. {
  141. int flags;
  142. size_t veclen;
  143. glob_t gbuf = {0};
  144. veclen = patterns.size();
  145. if(veclen == 0)
  146. return;
  147. flags = 0;
  148. glob(patterns[0].c_str(),flags,NULL,&gbuf);
  149. flags = GLOB_APPEND;
  150. for(size_t i = 1; i < veclen; i++)
  151. glob(patterns[i].c_str(),flags,NULL,&gbuf);
  152. for(size_t i = 0; i < gbuf.gl_pathc; ++i)
  153. strs.push_back(gbuf.gl_pathv[i]);
  154. globfree(&gbuf);
  155. }
  156. void
  157. realpathize(vector<string> &strs)
  158. {
  159. char *rv;
  160. for(size_t i = 0; i < strs.size(); i++)
  161. {
  162. rv = fs::realpath(strs[i]);
  163. if(rv == NULL)
  164. continue;
  165. strs[i] = rv;
  166. ::free(rv);
  167. }
  168. }
  169. int
  170. getfl(const int fd)
  171. {
  172. return ::fcntl(fd,F_GETFL,0);
  173. }
  174. int
  175. setfl(const int fd,
  176. const mode_t mode)
  177. {
  178. return ::fcntl(fd,F_SETFL,mode);
  179. }
  180. int
  181. mfs(const vector<string> &basepaths,
  182. const uint64_t minfreespace,
  183. string &path)
  184. {
  185. uint64_t mfs;
  186. const string *mfsbasepath;
  187. mfs = 0;
  188. mfsbasepath = NULL;
  189. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  190. {
  191. uint64_t spaceavail;
  192. const string &basepath = basepaths[i];
  193. if(!fs::spaceavail(basepath,spaceavail))
  194. continue;
  195. if(spaceavail < minfreespace)
  196. continue;
  197. if(spaceavail <= mfs)
  198. continue;
  199. mfs = spaceavail;
  200. mfsbasepath = &basepaths[i];
  201. }
  202. if(mfsbasepath == NULL)
  203. return (errno=ENOENT,-1);
  204. path = *mfsbasepath;
  205. return 0;
  206. }
  207. };