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.

133 lines
3.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 "config.hpp"
  15. #include "errno.hpp"
  16. #include "fs_path.hpp"
  17. #include "fs_truncate.hpp"
  18. #include "policy_rv.hpp"
  19. #include "ugid.hpp"
  20. #include "fuse.h"
  21. #include <string>
  22. #include <sys/types.h>
  23. #include <unistd.h>
  24. using std::string;
  25. namespace l
  26. {
  27. static
  28. int
  29. get_error(const PolicyRV &prv_,
  30. const string &basepath_)
  31. {
  32. for(int i = 0, ei = prv_.success.size(); i < ei; i++)
  33. {
  34. if(prv_.success[i].basepath == basepath_)
  35. return prv_.success[i].rv;
  36. }
  37. for(int i = 0, ei = prv_.error.size(); i < ei; i++)
  38. {
  39. if(prv_.error[i].basepath == basepath_)
  40. return prv_.error[i].rv;
  41. }
  42. return 0;
  43. }
  44. static
  45. void
  46. truncate_loop_core(const string &basepath_,
  47. const char *fusepath_,
  48. const off_t size_,
  49. PolicyRV *prv_)
  50. {
  51. string fullpath;
  52. fullpath = fs::path::make(basepath_,fusepath_);
  53. errno = 0;
  54. fs::truncate(fullpath,size_);
  55. prv_->insert(errno,basepath_);
  56. }
  57. static
  58. void
  59. truncate_loop(const StrVec &basepaths_,
  60. const char *fusepath_,
  61. const off_t size_,
  62. PolicyRV *prv_)
  63. {
  64. for(size_t i = 0, ei = basepaths_.size(); i != ei; i++)
  65. {
  66. l::truncate_loop_core(basepaths_[i],fusepath_,size_,prv_);
  67. }
  68. }
  69. static
  70. int
  71. truncate(const Policy::Action &actionFunc_,
  72. const Policy::Search &searchFunc_,
  73. const Branches &branches_,
  74. const char *fusepath_,
  75. const off_t size_)
  76. {
  77. int rv;
  78. PolicyRV prv;
  79. StrVec basepaths;
  80. rv = actionFunc_(branches_,fusepath_,&basepaths);
  81. if(rv == -1)
  82. return -errno;
  83. l::truncate_loop(basepaths,fusepath_,size_,&prv);
  84. if(prv.error.empty())
  85. return 0;
  86. if(prv.success.empty())
  87. return prv.error[0].rv;
  88. basepaths.clear();
  89. rv = searchFunc_(branches_,fusepath_,&basepaths);
  90. if(rv == -1)
  91. return -errno;
  92. return l::get_error(prv,basepaths[0]);
  93. }
  94. }
  95. namespace FUSE
  96. {
  97. int
  98. truncate(const char *fusepath_,
  99. off_t size_)
  100. {
  101. Config::Read cfg;
  102. const fuse_context *fc = fuse_get_context();
  103. const ugid::Set ugid(fc->uid,fc->gid);
  104. return l::truncate(cfg->func.truncate.policy,
  105. cfg->func.getattr.policy,
  106. cfg->branches,
  107. fusepath_,
  108. size_);
  109. }
  110. }