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