AbstractDiskWriter.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. if(File(filename_).exists()) {
  66. openExistingFile(totalLength);
  67. } else {
  68. initAndOpenFile(totalLength);
  69. }
  70. }
  71. void AbstractDiskWriter::closeFile()
  72. {
  73. if(fd_ >= 0) {
  74. close(fd_);
  75. fd_ = -1;
  76. }
  77. }
  78. void AbstractDiskWriter::openExistingFile(uint64_t totalLength)
  79. {
  80. if(!File(filename_).exists()) {
  81. throw DL_ABORT_EX
  82. (StringFormat(EX_FILE_OPEN, filename_.c_str(), MSG_FILE_NOT_FOUND).str());
  83. }
  84. int flags = O_BINARY;
  85. if(readOnly_) {
  86. flags |= O_RDONLY;
  87. } else {
  88. flags |= O_RDWR;
  89. }
  90. if((fd_ = open(filename_.c_str(), flags, OPEN_MODE)) < 0) {
  91. throw DL_ABORT_EX
  92. (StringFormat(EX_FILE_OPEN, filename_.c_str(), strerror(errno)).str());
  93. }
  94. }
  95. void AbstractDiskWriter::createFile(int addFlags)
  96. {
  97. assert(!filename_.empty());
  98. util::mkdirs(File(filename_).getDirname());
  99. if((fd_ = open(filename_.c_str(), O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags,
  100. OPEN_MODE)) < 0) {
  101. throw DL_ABORT_EX(StringFormat(EX_FILE_OPEN,
  102. filename_.c_str(), strerror(errno)).str());
  103. }
  104. }
  105. ssize_t AbstractDiskWriter::writeDataInternal(const unsigned char* data, size_t len)
  106. {
  107. ssize_t writtenLength = 0;
  108. while((size_t)writtenLength < len) {
  109. ssize_t ret = 0;
  110. while((ret = write(fd_, data+writtenLength, len-writtenLength)) == -1 && errno == EINTR);
  111. if(ret == -1) {
  112. return -1;
  113. }
  114. writtenLength += ret;
  115. }
  116. return writtenLength;
  117. }
  118. ssize_t AbstractDiskWriter::readDataInternal(unsigned char* data, size_t len)
  119. {
  120. ssize_t ret = 0;
  121. while((ret = read(fd_, data, len)) == -1 && errno == EINTR);
  122. return ret;
  123. }
  124. void AbstractDiskWriter::seek(off_t offset)
  125. {
  126. if(a2lseek(fd_, offset, SEEK_SET) == (off_t)-1) {
  127. throw DL_ABORT_EX
  128. (StringFormat(EX_FILE_SEEK, filename_.c_str(), strerror(errno)).str());
  129. }
  130. }
  131. void AbstractDiskWriter::writeData(const unsigned char* data, size_t len, off_t offset)
  132. {
  133. seek(offset);
  134. if(writeDataInternal(data, len) < 0) {
  135. // If errno is ENOSPC(not enough space in device), throw
  136. // DownloadFailureException and abort download instantly.
  137. if(errno == ENOSPC) {
  138. throw DOWNLOAD_FAILURE_EXCEPTION
  139. (StringFormat(EX_FILE_WRITE, filename_.c_str(), strerror(errno)).str());
  140. }
  141. throw DL_ABORT_EX(StringFormat(EX_FILE_WRITE,
  142. filename_.c_str(), strerror(errno)).str());
  143. }
  144. }
  145. ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, off_t offset)
  146. {
  147. ssize_t ret;
  148. seek(offset);
  149. if((ret = readDataInternal(data, len)) < 0) {
  150. throw DL_ABORT_EX(StringFormat(EX_FILE_READ,
  151. filename_.c_str(), strerror(errno)).str());
  152. }
  153. return ret;
  154. }
  155. void AbstractDiskWriter::truncate(uint64_t length)
  156. {
  157. if(fd_ == -1) {
  158. throw DL_ABORT_EX("File not opened.");
  159. }
  160. #ifdef __MINGW32__
  161. // Since mingw32's ftruncate cannot handle over 2GB files, we use SetEndOfFile
  162. // instead.
  163. HANDLE handle = LongToHandle(_get_osfhandle(fd_));
  164. seek(length);
  165. if(SetEndOfFile(handle) == 0) {
  166. throw DL_ABORT_EX(StringFormat("SetEndOfFile failed. cause: %s",
  167. GetLastError()).str());
  168. }
  169. #else
  170. if(ftruncate(fd_, length) == -1) {
  171. throw DL_ABORT_EX(StringFormat("ftruncate failed. cause: %s",
  172. strerror(errno)).str());
  173. }
  174. #endif
  175. }
  176. #ifdef HAVE_POSIX_FALLOCATE
  177. void AbstractDiskWriter::allocate(off_t offset, uint64_t length)
  178. {
  179. if(fd_ == -1) {
  180. throw DL_ABORT_EX("File not yet opened.");
  181. }
  182. int r = posix_fallocate(fd_, offset, length);
  183. if(r != 0) {
  184. throw DL_ABORT_EX(StringFormat("posix_fallocate failed. cause: %s",
  185. strerror(r)).str());
  186. }
  187. }
  188. #endif // HAVE_POSIX_FALLOCATE
  189. uint64_t AbstractDiskWriter::size()
  190. {
  191. return File(filename_).size();
  192. }
  193. void AbstractDiskWriter::enableDirectIO()
  194. {
  195. #ifdef ENABLE_DIRECT_IO
  196. if(directIOAllowed_) {
  197. int flg;
  198. while((flg = fcntl(fd_, F_GETFL)) == -1 && errno == EINTR);
  199. while(fcntl(fd_, F_SETFL, flg|O_DIRECT) == -1 && errno == EINTR);
  200. }
  201. #endif // ENABLE_DIRECT_IO
  202. }
  203. void AbstractDiskWriter::disableDirectIO()
  204. {
  205. #ifdef ENABLE_DIRECT_IO
  206. int flg;
  207. while((flg = fcntl(fd_, F_GETFL)) == -1 && errno == EINTR);
  208. while(fcntl(fd_, F_SETFL, flg&(~O_DIRECT)) == -1 && errno == EINTR);
  209. #endif // ENABLE_DIRECT_IO
  210. }
  211. void AbstractDiskWriter::enableReadOnly()
  212. {
  213. readOnly_ = true;
  214. }
  215. void AbstractDiskWriter::disableReadOnly()
  216. {
  217. readOnly_ = false;
  218. }
  219. } // namespace aria2