AbstractDiskWriter.cc 5.3 KB

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