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.

143 lines
2.8 KiB

6 years ago
6 years ago
  1. /*
  2. ISC License
  3. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "fs_statvfs.hpp"
  16. #include "statvfs_util.hpp"
  17. #include <cstdint>
  18. #include <map>
  19. #include <string>
  20. #include <pthread.h>
  21. #include <sys/statvfs.h>
  22. #include <time.h>
  23. struct Element
  24. {
  25. uint64_t time;
  26. struct statvfs st;
  27. };
  28. typedef std::map<std::string,Element> statvfs_cache;
  29. static uint64_t g_timeout = 0;
  30. static statvfs_cache g_cache;
  31. static pthread_mutex_t g_cache_lock = PTHREAD_MUTEX_INITIALIZER;
  32. namespace l
  33. {
  34. static
  35. uint64_t
  36. get_time(void)
  37. {
  38. uint64_t rv;
  39. rv = ::time(NULL);
  40. return rv;
  41. }
  42. }
  43. namespace fs
  44. {
  45. uint64_t
  46. statvfs_cache_timeout(void)
  47. {
  48. return g_timeout;
  49. }
  50. void
  51. statvfs_cache_timeout(const uint64_t timeout_)
  52. {
  53. g_timeout = timeout_;
  54. }
  55. int
  56. statvfs_cache(const char *path_,
  57. struct statvfs *st_)
  58. {
  59. int rv;
  60. Element *e;
  61. uint64_t now;
  62. if(g_timeout == 0)
  63. return fs::statvfs(path_,st_);
  64. rv = 0;
  65. now = l::get_time();
  66. pthread_mutex_lock(&g_cache_lock);
  67. e = &g_cache[path_];
  68. if((now - e->time) > g_timeout)
  69. {
  70. e->time = now;
  71. rv = fs::statvfs(path_,&e->st);
  72. }
  73. *st_ = e->st;
  74. pthread_mutex_unlock(&g_cache_lock);
  75. return rv;
  76. }
  77. int
  78. statvfs_cache_readonly(const std::string &path_,
  79. bool *readonly_)
  80. {
  81. int rv;
  82. struct statvfs st;
  83. rv = fs::statvfs_cache(path_.c_str(),&st);
  84. if(rv == 0)
  85. *readonly_ = StatVFS::readonly(st);
  86. return rv;
  87. }
  88. int
  89. statvfs_cache_spaceavail(const std::string &path_,
  90. uint64_t *spaceavail_)
  91. {
  92. int rv;
  93. struct statvfs st;
  94. rv = fs::statvfs_cache(path_.c_str(),&st);
  95. if(rv == 0)
  96. *spaceavail_ = StatVFS::spaceavail(st);
  97. return rv;
  98. }
  99. int
  100. statvfs_cache_spaceused(const std::string &path_,
  101. uint64_t *spaceused_)
  102. {
  103. int rv;
  104. struct statvfs st;
  105. rv = fs::statvfs_cache(path_.c_str(),&st);
  106. if(rv == 0)
  107. *spaceused_ = StatVFS::spaceused(st);
  108. return rv;
  109. }
  110. }