userver: userver/fs/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/write.hpp
4/// @brief filesystem write functions
5
6#include <filesystem>
7#include <string>
8#include <string_view>
9
10#include <userver/engine/task/task_processor_fwd.hpp>
11#include <userver/fs/blocking/impl/permissions.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace fs {
16
17/// @{
18/// @brief Create directory and all necessary parent elements. Condition when
19/// path already exists and is a directory treated as "success" and no exception
20/// is thrown.
21/// @param async_tp TaskProcessor for synchronous waiting
22/// @param path directory to create
23/// @param perms permissions to set for the created directories
24/// @throws std::runtime_error if there was an error while
25/// creating directories
26void CreateDirectories(engine::TaskProcessor& async_tp, std::string_view path, std::filesystem::perms perms);
27
28/// @overload
29/// Accepts `boost::filesystem::perms`.
30/// @deprecated use the std::filesystem overload
31template <blocking::impl::LegacyPermissions Permissions>
32void CreateDirectories(engine::TaskProcessor& async_tp, std::string_view path, Permissions perms) {
33 fs::CreateDirectories(async_tp, path, blocking::impl::ToStdPermissions(perms));
34}
35
36void CreateDirectories(engine::TaskProcessor& async_tp, std::string_view path);
37/// @}
38
39/// @brief Rewrite file contents asynchronously
40/// It doesn't provide strict atomic guarantees. If you need them, use
41/// `fs::RewriteFileContentsAtomically`.
42/// @param async_tp TaskProcessor for synchronous waiting
43/// @param path file to rewrite
44/// @param contents new file contents
45/// @throws std::runtime_error if failed to overwrite
46void RewriteFileContents(engine::TaskProcessor& async_tp, const std::string& path, std::string_view contents);
47
48/// @brief Rewrite file contents asynchronously
49/// It doesn't provide strict atomic guarantees. If you need them, use
50/// `fs::RewriteFileContentsAtomically`.
51/// @param path file to rewrite
52/// @param contents new file contents
53/// @throws std::runtime_error if failed to overwrite
54void RewriteFileContents(const std::string& path, std::string_view contents);
55
56/// @brief Renames existing file
57/// @param async_tp TaskProcessor for synchronous waiting
58/// @param source path to move from
59/// @param destination path to move to
60/// @throws std::runtime_error
61void Rename(engine::TaskProcessor& async_tp, const std::string& source, const std::string& destination);
62
63/// @brief Rewrite file contents atomically
64/// Write contents to temporary file in the same directory,
65/// then atomically replaces the destination file with the temporary file.
66/// Effectively does write()+sync()+rename()+sync(directory).
67/// It does both sync(2) for file and on the directory, so after successful
68/// return the file must persist on the filesystem.
69/// @param async_tp TaskProcessor for synchronous waiting
70/// @param path file path to rewrite
71/// @param contents new file contents
72/// @param perms new file permissions
73/// @param options how to apply permissions
74/// @throws std::runtime_error
76 engine::TaskProcessor& async_tp,
77 const std::string& path,
78 std::string_view contents,
79 std::filesystem::perms perms,
80 std::filesystem::perm_options options = std::filesystem::perm_options::replace
81);
82
83/// @overload
84/// Accepts `boost::filesystem::perms` with combined permission and operation flags.
85/// @deprecated use the std::filesystem overload
86template <blocking::impl::LegacyPermissions Permissions>
88 engine::TaskProcessor& async_tp,
89 const std::string& path,
90 std::string_view contents,
91 Permissions perms
92) {
93 fs::RewriteFileContentsAtomically(
94 async_tp,
95 path,
96 contents,
97 blocking::impl::ToStdPermissions(perms),
98 blocking::impl::ToStdPermOptions(perms)
99 );
100}
101
102/// @brief Change file mode
103/// @pre The path must not be a symlink.
104/// @param async_tp TaskProcessor for synchronous waiting
105/// @param path file path to chmod
106/// @param perms new file permissions
107/// @param options how to apply permissions
108/// @throws std::runtime_error
109void Chmod(
110 engine::TaskProcessor& async_tp,
111 const std::string& path,
112 std::filesystem::perms perms,
113 std::filesystem::perm_options options = std::filesystem::perm_options::replace
114);
115
116/// @overload
117/// Accepts `boost::filesystem::perms` with combined permission and operation flags.
118/// @deprecated use the std::filesystem overload
119template <blocking::impl::LegacyPermissions Permissions>
120void Chmod(engine::TaskProcessor& async_tp, const std::string& path, Permissions perms) {
121 fs::Chmod(async_tp, path, blocking::impl::ToStdPermissions(perms), blocking::impl::ToStdPermOptions(perms));
122}
123
124/// @brief Remove existing file
125/// @param async_tp TaskProcessor for synchronous waiting
126/// @param path file path to chmod
127/// @returns true if successfully removed, false if file doesn't exist
128/// @throws std::runtime_error
129bool RemoveSingleFile(engine::TaskProcessor& async_tp, const std::string& path);
130
131} // namespace fs
132
133USERVER_NAMESPACE_END