summaryrefslogtreecommitdiff
path: root/src/AtomicFile.cpp
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2019-09-23 22:36:37 +0200
committerJoel Rosdahl <joel@rosdahl.net>2019-10-05 23:16:31 +0200
commitdba7713cb17e3ed556133f83e3a219e503335883 (patch)
treeb6afc5392ccd0ac0cab49d598e8b8612c8d50b54 /src/AtomicFile.cpp
parentd5eb883a628119c8436750f312099d4ee046de10 (diff)
downloadccache-dba7713cb17e3ed556133f83e3a219e503335883.tar.gz
Extract temporary file creation code into a function
Also, use cstdio instead of iostream since that will be needed in upcoming commits to get hold on the underlying file descriptor.
Diffstat (limited to 'src/AtomicFile.cpp')
-rw-r--r--src/AtomicFile.cpp30
1 files changed, 11 insertions, 19 deletions
diff --git a/src/AtomicFile.cpp b/src/AtomicFile.cpp
index 994b8c1b..c73d39a3 100644
--- a/src/AtomicFile.cpp
+++ b/src/AtomicFile.cpp
@@ -21,31 +21,23 @@
#include "Error.hpp"
#include "ccache.hpp"
+#include <cassert>
#include <cerrno>
#include <fmt/core.h>
#include <unistd.h>
AtomicFile::AtomicFile(const std::string& path, Mode mode) : m_path(path)
{
- char* tmp_path = x_strdup(path.c_str());
- int fd = create_tmp_fd(&tmp_path);
- m_tmp_path = tmp_path;
- m_stream.open(tmp_path,
- mode == Mode::Binary ? std::ios::out | std::ios::binary
- : std::ios::out);
- free(tmp_path);
- ::close(fd);
-
- if (!m_stream) {
- throw Error(fmt::format("failed to create {}: {}", path, strerror(errno)));
- }
+ auto fd_and_path = util::create_temp_fd(path);
+ m_stream = fdopen(fd_and_path.first, mode == Mode::Binary ? "w+b" : "w+");
+ m_tmp_path = std::move(fd_and_path.second);
}
AtomicFile::~AtomicFile()
{
- if (m_stream.is_open()) {
+ if (m_stream) {
// close() was not called so remove the lingering temporary file.
- m_stream.close();
+ fclose(m_stream);
tmp_unlink(m_tmp_path.c_str());
}
}
@@ -53,8 +45,7 @@ AtomicFile::~AtomicFile()
void
AtomicFile::write(const std::string& data)
{
- m_stream.write(data.data(), data.size());
- if (!m_stream) {
+ if (fwrite(data.data(), data.size(), 1, m_stream) != 1) {
throw Error(
fmt::format("failed to write data to {}: {}", m_path, strerror(errno)));
}
@@ -63,8 +54,7 @@ AtomicFile::write(const std::string& data)
void
AtomicFile::write(const std::vector<uint8_t>& data)
{
- m_stream.write(reinterpret_cast<const char*>(data.data()), data.size());
- if (!m_stream) {
+ if (fwrite(data.data(), data.size(), 1, m_stream) != 1) {
throw Error(
fmt::format("failed to write data to {}: {}", m_path, strerror(errno)));
}
@@ -73,7 +63,9 @@ AtomicFile::write(const std::vector<uint8_t>& data)
void
AtomicFile::close()
{
- m_stream.close();
+ assert(m_stream);
+ fclose(m_stream);
+ m_stream = nullptr;
if (x_rename(m_tmp_path.c_str(), m_path.c_str()) != 0) {
throw Error(fmt::format("failed to rename {} to {}", m_tmp_path, m_path));
}