userver: userver/fs/blocking/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/blocking/write.hpp
4/// @brief Functions for synchronous (blocking) file write operations
5/// @ingroup userver_universal
6
7#include <string>
8#include <string_view>
9
10#include <userver/utils/boost_filesystem_file_status.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace fs::blocking {
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 path directory to create
20/// @param perms new directory permissions, default=0755
21/// @throws std::runtime_error if an error occurred while creating directories
22void CreateDirectories(std::string_view path, boost::filesystem::perms perms);
23
24/// @overload
25void CreateDirectories(std::string_view path);
26
27/// @brief Rewrite file contents synchronously
28/// @param path file to rewrite
29/// @param contents new file contents
30/// @throws std::runtime_error if failed to overwrite
31/// @see fs::RewriteFileContents
32void RewriteFileContents(const std::string& path, std::string_view contents);
33
34/// @brief Rewrite file contents synchronously and call `fsync`
35///
36/// Blocks until the file is actually written to disk. This does not typically
37/// speed up the write operation, but is required for the atomic file write
38/// technique.
39///
40/// This function alone does not implement atomic file writes. If you need them,
41/// consider using:
42///
43/// @see fs::RewriteFileContentsAtomically
44/// @see fs::blocking::RewriteFileContentsAtomically
45///
46/// @param path file to rewrite
47/// @param contents new file contents
48/// @throws std::runtime_error if failed to overwrite
49void RewriteFileContentsFSync(const std::string& path, std::string_view contents);
50
51/// @brief flushes directory contents on disk using sync(2)
52/// @param path directory to flush
53void SyncDirectoryContents(const std::string& path);
54
55/// @brief Renames existing file synchronously
56/// @param source path to move from
57/// @param destination path to move to
58/// @throws std::runtime_error
59void Rename(const std::string& source, const std::string& destination);
60
61/// @brief Rewrite file contents atomically
62///
63/// Writes contents to a temporary file in the same directory,
64/// then atomically replaces the destination file with the temporary file.
65/// Effectively does write()+sync()+rename()+sync(directory).
66/// It does both sync(2) for file and on the directory, so after successful
67/// return the file will persist on the filesystem.
68///
69/// @param path file path to rewrite
70/// @param contents new file contents
71/// @param perms new file permissions
72/// @throws std::runtime_error
73void RewriteFileContentsAtomically(const std::string& path, std::string_view contents, boost::filesystem::perms perms);
74
75/// @brief Change file mode synchronously
76/// @param path file path to chmod
77/// @param perms new file permissions
78/// @throws std::runtime_error
79void Chmod(const std::string& path, boost::filesystem::perms perms);
80
81/// @brief Remove existing file synchronously
82/// @param path file path to chmod
83/// @returns true if successfully removed, false if file doesn't exist
84/// @throws std::runtime_error
85bool RemoveSingleFile(const std::string& path);
86
87} // namespace fs::blocking
88
89USERVER_NAMESPACE_END