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.

156 lines
3.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
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 <string>
  15. #include <vector>
  16. #include <fcntl.h>
  17. #include <limits.h>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include "errno.hpp"
  21. #include "fs_attr.hpp"
  22. #include "fs_base_realpath.hpp"
  23. #include "fs_base_stat.hpp"
  24. #include "fs_exists.hpp"
  25. #include "fs_path.hpp"
  26. #include "fs_statvfs_cache.hpp"
  27. #include "fs_xattr.hpp"
  28. #include "str.hpp"
  29. using std::string;
  30. using std::vector;
  31. namespace fs
  32. {
  33. void
  34. findallfiles(const vector<string> &basepaths,
  35. const char *fusepath,
  36. vector<string> &paths)
  37. {
  38. string fullpath;
  39. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  40. {
  41. fullpath = fs::path::make(basepaths[i],fusepath);
  42. if(!fs::exists(fullpath))
  43. continue;
  44. paths.push_back(fullpath);
  45. }
  46. }
  47. int
  48. findonfs(const vector<string> &basepaths,
  49. const string &fusepath,
  50. const int fd,
  51. string &basepath)
  52. {
  53. int rv;
  54. dev_t dev;
  55. string fullpath;
  56. struct stat st;
  57. rv = fs::fstat(fd,&st);
  58. if(rv == -1)
  59. return -1;
  60. dev = st.st_dev;
  61. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  62. {
  63. fullpath = fs::path::make(basepaths[i],fusepath);
  64. rv = fs::lstat(fullpath,&st);
  65. if(rv == -1)
  66. continue;
  67. if(st.st_dev != dev)
  68. continue;
  69. basepath = basepaths[i];
  70. return 0;
  71. }
  72. return (errno=ENOENT,-1);
  73. }
  74. void
  75. realpathize(vector<string> &strs)
  76. {
  77. char *rv;
  78. for(size_t i = 0; i < strs.size(); i++)
  79. {
  80. rv = fs::realpath(strs[i]);
  81. if(rv == NULL)
  82. continue;
  83. strs[i] = rv;
  84. ::free(rv);
  85. }
  86. }
  87. int
  88. getfl(const int fd)
  89. {
  90. return ::fcntl(fd,F_GETFL,0);
  91. }
  92. int
  93. setfl(const int fd,
  94. const mode_t mode)
  95. {
  96. return ::fcntl(fd,F_SETFL,mode);
  97. }
  98. int
  99. mfs(const vector<string> &basepaths,
  100. const uint64_t minfreespace,
  101. string &path)
  102. {
  103. int rv;
  104. uint64_t mfs;
  105. uint64_t spaceavail;
  106. const string *mfsbasepath;
  107. mfs = 0;
  108. mfsbasepath = NULL;
  109. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  110. {
  111. rv = fs::statvfs_cache_spaceavail(basepaths[i],&spaceavail);
  112. if(rv == -1)
  113. continue;
  114. if(spaceavail < minfreespace)
  115. continue;
  116. if(spaceavail <= mfs)
  117. continue;
  118. mfs = spaceavail;
  119. mfsbasepath = &basepaths[i];
  120. }
  121. if(mfsbasepath == NULL)
  122. return (errno=ENOENT,-1);
  123. path = *mfsbasepath;
  124. return 0;
  125. }
  126. };