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.

183 lines
4.2 KiB

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 "errno.hpp"
  15. #include "fs_clonefile.hpp"
  16. #include "fs_clonepath.hpp"
  17. #include "fs_close.hpp"
  18. #include "fs_file_size.hpp"
  19. #include "fs_findonfs.hpp"
  20. #include "fs_getfl.hpp"
  21. #include "fs_has_space.hpp"
  22. #include "fs_mktemp.hpp"
  23. #include "fs_open.hpp"
  24. #include "fs_path.hpp"
  25. #include "fs_rename.hpp"
  26. #include "fs_stat.hpp"
  27. #include "fs_unlink.hpp"
  28. #include "policy.hpp"
  29. #include "ugid.hpp"
  30. #include <string>
  31. #include <vector>
  32. #include <fcntl.h>
  33. #include <stdlib.h>
  34. #include <sys/stat.h>
  35. #include <sys/types.h>
  36. #include <unistd.h>
  37. using std::string;
  38. using std::vector;
  39. static
  40. int
  41. cleanup_flags(const int flags_)
  42. {
  43. int rv;
  44. rv = flags_;
  45. rv = (rv & ~O_TRUNC);
  46. rv = (rv & ~O_CREAT);
  47. rv = (rv & ~O_EXCL);
  48. return rv;
  49. }
  50. namespace l
  51. {
  52. static
  53. int
  54. movefile(const Policy::Create &createFunc_,
  55. const Branches::CPtr &branches_,
  56. const string &fusepath_,
  57. int origfd_)
  58. {
  59. int rv;
  60. int srcfd;
  61. int dstfd;
  62. int dstfd_flags;
  63. int origfd_flags;
  64. int64_t srcfd_size;
  65. string fusedir;
  66. string srcfd_branch;
  67. string srcfd_filepath;
  68. string dstfd_filepath;
  69. string dstfd_tmp_filepath;
  70. vector<string> dstfd_branch;
  71. srcfd = -1;
  72. dstfd = -1;
  73. rv = fs::findonfs(branches_,fusepath_,origfd_,&srcfd_branch);
  74. if(rv == -1)
  75. return -errno;
  76. rv = createFunc_(branches_,fusepath_,&dstfd_branch);
  77. if(rv == -1)
  78. return -errno;
  79. origfd_flags = fs::getfl(origfd_);
  80. if(origfd_flags == -1)
  81. return -errno;
  82. srcfd_size = fs::file_size(origfd_);
  83. if(srcfd_size == -1)
  84. return -errno;
  85. if(fs::has_space(dstfd_branch[0],srcfd_size) == false)
  86. return -ENOSPC;
  87. fusedir = fs::path::dirname(fusepath_);
  88. rv = fs::clonepath(srcfd_branch,dstfd_branch[0],fusedir);
  89. if(rv == -1)
  90. return -ENOSPC;
  91. srcfd_filepath = srcfd_branch;
  92. fs::path::append(srcfd_filepath,fusepath_);
  93. srcfd = fs::open(srcfd_filepath,O_RDONLY);
  94. if(srcfd == -1)
  95. return -ENOSPC;
  96. dstfd_filepath = dstfd_branch[0];
  97. fs::path::append(dstfd_filepath,fusepath_);
  98. std::tie(dstfd,dstfd_tmp_filepath) = fs::mktemp(dstfd_filepath,O_WRONLY);
  99. if(dstfd < 0)
  100. {
  101. fs::close(srcfd);
  102. return -ENOSPC;
  103. }
  104. rv = fs::clonefile(srcfd,dstfd);
  105. if(rv == -1)
  106. {
  107. fs::close(srcfd);
  108. fs::close(dstfd);
  109. fs::unlink(dstfd_tmp_filepath);
  110. return -ENOSPC;
  111. }
  112. rv = fs::rename(dstfd_tmp_filepath,dstfd_filepath);
  113. if(rv == -1)
  114. {
  115. fs::close(srcfd);
  116. fs::close(dstfd);
  117. fs::unlink(dstfd_tmp_filepath);
  118. return -ENOSPC;
  119. }
  120. fs::close(srcfd);
  121. fs::close(dstfd);
  122. dstfd_flags = ::cleanup_flags(origfd_flags);
  123. rv = fs::open(dstfd_filepath,dstfd_flags);
  124. if(rv == -1)
  125. {
  126. fs::unlink(dstfd_tmp_filepath);
  127. return -ENOSPC;
  128. }
  129. fs::unlink(srcfd_filepath);
  130. return rv;
  131. }
  132. }
  133. namespace fs
  134. {
  135. int
  136. movefile(const Policy::Create &policy_,
  137. const Branches::CPtr &basepaths_,
  138. const string &fusepath_,
  139. int origfd_)
  140. {
  141. return l::movefile(policy_,basepaths_,fusepath_,origfd_);
  142. }
  143. int
  144. movefile_as_root(const Policy::Create &policy_,
  145. const Branches::CPtr &basepaths_,
  146. const string &fusepath_,
  147. int origfd_)
  148. {
  149. const ugid::Set ugid(0,0);
  150. return fs::movefile(policy_,basepaths_,fusepath_,origfd_);
  151. }
  152. }