userver: userver/fs/read.hpp Source File
Loading...
Searching...
No Matches
read.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/fs/read.hpp
4/// @brief functions for asynchronous file read operations
5
6#include <memory>
7#include <string>
8#include <unordered_map>
9
10#include <userver/engine/task/task_processor_fwd.hpp>
11#include <userver/utils/flags.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15/// @brief filesystem support
16namespace fs {
17
18/// @brief Struct file with load data
20 std::string data;
21 std::string extension;
22};
23
24using FileInfoWithDataConstPtr = std::shared_ptr<const FileInfoWithData>;
25using FileInfoWithDataMap = std::unordered_map<std::string, FileInfoWithDataConstPtr>;
26
27enum class SettingsReadFile {
28 kNone = 0,
29 /// Skip hidden files,
30 kSkipHidden = 1 << 0,
31};
32
33/// @brief Returns relative path from full path
34/// @param path full path, must start with `dir`
35/// @param dir directory path to get relative path
36/// @note it does not access filesystem, the relative path is calculated
37/// lexically.
38std::string GetLexicallyRelative(std::string_view path, std::string_view dir);
39
40/// @brief Returns files from recursively traversed directory
41/// @param async_tp TaskProcessor for synchronous waiting
42/// @param path to directory to traverse recursively
43/// @param flags settings read files
44/// @returns map with relative to `path` filepaths and file info
45/// @throws std::runtime_error if read fails for any reason (e.g. no such file,
46/// read error, etc.),
49 const std::string& path,
51);
52
53/// @brief Reads file contents asynchronously
54/// @param async_tp TaskProcessor for synchronous waiting
55/// @param path file to open
56/// @returns file contents
57/// @throws std::runtime_error if read fails for any reason (e.g. no such file,
58/// read error, etc.),
59std::string ReadFileContents(engine::TaskProcessor& async_tp, const std::string& path);
60
61/// @brief Checks whether the file exists asynchronously
62/// @param async_tp TaskProcessor for synchronous waiting
63/// @param path file path to check
64/// @returns true if file exists, false if file doesn't exist
65/// @throws std::runtime_error if something goes wrong (e.g. out of file
66/// descriptors)
67bool FileExists(engine::TaskProcessor& async_tp, const std::string& path);
68
69} // namespace fs
70
71USERVER_NAMESPACE_END