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.

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