userver: userver/fs/write.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 Renames existing file
39/// @param async_tp TaskProcessor for synchronous waiting
40/// @param source path to move from
41/// @param destination path to move to
42/// @throws std::runtime_error
43void Rename(engine::TaskProcessor& async_tp, const std::string& source, const std::string& destination);
44
45/// @brief Rewrite file contents atomically
46/// Write contents to temporary file in the same directory,
47/// then atomically replaces the destination file with the temporary file.
48/// Effectively does write()+sync()+rename()+sync(directory).
49/// It does both sync(2) for file and on the directory, so after successful
50/// return the file must persist on the filesystem.
51/// @param async_tp TaskProcessor for synchronous waiting
52/// @param path file path to rewrite
53/// @param contents new file contents
54/// @param perms new file permissions
55/// @throws std::runtime_error
57 engine::TaskProcessor& async_tp,
58 const std::string& path,
59 std::string_view contents,
60 boost::filesystem::perms perms
61);
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, boost::filesystem::perms perms);
69
70/// @brief Remove existing file
71/// @param async_tp TaskProcessor for synchronous waiting
72/// @param path file path to chmod
73/// @returns true if successfully removed, false if file doesn't exist
74/// @throws std::runtime_error
75bool RemoveSingleFile(engine::TaskProcessor& async_tp, const std::string& path);
76
77} // namespace fs
78
79USERVER_NAMESPACE_END