Motion Master 6.0.0-alpha.86
Next-generation motion control software
Loading...
Searching...
No Matches
user_cache.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <expected>
5#include <filesystem>
6#include <functional>
7#include <nlohmann/json_fwd.hpp>
8#include <set>
9#include <span>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace mm::core {
15
20 std::string path;
21 uint64_t size = 0;
22 int64_t modifiedMs = 0;
25 bool deletable = true;
26};
27
29void to_json(nlohmann::json& j, const UserCacheFile& f);
30
65class UserCache {
66 public:
69 explicit UserCache(std::filesystem::path root);
70
72 const std::filesystem::path& root() const { return root_; }
73
83 std::expected<std::filesystem::path, std::string> resolve(std::string_view relPath) const;
84
93 std::expected<std::vector<UserCacheFile>, std::string> list() const;
94
97 std::expected<std::vector<uint8_t>, std::string> read(std::string_view relPath) const;
98
104 std::expected<void, std::string> write(std::string_view relPath, std::span<const uint8_t> data);
105
113 std::expected<bool, std::string> remove(std::string_view relPath);
114
133 void retain(std::string relPath);
134
139 bool isRetained(std::string_view relPath) const;
140
141 private:
142 std::filesystem::path root_;
144 std::set<std::string, std::less<>> retained_;
145};
146
147} // namespace mm::core
A flat, user-writable file store rooted at Motion Master's per-user cache directory.
Definition user_cache.h:65
std::expected< std::filesystem::path, std::string > resolve(std::string_view relPath) const
Validates relPath and maps it to an absolute path under the root.
Definition user_cache.cc:137
void retain(std::string relPath)
Marks a path as one this process holds open, so remove refuses it and list reports it as not deletabl...
Definition user_cache.cc:267
std::expected< std::vector< UserCacheFile >, std::string > list() const
Lists every file under the root, recursively.
Definition user_cache.cc:160
const std::filesystem::path & root() const
The cache root. Surfaced to users so they can find the files on disk themselves.
Definition user_cache.h:72
bool isRetained(std::string_view relPath) const
Whether retain covers relPath, and so will be refused by remove.
Definition user_cache.cc:269
std::expected< bool, std::string > remove(std::string_view relPath)
Removes a file, or a directory and everything under it.
Definition user_cache.cc:271
std::expected< void, std::string > write(std::string_view relPath, std::span< const uint8_t > data)
Writes (creating or replacing) a file, creating parent directories as needed.
Definition user_cache.cc:228
std::expected< std::vector< uint8_t >, std::string > read(std::string_view relPath) const
Reads a file's full contents.
Definition user_cache.cc:201
Definition http_server.h:25
void to_json(nlohmann::json &j, const SystemInfo &info)
Definition system_info.cc:301
One file in the user cache, as reported by UserCache::list.
Definition user_cache.h:17
uint64_t size
File size in bytes.
Definition user_cache.h:21
int64_t modifiedMs
Definition user_cache.h:22
bool deletable
Definition user_cache.h:25
std::string path
Definition user_cache.h:20