userver: userver/fs/blocking/write.hpp Source File
Loading...
Searching...
No Matches
write.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/fs/blocking/write.hpp
4/// @brief Functions for synchronous (blocking) file write operations
5/// @ingroup userver_universal
6
7#include <filesystem>
8#include <string_view>
9
10#include <userver/fs/blocking/impl/permissions.hpp>
11#include <userver/utils/zstring_view.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace fs::blocking {
16
17/// @brief Create directory and all necessary parent elements. Condition when
18/// path already exists and is a directory treated as "success" and no exception
19/// is thrown.
20/// @param path directory to create
21/// @param perms new directory permissions, default=0755
22/// @throws std::runtime_error if an error occurred while creating directories
23void CreateDirectories(std::string_view path, std::filesystem::perms perms);
24
25/// @overload
26/// Accepts `boost::filesystem::perms`.
27/// @deprecated use the std::filesystem overload
28template <impl::LegacyPermissions Permissions>
29void CreateDirectories(std::string_view path, Permissions perms) {
30 fs::blocking::CreateDirectories(path, impl::ToStdPermissions(perms));
31}
32
33/// @overload
34void CreateDirectories(std::string_view path);
35
36/// @brief Rewrite file contents synchronously
37/// @param path file to rewrite
38/// @param contents new file contents
39/// @throws std::runtime_error if failed to overwrite
40/// @see fs::RewriteFileContents
41void RewriteFileContents(utils::zstring_view path, std::string_view contents);
42
43/// @brief Rewrite file contents synchronously and call `fsync`
44///
45/// Blocks until the file is actually written to disk. This does not typically
46/// speed up the write operation, but is required for the atomic file write
47/// technique.
48///
49/// This function alone does not implement atomic file writes. If you need them,
50/// consider using:
51///
52/// @see fs::RewriteFileContentsAtomically
53/// @see fs::blocking::RewriteFileContentsAtomically
54///
55/// @param path file to rewrite
56/// @param contents new file contents
57/// @throws std::runtime_error if failed to overwrite
58void RewriteFileContentsFSync(utils::zstring_view path, std::string_view contents);
59
60/// @brief flushes directory contents on disk using sync(2)
61/// @param path directory to flush
62void SyncDirectoryContents(utils::zstring_view path);
63
64/// @brief Renames existing file synchronously
65/// @param source path to move from
66/// @param destination path to move to
67/// @throws std::runtime_error
68void Rename(utils::zstring_view source, utils::zstring_view destination);
69
70/// @brief Rewrite file contents atomically
71///
72/// Writes contents to a temporary file in the same directory,
73/// then atomically replaces the destination file with the temporary file.
74/// Effectively does write()+sync()+rename()+sync(directory).
75/// It does both sync(2) for file and on the directory, so after successful
76/// return the file will persist on the filesystem.
77///
78/// @param path file path to rewrite
79/// @param contents new file contents
80/// @param perms new file permissions
81/// @param options how to apply permissions to the temporary file
82/// @throws std::runtime_error
84 utils::zstring_view path,
85 std::string_view contents,
86 std::filesystem::perms perms,
87 std::filesystem::perm_options options = std::filesystem::perm_options::replace
88);
89
90/// @overload
91/// Accepts `boost::filesystem::perms` with combined permission and operation flags.
92/// @deprecated use the std::filesystem overload
93template <impl::LegacyPermissions Permissions>
94void RewriteFileContentsAtomically(utils::zstring_view path, std::string_view contents, Permissions perms) {
95 fs::blocking::RewriteFileContentsAtomically(
96 path,
97 contents,
98 impl::ToStdPermissions(perms),
99 impl::ToStdPermOptions(perms)
100 );
101}
102
103/// @brief Change file mode synchronously
104/// @pre The path must not be a symlink.
105/// @param path file path to chmod
106/// @param perms new file permissions
107/// @param options whether to replace, add or remove permissions and whether to follow symlinks
108/// @throws std::runtime_error
109void Chmod(
110 utils::zstring_view path,
111 std::filesystem::perms perms,
112 std::filesystem::perm_options options = std::filesystem::perm_options::replace
113);
114
115/// @overload
116/// Accepts `boost::filesystem::perms` with combined permission and operation flags.
117/// @deprecated use the std::filesystem overload
118template <impl::LegacyPermissions Permissions>
119void Chmod(utils::zstring_view path, Permissions perms) {
120 fs::blocking::Chmod(path, impl::ToStdPermissions(perms), impl::ToStdPermOptions(perms));
121}
122
123/// @brief Remove existing file synchronously
124/// @param path file path to chmod
125/// @returns true if successfully removed, false if file doesn't exist
126/// @throws std::runtime_error
127bool RemoveSingleFile(utils::zstring_view path);
128
129} // namespace fs::blocking
130
131USERVER_NAMESPACE_END