AbstractDiskWriter.cc 5.1 KB

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