AbstractDiskWriter.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_ = a2open(utf8ToWChar(filename_).c_str(),
  89. 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. utf8ToNative(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_ = a2open(utf8ToWChar(filename_).c_str(),
  106. O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags,
  107. OPEN_MODE)) == -1 && errno == EINTR);
  108. if(fd_ < 0) {
  109. int errNum = errno;
  110. throw DL_ABORT_EX3
  111. (errNum,
  112. fmt(EX_FILE_OPEN,
  113. utf8ToNative(filename_).c_str(),
  114. util::safeStrerror(errNum).c_str()),
  115. error_code::FILE_CREATE_ERROR);
  116. }
  117. }
  118. ssize_t AbstractDiskWriter::writeDataInternal(const unsigned char* data, size_t len)
  119. {
  120. ssize_t writtenLength = 0;
  121. while((size_t)writtenLength < len) {
  122. ssize_t ret = 0;
  123. while((ret = write(fd_, data+writtenLength, len-writtenLength)) == -1 && errno == EINTR);
  124. if(ret == -1) {
  125. return -1;
  126. }
  127. writtenLength += ret;
  128. }
  129. return writtenLength;
  130. }
  131. ssize_t AbstractDiskWriter::readDataInternal(unsigned char* data, size_t len)
  132. {
  133. ssize_t ret = 0;
  134. while((ret = read(fd_, data, len)) == -1 && errno == EINTR);
  135. return ret;
  136. }
  137. void AbstractDiskWriter::seek(off_t offset)
  138. {
  139. if(a2lseek(fd_, offset, SEEK_SET) == (off_t)-1) {
  140. int errNum = errno;
  141. throw DL_ABORT_EX2(fmt(EX_FILE_SEEK,
  142. utf8ToNative(filename_).c_str(),
  143. util::safeStrerror(errNum).c_str()),
  144. error_code::FILE_IO_ERROR);
  145. }
  146. }
  147. void AbstractDiskWriter::writeData(const unsigned char* data, size_t len, off_t offset)
  148. {
  149. seek(offset);
  150. if(writeDataInternal(data, len) < 0) {
  151. int errNum = errno;
  152. // If errno is ENOSPC(not enough space in device), throw
  153. // DownloadFailureException and abort download instantly.
  154. if(errNum == ENOSPC) {
  155. throw DOWNLOAD_FAILURE_EXCEPTION3
  156. (errNum,
  157. fmt(EX_FILE_WRITE,
  158. utf8ToNative(filename_).c_str(),
  159. util::safeStrerror(errNum).c_str()),
  160. error_code::NOT_ENOUGH_DISK_SPACE);
  161. } else {
  162. throw DL_ABORT_EX3
  163. (errNum,
  164. fmt(EX_FILE_WRITE,
  165. utf8ToNative(filename_).c_str(),
  166. util::safeStrerror(errNum).c_str()),
  167. error_code::FILE_IO_ERROR);
  168. }
  169. }
  170. }
  171. ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, off_t offset)
  172. {
  173. ssize_t ret;
  174. seek(offset);
  175. if((ret = readDataInternal(data, len)) < 0) {
  176. int errNum = errno;
  177. throw DL_ABORT_EX3
  178. (errNum,
  179. fmt(EX_FILE_READ,
  180. utf8ToNative(filename_).c_str(),
  181. util::safeStrerror(errNum).c_str()),
  182. error_code::FILE_IO_ERROR);
  183. }
  184. return ret;
  185. }
  186. void AbstractDiskWriter::truncate(uint64_t length)
  187. {
  188. if(fd_ == -1) {
  189. throw DL_ABORT_EX("File not opened.");
  190. }
  191. #ifdef __MINGW32__
  192. // Since mingw32's ftruncate cannot handle over 2GB files, we use SetEndOfFile
  193. // instead.
  194. HANDLE handle = LongToHandle(_get_osfhandle(fd_));
  195. seek(length);
  196. if(SetEndOfFile(handle) == 0) {
  197. throw DL_ABORT_EX2(fmt("SetEndOfFile failed. cause: %lx",
  198. GetLastError()),
  199. error_code::FILE_IO_ERROR);
  200. }
  201. #else
  202. if(ftruncate(fd_, length) == -1) {
  203. int errNum = errno;
  204. throw DL_ABORT_EX3(errNum,
  205. fmt("ftruncate failed. cause: %s",
  206. util::safeStrerror(errNum).c_str()),
  207. error_code::FILE_IO_ERROR);
  208. }
  209. #endif
  210. }
  211. void AbstractDiskWriter::allocate(off_t offset, uint64_t length)
  212. {
  213. #ifdef HAVE_SOME_FALLOCATE
  214. if(fd_ == -1) {
  215. throw DL_ABORT_EX("File not yet opened.");
  216. }
  217. # ifdef __MINGW32__
  218. LARGE_INTEGER fileLength;
  219. fileLength.QuadPart = offset+length;
  220. HANDLE handle = LongToHandle(_get_osfhandle(fd_));
  221. int r;
  222. r = SetFilePointerEx(handle, fileLength, 0, FILE_BEGIN);
  223. if(r == 0) {
  224. throw DL_ABORT_EX2(fmt("SetFilePointerEx failed. cause: %lx",
  225. GetLastError()),
  226. error_code::FILE_IO_ERROR);
  227. }
  228. r = SetEndOfFile(handle);
  229. if(r == 0) {
  230. throw DL_ABORT_EX2(fmt("SetEndOfFile failed. cause: %lx",
  231. GetLastError()),
  232. error_code::FILE_IO_ERROR);
  233. }
  234. # elif HAVE_FALLOCATE
  235. // For linux, we use fallocate to detect file system supports
  236. // fallocate or not.
  237. int r;
  238. while((r = fallocate(fd_, 0, offset, length)) == -1 && errno == EINTR);
  239. int errNum = errno;
  240. if(r == -1) {
  241. throw DL_ABORT_EX3(errNum,
  242. fmt("fallocate failed. cause: %s",
  243. util::safeStrerror(errNum).c_str()),
  244. error_code::FILE_IO_ERROR);
  245. }
  246. # elif HAVE_POSIX_FALLOCATE
  247. int r = posix_fallocate(fd_, offset, length);
  248. if(r != 0) {
  249. throw DL_ABORT_EX3(r,
  250. fmt("posix_fallocate failed. cause: %s",
  251. util::safeStrerror(r).c_str()),
  252. error_code::FILE_IO_ERROR);
  253. }
  254. # else
  255. # error "no *_fallocate function available."
  256. # endif
  257. #endif // HAVE_SOME_FALLOCATE
  258. }
  259. uint64_t AbstractDiskWriter::size()
  260. {
  261. return File(filename_).size();
  262. }
  263. void AbstractDiskWriter::enableReadOnly()
  264. {
  265. readOnly_ = true;
  266. }
  267. void AbstractDiskWriter::disableReadOnly()
  268. {
  269. readOnly_ = false;
  270. }
  271. } // namespace aria2