AbstractDiskWriter.cc 5.1 KB

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