FileWriter.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <algorithm>
  20. #include <cassert>
  21. #include <sstream>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <utime.h>
  27. #include <errno.h>
  28. #include "config.h"
  29. using namespace zipper;
  30. class FileWriter::FileWriterImpl
  31. {
  32. public:
  33. FileWriterImpl(
  34. const std::string& filename,
  35. mode_t createPermissions,
  36. const timeval& modTime
  37. ) :
  38. m_filename(filename),
  39. m_modTime(modTime),
  40. m_fd(-1),
  41. m_closeOnExit(true),
  42. m_setModTimeOnExit(true)
  43. {
  44. m_fd =
  45. ::open(
  46. filename.c_str(),
  47. O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
  48. createPermissions);
  49. if (m_fd < 0)
  50. {
  51. std::string errMsg(zipper::strerror(errno));
  52. std::stringstream message;
  53. message << "Could not open file \"" << filename << "\": " <<
  54. errMsg;
  55. throw IOException(message.str());
  56. }
  57. }
  58. FileWriterImpl(const std::string& filename, int fd, bool closeFd) :
  59. m_filename(filename),
  60. m_fd(fd),
  61. m_closeOnExit(closeFd),
  62. m_setModTimeOnExit(false)
  63. {
  64. }
  65. ~FileWriterImpl()
  66. {
  67. close();
  68. if (m_setModTimeOnExit)
  69. {
  70. struct timeval times[2];
  71. if (s_now.tv_sec == m_modTime.tv_sec)
  72. {
  73. gettimeofday(&times[0], NULL);
  74. times[1] = times[0];
  75. }
  76. else
  77. {
  78. times[0] = m_modTime;
  79. times[1] = m_modTime;
  80. }
  81. utimes(m_filename.c_str(), times);
  82. }
  83. }
  84. virtual void writeData(
  85. zsize_t offset, zsize_t bytes, const uint8_t* data
  86. ) const
  87. {
  88. assert(m_fd >= 0);
  89. zsize_t bytesWritten(0);
  90. while(bytesWritten < bytes)
  91. {
  92. ssize_t currentBytes(
  93. pwrite(
  94. m_fd,
  95. data + bytesWritten,
  96. bytes - bytesWritten,
  97. offset + bytesWritten)
  98. );
  99. if (currentBytes >= 0)
  100. {
  101. bytesWritten += static_cast<zsize_t>(currentBytes);
  102. }
  103. else if ((currentBytes < 0) && (errno != EINTR))
  104. {
  105. std::string errMsg(zipper::strerror(errno));
  106. throw IOException(errMsg);
  107. }
  108. }
  109. }
  110. zsize_t getSize() const
  111. {
  112. assert(m_fd >= 0);
  113. zsize_t result(lseek(m_fd, 0, SEEK_END));
  114. return result;
  115. }
  116. private:
  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. timeval m_modTime;
  127. int m_fd;
  128. bool m_closeOnExit;
  129. bool m_setModTimeOnExit;
  130. };
  131. FileWriter::FileWriter(
  132. const std::string& filename,
  133. mode_t createPermissions,
  134. const timeval& modTime) :
  135. m_impl(new FileWriterImpl(filename, createPermissions, modTime))
  136. {
  137. }
  138. FileWriter::FileWriter(const std::string& filename, int fd, bool closeFd) :
  139. m_impl(new FileWriterImpl(filename, fd, closeFd))
  140. {
  141. }
  142. FileWriter::~FileWriter()
  143. {
  144. delete m_impl;
  145. }
  146. void
  147. FileWriter::writeData(zsize_t offset, zsize_t bytes, const uint8_t* data)
  148. {
  149. m_impl->writeData(offset, bytes, data);
  150. }
  151. zsize_t
  152. FileWriter::getSize() const
  153. {
  154. return m_impl->getSize();
  155. }