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.

221 lines
4.3 KiB

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