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.

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