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.

164 lines
4.7 KiB

  1. /*
  2. Copyright (c) 2018, 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 "config.hpp"
  15. #include "errno.hpp"
  16. #include "fs_lstat.hpp"
  17. #include "fs_path.hpp"
  18. #include "fs_statvfs.hpp"
  19. #include "statvfs_util.hpp"
  20. #include "ugid.hpp"
  21. #include "fuse.h"
  22. #include <algorithm>
  23. #include <limits>
  24. #include <map>
  25. #include <string>
  26. #include <vector>
  27. using std::string;
  28. using std::map;
  29. using std::vector;
  30. namespace l
  31. {
  32. static
  33. void
  34. normalize_statvfs(struct statvfs *fsstat_,
  35. const unsigned long min_bsize_,
  36. const unsigned long min_frsize_,
  37. const unsigned long min_namemax_)
  38. {
  39. fsstat_->f_blocks = (fsblkcnt_t)((fsstat_->f_blocks * fsstat_->f_frsize) / min_frsize_);
  40. fsstat_->f_bfree = (fsblkcnt_t)((fsstat_->f_bfree * fsstat_->f_frsize) / min_frsize_);
  41. fsstat_->f_bavail = (fsblkcnt_t)((fsstat_->f_bavail * fsstat_->f_frsize) / min_frsize_);
  42. fsstat_->f_bsize = min_bsize_;
  43. fsstat_->f_frsize = min_frsize_;
  44. fsstat_->f_namemax = min_namemax_;
  45. }
  46. static
  47. void
  48. merge_statvfs(struct statvfs * const out_,
  49. const struct statvfs * const in_)
  50. {
  51. out_->f_blocks += in_->f_blocks;
  52. out_->f_bfree += in_->f_bfree;
  53. out_->f_bavail += in_->f_bavail;
  54. out_->f_files += in_->f_files;
  55. out_->f_ffree += in_->f_ffree;
  56. out_->f_favail += in_->f_favail;
  57. }
  58. static
  59. bool
  60. should_ignore(const StatFSIgnore ignore_,
  61. const Branch &branch_,
  62. const bool readonly_)
  63. {
  64. return ((((ignore_ == StatFSIgnore::ENUM::RO) || readonly_) &&
  65. (branch_.ro_or_nc())) ||
  66. ((ignore_ == StatFSIgnore::ENUM::NC) && (branch_.nc())));
  67. }
  68. static
  69. int
  70. statfs(const Branches::CPtr &branches_,
  71. const char *fusepath_,
  72. const StatFS mode_,
  73. const StatFSIgnore ignore_,
  74. struct statvfs *fsstat_)
  75. {
  76. int rv;
  77. string fullpath;
  78. struct stat st;
  79. struct statvfs stvfs;
  80. unsigned long min_bsize;
  81. unsigned long min_frsize;
  82. unsigned long min_namemax;
  83. map<dev_t,struct statvfs> fsstats;
  84. min_bsize = std::numeric_limits<unsigned long>::max();
  85. min_frsize = std::numeric_limits<unsigned long>::max();
  86. min_namemax = std::numeric_limits<unsigned long>::max();
  87. for(const auto &branch : *branches_)
  88. {
  89. fullpath = ((mode_ == StatFS::ENUM::FULL) ?
  90. fs::path::make(branch.path,fusepath_) :
  91. branch.path);
  92. rv = fs::lstat(fullpath,&st);
  93. if(rv == -1)
  94. continue;
  95. rv = fs::lstatvfs(fullpath,&stvfs);
  96. if(rv == -1)
  97. continue;
  98. if(stvfs.f_bsize && (min_bsize > stvfs.f_bsize))
  99. min_bsize = stvfs.f_bsize;
  100. if(stvfs.f_frsize && (min_frsize > stvfs.f_frsize))
  101. min_frsize = stvfs.f_frsize;
  102. if(stvfs.f_namemax && (min_namemax > stvfs.f_namemax))
  103. min_namemax = stvfs.f_namemax;
  104. if(l::should_ignore(ignore_,branch,StatVFS::readonly(stvfs)))
  105. {
  106. stvfs.f_bavail = 0;
  107. stvfs.f_favail = 0;
  108. }
  109. fsstats.insert(std::make_pair(st.st_dev,stvfs));
  110. }
  111. map<dev_t,struct statvfs>::iterator iter = fsstats.begin();
  112. map<dev_t,struct statvfs>::iterator enditer = fsstats.end();
  113. if(iter != enditer)
  114. {
  115. *fsstat_ = iter->second;
  116. l::normalize_statvfs(fsstat_,min_bsize,min_frsize,min_namemax);
  117. for(++iter; iter != enditer; ++iter)
  118. {
  119. l::normalize_statvfs(&iter->second,min_bsize,min_frsize,min_namemax);
  120. l::merge_statvfs(fsstat_,&iter->second);
  121. }
  122. }
  123. return 0;
  124. }
  125. }
  126. namespace FUSE
  127. {
  128. int
  129. statfs(const char *fusepath_,
  130. struct statvfs *st_)
  131. {
  132. Config::Read cfg;
  133. const fuse_context *fc = fuse_get_context();
  134. const ugid::Set ugid(fc->uid,fc->gid);
  135. return l::statfs(cfg->branches,
  136. fusepath_,
  137. cfg->statfs,
  138. cfg->statfs_ignore,
  139. st_);
  140. }
  141. }