AbstractDiskWriter.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "AbstractDiskWriter.h"
  36. #include <unistd.h>
  37. #include <cerrno>
  38. #include <cstring>
  39. #include <cassert>
  40. #ifdef __MINGW32__
  41. # include <windows.h>
  42. #endif // __MINGW32__
  43. #include "File.h"
  44. #include "util.h"
  45. #include "message.h"
  46. #include "LogFactory.h"
  47. #include "Logger.h"
  48. #include "DlAbortEx.h"
  49. #include "a2io.h"
  50. #include "StringFormat.h"
  51. #include "DownloadFailureException.h"
  52. namespace aria2 {
  53. AbstractDiskWriter::AbstractDiskWriter(const std::string& filename):
  54. filename_(filename),
  55. fd_(-1),
  56. readOnly_(false),
  57. directIOAllowed_(false),
  58. logger_(LogFactory::getInstance()) {}
  59. AbstractDiskWriter::~AbstractDiskWriter()
  60. {
  61. closeFile();
  62. }
  63. void AbstractDiskWriter::openFile(uint64_t totalLength)
  64. {
  65. try {
  66. openExistingFile(totalLength);
  67. } catch(RecoverableException& e) {
  68. if(e.getErrno() == ENOENT) {
  69. initAndOpenFile(totalLength);
  70. } else {
  71. throw;
  72. }
  73. }
  74. }
  75. void AbstractDiskWriter::closeFile()
  76. {
  77. if(fd_ >= 0) {
  78. close(fd_);
  79. fd_ = -1;
  80. }
  81. }
  82. void AbstractDiskWriter::openExistingFile(uint64_t totalLength)
  83. {
  84. int flags = O_BINARY;
  85. if(readOnly_) {
  86. flags |= O_RDONLY;
  87. } else {
  88. flags |= O_RDWR;
  89. }
  90. while((fd_ = open(filename_.c_str(), flags, OPEN_MODE)) == -1 &&
  91. errno == EINTR);
  92. if(fd_ < 0) {
  93. int errNum = errno;
  94. throw DL_ABORT_EX2
  95. (errNum,
  96. StringFormat(EX_FILE_OPEN,
  97. filename_.c_str(),
  98. util::safeStrerror(errNum).c_str()).str());
  99. }
  100. }
  101. void AbstractDiskWriter::createFile(int addFlags)
  102. {
  103. assert(!filename_.empty());
  104. util::mkdirs(File(filename_).getDirname());
  105. while((fd_ = open(filename_.c_str(), O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags,
  106. OPEN_MODE)) == -1 && errno == EINTR);
  107. if(fd_ < 0) {
  108. int errNum = errno;
  109. throw DL_ABORT_EX2
  110. (errNum,
  111. StringFormat(EX_FILE_OPEN,
  112. filename_.c_str(),
  113. util::safeStrerror(errNum).c_str()).str());
  114. }
  115. }
  116. ssize_t AbstractDiskWriter::writeDataInternal(const unsigned char* data, size_t len)
  117. {
  118. ssize_t writtenLength = 0;
  119. while((size_t)writtenLength < len) {
  120. ssize_t ret = 0;
  121. while((ret = write(fd_, data+writtenLength, len-writtenLength)) == -1 && errno == EINTR);
  122. if(ret == -1) {
  123. return -1;
  124. }
  125. writtenLength += ret;
  126. }
  127. return writtenLength;
  128. }
  129. ssize_t AbstractDiskWriter::readDataInternal(unsigned char* data, size_t len)
  130. {
  131. ssize_t ret = 0;
  132. while((ret = read(fd_, data, len)) == -1 && errno == EINTR);
  133. return ret;
  134. }
  135. void AbstractDiskWriter::seek(off_t offset)
  136. {
  137. if(a2lseek(fd_, offset, SEEK_SET) == (off_t)-1) {
  138. int errNum = errno;
  139. throw DL_ABORT_EX
  140. (StringFormat(EX_FILE_SEEK,
  141. filename_.c_str(),
  142. util::safeStrerror(errNum).c_str()).str());
  143. }
  144. }
  145. void AbstractDiskWriter::writeData(const unsigned char* data, size_t len, off_t offset)
  146. {
  147. seek(offset);
  148. if(writeDataInternal(data, len) < 0) {
  149. int errNum = errno;
  150. // If errno is ENOSPC(not enough space in device), throw
  151. // DownloadFailureException and abort download instantly.
  152. if(errNum == ENOSPC) {
  153. throw DOWNLOAD_FAILURE_EXCEPTION
  154. (StringFormat(EX_FILE_WRITE,
  155. filename_.c_str(),
  156. util::safeStrerror(errNum).c_str()).str());
  157. } else {
  158. throw DL_ABORT_EX
  159. (StringFormat(EX_FILE_WRITE,
  160. filename_.c_str(),
  161. util::safeStrerror(errNum).c_str()).str());
  162. }
  163. }
  164. }
  165. ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, off_t offset)
  166. {
  167. ssize_t ret;
  168. seek(offset);
  169. if((ret = readDataInternal(data, len)) < 0) {
  170. int errNum = errno;
  171. throw DL_ABORT_EX
  172. (StringFormat(EX_FILE_READ,
  173. filename_.c_str(),
  174. util::safeStrerror(errNum).c_str()).str());
  175. }
  176. return ret;
  177. }
  178. void AbstractDiskWriter::truncate(uint64_t length)
  179. {
  180. if(fd_ == -1) {
  181. throw DL_ABORT_EX("File not opened.");
  182. }
  183. #ifdef __MINGW32__
  184. // Since mingw32's ftruncate cannot handle over 2GB files, we use SetEndOfFile
  185. // instead.
  186. HANDLE handle = LongToHandle(_get_osfhandle(fd_));
  187. seek(length);
  188. if(SetEndOfFile(handle) == 0) {
  189. throw DL_ABORT_EX(StringFormat("SetEndOfFile failed. cause: %s",
  190. GetLastError()).str());
  191. }
  192. #else
  193. if(ftruncate(fd_, length) == -1) {
  194. int errNum = errno;
  195. throw DL_ABORT_EX(StringFormat("ftruncate failed. cause: %s",
  196. util::safeStrerror(errNum).c_str()).str());
  197. }
  198. #endif
  199. }
  200. void AbstractDiskWriter::allocate(off_t offset, uint64_t length)
  201. {
  202. #ifdef HAVE_SOME_FALLOCATE
  203. if(fd_ == -1) {
  204. throw DL_ABORT_EX("File not yet opened.");
  205. }
  206. # ifdef HAVE_FALLOCATE
  207. // For linux, we use fallocate to detect file system supports
  208. // fallocate or not.
  209. int r;
  210. while((r = fallocate(fd_, 0, offset, length)) == -1 && errno == EINTR);
  211. int errNum = errno;
  212. if(r == -1) {
  213. throw DL_ABORT_EX(StringFormat("fallocate failed. cause: %s",
  214. util::safeStrerror(errNum).c_str()).str());
  215. }
  216. # elif HAVE_POSIX_FALLOCATE
  217. int r = posix_fallocate(fd_, offset, length);
  218. if(r != 0) {
  219. throw DL_ABORT_EX(StringFormat("posix_fallocate failed. cause: %s",
  220. util::safeStrerror(r).c_str()).str());
  221. }
  222. # else
  223. # error "no *_fallocate function available."
  224. # endif
  225. #endif // HAVE_SOME_FALLOCATE
  226. }
  227. uint64_t AbstractDiskWriter::size()
  228. {
  229. return File(filename_).size();
  230. }
  231. void AbstractDiskWriter::enableDirectIO()
  232. {
  233. #ifdef ENABLE_DIRECT_IO
  234. if(directIOAllowed_) {
  235. int flg;
  236. while((flg = fcntl(fd_, F_GETFL)) == -1 && errno == EINTR);
  237. while(fcntl(fd_, F_SETFL, flg|O_DIRECT) == -1 && errno == EINTR);
  238. }
  239. #endif // ENABLE_DIRECT_IO
  240. }
  241. void AbstractDiskWriter::disableDirectIO()
  242. {
  243. #ifdef ENABLE_DIRECT_IO
  244. int flg;
  245. while((flg = fcntl(fd_, F_GETFL)) == -1 && errno == EINTR);
  246. while(fcntl(fd_, F_SETFL, flg&(~O_DIRECT)) == -1 && errno == EINTR);
  247. #endif // ENABLE_DIRECT_IO
  248. }
  249. void AbstractDiskWriter::enableReadOnly()
  250. {
  251. readOnly_ = true;
  252. }
  253. void AbstractDiskWriter::disableReadOnly()
  254. {
  255. readOnly_ = false;
  256. }
  257. } // namespace aria2