AbstractDiskWriter.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. {}
  57. AbstractDiskWriter::~AbstractDiskWriter()
  58. {
  59. closeFile();
  60. }
  61. void AbstractDiskWriter::openFile(uint64_t totalLength)
  62. {
  63. try {
  64. openExistingFile(totalLength);
  65. } catch(RecoverableException& e) {
  66. if(e.getErrNum() == ENOENT) {
  67. initAndOpenFile(totalLength);
  68. } else {
  69. throw;
  70. }
  71. }
  72. }
  73. void AbstractDiskWriter::closeFile()
  74. {
  75. if(fd_ >= 0) {
  76. close(fd_);
  77. fd_ = -1;
  78. }
  79. }
  80. void AbstractDiskWriter::openExistingFile(uint64_t totalLength)
  81. {
  82. int flags = O_BINARY;
  83. if(readOnly_) {
  84. flags |= O_RDONLY;
  85. } else {
  86. flags |= O_RDWR;
  87. }
  88. while((fd_ = open(filename_.c_str(), flags, OPEN_MODE)) == -1 &&
  89. errno == EINTR);
  90. if(fd_ < 0) {
  91. int errNum = errno;
  92. throw DL_ABORT_EX3
  93. (errNum,
  94. fmt(EX_FILE_OPEN,
  95. filename_.c_str(),
  96. util::safeStrerror(errNum).c_str()),
  97. error_code::FILE_OPEN_ERROR);
  98. }
  99. }
  100. void AbstractDiskWriter::createFile(int addFlags)
  101. {
  102. assert(!filename_.empty());
  103. util::mkdirs(File(filename_).getDirname());
  104. while((fd_ = open(filename_.c_str(), O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags,
  105. OPEN_MODE)) == -1 && errno == EINTR);
  106. if(fd_ < 0) {
  107. int errNum = errno;
  108. throw DL_ABORT_EX3
  109. (errNum,
  110. fmt(EX_FILE_OPEN,
  111. filename_.c_str(),
  112. util::safeStrerror(errNum).c_str()),
  113. error_code::FILE_CREATE_ERROR);
  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_EX2(fmt(EX_FILE_SEEK,
  140. filename_.c_str(),
  141. util::safeStrerror(errNum).c_str()),
  142. error_code::FILE_IO_ERROR);
  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_EXCEPTION3
  154. (errNum,
  155. fmt(EX_FILE_WRITE,
  156. filename_.c_str(),
  157. util::safeStrerror(errNum).c_str()),
  158. error_code::NOT_ENOUGH_DISK_SPACE);
  159. } else {
  160. throw DL_ABORT_EX3
  161. (errNum,
  162. fmt(EX_FILE_WRITE,
  163. filename_.c_str(),
  164. util::safeStrerror(errNum).c_str()),
  165. error_code::FILE_IO_ERROR);
  166. }
  167. }
  168. }
  169. ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, off_t offset)
  170. {
  171. ssize_t ret;
  172. seek(offset);
  173. if((ret = readDataInternal(data, len)) < 0) {
  174. int errNum = errno;
  175. throw DL_ABORT_EX3
  176. (errNum,
  177. fmt(EX_FILE_READ,
  178. filename_.c_str(),
  179. util::safeStrerror(errNum).c_str()),
  180. error_code::FILE_IO_ERROR);
  181. }
  182. return ret;
  183. }
  184. void AbstractDiskWriter::truncate(uint64_t length)
  185. {
  186. if(fd_ == -1) {
  187. throw DL_ABORT_EX("File not opened.");
  188. }
  189. #ifdef __MINGW32__
  190. // Since mingw32's ftruncate cannot handle over 2GB files, we use SetEndOfFile
  191. // instead.
  192. HANDLE handle = LongToHandle(_get_osfhandle(fd_));
  193. seek(length);
  194. if(SetEndOfFile(handle) == 0) {
  195. throw DL_ABORT_EX2(fmt("SetEndOfFile failed. cause: %lx",
  196. GetLastError()),
  197. error_code::FILE_IO_ERROR);
  198. }
  199. #else
  200. if(ftruncate(fd_, length) == -1) {
  201. int errNum = errno;
  202. throw DL_ABORT_EX3(errNum,
  203. fmt("ftruncate failed. cause: %s",
  204. util::safeStrerror(errNum).c_str()),
  205. error_code::FILE_IO_ERROR);
  206. }
  207. #endif
  208. }
  209. void AbstractDiskWriter::allocate(off_t offset, uint64_t length)
  210. {
  211. #ifdef HAVE_SOME_FALLOCATE
  212. if(fd_ == -1) {
  213. throw DL_ABORT_EX("File not yet opened.");
  214. }
  215. # ifdef __MINGW32__
  216. LARGE_INTEGER fileLength;
  217. fileLength.QuadPart = offset+length;
  218. HANDLE handle = LongToHandle(_get_osfhandle(fd_));
  219. int r;
  220. r = SetFilePointerEx(handle, fileLength, 0, FILE_BEGIN);
  221. if(r == 0) {
  222. throw DL_ABORT_EX2(fmt("SetFilePointerEx failed. cause: %lx",
  223. GetLastError()),
  224. error_code::FILE_IO_ERROR);
  225. }
  226. r = SetEndOfFile(handle);
  227. if(r == 0) {
  228. throw DL_ABORT_EX2(fmt("SetEndOfFile failed. cause: %lx",
  229. GetLastError()),
  230. error_code::FILE_IO_ERROR);
  231. }
  232. # elif HAVE_FALLOCATE
  233. // For linux, we use fallocate to detect file system supports
  234. // fallocate or not.
  235. int r;
  236. while((r = fallocate(fd_, 0, offset, length)) == -1 && errno == EINTR);
  237. int errNum = errno;
  238. if(r == -1) {
  239. throw DL_ABORT_EX3(errNum,
  240. fmt("fallocate failed. cause: %s",
  241. util::safeStrerror(errNum).c_str()),
  242. error_code::FILE_IO_ERROR);
  243. }
  244. # elif HAVE_POSIX_FALLOCATE
  245. int r = posix_fallocate(fd_, offset, length);
  246. if(r != 0) {
  247. throw DL_ABORT_EX3(r,
  248. fmt("posix_fallocate failed. cause: %s",
  249. util::safeStrerror(r).c_str()),
  250. error_code::FILE_IO_ERROR);
  251. }
  252. # else
  253. # error "no *_fallocate function available."
  254. # endif
  255. #endif // HAVE_SOME_FALLOCATE
  256. }
  257. uint64_t AbstractDiskWriter::size()
  258. {
  259. return File(filename_).size();
  260. }
  261. void AbstractDiskWriter::enableReadOnly()
  262. {
  263. readOnly_ = true;
  264. }
  265. void AbstractDiskWriter::disableReadOnly()
  266. {
  267. readOnly_ = false;
  268. }
  269. } // namespace aria2