game_launcher
1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
atomic_write.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// ==============================================================================
3
// atomic_write.h
4
// ==============================================================================
5
// Durable atomic file replacement.
6
//
7
// `std::ofstream out(path); out << contents;` is the obvious pattern for
8
// writing config/state files, and it is *wrong* for any data the user
9
// would miss after a power loss. A direct overwrite leaves the previous
10
// contents truncated; a flush() only pushes user-space buffers to the
11
// kernel, not to disk; even after close(), an unflushed page-cache write
12
// can be lost.
13
//
14
// The canonical safe pattern for POSIX is:
15
// 1. write to a sibling `.tmp` file under the same directory
16
// 2. fsync(fd) so the data hits stable storage
17
// 3. rename(tmp, target) — atomic per rename(2)
18
// 4. fsync(parent_dirfd) so the rename itself is durable
19
//
20
// References:
21
// https://man7.org/linux/man-pages/man2/rename.2.html
22
// https://man7.org/linux/man-pages/man2/fsync.2.html
23
// https://lwn.net/Articles/322823/ (LWN: "Ext4, fsync, and renames")
24
// POSIX.1-2017 §3.3.149 (atomicity of rename within a single filesystem)
25
//
26
// On Windows the equivalent is `MoveFileExW(MOVEFILE_REPLACE_EXISTING |
27
// MOVEFILE_WRITE_THROUGH)`; not implemented here yet — Windows callers
28
// fall back to a non-fsync'd write on that platform.
29
// ==============================================================================
30
31
#pragma once
32
33
#include <filesystem>
34
#include <string>
35
#include <string_view>
36
37
namespace
core
38
{
39
40
/// Atomically and durably write @p contents to @p targetPath.
41
///
42
/// Returns true on success. On failure, leaves @p targetPath unchanged
43
/// (any temp file is removed). If @p errOut is non-null, a human-readable
44
/// reason is written there.
45
///
46
/// Implementation notes:
47
/// - The temp file is `<targetPath>.tmp.<pid>` to avoid clashes between
48
/// concurrent writers in the same process or across launcher
49
/// instances. Two writers landing on the same target end with
50
/// last-writer-wins, which is the documented contract for atomic
51
/// file replace.
52
/// - The parent directory must already exist; the helper does NOT
53
/// `mkdir -p`, since most callers want to fail loud if the path is
54
/// wrong (e.g. user pointed XDG_DATA_HOME at a non-existent dir).
55
/// - Permissions: the temp file is created with mode 0600 so a
56
/// world-readable inherited-umask doesn't briefly expose secrets
57
/// (e.g. tokens cached alongside ETags).
58
bool
atomicWriteFile
(
const
std::filesystem::path& targetPath, std::string_view contents,
59
std::string* errOut =
nullptr
);
60
61
}
// namespace core
core
Definition
app_controller.h:15
core::atomicWriteFile
bool atomicWriteFile(const std::filesystem::path &targetPath, std::string_view contents, std::string *errOut=nullptr)
Atomically and durably write contents to targetPath.
Definition
atomic_write.cpp:130
core
include
util
atomic_write.h
Generated by
1.16.1