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 <boost/filesystem/operations.hpp>
9#include <userver/engine/task/task_processor_fwd.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,
24 boost::filesystem::perms perms);
25
26void CreateDirectories(engine::TaskProcessor& async_tp, std::string_view path);
27/// @}
28
29/// @brief Rewrite file contents asynchronously
30/// It doesn't provide strict atomic guarantees. If you need them, use
31/// `fs::RewriteFileContentsAtomically`.
32/// @param async_tp TaskProcessor for synchronous waiting
33/// @param path file to rewrite
34/// @param contents new file contents
35/// @throws std::runtime_error if failed to overwrite
36void RewriteFileContents(engine::TaskProcessor& async_tp,
37 const std::string& path, std::string_view contents);
38
39/// @brief Renames existing file
40/// @param async_tp TaskProcessor for synchronous waiting
41/// @param source path to move from
42/// @param destination path to move to
43/// @throws std::runtime_error
44void Rename(engine::TaskProcessor& async_tp, const std::string& source,
45 const std::string& destination);
46
47/// @brief Rewrite file contents atomically
48/// Write contents to temporary file in the same directory,
49/// then atomically replaces the destination file with the temporary file.
50/// Effectively does write()+sync()+rename()+sync(directory).
51/// It does both sync(2) for file and on the directory, so after successful
52/// return the file must persist on the filesystem.
53/// @param async_tp TaskProcessor for synchronous waiting
54/// @param path file path to rewrite
55/// @param contents new file contents
56/// @param perms new file permissions
57/// @throws std::runtime_error
58void RewriteFileContentsAtomically(engine::TaskProcessor& async_tp,
59 const std::string& path,
60 std::string_view contents,
61 boost::filesystem::perms perms);
62
63/// @brief Change file mode
64/// @param async_tp TaskProcessor for synchronous waiting
65/// @param path file path to chmod
66/// @param perms new file permissions
67/// @throws std::runtime_error
68void Chmod(engine::TaskProcessor& async_tp, const std::string& path,
69 boost::filesystem::perms perms);
70
71/// @brief Remove existing file
72/// @param async_tp TaskProcessor for synchronous waiting
73/// @param path file path to chmod
74/// @returns true if successfully removed, false if file doesn't exist
75/// @throws std::runtime_error
76bool RemoveSingleFile(engine::TaskProcessor& async_tp, const std::string& path);
77
78} // namespace fs
79
80USERVER_NAMESPACE_END