6#include "absl/status/status.h"
7#include "absl/status/statusor.h"
8#include "absl/strings/str_cat.h"
9#include "absl/strings/str_format.h"
10#include "absl/time/time.h"
18std::filesystem::path DetermineDefaultRoot() {
19 if (
const char* env_root = std::getenv(
"YAZE_SANDBOX_ROOT")) {
20 return std::filesystem::path(env_root);
23 auto temp_dir = std::filesystem::temp_directory_path(ec);
26 return std::filesystem::current_path() /
"yaze" /
"sandboxes";
28 return temp_dir /
"yaze" /
"sandboxes";
32 absl::string_view
id) {
33 return root / std::string(
id);
44 : root_directory_(DetermineDefaultRoot()) {}
47 std::lock_guard<std::mutex> lock(
mutex_);
60 return absl::InternalError(
61 absl::StrCat(
"Failed to create sandbox root at ",
65 return absl::OkStatus();
69 absl::Time now = absl::Now();
70 std::string time_component =
71 absl::FormatTime(
"%Y%m%dT%H%M%S", now, absl::LocalTimeZone());
73 return absl::StrCat(time_component,
"-", sequence_);
76absl::StatusOr<RomSandboxManager::SandboxMetadata>
79 return absl::FailedPreconditionError(
80 "Cannot create sandbox: ROM is not loaded");
83 std::filesystem::path source_path(rom.
filename());
84 if (source_path.empty()) {
85 return absl::FailedPreconditionError(
86 "Cannot create sandbox: ROM filename is empty");
89 std::unique_lock<std::mutex> lock(
mutex_);
93 std::filesystem::path sandbox_dir =
98 if (!std::filesystem::create_directories(sandbox_dir, ec) && ec) {
99 return absl::InternalError(
100 absl::StrCat(
"Failed to create sandbox directory at ",
101 sandbox_dir.string(),
": ", ec.message()));
104 std::filesystem::path sandbox_rom_path = sandbox_dir / source_path.filename();
107 settings.
filename = sandbox_rom_path.string();
111 absl::Status save_status = rom.
SaveToFile(settings);
112 if (!save_status.ok()) {
113 std::error_code cleanup_ec;
114 std::filesystem::remove_all(sandbox_dir, cleanup_ec);
121 .directory = sandbox_dir,
122 .rom_path = sandbox_rom_path,
123 .source_rom = source_path.string(),
124 .description = std::string(description),
125 .created_at = absl::Now(),
132absl::StatusOr<RomSandboxManager::SandboxMetadata>
134 std::lock_guard<std::mutex> lock(
mutex_);
136 return absl::NotFoundError(
"No active sandbox");
140 return absl::NotFoundError(
"Active sandbox metadata missing");
148 return meta.rom_path;
151std::vector<RomSandboxManager::SandboxMetadata>
153 std::lock_guard<std::mutex> lock(
mutex_);
154 std::vector<SandboxMetadata> list;
156 for (
const auto& [_, metadata] :
sandboxes_) {
157 list.push_back(metadata);
159 std::sort(list.begin(), list.end(),
161 return a.created_at < b.created_at;
167 std::lock_guard<std::mutex> lock(
mutex_);
170 return absl::NotFoundError(
"Sandbox not found");
173 std::filesystem::remove_all(it->second.directory, ec);
175 return absl::InternalError(
176 absl::StrCat(
"Failed to remove sandbox directory: ", ec.message()));
182 return absl::OkStatus();
186 absl::Duration max_age) {
187 std::vector<std::string> to_remove;
189 std::lock_guard<std::mutex> lock(
mutex_);
190 absl::Time threshold = absl::Now() - max_age;
191 for (
const auto& [
id, metadata] :
sandboxes_) {
192 if (metadata.created_at < threshold) {
193 to_remove.push_back(
id);
199 for (
const auto&
id : to_remove) {
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
absl::Status SaveToFile(const SaveSettings &settings)
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::string GenerateSandboxIdLocked()
absl::Status EnsureRootExistsLocked()
std::optional< std::string > active_sandbox_id_
absl::StatusOr< std::filesystem::path > ActiveSandboxRomPath() const
const std::filesystem::path & RootDirectory() const
std::unordered_map< std::string, SandboxMetadata > sandboxes_
#define ASSIGN_OR_RETURN(type_variable_name, expression)
std::filesystem::path ResolveUniqueDirectory(const std::filesystem::path &root, absl::string_view id)
#define RETURN_IF_ERROR(expr)