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.

231 lines
4.8 KiB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2015 Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #ifdef __linux__
  27. #include <sys/sendfile.h>
  28. #endif
  29. #include <string>
  30. #include <vector>
  31. #include "fs_attr.hpp"
  32. #include "fs_xattr.hpp"
  33. using std::string;
  34. using std::vector;
  35. namespace fs
  36. {
  37. static
  38. ssize_t
  39. sendfile(const int fdin,
  40. const int fdout,
  41. const size_t count)
  42. {
  43. #if defined __linux__
  44. off_t offset = 0;
  45. return ::sendfile(fdout,fdin,&offset,count);
  46. #else
  47. return (errno=EINVAL,-1);
  48. #endif
  49. }
  50. int
  51. writen(const int fd,
  52. const char *buf,
  53. const size_t count)
  54. {
  55. size_t nleft;
  56. ssize_t nwritten;
  57. nleft = count;
  58. while(nleft > 0)
  59. {
  60. nwritten = ::write(fd,buf,nleft);
  61. if(nwritten == -1)
  62. {
  63. if(errno == EINTR)
  64. continue;
  65. return -1;
  66. }
  67. nleft -= nwritten;
  68. buf += nwritten;
  69. }
  70. return count;
  71. }
  72. static
  73. int
  74. copyfile_rw(const int fdin,
  75. const int fdout,
  76. const size_t count,
  77. const size_t blocksize)
  78. {
  79. ssize_t nr;
  80. ssize_t nw;
  81. ssize_t bufsize;
  82. size_t totalwritten;
  83. vector<char> buf;
  84. bufsize = (blocksize * 16);
  85. buf.resize(bufsize);
  86. totalwritten = 0;
  87. while(totalwritten < count)
  88. {
  89. nr = ::read(fdin,&buf[0],bufsize);
  90. if(nr == -1)
  91. {
  92. if(errno == EINTR)
  93. continue;
  94. else
  95. return -1;
  96. }
  97. nw = writen(fdout,&buf[0],nr);
  98. if(nw == -1)
  99. return -1;
  100. totalwritten += nw;
  101. }
  102. return count;
  103. }
  104. static
  105. int
  106. copydata(const int fdin,
  107. const int fdout,
  108. const size_t count,
  109. const size_t blocksize)
  110. {
  111. int rv;
  112. ::posix_fadvise(fdin,0,count,POSIX_FADV_WILLNEED);
  113. ::posix_fadvise(fdin,0,count,POSIX_FADV_SEQUENTIAL);
  114. ::posix_fallocate(fdout,0,count);
  115. rv = fs::sendfile(fdin,fdout,count);
  116. if((rv == -1) && ((errno == EINVAL) || (errno == ENOSYS)))
  117. return fs::copyfile_rw(fdin,fdout,count,blocksize);
  118. return rv;
  119. }
  120. static
  121. bool
  122. ignorable_error(const int err)
  123. {
  124. switch(err)
  125. {
  126. case ENOTTY:
  127. case ENOTSUP:
  128. #if ENOTSUP != EOPNOTSUPP
  129. case EOPNOTSUPP:
  130. #endif
  131. return true;
  132. }
  133. return false;
  134. }
  135. int
  136. clonefile(const int fdin,
  137. const int fdout)
  138. {
  139. int rv;
  140. struct stat stin;
  141. rv = ::fstat(fdin,&stin);
  142. if(rv == -1)
  143. return -1;
  144. rv = copydata(fdin,fdout,stin.st_size,stin.st_blksize);
  145. if(rv == -1)
  146. return -1;
  147. rv = fs::attr::copy(fdin,fdout);
  148. if(rv == -1 && !ignorable_error(errno))
  149. return -1;
  150. rv = fs::xattr::copy(fdin,fdout);
  151. if(rv == -1 && !ignorable_error(errno))
  152. return -1;
  153. rv = ::fchown(fdout,stin.st_uid,stin.st_gid);
  154. if(rv == -1)
  155. return -1;
  156. rv = ::fchmod(fdout,stin.st_mode);
  157. if(rv == -1)
  158. return -1;
  159. struct timespec times[2];
  160. times[0] = stin.st_atim;
  161. times[1] = stin.st_mtim;
  162. rv = ::futimens(fdout,times);
  163. if(rv == -1)
  164. return -1;
  165. return 0;
  166. }
  167. int
  168. clonefile(const string &in,
  169. const string &out)
  170. {
  171. int rv;
  172. int fdin;
  173. int fdout;
  174. int error;
  175. fdin = ::open(in.c_str(),O_RDONLY|O_NOFOLLOW);
  176. if(fdin == -1)
  177. return -1;
  178. const int flags = O_CREAT|O_LARGEFILE|O_NOATIME|O_NOFOLLOW|O_TRUNC|O_WRONLY;
  179. const int mode = S_IWUSR;
  180. fdout = ::open(out.c_str(),flags,mode);
  181. if(fdout == -1)
  182. return -1;
  183. rv = fs::clonefile(fdin,fdout);
  184. error = errno;
  185. ::close(fdin);
  186. ::close(fdout);
  187. errno = error;
  188. return rv;
  189. }
  190. }