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.

160 lines
4.0 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 "errno.h"
  15. #include "fs_attr.hpp"
  16. #include "fs_clonepath.hpp"
  17. #include "fs_lchown.hpp"
  18. #include "fs_lstat.hpp"
  19. #include "fs_lutimens.hpp"
  20. #include "fs_mkdir.hpp"
  21. #include "fs_path.hpp"
  22. #include "fs_xattr.hpp"
  23. #include "ugid.hpp"
  24. #include <string>
  25. using std::string;
  26. namespace l
  27. {
  28. static
  29. bool
  30. ignorable_error(const int err_)
  31. {
  32. switch(err_)
  33. {
  34. case ENOTTY:
  35. case ENOTSUP:
  36. #if ENOTSUP != EOPNOTSUPP
  37. case EOPNOTSUPP:
  38. #endif
  39. return true;
  40. }
  41. return false;
  42. }
  43. }
  44. namespace fs
  45. {
  46. /*
  47. Attempts to clone a path.
  48. The directories which already exist are left alone.
  49. The new directories have metadata set to match the original if
  50. possible. Optionally ignore errors on metadata copies.
  51. */
  52. int
  53. clonepath(const string &fromsrc_,
  54. const string &tosrc_,
  55. const char *relative_,
  56. const bool return_metadata_errors_)
  57. {
  58. int rv;
  59. struct stat st;
  60. string topath;
  61. string frompath;
  62. string dirname;
  63. if((relative_ == NULL) || (relative_[0] == '\0'))
  64. return 0;
  65. dirname = fs::path::dirname(relative_);
  66. if(dirname != "/")
  67. {
  68. rv = fs::clonepath(fromsrc_,tosrc_,dirname,return_metadata_errors_);
  69. if(rv == -1)
  70. return -1;
  71. }
  72. frompath = fs::path::make(fromsrc_,relative_);
  73. rv = fs::lstat(frompath,&st);
  74. if(rv == -1)
  75. return -1;
  76. else if(!S_ISDIR(st.st_mode))
  77. return (errno=ENOTDIR,-1);
  78. topath = fs::path::make(tosrc_,relative_);
  79. rv = fs::mkdir(topath,st.st_mode);
  80. if(rv == -1)
  81. {
  82. if(errno == EEXIST)
  83. return 0;
  84. else
  85. return -1;
  86. }
  87. // it may not support it... it's fine...
  88. rv = fs::attr::copy(frompath,topath);
  89. if(return_metadata_errors_ && (rv == -1) && !l::ignorable_error(errno))
  90. return -1;
  91. // it may not support it... it's fine...
  92. rv = fs::xattr::copy(frompath,topath);
  93. if(return_metadata_errors_ && (rv == -1) && !l::ignorable_error(errno))
  94. return -1;
  95. rv = fs::lchown_check_on_error(topath,st);
  96. if(return_metadata_errors_ && (rv == -1))
  97. return -1;
  98. rv = fs::lutimens(topath,st);
  99. if(return_metadata_errors_ && (rv == -1))
  100. return -1;
  101. return 0;
  102. }
  103. int
  104. clonepath(const string &from_,
  105. const string &to_,
  106. const string &relative_,
  107. const bool return_metadata_errors_)
  108. {
  109. return fs::clonepath(from_,
  110. to_,
  111. relative_.c_str(),
  112. return_metadata_errors_);
  113. }
  114. int
  115. clonepath_as_root(const string &from_,
  116. const string &to_,
  117. const char *relative_,
  118. const bool return_metadata_errors_)
  119. {
  120. if((relative_ == NULL) || (relative_[0] == '\0'))
  121. return 0;
  122. if(from_ == to_)
  123. return 0;
  124. {
  125. const ugid::SetRootGuard ugidGuard;
  126. return fs::clonepath(from_,to_,relative_,return_metadata_errors_);
  127. }
  128. }
  129. int
  130. clonepath_as_root(const string &from_,
  131. const string &to_,
  132. const string &relative_,
  133. const bool return_metadata_errors_)
  134. {
  135. return fs::clonepath_as_root(from_,to_,relative_.c_str(),return_metadata_errors_);
  136. }
  137. }