AbstractDiskWriter.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "DlAbortEx.h"
  37. #include "File.h"
  38. #include "Util.h"
  39. #include "message.h"
  40. #include "LogFactory.h"
  41. #include "a2io.h"
  42. #include <unistd.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <errno.h>
  46. #include <fcntl.h>
  47. AbstractDiskWriter::AbstractDiskWriter():
  48. fd(-1),
  49. fileAllocator(0),
  50. glowFileAllocator(0),
  51. logger(LogFactory::getInstance())
  52. {}
  53. AbstractDiskWriter::~AbstractDiskWriter()
  54. {
  55. closeFile();
  56. }
  57. void AbstractDiskWriter::openFile(const string& filename, int64_t totalLength) {
  58. File f(filename);
  59. if(f.exists()) {
  60. openExistingFile(filename);
  61. } else {
  62. initAndOpenFile(filename, totalLength);
  63. }
  64. }
  65. void AbstractDiskWriter::closeFile() {
  66. if(fd >= 0) {
  67. close(fd);
  68. fd = -1;
  69. }
  70. }
  71. void AbstractDiskWriter::openExistingFile(const string& filename, int64_t totalLength) {
  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. if(f.size() < totalLength) {
  81. if(!fileAllocator.isNull()) {
  82. logger->notice(MSG_ALLOCATING_FILE,
  83. filename.c_str(),
  84. Util::ullitos(totalLength).c_str());
  85. glowFileAllocator->allocate(fd, totalLength);
  86. }
  87. }
  88. }
  89. void AbstractDiskWriter::createFile(const string& filename, int32_t addFlags) {
  90. this->filename = filename;
  91. // TODO proper filename handling needed
  92. assert(filename.size());
  93. // if(filename.empty()) {
  94. // filename = "index.html";
  95. // }
  96. if((fd = open(filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags, OPEN_MODE)) < 0) {
  97. throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
  98. }
  99. }
  100. int32_t AbstractDiskWriter::writeDataInternal(const char* data, int32_t len) {
  101. return write(fd, data, len);
  102. }
  103. int32_t AbstractDiskWriter::readDataInternal(char* data, int32_t len) {
  104. return read(fd, data, len);
  105. }
  106. void AbstractDiskWriter::seek(int64_t offset) {
  107. if(offset != lseek(fd, offset, SEEK_SET)) {
  108. throw new DlAbortEx(EX_FILE_SEEK, filename.c_str(), strerror(errno));
  109. }
  110. }
  111. void AbstractDiskWriter::writeData(const char* data, int32_t len, int64_t offset) {
  112. seek(offset);
  113. if(writeDataInternal(data, len) < 0) {
  114. throw new DlAbortEx(EX_FILE_WRITE, filename.c_str(), strerror(errno));
  115. }
  116. }
  117. int32_t AbstractDiskWriter::readData(char* data, int32_t len, int64_t offset) {
  118. int32_t ret;
  119. seek(offset);
  120. if((ret = readDataInternal(data, len)) < 0) {
  121. throw new DlAbortEx(EX_FILE_READ, filename.c_str(), strerror(errno));
  122. }
  123. return ret;
  124. }
  125. void AbstractDiskWriter::truncate(int64_t length)
  126. {
  127. ftruncate(fd, length);
  128. }
  129. // TODO the file descriptor fd must be opened before calling this function.
  130. int64_t AbstractDiskWriter::size() const
  131. {
  132. struct stat fileStat;
  133. if(fstat(fd, &fileStat) < 0) {
  134. return 0;
  135. }
  136. return fileStat.st_size;
  137. }