yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
rom_sandbox_manager.h
Go to the documentation of this file.
1#ifndef YAZE_SRC_CLI_SERVICE_ROM_SANDBOX_MANAGER_H_
2#define YAZE_SRC_CLI_SERVICE_ROM_SANDBOX_MANAGER_H_
3
4#include <filesystem>
5#include <mutex>
6#include <optional>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
11#include "absl/base/thread_annotations.h"
12#include "absl/status/status.h"
13#include "absl/status/statusor.h"
14#include "absl/strings/string_view.h"
15#include "absl/time/time.h"
16
17#include "app/rom.h"
18
19namespace yaze {
20namespace cli {
21
22// RomSandboxManager coordinates creation and lifecycle management of sandboxed
23// ROM copies. Agent workflows operate on sandboxes so that proposals can be
24// reviewed before landing in the primary project ROM. The manager currently
25// tracks sandboxes in-memory for the running process and persists files to a
26// configurable root directory on disk.
28 public:
30 std::string id;
31 std::filesystem::path directory;
32 std::filesystem::path rom_path;
33 std::string source_rom;
34 std::string description;
35 absl::Time created_at;
36 };
37
39
40 // Set the root directory used for new sandboxes. Must be called before any
41 // sandboxes are created. If not set, a default rooted at the system temporary
42 // directory is used (or the value of the YAZE_SANDBOX_ROOT environment
43 // variable when present).
44 void SetRootDirectory(const std::filesystem::path& root);
45
46 const std::filesystem::path& RootDirectory() const;
47
48 // Creates a new sandbox by copying the provided ROM into a unique directory
49 // under the root. The new sandbox becomes the active sandbox for the current
50 // process. Metadata is returned describing the sandbox on success.
51 absl::StatusOr<SandboxMetadata> CreateSandbox(Rom& rom,
52 absl::string_view description);
53
54 // Returns the metadata for the active sandbox if one exists.
55 absl::StatusOr<SandboxMetadata> ActiveSandbox() const;
56
57 // Returns the absolute path to the active sandbox ROM copy. Equivalent to
58 // ActiveSandbox()->rom_path but with more descriptive errors when unset.
59 absl::StatusOr<std::filesystem::path> ActiveSandboxRomPath() const;
60
61 // List all sandboxes tracked during the current process lifetime. This will
62 // include the active sandbox (if any) and previously created sandboxes that
63 // have not been cleaned up.
64 std::vector<SandboxMetadata> ListSandboxes() const;
65
66 // Removes the sandbox identified by |id| from the index and deletes its on
67 // disk directory. If the sandbox is currently active the active sandbox is
68 // cleared. Missing sandboxes result in a NotFound status.
69 absl::Status RemoveSandbox(const std::string& id);
70
71 // Deletes any sandboxes that are older than |max_age|, returning the number
72 // of sandboxes removed. This is currently best-effort; individual removals
73 // may produce errors which are aggregated into the returned status.
74 absl::StatusOr<int> CleanupOlderThan(absl::Duration max_age);
75
76 private:
78
79 absl::Status EnsureRootExistsLocked();
80 std::string GenerateSandboxIdLocked();
81
82 std::filesystem::path root_directory_;
83 mutable std::mutex mutex_;
84 std::unordered_map<std::string, SandboxMetadata> sandboxes_;
85 std::optional<std::string> active_sandbox_id_;
86 int sequence_ ABSL_GUARDED_BY(mutex_) = 0;
87};
88
89} // namespace cli
90} // namespace yaze
91
92#endif // YAZE_SRC_CLI_SERVICE_ROM_SANDBOX_MANAGER_H_
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:71
absl::StatusOr< SandboxMetadata > CreateSandbox(Rom &rom, absl::string_view description)
absl::Status RemoveSandbox(const std::string &id)
void SetRootDirectory(const std::filesystem::path &root)
absl::StatusOr< SandboxMetadata > ActiveSandbox() const
absl::StatusOr< int > CleanupOlderThan(absl::Duration max_age)
std::filesystem::path root_directory_
std::vector< SandboxMetadata > ListSandboxes() const
static RomSandboxManager & Instance()
std::optional< std::string > active_sandbox_id_
absl::StatusOr< std::filesystem::path > ActiveSandboxRomPath() const
const std::filesystem::path & RootDirectory() const
int sequence_ ABSL_GUARDED_BY(mutex_)=0
std::unordered_map< std::string, SandboxMetadata > sandboxes_
Main namespace for the application.