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"
19std::filesystem::path DetermineDefaultRoot() {
20 if (
const char* env_root = std::getenv(
"YAZE_SANDBOX_ROOT")) {
21 return std::filesystem::path(env_root);
24 auto temp_dir = std::filesystem::temp_directory_path(ec);
27 return std::filesystem::current_path() /
"yaze" /
"sandboxes";
29 return temp_dir /
"yaze" /
"sandboxes";
33 const std::filesystem::path& root,
34 absl::string_view
id) {
35 return root / std::string(
id);
46 : root_directory_(DetermineDefaultRoot()) {}
49 std::lock_guard<std::mutex> lock(
mutex_);
62 return absl::InternalError(absl::StrCat(
67 return absl::OkStatus();
71 absl::Time now = absl::Now();
72 std::string time_component = absl::FormatTime(
"%Y%m%dT%H%M%S", now,
73 absl::LocalTimeZone());
75 return absl::StrCat(time_component,
"-", sequence_);
78absl::StatusOr<RomSandboxManager::SandboxMetadata>
81 return absl::FailedPreconditionError(
82 "Cannot create sandbox: ROM is not loaded");
85 std::filesystem::path source_path(rom.
filename());
86 if (source_path.empty()) {
87 return absl::FailedPreconditionError(
88 "Cannot create sandbox: ROM filename is empty");
91 std::unique_lock<std::mutex> lock(
mutex_);
95 std::filesystem::path sandbox_dir =
100 if (!std::filesystem::create_directories(sandbox_dir, ec) && ec) {
101 return absl::InternalError(absl::StrCat(
102 "Failed to create sandbox directory at ", sandbox_dir.string(),
103 ": ", ec.message()));
106 std::filesystem::path sandbox_rom_path = sandbox_dir / source_path.filename();
109 settings.
filename = sandbox_rom_path.string();
114 absl::Status save_status = rom.
SaveToFile(settings);
115 if (!save_status.ok()) {
116 std::error_code cleanup_ec;
117 std::filesystem::remove_all(sandbox_dir, cleanup_ec);
124 .directory = sandbox_dir,
125 .rom_path = sandbox_rom_path,
126 .source_rom = source_path.string(),
127 .description = std::string(description),
128 .created_at = absl::Now(),
135absl::StatusOr<RomSandboxManager::SandboxMetadata>
137 std::lock_guard<std::mutex> lock(
mutex_);
139 return absl::NotFoundError(
"No active sandbox");
143 return absl::NotFoundError(
"Active sandbox metadata missing");
148absl::StatusOr<std::filesystem::path>
151 return meta.rom_path;
154std::vector<RomSandboxManager::SandboxMetadata>
156 std::lock_guard<std::mutex> lock(
mutex_);
157 std::vector<SandboxMetadata> list;
159 for (
const auto& [_, metadata] :
sandboxes_) {
160 list.push_back(metadata);
162 std::sort(list.begin(), list.end(),
164 return a.created_at < b.created_at;
170 std::lock_guard<std::mutex> lock(
mutex_);
173 return absl::NotFoundError(
"Sandbox not found");
176 std::filesystem::remove_all(it->second.directory, ec);
178 return absl::InternalError(absl::StrCat(
179 "Failed to remove sandbox directory: ", ec.message()));
185 return absl::OkStatus();
189 std::vector<std::string> to_remove;
191 std::lock_guard<std::mutex> lock(
mutex_);
192 absl::Time threshold = absl::Now() - max_age;
193 for (
const auto& [
id, metadata] :
sandboxes_) {
194 if (metadata.created_at < threshold) {
195 to_remove.push_back(
id);
201 for (
const auto&
id : to_remove) {
The Rom class is used to load, save, and modify Rom data.
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 RETURN_IF_ERROR(expression)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
std::filesystem::path ResolveUniqueDirectory(const std::filesystem::path &root, absl::string_view id)
Main namespace for the application.