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 <string>
7
8#include <userver/engine/task/task_processor_fwd.hpp>
9#include <userver/utils/boost_filesystem_file_status.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace fs {
14
15/// @{
16/// @brief Create directory and all necessary parent elements. Condition when
17/// path already exists and is a directory treated as "success" and no exception
18/// is thrown.
19/// @param async_tp TaskProcessor for synchronous waiting
20/// @param path directory to create
21/// @throws std::runtime_error if there was an error while
22/// creating directories
23void CreateDirectories(engine::TaskProcessor& async_tp, std::string_view path, boost::filesystem::perms perms);
24
25void CreateDirectories(engine::TaskProcessor& async_tp, std::string_view path);
26/// @}
27
28/// @brief Rewrite file contents asynchronously
29/// It doesn't provide strict atomic guarantees. If you need them, use
30/// `fs::RewriteFileContentsAtomically`.
31/// @param async_tp TaskProcessor for synchronous waiting
32/// @param path file to rewrite
33/// @param contents new file contents
34/// @throws std::runtime_error if failed to overwrite
35void RewriteFileContents(engine::TaskProcessor& async_tp, const std::string& path, std::string_view contents);
36
37/// @brief Renames existing file
38/// @param async_tp TaskProcessor for synchronous waiting
39/// @param source path to move from
40/// @param destination path to move to
41/// @throws std::runtime_error
42void Rename(engine::TaskProcessor& async_tp, const std::string& source, const std::string& destination);
43
44/// @brief Rewrite file contents atomically
45/// Write contents to temporary file in the same directory,
46/// then atomically replaces the destination file with the temporary file.
47/// Effectively does write()+sync()+rename()+sync(directory).
48/// It does both sync(2) for file and on the directory, so after successful
49/// return the file must persist on the filesystem.
50/// @param async_tp TaskProcessor for synchronous waiting
51/// @param path file path to rewrite
52/// @param contents new file contents
53/// @param perms new file permissions
54/// @throws std::runtime_error
56 engine::TaskProcessor& async_tp,
57 const std::string& path,
58 std::string_view contents,
59 boost::filesystem::perms perms
60);
61
62/// @brief Change file mode
63/// @param async_tp TaskProcessor for synchronous waiting
64/// @param path file path to chmod
65/// @param perms new file permissions
66/// @throws std::runtime_error
67void Chmod(engine::TaskProcessor& async_tp, const std::string& path, boost::filesystem::perms perms);
68
69/// @brief Remove existing file
70/// @param async_tp TaskProcessor for synchronous waiting
71/// @param path file path to chmod
72/// @returns true if successfully removed, false if file doesn't exist
73/// @throws std::runtime_error
74bool RemoveSingleFile(engine::TaskProcessor& async_tp, const std::string& path);
75
76} // namespace fs
77
78USERVER_NAMESPACE_END