AbstractDiskWriter.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "DlAbortEx.h"
  47. #include "a2io.h"
  48. #include "fmt.h"
  49. #include "DownloadFailureException.h"
  50. #include "error_code.h"
  51. namespace aria2 {
  52. AbstractDiskWriter::AbstractDiskWriter(const std::string& filename)
  53. : filename_(filename),
  54. fd_(-1),
  55. readOnly_(false),
  56. directIOAllowed_(false)
  57. {}
  58. AbstractDiskWriter::~AbstractDiskWriter()
  59. {
  60. closeFile();
  61. }
  62. void AbstractDiskWriter::openFile(uint64_t totalLength)
  63. {
  64. try {
  65. openExistingFile(totalLength);
  66. } catch(RecoverableException& e) {
  67. if(e.getErrNum() == ENOENT) {
  68. initAndOpenFile(totalLength);
  69. } else {
  70. throw;
  71. }
  72. }
  73. }
  74. void AbstractDiskWriter::closeFile()
  75. {
  76. if(fd_ >= 0) {
  77. close(fd_);
  78. fd_ = -1;
  79. }
  80. }
  81. void AbstractDiskWriter::openExistingFile(uint64_t totalLength)
  82. {
  83. int flags = O_BINARY;
  84. if(readOnly_) {
  85. flags |= O_RDONLY;
  86. } else {
  87. flags |= O_RDWR;
  88. }
  89. while((fd_ = open(filename_.c_str(), flags, OPEN_MODE)) == -1 &&
  90. errno == EINTR);
  91. if(fd_ < 0) {
  92. int errNum = errno;
  93. throw DL_ABORT_EX3
  94. (errNum,
  95. fmt(EX_FILE_OPEN,
  96. filename_.c_str(),
  97. util::safeStrerror(errNum).c_str()),
  98. error_code::FILE_OPEN_ERROR);
  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_EX3
  110. (errNum,
  111. fmt(EX_FILE_OPEN,
  112. filename_.c_str(),
  113. util::safeStrerror(errNum).c_str()),
  114. error_code::FILE_CREATE_ERROR);
  115. }
  116. }
  117. ssize_t AbstractDiskWriter::writeDataInternal(const unsigned char* data, size_t len)
  118. {
  119. ssize_t writtenLength = 0;
  120. while((size_t)writtenLength < len) {
  121. ssize_t ret = 0;
  122. while((ret = write(fd_, data+writtenLength, len-writtenLength)) == -1 && errno == EINTR);
  123. if(ret == -1) {
  124. return -1;
  125. }
  126. writtenLength += ret;
  127. }
  128. return writtenLength;
  129. }
  130. ssize_t AbstractDiskWriter::readDataInternal(unsigned char* data, size_t len)
  131. {
  132. ssize_t ret = 0;
  133. while((ret = read(fd_, data, len)) == -1 && errno == EINTR);
  134. return ret;
  135. }
  136. void AbstractDiskWriter::seek(off_t offset)
  137. {
  138. if(a2lseek(fd_, offset, SEEK_SET) == (off_t)-1) {
  139. int errNum = errno;
  140. throw DL_ABORT_EX2(fmt(EX_FILE_SEEK,
  141. filename_.c_str(),
  142. util::safeStrerror(errNum).c_str()),
  143. error_code::FILE_IO_ERROR);
  144. }
  145. }
  146. void AbstractDiskWriter::writeData(const unsigned char* data, size_t len, off_t offset)
  147. {
  148. seek(offset);
  149. if(writeDataInternal(data, len) < 0) {
  150. int errNum = errno;
  151. // If errno is ENOSPC(not enough space in device), throw
  152. // DownloadFailureException and abort download instantly.
  153. if(errNum == ENOSPC) {
  154. throw DOWNLOAD_FAILURE_EXCEPTION3
  155. (errNum,
  156. fmt(EX_FILE_WRITE,
  157. filename_.c_str(),
  158. util::safeStrerror(errNum).c_str()),
  159. error_code::NOT_ENOUGH_DISK_SPACE);
  160. } else {
  161. throw DL_ABORT_EX3
  162. (errNum,
  163. fmt(EX_FILE_WRITE,
  164. filename_.c_str(),
  165. util::safeStrerror(errNum).c_str()),
  166. error_code::FILE_IO_ERROR);
  167. }
  168. }
  169. }
  170. ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, off_t offset)
  171. {
  172. ssize_t ret;
  173. seek(offset);
  174. if((ret = readDataInternal(data, len)) < 0) {
  175. int errNum = errno;
  176. throw DL_ABORT_EX3
  177. (errNum,
  178. fmt(EX_FILE_READ,
  179. filename_.c_str(),
  180. util::safeStrerror(errNum).c_str()),
  181. error_code::FILE_IO_ERROR);
  182. }
  183. return ret;
  184. }
  185. void AbstractDiskWriter::truncate(uint64_t length)
  186. {
  187. if(fd_ == -1) {
  188. throw DL_ABORT_EX("File not opened.");
  189. }
  190. #ifdef __MINGW32__
  191. // Since mingw32's ftruncate cannot handle over 2GB files, we use SetEndOfFile
  192. // instead.
  193. HANDLE handle = LongToHandle(_get_osfhandle(fd_));
  194. seek(length);
  195. if(SetEndOfFile(handle) == 0) {
  196. throw DL_ABORT_EX2(fmt("SetEndOfFile failed. cause: %lx",
  197. GetLastError()),
  198. error_code::FILE_IO_ERROR);
  199. }
  200. #else
  201. if(ftruncate(fd_, length) == -1) {
  202. int errNum = errno;
  203. throw DL_ABORT_EX3(errNum,
  204. fmt("ftruncate failed. cause: %s",
  205. util::safeStrerror(errNum).c_str()),
  206. error_code::FILE_IO_ERROR);
  207. }
  208. #endif
  209. }
  210. void AbstractDiskWriter::allocate(off_t offset, uint64_t length)
  211. {
  212. #ifdef HAVE_SOME_FALLOCATE
  213. if(fd_ == -1) {
  214. throw DL_ABORT_EX("File not yet opened.");
  215. }
  216. # ifdef __MINGW32__
  217. LARGE_INTEGER fileLength;
  218. fileLength.QuadPart = offset+length;
  219. HANDLE handle = LongToHandle(_get_osfhandle(fd_));
  220. int r;
  221. r = SetFilePointerEx(handle, fileLength, 0, FILE_BEGIN);
  222. if(r == 0) {
  223. throw DL_ABORT_EX2(fmt("SetFilePointerEx failed. cause: %lx",
  224. GetLastError()),
  225. error_code::FILE_IO_ERROR);
  226. }
  227. r = SetEndOfFile(handle);
  228. if(r == 0) {
  229. throw DL_ABORT_EX2(fmt("SetEndOfFile failed. cause: %lx",
  230. GetLastError()),
  231. error_code::FILE_IO_ERROR);
  232. }
  233. # elif HAVE_FALLOCATE
  234. // For linux, we use fallocate to detect file system supports
  235. // fallocate or not.
  236. int r;
  237. while((r = fallocate(fd_, 0, offset, length)) == -1 && errno == EINTR);
  238. int errNum = errno;
  239. if(r == -1) {
  240. throw DL_ABORT_EX3(errNum,
  241. fmt("fallocate failed. cause: %s",
  242. util::safeStrerror(errNum).c_str()),
  243. error_code::FILE_IO_ERROR);
  244. }
  245. # elif HAVE_POSIX_FALLOCATE
  246. int r = posix_fallocate(fd_, offset, length);
  247. if(r != 0) {
  248. throw DL_ABORT_EX3(r,
  249. fmt("posix_fallocate failed. cause: %s",
  250. util::safeStrerror(r).c_str()),
  251. error_code::FILE_IO_ERROR);
  252. }
  253. # else
  254. # error "no *_fallocate function available."
  255. # endif
  256. #endif // HAVE_SOME_FALLOCATE
  257. }
  258. uint64_t AbstractDiskWriter::size()
  259. {
  260. return File(filename_).size();
  261. }
  262. void AbstractDiskWriter::enableDirectIO()
  263. {
  264. #ifdef ENABLE_DIRECT_IO
  265. if(directIOAllowed_) {
  266. int flg;
  267. while((flg = fcntl(fd_, F_GETFL)) == -1 && errno == EINTR);
  268. while(fcntl(fd_, F_SETFL, flg|O_DIRECT) == -1 && errno == EINTR);
  269. }
  270. #endif // ENABLE_DIRECT_IO
  271. }
  272. void AbstractDiskWriter::disableDirectIO()
  273. {
  274. #ifdef ENABLE_DIRECT_IO
  275. int flg;
  276. while((flg = fcntl(fd_, F_GETFL)) == -1 && errno == EINTR);
  277. while(fcntl(fd_, F_SETFL, flg&(~O_DIRECT)) == -1 && errno == EINTR);
  278. #endif // ENABLE_DIRECT_IO
  279. }
  280. void AbstractDiskWriter::enableReadOnly()
  281. {
  282. readOnly_ = true;
  283. }
  284. void AbstractDiskWriter::disableReadOnly()
  285. {
  286. readOnly_ = false;
  287. }
  288. } // namespace aria2