FileReader.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "config.h"
  27. using namespace zipper;
  28. const timeval zipper::s_now = {0,0};
  29. class FileReader::FileReaderImpl
  30. {
  31. public:
  32. FileReaderImpl(const std::string& filename) :
  33. m_filename(filename),
  34. m_fd(-1),
  35. m_closeOnExit(true)
  36. {
  37. m_fd = ::open(filename.c_str(), O_RDONLY | O_BINARY);
  38. if (m_fd < 0)
  39. {
  40. std::string errMsg(zipper::strerror(errno));
  41. std::stringstream message;
  42. message << "Could not open file \"" << filename << "\": " <<
  43. errMsg;
  44. throw IOException(message.str());
  45. }
  46. initStats();
  47. }
  48. FileReaderImpl(const std::string& filename, int fd, bool closeFd) :
  49. m_filename(filename),
  50. m_fd(fd),
  51. m_closeOnExit(closeFd)
  52. {
  53. initStats();
  54. }
  55. ~FileReaderImpl()
  56. {
  57. close();
  58. }
  59. const std::string& getSourceName() const { return m_filename; }
  60. const timeval& getModTime() const { return m_modTime; }
  61. zsize_t getSize() const { return m_size; }
  62. virtual void readData(
  63. zsize_t offset, zsize_t bytes, uint8_t* dest
  64. ) const
  65. {
  66. assert(m_fd >= 0);
  67. zsize_t bytesRead(0);
  68. while(bytesRead < bytes)
  69. {
  70. ssize_t currentBytes(
  71. pread(
  72. m_fd,
  73. dest + bytesRead,
  74. bytes - bytesRead,
  75. offset + bytesRead)
  76. );
  77. if (currentBytes > 0)
  78. {
  79. bytesRead += static_cast<zsize_t>(currentBytes);
  80. }
  81. else if (currentBytes == 0)
  82. {
  83. throw FormatException("Unexpected end-of-file");
  84. }
  85. else if ((currentBytes < 0) && (errno != EINTR))
  86. {
  87. std::string errMsg(zipper::strerror(errno));
  88. throw IOException(errMsg);
  89. }
  90. }
  91. }
  92. private:
  93. void initStats()
  94. {
  95. // If we fail here, we need to essentially run the dtor manually.
  96. // initStats is called from the constructors, and so the dtor will
  97. // NOT run if an exception is thrown.
  98. struct stat buf;
  99. int result(fstat(m_fd, &buf));
  100. if (result != 0)
  101. {
  102. int errnoLocal = errno;
  103. close();
  104. std::string errMsg(zipper::strerror(errnoLocal));
  105. std::stringstream message;
  106. message << "Could not get filesize for file " <<
  107. "\"" << m_filename << "\": " << errMsg;
  108. throw IOException(message.str());
  109. }
  110. else
  111. {
  112. m_size = buf.st_size;
  113. m_modTime.tv_sec = buf.st_mtime;
  114. m_modTime.tv_usec = 0;
  115. }
  116. }
  117. void close()
  118. {
  119. if ((m_fd >= 0) && m_closeOnExit)
  120. {
  121. ::close(m_fd);
  122. m_fd = -1;
  123. }
  124. }
  125. std::string m_filename;
  126. int m_fd;
  127. bool m_closeOnExit;
  128. zsize_t m_size;
  129. timeval m_modTime;
  130. };
  131. FileReader::FileReader(const std::string& filename) :
  132. m_impl(new FileReaderImpl(filename))
  133. {
  134. }
  135. FileReader::FileReader(const std::string& filename, int fd, bool closeFd) :
  136. m_impl(new FileReaderImpl(filename, fd, closeFd))
  137. {
  138. }
  139. FileReader::~FileReader()
  140. {
  141. delete m_impl;
  142. }
  143. const std::string&
  144. FileReader::getSourceName() const
  145. {
  146. return m_impl->getSourceName();
  147. }
  148. const timeval&
  149. FileReader::getModTime() const
  150. {
  151. return m_impl->getModTime();
  152. }
  153. zsize_t
  154. FileReader::getSize() const
  155. {
  156. return m_impl->getSize();
  157. }
  158. void
  159. FileReader::readData(
  160. zsize_t offset, zsize_t bytes, uint8_t* dest
  161. ) const
  162. {
  163. return m_impl->readData(offset, bytes, dest);
  164. }