dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
AtomicFileWriter.hpp
1// SPDX-FileCopyrightText: 2026 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <span>
13#include <string_view>
14
15// Project includes
16#include "EnumString.hpp"
17#include "FileSystem.hpp"
18#include "Log.hpp"
19#include "PrivateState.hpp"
20#include "Utility.hpp"
21
22namespace dfx::Utils
23{
64{
65 DFX_PRIVATE_STATE(AtomicFileWriter, DFX_UTILITIES_LOGGER())
66
67public:
81
83 enum class Backend
84 {
87 };
88
90 struct Options
91 {
95 mode_t mode = 0644;
96
98 bool fsyncFile = true;
99
102 bool fsyncDir = true;
103
106 };
107
108public:
114 explicit AtomicFileWriter(fs::path p);
119 AtomicFileWriter(fs::path p, Options options);
121
123 fs::path const & path() const noexcept;
124
126 State state() const noexcept;
128 bool isOpen() const noexcept { return state() == State::Open; }
130 bool isDiscarded() const noexcept { return state() == State::Discarded; }
132 bool isPublished() const noexcept { return isOneOf(state(), { State::Published, State::Durable }); }
134 bool isDurable() const noexcept { return state() == State::Durable; }
135
136public:
145 void write(std::byte const * data, std::size_t len)
146 { write(std::string_view(reinterpret_cast<std::string_view::value_type const *>(data), len)); }
147
148
156 void write(std::span<std::byte const> data)
157 { write(data.data(), data.size()); }
158
159
168 void write(std::string_view data);
169
170public:
180 void commit();
181
186 void discard() noexcept;
187};
188} // !namespace dfx::Utils
189
Macro-based enum <-> string utilities for dfx.
#define DFX_DECLARE_ENUM_STRING_FUNCTIONS(E)
Declare the enum string API (and std::formatter) for enum type E.
Definition EnumString.hpp:327
Macros to implement the Private State (PIMPL-like) idiom with value semantics.
#define DFX_PRIVATE_STATE(Class, logger)
Declare a private implementation state for a class.
Definition PrivateState.hpp:52
#define DFX_PRIVATE_STATE_DECLARE_RULE_OF_5(Class)
Declare Rule-of-5 special member functions using the private state pattern.
Definition PrivateState.hpp:82
Miscellaneous small utility helpers (strings, parsing, formatting).
State state() const noexcept
Returns the current state.
void discard() noexcept
Discard the staged file and release resources.
AtomicFileWriter(fs::path p)
Construct an atomic writer targeting p with default options.
bool isPublished() const noexcept
Convenience: true if the file has been published (atomically visible).
Definition AtomicFileWriter.hpp:132
State
Current lifecycle state of the writer.
Definition AtomicFileWriter.hpp:75
@ Discarded
The writer has been discarded: resources are released and nothing new is published.
Definition AtomicFileWriter.hpp:77
@ Published
The target path has been replaced/created atomically (visible in the filesystem).
Definition AtomicFileWriter.hpp:78
@ Open
The writer is active: data can be written and can be committed or discarded.
Definition AtomicFileWriter.hpp:76
@ Durable
Published + parent directory sync succeeded (when enabled), providing durability for the rename/link.
Definition AtomicFileWriter.hpp:79
void write(std::span< std::byte const > data)
Append raw bytes to the staged file.
Definition AtomicFileWriter.hpp:156
bool isDiscarded() const noexcept
Convenience: true if state() == State::Discarded.
Definition AtomicFileWriter.hpp:130
AtomicFileWriter(fs::path p, Options options)
Construct an atomic writer targeting p with explicit options.
void write(std::string_view data)
Append text/binary data to the staged file.
fs::path const & path() const noexcept
Returns the target path.
void commit()
Publish the staged file to the target path.
Backend
Filesystem operation used to create the temporary file.
Definition AtomicFileWriter.hpp:84
@ Mktemp
Use mkostemp function to create the temporary file.
Definition AtomicFileWriter.hpp:86
@ TmpFile
Use open(O_TMPFILE) preferably (will fallback to Mktemp if EOPNOTSUPP).
Definition AtomicFileWriter.hpp:85
bool isOpen() const noexcept
Convenience: true if state() == State::Open.
Definition AtomicFileWriter.hpp:128
bool isDurable() const noexcept
Convenience: true if the file publish operation is durable.
Definition AtomicFileWriter.hpp:134
void write(std::byte const *data, std::size_t len)
Append raw bytes to the staged file.
Definition AtomicFileWriter.hpp:145
Definition SystemConfigCommandHandler.hpp:15
bool isOneOf(T1 val, std::initializer_list< T2 const > list)
Test whether val is equal to one of the elements in list.
Definition Utility.hpp:116
Definition Message.hpp:21
Options controlling publish permissions and durability.
Definition AtomicFileWriter.hpp:91
mode_t mode
Permissions applied to the published file.
Definition AtomicFileWriter.hpp:95
Backend backend
Backend that will be used to create the temporary file.
Definition AtomicFileWriter.hpp:105
bool fsyncDir
If true, attempt to sync the parent directory after publishing. This is what makes the directory entr...
Definition AtomicFileWriter.hpp:102
bool fsyncFile
If true, sync the staged file contents before publishing.
Definition AtomicFileWriter.hpp:98