AbstractDiskWriter.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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():
  54. fd(-1),
  55. logger(LogFactory::getInstance()) {}
  56. AbstractDiskWriter::~AbstractDiskWriter()
  57. {
  58. closeFile();
  59. }
  60. void AbstractDiskWriter::openFile(const std::string& filename, uint64_t totalLength)
  61. {
  62. File f(filename);
  63. if(f.exists()) {
  64. openExistingFile(filename, totalLength);
  65. } else {
  66. initAndOpenFile(filename, totalLength);
  67. }
  68. }
  69. void AbstractDiskWriter::closeFile()
  70. {
  71. if(fd >= 0) {
  72. close(fd);
  73. fd = -1;
  74. }
  75. }
  76. void AbstractDiskWriter::openExistingFile(const std::string& filename,
  77. uint64_t totalLength)
  78. {
  79. this->filename = filename;
  80. File f(filename);
  81. if(!f.exists()) {
  82. throw DlAbortEx
  83. (StringFormat(EX_FILE_OPEN, filename.c_str(), MSG_FILE_NOT_FOUND).str());
  84. }
  85. if((fd = open(filename.c_str(), O_RDWR|O_BINARY, OPEN_MODE)) < 0) {
  86. throw DlAbortEx
  87. (StringFormat(EX_FILE_OPEN, filename.c_str(), strerror(errno)).str());
  88. }
  89. }
  90. void AbstractDiskWriter::createFile(const std::string& filename, int addFlags)
  91. {
  92. this->filename = filename;
  93. assert(filename.size());
  94. Util::mkdirs(File(filename).getDirname());
  95. if((fd = open(filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags, OPEN_MODE)) < 0) {
  96. throw DlAbortEx(StringFormat(EX_FILE_OPEN, filename.c_str(), strerror(errno)).str());
  97. }
  98. }
  99. ssize_t AbstractDiskWriter::writeDataInternal(const unsigned char* data, size_t len)
  100. {
  101. ssize_t writtenLength = 0;
  102. while((size_t)writtenLength < len) {
  103. ssize_t ret = 0;
  104. while((ret = write(fd, data+writtenLength, len-writtenLength)) == -1 && errno == EINTR);
  105. if(ret == -1) {
  106. return -1;
  107. }
  108. writtenLength += ret;
  109. }
  110. return writtenLength;
  111. }
  112. ssize_t AbstractDiskWriter::readDataInternal(unsigned char* data, size_t len)
  113. {
  114. ssize_t ret = 0;
  115. while((ret = read(fd, data, len)) == -1 && errno == EINTR);
  116. return ret;
  117. }
  118. void AbstractDiskWriter::seek(off_t offset)
  119. {
  120. if(lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  121. throw DlAbortEx
  122. (StringFormat(EX_FILE_SEEK, filename.c_str(), strerror(errno)).str());
  123. }
  124. }
  125. void AbstractDiskWriter::writeData(const unsigned char* data, size_t len, off_t offset)
  126. {
  127. seek(offset);
  128. if(writeDataInternal(data, len) < 0) {
  129. // If errno is ENOSPC(not enough space in device), throw
  130. // DownloadFailureException and abort download instantly.
  131. if(errno == ENOSPC) {
  132. throw DownloadFailureException
  133. (StringFormat(EX_FILE_WRITE, filename.c_str(), strerror(errno)).str());
  134. }
  135. throw DlAbortEx(StringFormat(EX_FILE_WRITE, filename.c_str(), strerror(errno)).str());
  136. }
  137. }
  138. ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, off_t offset)
  139. {
  140. ssize_t ret;
  141. seek(offset);
  142. if((ret = readDataInternal(data, len)) < 0) {
  143. throw DlAbortEx(StringFormat(EX_FILE_READ, filename.c_str(), strerror(errno)).str());
  144. }
  145. return ret;
  146. }
  147. void AbstractDiskWriter::truncate(uint64_t length)
  148. {
  149. if(fd == -1) {
  150. throw DlAbortEx("File not opened.");
  151. }
  152. #ifdef __MINGW32__
  153. // Since mingw32's ftruncate cannot handle over 2GB files, we use SetEndOfFile
  154. // instead.
  155. HANDLE handle = LongToHandle(_get_osfhandle(fd));
  156. seek(length);
  157. if(SetEndOfFile(handle) == 0) {
  158. throw DlAbortEx(StringFormat("SetEndOfFile failed. cause: %s",
  159. GetLastError()).str());
  160. }
  161. #else
  162. if(ftruncate(fd, length) == -1) {
  163. throw DlAbortEx(StringFormat("ftruncate failed. cause: %s",
  164. strerror(errno)).str());
  165. }
  166. #endif
  167. }
  168. // TODO the file descriptor fd must be opened before calling this function.
  169. uint64_t AbstractDiskWriter::size() const
  170. {
  171. if(fd == -1) {
  172. throw DlAbortEx("File not opened.");
  173. }
  174. a2_struct_stat fileStat;
  175. if(fstat(fd, &fileStat) < 0) {
  176. return 0;
  177. }
  178. return fileStat.st_size;
  179. }
  180. void AbstractDiskWriter::enableDirectIO()
  181. {
  182. #ifdef ENABLE_DIRECT_IO
  183. if(_directIOAllowed) {
  184. int flg;
  185. while((flg = fcntl(fd, F_GETFL)) == -1 && errno == EINTR);
  186. while(fcntl(fd, F_SETFL, flg|O_DIRECT) == -1 && errno == EINTR);
  187. }
  188. #endif // ENABLE_DIRECT_IO
  189. }
  190. void AbstractDiskWriter::disableDirectIO()
  191. {
  192. #ifdef ENABLE_DIRECT_IO
  193. int flg;
  194. while((flg = fcntl(fd, F_GETFL)) == -1 && errno == EINTR);
  195. while(fcntl(fd, F_SETFL, flg&(~O_DIRECT)) == -1 && errno == EINTR);
  196. #endif // ENABLE_DIRECT_IO
  197. }
  198. } // namespace aria2