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 <boost/filesystem/operations.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,
50 std::string_view contents);
51
52/// @brief flushes directory contents on disk using sync(2)
53/// @param path directory to flush
54void SyncDirectoryContents(const std::string& path);
55
56/// @brief Renames existing file synchronously
57/// @param source path to move from
58/// @param destination path to move to
59/// @throws std::runtime_error
60void Rename(const std::string& source, const std::string& destination);
61
62/// @brief Rewrite file contents atomically
63///
64/// Writes contents to a temporary file in the same directory,
65/// then atomically replaces the destination file with the temporary file.
66/// Effectively does write()+sync()+rename()+sync(directory).
67/// It does both sync(2) for file and on the directory, so after successful
68/// return the file will persist on the filesystem.
69///
70/// @param path file path to rewrite
71/// @param contents new file contents
72/// @param perms new file permissions
73/// @throws std::runtime_error
74void RewriteFileContentsAtomically(const std::string& path,
75 std::string_view contents,
76 boost::filesystem::perms perms);
77
78/// @brief Change file mode synchronously
79/// @param path file path to chmod
80/// @param perms new file permissions
81/// @throws std::runtime_error
82void Chmod(const std::string& path, boost::filesystem::perms perms);
83
84/// @brief Remove existing file synchronously
85/// @param path file path to chmod
86/// @returns true if successfully removed, false if file doesn't exist
87/// @throws std::runtime_error
88bool RemoveSingleFile(const std::string& path);
89
90} // namespace fs::blocking
91
92USERVER_NAMESPACE_END