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.

223 lines
4.5 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 <stdlib.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #include <errno.h>
  20. #ifdef __linux__
  21. #include <sys/sendfile.h>
  22. #endif
  23. #include <string>
  24. #include <vector>
  25. #include "fs_attr.hpp"
  26. #include "fs_xattr.hpp"
  27. using std::string;
  28. using std::vector;
  29. namespace fs
  30. {
  31. static
  32. ssize_t
  33. sendfile(const int fdin,
  34. const int fdout,
  35. const size_t count)
  36. {
  37. #if defined __linux__
  38. off_t offset = 0;
  39. return ::sendfile(fdout,fdin,&offset,count);
  40. #else
  41. return (errno=EINVAL,-1);
  42. #endif
  43. }
  44. int
  45. writen(const int fd,
  46. const char *buf,
  47. const size_t count)
  48. {
  49. size_t nleft;
  50. ssize_t nwritten;
  51. nleft = count;
  52. while(nleft > 0)
  53. {
  54. nwritten = ::write(fd,buf,nleft);
  55. if(nwritten == -1)
  56. {
  57. if(errno == EINTR)
  58. continue;
  59. return -1;
  60. }
  61. nleft -= nwritten;
  62. buf += nwritten;
  63. }
  64. return count;
  65. }
  66. static
  67. int
  68. copyfile_rw(const int fdin,
  69. const int fdout,
  70. const size_t count,
  71. const size_t blocksize)
  72. {
  73. ssize_t nr;
  74. ssize_t nw;
  75. ssize_t bufsize;
  76. size_t totalwritten;
  77. vector<char> buf;
  78. bufsize = (blocksize * 16);
  79. buf.resize(bufsize);
  80. totalwritten = 0;
  81. while(totalwritten < count)
  82. {
  83. nr = ::read(fdin,&buf[0],bufsize);
  84. if(nr == -1)
  85. {
  86. if(errno == EINTR)
  87. continue;
  88. else
  89. return -1;
  90. }
  91. nw = writen(fdout,&buf[0],nr);
  92. if(nw == -1)
  93. return -1;
  94. totalwritten += nw;
  95. }
  96. return count;
  97. }
  98. static
  99. int
  100. copydata(const int fdin,
  101. const int fdout,
  102. const size_t count,
  103. const size_t blocksize)
  104. {
  105. int rv;
  106. ::posix_fadvise(fdin,0,count,POSIX_FADV_WILLNEED);
  107. ::posix_fadvise(fdin,0,count,POSIX_FADV_SEQUENTIAL);
  108. ::posix_fallocate(fdout,0,count);
  109. rv = fs::sendfile(fdin,fdout,count);
  110. if((rv == -1) && ((errno == EINVAL) || (errno == ENOSYS)))
  111. return fs::copyfile_rw(fdin,fdout,count,blocksize);
  112. return rv;
  113. }
  114. static
  115. bool
  116. ignorable_error(const int err)
  117. {
  118. switch(err)
  119. {
  120. case ENOTTY:
  121. case ENOTSUP:
  122. #if ENOTSUP != EOPNOTSUPP
  123. case EOPNOTSUPP:
  124. #endif
  125. return true;
  126. }
  127. return false;
  128. }
  129. int
  130. clonefile(const int fdin,
  131. const int fdout)
  132. {
  133. int rv;
  134. struct stat stin;
  135. rv = ::fstat(fdin,&stin);
  136. if(rv == -1)
  137. return -1;
  138. rv = copydata(fdin,fdout,stin.st_size,stin.st_blksize);
  139. if(rv == -1)
  140. return -1;
  141. rv = fs::attr::copy(fdin,fdout);
  142. if(rv == -1 && !ignorable_error(errno))
  143. return -1;
  144. rv = fs::xattr::copy(fdin,fdout);
  145. if(rv == -1 && !ignorable_error(errno))
  146. return -1;
  147. rv = ::fchown(fdout,stin.st_uid,stin.st_gid);
  148. if(rv == -1)
  149. return -1;
  150. rv = ::fchmod(fdout,stin.st_mode);
  151. if(rv == -1)
  152. return -1;
  153. struct timespec times[2];
  154. times[0] = stin.st_atim;
  155. times[1] = stin.st_mtim;
  156. rv = ::futimens(fdout,times);
  157. if(rv == -1)
  158. return -1;
  159. return 0;
  160. }
  161. int
  162. clonefile(const string &in,
  163. const string &out)
  164. {
  165. int rv;
  166. int fdin;
  167. int fdout;
  168. int error;
  169. fdin = ::open(in.c_str(),O_RDONLY|O_NOFOLLOW);
  170. if(fdin == -1)
  171. return -1;
  172. const int flags = O_CREAT|O_LARGEFILE|O_NOATIME|O_NOFOLLOW|O_TRUNC|O_WRONLY;
  173. const int mode = S_IWUSR;
  174. fdout = ::open(out.c_str(),flags,mode);
  175. if(fdout == -1)
  176. return -1;
  177. rv = fs::clonefile(fdin,fdout);
  178. error = errno;
  179. ::close(fdin);
  180. ::close(fdout);
  181. errno = error;
  182. return rv;
  183. }
  184. }