AbstractDiskWriter.cc 5.6 KB

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