AbstractDiskWriter.cc 7.9 KB

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