FileReader.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2011 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of libzipper.
  4. //
  5. // libzipper is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // libzipper is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with libzipper. If not, see <http://www.gnu.org/licenses/>.
  17. #include "zipper.hh"
  18. #include "strerror.hh"
  19. #include <cassert>
  20. #include <sstream>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. using namespace zipper;
  27. const timeval zipper::s_now = {0,0};
  28. class FileReader::FileReaderImpl
  29. {
  30. public:
  31. FileReaderImpl(const std::string& filename) :
  32. m_filename(filename),
  33. m_fd(-1),
  34. m_closeOnExit(true)
  35. {
  36. m_fd = ::open(filename.c_str(), O_RDONLY);
  37. if (m_fd < 0)
  38. {
  39. std::string errMsg(zipper::strerror(errno));
  40. std::stringstream message;
  41. message << "Could not open file \"" << filename << "\": " <<
  42. errMsg;
  43. throw IOException(message.str());
  44. }
  45. initStats();
  46. }
  47. FileReaderImpl(const std::string& filename, int fd, bool closeFd) :
  48. m_filename(filename),
  49. m_fd(fd),
  50. m_closeOnExit(closeFd)
  51. {
  52. initStats();
  53. }
  54. ~FileReaderImpl()
  55. {
  56. close();
  57. }
  58. const std::string& getSourceName() const { return m_filename; }
  59. const timeval& getModTime() const { return m_modTime; }
  60. zsize_t getSize() const { return m_size; }
  61. virtual void readData(
  62. zsize_t offset, zsize_t bytes, uint8_t* dest
  63. ) const
  64. {
  65. assert(m_fd >= 0);
  66. zsize_t bytesRead(0);
  67. while(bytesRead < bytes)
  68. {
  69. ssize_t currentBytes(
  70. pread(
  71. m_fd,
  72. dest + bytesRead,
  73. bytes - bytesRead,
  74. offset + bytesRead)
  75. );
  76. if (currentBytes > 0)
  77. {
  78. bytesRead += static_cast<zsize_t>(currentBytes);
  79. }
  80. else if (currentBytes == 0)
  81. {
  82. throw FormatException("Unexpected end-of-file");
  83. }
  84. else if ((currentBytes < 0) && (errno != EINTR))
  85. {
  86. std::string errMsg(zipper::strerror(errno));
  87. throw IOException(errMsg);
  88. }
  89. }
  90. }
  91. private:
  92. void initStats()
  93. {
  94. // If we fail here, we need to essentially run the dtor manually.
  95. // initStats is called from the constructors, and so the dtor will
  96. // NOT run if an exception is thrown.
  97. struct stat buf;
  98. int result(fstat(m_fd, &buf));
  99. if (result != 0)
  100. {
  101. int errnoLocal = errno;
  102. close();
  103. std::string errMsg(zipper::strerror(errnoLocal));
  104. std::stringstream message;
  105. message << "Could not get filesize for file " <<
  106. "\"" << m_filename << "\": " << errMsg;
  107. throw IOException(message.str());
  108. }
  109. else
  110. {
  111. m_size = buf.st_size;
  112. m_modTime.tv_sec = buf.st_mtime;
  113. m_modTime.tv_usec = 0;
  114. }
  115. }
  116. void close()
  117. {
  118. if ((m_fd >= 0) && m_closeOnExit)
  119. {
  120. ::close(m_fd);
  121. m_fd = -1;
  122. }
  123. }
  124. std::string m_filename;
  125. int m_fd;
  126. bool m_closeOnExit;
  127. zsize_t m_size;
  128. timeval m_modTime;
  129. };
  130. FileReader::FileReader(const std::string& filename) :
  131. m_impl(new FileReaderImpl(filename))
  132. {
  133. }
  134. FileReader::FileReader(const std::string& filename, int fd, bool closeFd) :
  135. m_impl(new FileReaderImpl(filename, fd, closeFd))
  136. {
  137. }
  138. FileReader::~FileReader()
  139. {
  140. delete m_impl;
  141. }
  142. const std::string&
  143. FileReader::getSourceName() const
  144. {
  145. return m_impl->getSourceName();
  146. }
  147. const timeval&
  148. FileReader::getModTime() const
  149. {
  150. return m_impl->getModTime();
  151. }
  152. zsize_t
  153. FileReader::getSize() const
  154. {
  155. return m_impl->getSize();
  156. }
  157. void
  158. FileReader::readData(
  159. zsize_t offset, zsize_t bytes, uint8_t* dest
  160. ) const
  161. {
  162. return m_impl->readData(offset, bytes, dest);
  163. }