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