AbstractDiskWriter.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <unistd.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <errno.h>
  45. #include <fcntl.h>
  46. AbstractDiskWriter::AbstractDiskWriter():
  47. fd(0),
  48. fileAllocator(0),
  49. logger(LogFactory::getInstance())
  50. {}
  51. AbstractDiskWriter::~AbstractDiskWriter()
  52. {
  53. closeFile();
  54. }
  55. void AbstractDiskWriter::openFile(const string& filename, uint64_t totalLength) {
  56. File f(filename);
  57. if(f.exists()) {
  58. openExistingFile(filename);
  59. } else {
  60. initAndOpenFile(filename, totalLength);
  61. }
  62. }
  63. void AbstractDiskWriter::closeFile() {
  64. if(fd >= 0) {
  65. close(fd);
  66. fd = -1;
  67. }
  68. }
  69. void AbstractDiskWriter::openExistingFile(const string& filename) {
  70. this->filename = filename;
  71. File f(filename);
  72. if(!f.isFile()) {
  73. throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), "file not found");
  74. }
  75. if((fd = open(filename.c_str(), O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
  76. throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
  77. }
  78. }
  79. void AbstractDiskWriter::createFile(const string& filename, int32_t addFlags) {
  80. this->filename = filename;
  81. // TODO proper filename handling needed
  82. assert(filename.size());
  83. // if(filename.empty()) {
  84. // filename = "index.html";
  85. // }
  86. if((fd = open(filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|addFlags, S_IRUSR|S_IWUSR)) < 0) {
  87. throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
  88. }
  89. }
  90. int32_t AbstractDiskWriter::writeDataInternal(const char* data, uint32_t len) {
  91. return write(fd, data, len);
  92. }
  93. int AbstractDiskWriter::readDataInternal(char* data, uint32_t len) {
  94. return read(fd, data, len);
  95. }
  96. string AbstractDiskWriter::messageDigest(int64_t offset, uint64_t length,
  97. const MessageDigestContext::DigestAlgo& algo)
  98. {
  99. #ifdef ENABLE_MESSAGE_DIGEST
  100. MessageDigestContext ctx(algo);
  101. ctx.digestInit();
  102. int32_t BUFSIZE = 16*1024;
  103. char buf[BUFSIZE];
  104. for(uint64_t i = 0; i < length/BUFSIZE; i++) {
  105. int32_t rs = readData(buf, BUFSIZE, offset);
  106. if(BUFSIZE != readData(buf, BUFSIZE, offset)) {
  107. throw new DlAbortEx(EX_FILE_SHA1SUM, filename.c_str(), strerror(errno));
  108. }
  109. ctx.digestUpdate(buf, BUFSIZE);
  110. offset += BUFSIZE;
  111. }
  112. int32_t r = length%BUFSIZE;
  113. if(r > 0) {
  114. int32_t rs = readData(buf, r, offset);
  115. if(r != readData(buf, r, offset)) {
  116. throw new DlAbortEx(EX_FILE_SHA1SUM, filename.c_str(), strerror(errno));
  117. }
  118. ctx.digestUpdate(buf, r);
  119. }
  120. unsigned char hashValue[20];
  121. ctx.digestFinal(hashValue);
  122. return Util::toHex(hashValue, 20);
  123. #else
  124. return "";
  125. #endif // ENABLE_MESSAGE_DIGEST
  126. }
  127. void AbstractDiskWriter::seek(int64_t offset) {
  128. if(offset != lseek(fd, offset, SEEK_SET)) {
  129. throw new DlAbortEx(EX_FILE_SEEK, filename.c_str(), strerror(errno));
  130. }
  131. }
  132. void AbstractDiskWriter::writeData(const char* data, uint32_t len, int64_t offset) {
  133. seek(offset);
  134. if(writeDataInternal(data, len) < 0) {
  135. throw new DlAbortEx(EX_FILE_WRITE, filename.c_str(), strerror(errno));
  136. }
  137. }
  138. int AbstractDiskWriter::readData(char* data, uint32_t len, int64_t offset) {
  139. int32_t ret;
  140. seek(offset);
  141. if((ret = readDataInternal(data, len)) < 0) {
  142. throw new DlAbortEx(EX_FILE_READ, filename.c_str(), strerror(errno));
  143. }
  144. return ret;
  145. }