20#include <nlohmann/json.hpp>
27using json = nlohmann::json;
28namespace fs = std::filesystem;
39 return std::to_string(
sizeBytes / 1024) +
" KB";
41 return std::to_string(
sizeBytes / (1024 * 1024)) +
" MB";
42 return std::to_string(
sizeBytes / (1024LL * 1024 * 1024)) +
" GB";
47 auto time = std::chrono::system_clock::to_time_t(
created);
49 localtime_r(&time, &tm);
51 ss << std::put_time(&tm,
"%Y-%m-%d %H:%M:%S");
98 const char* home = std::getenv(
"HOME");
101 snapshotDir = fs::path(home) /
".local/share/gamelauncher/snapshots";
114 if (!impl_->snapshotDir.empty())
116 fs::create_directories(impl_->snapshotDir, ec);
119 Logger::error(
"[PrefixSnapshot] Failed to create snapshot directory {}: {}",
120 impl_->snapshotDir.string(), ec.message());
137std::string PrefixSnapshotService::generateSnapshotId()
const
140 auto now = std::chrono::system_clock::now();
142 std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
144 std::random_device rd;
145 std::mt19937 gen(rd());
146 std::uniform_int_distribution<> dis(1000, 9999);
148 std::stringstream ss;
149 ss <<
"snap_" << timestamp <<
"_" << dis(gen);
153std::string PrefixSnapshotService::detectWineVersion(
const fs::path& prefixPath)
const
156 fs::path systemReg = prefixPath /
"system.reg";
157 if (!fs::exists(systemReg))
162 std::ifstream file(systemReg);
164 while (std::getline(file, line))
167 if (line.find(
"\"ProductName\"") != std::string::npos)
169 auto pos = line.find(
"Wine");
170 if (pos != std::string::npos)
173 return line.substr(pos);
181std::string PrefixSnapshotService::detectArchitecture(
const fs::path& prefixPath)
const
184 if (fs::exists(prefixPath /
"drive_c/windows/syswow64"))
191void PrefixSnapshotService::loadSnapshotIndex()
193 fs::path indexPath = impl_->snapshotDir /
"index.json";
195 if (!fs::exists(indexPath))
202 std::ifstream file(indexPath);
203 json j = json::parse(file);
205 for (
const auto& item : j[
"snapshots"])
208 snap.
id = item.value(
"id",
"");
209 snap.
name = item.value(
"name",
"");
211 snap.
prefixPath = item.value(
"prefixPath",
"");
218 fs::path canonical = fs::weakly_canonical(snap.
snapshotPath, ec);
219 if (ec || canonical.empty())
221 Logger::error(
"[PrefixSnapshot] Rejected snapshot '{}': invalid path '{}'",
225 fs::path rel = fs::relative(canonical, impl_->snapshotDir, ec);
226 if (ec || rel.empty() || *rel.begin() ==
"..")
228 Logger::error(
"[PrefixSnapshot] Rejected snapshot '{}': path '{}' is outside "
229 "snapshot directory",
238 "[PrefixSnapshot] Skipping snapshot '{}': directory no longer exists",
244 snap.
sizeBytes = item.value(
"sizeBytes", 0);
245 snap.
fileCount = item.value(
"fileCount", 0);
248 snap.
gameId = item.value(
"gameId",
"");
249 snap.
gameName = item.value(
"gameName",
"");
253 if (item.contains(
"created"))
255 snap.
created = std::chrono::system_clock::time_point(
256 std::chrono::seconds(item[
"created"].get<int64_t>()));
259 impl_->snapshots[snap.
id] = snap;
262 Logger::info(
"[PrefixSnapshot] Loaded {} snapshots from index", impl_->snapshots.size());
264 catch (
const std::exception& e)
266 Logger::error(
"[PrefixSnapshot] Failed to load snapshot index: {}", e.what());
270void PrefixSnapshotService::saveSnapshotIndex()
272 fs::path indexPath = impl_->snapshotDir /
"index.json";
277 j[
"snapshots"] = json::array();
279 for (
const auto& [
id, snap] : impl_->snapshots)
282 item[
"id"] = snap.
id;
283 item[
"name"] = snap.
name;
285 item[
"prefixPath"] = snap.
prefixPath.string();
288 std::chrono::duration_cast<std::chrono::seconds>(snap.
created.time_since_epoch())
294 item[
"gameId"] = snap.
gameId;
299 j[
"snapshots"].push_back(item);
303 fs::path tmpPath = indexPath;
306 std::ofstream file(tmpPath);
309 std::error_code renameEc;
310 fs::rename(tmpPath, indexPath, renameEc);
313 Logger::error(
"[PrefixSnapshot] Failed to atomically replace index: {}",
315 fs::remove(tmpPath, renameEc);
318 catch (
const std::exception& e)
320 Logger::error(
"[PrefixSnapshot] Failed to save snapshot index: {}", e.what());
324bool PrefixSnapshotService::copyPrefix(
const fs::path& src,
const fs::path& dest,
325 const ProgressCallback& callback,
326 std::atomic<bool>& cancelled)
330 int64_t totalBytes = 0;
333 for (
const auto& entry : fs::recursive_directory_iterator(src, ec))
335 if (cancelled.load())
337 if (entry.is_regular_file())
340 totalBytes +=
static_cast<int64_t
>(entry.file_size());
345 Logger::error(
"[PrefixSnapshot] Directory iteration error: {}", ec.message());
351 SnapshotProgress progress;
359 int filesProcessed = 0;
360 int64_t bytesProcessed = 0;
361 int filesSkipped = 0;
363 for (
const auto& entry : fs::recursive_directory_iterator(src, ec))
365 if (cancelled.load())
368 fs::path relativePath = fs::relative(entry.path(), src);
369 fs::path destPath = dest / relativePath;
371 if (entry.is_directory())
373 fs::create_directories(destPath, ec);
375 else if (entry.is_regular_file() && !entry.is_symlink())
380 fs::create_directories(destPath.parent_path(), ec);
381 fs::copy_file(entry.path(), destPath, fs::copy_options::overwrite_existing, ec);
385 Logger::error(
"[PrefixSnapshot] Failed to copy {}: {}", entry.path().string(),
392 bytesProcessed +=
static_cast<int64_t
>(entry.file_size());
394 if (callback && filesProcessed % 100 == 0)
396 SnapshotProgress progress;
403 progress.
percentComplete = totalFiles > 0 ? (filesProcessed * 100) / totalFiles : 0;
407 else if (entry.is_symlink())
410 fs::path target = fs::read_symlink(entry.path(), ec);
416 std::error_code ec1, ec2;
417 auto resolvedTarget = fs::weakly_canonical(destPath.parent_path() / target, ec1);
418 auto resolvedDest = fs::weakly_canonical(dest, ec2);
419 std::error_code relEc;
421 (!ec1 && !ec2) ? fs::relative(resolvedTarget, resolvedDest, relEc) : fs::path{};
422 if (!ec1 && !ec2 && !relEc && !rel.empty() && *rel.begin() !=
"..")
424 fs::create_directories(destPath.parent_path(), ec);
425 fs::create_symlink(target, destPath, ec);
429 Logger::warn(
"[PrefixSnapshot] Skipping symlink pointing outside prefix: {}",
430 entry.path().string());
437 Logger::error(
"[PrefixSnapshot] Directory iteration error: {}", ec.message());
441 if (filesSkipped > 0)
443 Logger::error(
"[PrefixSnapshot] {} file(s) could not be copied — snapshot is incomplete",
455std::optional<PrefixSnapshot>
457 const std::string& description,
460 Logger::info(
"[PrefixSnapshot] Creating snapshot '{}' for {}", name, prefixPath.string());
462 if (!fs::exists(prefixPath) || !fs::is_directory(prefixPath))
464 Logger::error(
"[PrefixSnapshot] Invalid prefix path: {}", prefixPath.string());
465 if (progressCallback)
470 progressCallback(progress);
476 auto cancelToken = std::make_shared<std::atomic<bool>>(
false);
478 std::lock_guard<std::mutex> lk(impl_->cancelMutex);
479 impl_->activeCancelFlags.erase(std::remove_if(impl_->activeCancelFlags.begin(),
480 impl_->activeCancelFlags.end(),
481 [](
const auto& wp) { return wp.expired(); }),
482 impl_->activeCancelFlags.end());
483 impl_->activeCancelFlags.push_back(cancelToken);
488 snapshot.
id = generateSnapshotId();
489 snapshot.
name = name;
493 snapshot.
created = std::chrono::system_clock::now();
494 snapshot.
wineVersion = detectWineVersion(prefixPath);
498 if (progressCallback)
502 progressCallback(progress);
506 if (!copyPrefix(prefixPath, snapshot.
snapshotPath, progressCallback, *cancelToken))
512 if (progressCallback)
517 progressCallback(progress);
524 for (
const auto& entry : fs::recursive_directory_iterator(snapshot.
snapshotPath, ec))
526 if (entry.is_regular_file())
529 snapshot.
sizeBytes +=
static_cast<int64_t
>(entry.file_size());
534 Logger::error(
"[PrefixSnapshot] Directory iteration error: {}", ec.message());
539 std::lock_guard<std::mutex> lock(impl_->mutex);
540 impl_->snapshots[snapshot.
id] = snapshot;
544 Logger::info(
"[PrefixSnapshot] Created snapshot {} ({} files, {})", snapshot.
id,
547 if (progressCallback)
552 progressCallback(progress);
558std::future<std::optional<PrefixSnapshot>>
560 const std::string& description,
563 return std::async(std::launch::async, [
this, prefixPath, name, description, progressCallback]()
564 {
return createSnapshot(prefixPath, name, description, progressCallback); });
568 const fs::path& prefixPath,
const std::string& parentSnapshotId,
const std::string& name,
574 Logger::error(
"[PrefixSnapshot] Parent snapshot not found: {}", parentSnapshotId);
580 auto snapshot =
createSnapshot(prefixPath, name,
"", progressCallback);
583 snapshot->isIncremental =
true;
584 snapshot->parentSnapshotId = parentSnapshotId;
586 std::lock_guard<std::mutex> lock(impl_->mutex);
587 auto it = impl_->snapshots.find(snapshot->id);
588 if (it != impl_->snapshots.end())
590 it->second.isIncremental =
true;
591 it->second.parentSnapshotId = parentSnapshotId;
600 const std::string& gameId,
601 const std::string& gameName,
602 const std::string& reason)
604 std::string name = gameName +
" - " + reason;
609 snapshot->gameId = gameId;
610 snapshot->gameName = gameName;
612 std::lock_guard<std::mutex> lock(impl_->mutex);
613 auto it = impl_->snapshots.find(snapshot->id);
614 if (it != impl_->snapshots.end())
616 it->second.gameId = gameId;
617 it->second.gameName = gameName;
635 Logger::error(
"[PrefixSnapshot] Snapshot not found: {}", snapshotId);
639 const auto& snapshot = *snapshotOpt;
640 Logger::info(
"[PrefixSnapshot] Restoring snapshot {} to {}", snapshotId,
641 snapshot.prefixPath.string());
644 if (!fs::exists(snapshot.snapshotPath) || !fs::is_directory(snapshot.snapshotPath))
646 Logger::error(
"[PrefixSnapshot] Snapshot directory not found: {}",
647 snapshot.snapshotPath.string());
648 if (progressCallback)
653 progressCallback(progress);
659 auto cancelToken = std::make_shared<std::atomic<bool>>(
false);
661 std::lock_guard<std::mutex> lk(impl_->cancelMutex);
662 impl_->activeCancelFlags.erase(std::remove_if(impl_->activeCancelFlags.begin(),
663 impl_->activeCancelFlags.end(),
664 [](
const auto& wp) { return wp.expired(); }),
665 impl_->activeCancelFlags.end());
666 impl_->activeCancelFlags.push_back(cancelToken);
671 createSnapshot(snapshot.prefixPath,
"Auto-backup before restore",
"", progressCallback);
674 Logger::warn(
"[PrefixSnapshot] Failed to create backup, proceeding anyway");
679 for (
const auto& entry : fs::directory_iterator(snapshot.prefixPath, ec))
681 fs::remove_all(entry.path(), ec);
685 if (!copyPrefix(snapshot.snapshotPath, snapshot.prefixPath, progressCallback, *cancelToken))
687 Logger::error(
"[PrefixSnapshot] Failed to restore snapshot");
692 Logger::info(
"[PrefixSnapshot] Attempting to restore backup");
693 copyPrefix(backup->snapshotPath, snapshot.prefixPath,
nullptr, *cancelToken);
696 if (progressCallback)
701 progressCallback(progress);
706 Logger::info(
"[PrefixSnapshot] Successfully restored snapshot {}", snapshotId);
708 if (progressCallback)
713 progressCallback(progress);
723 return std::async(std::launch::async, [
this, snapshotId, progressCallback]()
728 const std::vector<fs::path>& files)
736 const auto& snapshot = *snapshotOpt;
739 for (
const auto& file : files)
742 for (
const auto& component : file)
744 if (component ==
"..")
746 Logger::error(
"[PrefixSnapshot] Rejected restore path with traversal: {}",
752 fs::path srcPath = snapshot.snapshotPath / file;
753 fs::path destPath = snapshot.prefixPath / file;
755 if (!fs::exists(srcPath))
757 Logger::warn(
"[PrefixSnapshot] File not in snapshot: {}", file.string());
761 fs::create_directories(destPath.parent_path(), ec);
762 fs::copy_file(srcPath, destPath, fs::copy_options::overwrite_existing, ec);
766 Logger::error(
"[PrefixSnapshot] Failed to restore {}: {}", file.string(), ec.message());
779 std::lock_guard<std::mutex> lock(impl_->mutex);
781 std::vector<PrefixSnapshot> result;
782 result.reserve(impl_->snapshots.size());
784 for (
const auto& [
id, snap] : impl_->snapshots)
786 result.push_back(snap);
791 { return a.created > b.created; });
796std::vector<PrefixSnapshot>
799 std::lock_guard<std::mutex> lock(impl_->mutex);
801 std::vector<PrefixSnapshot> result;
802 for (
const auto& [
id, snap] : impl_->snapshots)
806 result.push_back(snap);
811 { return a.created > b.created; });
816std::vector<PrefixSnapshot>
819 std::lock_guard<std::mutex> lock(impl_->mutex);
821 std::vector<PrefixSnapshot> result;
822 for (
const auto& [
id, snap] : impl_->snapshots)
824 if (snap.
gameId == gameId)
826 result.push_back(snap);
831 { return a.created > b.created; });
836std::optional<PrefixSnapshot>
839 std::lock_guard<std::mutex> lock(impl_->mutex);
841 auto it = impl_->snapshots.find(snapshotId);
842 if (it != impl_->snapshots.end())
850bool PrefixSnapshotService::deleteSnapshotUnlocked(
const std::string& snapshotId)
852 auto it = impl_->snapshots.find(snapshotId);
853 if (it == impl_->snapshots.end())
860 fs::remove_all(it->second.snapshotPath, ec);
864 Logger::error(
"[PrefixSnapshot] Failed to delete snapshot files: {}", ec.message());
867 impl_->snapshots.erase(it);
870 Logger::info(
"[PrefixSnapshot] Deleted snapshot {}", snapshotId);
876 std::lock_guard<std::mutex> lock(impl_->mutex);
877 return deleteSnapshotUnlocked(snapshotId);
882 std::lock_guard<std::mutex> lock(impl_->mutex);
885 std::vector<std::string> toDelete;
886 for (
const auto& [
id, snap] : impl_->snapshots)
890 toDelete.push_back(
id);
895 for (
const auto&
id : toDelete)
897 if (deleteSnapshotUnlocked(
id))
907 const std::string& newName)
909 std::lock_guard<std::mutex> lock(impl_->mutex);
911 auto it = impl_->snapshots.find(snapshotId);
912 if (it == impl_->snapshots.end())
917 it->second.name = newName;
923 const std::string& description)
925 std::lock_guard<std::mutex> lock(impl_->mutex);
927 auto it = impl_->snapshots.find(snapshotId);
928 if (it == impl_->snapshots.end())
933 it->second.description = description;
943 const std::string& snapshotId2)
const
950 if (!snap1 || !snap2)
956 std::set<fs::path> files1, files2;
957 std::map<fs::path, int64_t> sizes1, sizes2;
960 for (
const auto& entry : fs::recursive_directory_iterator(snap1->snapshotPath, ec))
962 if (entry.is_regular_file())
964 fs::path rel = fs::relative(entry.path(), snap1->snapshotPath);
966 sizes1[rel] =
static_cast<int64_t
>(entry.file_size());
971 Logger::error(
"[PrefixSnapshot] Directory iteration error: {}", ec.message());
975 for (
const auto& entry : fs::recursive_directory_iterator(snap2->snapshotPath, ec))
977 if (entry.is_regular_file())
979 fs::path rel = fs::relative(entry.path(), snap2->snapshotPath);
981 sizes2[rel] =
static_cast<int64_t
>(entry.file_size());
986 Logger::error(
"[PrefixSnapshot] Directory iteration error: {}", ec.message());
991 for (
const auto& file : files1)
993 if (files2.find(file) == files2.end())
997 else if (sizes1[file] != sizes2[file])
1003 for (
const auto& file : files2)
1005 if (files1.find(file) == files1.end())
1015 const std::string& snapshotId)
const
1027 std::set<fs::path> currentFiles, snapshotFiles;
1028 std::map<fs::path, int64_t> currentSizes, snapshotSizes;
1031 for (
const auto& entry : fs::recursive_directory_iterator(prefixPath, ec))
1033 if (entry.is_regular_file())
1035 fs::path rel = fs::relative(entry.path(), prefixPath);
1036 currentFiles.insert(rel);
1037 currentSizes[rel] =
static_cast<int64_t
>(entry.file_size());
1042 Logger::error(
"[PrefixSnapshot] Directory iteration error: {}", ec.message());
1046 for (
const auto& entry : fs::recursive_directory_iterator(snap->
snapshotPath, ec))
1048 if (entry.is_regular_file())
1050 fs::path rel = fs::relative(entry.path(), snap->
snapshotPath);
1051 snapshotFiles.insert(rel);
1052 snapshotSizes[rel] =
static_cast<int64_t
>(entry.file_size());
1057 Logger::error(
"[PrefixSnapshot] Directory iteration error: {}", ec.message());
1061 for (
const auto& file : currentFiles)
1063 if (snapshotFiles.find(file) == snapshotFiles.end())
1067 else if (currentSizes[file] != snapshotSizes[file])
1073 for (
const auto& file : snapshotFiles)
1075 if (currentFiles.find(file) == currentFiles.end())
1086 std::vector<fs::path> files;
1095 for (
const auto& entry : fs::recursive_directory_iterator(snap->
snapshotPath, ec))
1097 if (entry.is_regular_file())
1099 files.push_back(fs::relative(entry.path(), snap->
snapshotPath));
1104 Logger::error(
"[PrefixSnapshot] Directory iteration error: {}", ec.message());
1116 std::lock_guard<std::mutex> lock(impl_->mutex);
1119 for (
const auto& [
id, snap] : impl_->snapshots)
1128 std::lock_guard<std::mutex> lock(impl_->mutex);
1131 std::map<std::string, std::vector<PrefixSnapshot>> byPrefix;
1132 for (
const auto& [
id, snap] : impl_->snapshots)
1134 byPrefix[snap.
prefixPath.string()].push_back(snap);
1139 for (
auto& [prefix, snaps] : byPrefix)
1143 { return a.created > b.created; });
1146 for (
size_t i =
static_cast<size_t>(keepCount); i < snaps.size(); i++)
1148 if (deleteSnapshotUnlocked(snaps[i].
id))
1155 Logger::info(
"[PrefixSnapshot] Cleaned up {} old snapshots", deleted);
1161 auto now = std::chrono::system_clock::now();
1162 auto maxAge = std::chrono::hours(maxAgeDays * 24);
1166 for (
const auto& snap : snapshots)
1174 auto age = now - snap.
created;
1184 Logger::info(
"[PrefixSnapshot] Cleaned up {} old auto-snapshots", deleted);
1189 const fs::path& outputPath,
1203 int result = std::system(cmd.c_str());
1204 if (result == -1 || !WIFEXITED(result) || WEXITSTATUS(result) != 0)
1206 Logger::error(
"[PrefixSnapshot] Failed to export snapshot");
1210 Logger::info(
"[PrefixSnapshot] Exported snapshot to {}", outputPath.string());
1214std::optional<PrefixSnapshot>
1218 if (!fs::exists(archivePath))
1220 Logger::error(
"[PrefixSnapshot] Archive not found: {}", archivePath.string());
1221 return std::nullopt;
1225 std::string
id = generateSnapshotId();
1226 fs::path destDir = impl_->snapshotDir / id;
1236 std::string archiveTopDir;
1238 std::string listCmd =
1243 Logger::error(
"[PrefixSnapshot] Failed to list archive contents");
1244 return std::nullopt;
1246 std::array<char, 4096> buf;
1247 bool traversalFound =
false;
1248 while (fgets(buf.data(),
static_cast<int>(buf.size()), listPipe.
get()) !=
nullptr)
1250 std::string entry(buf.data());
1251 if (!entry.empty() && entry.back() ==
'\n')
1254 if (archiveTopDir.empty())
1256 fs::path entryPath(entry);
1257 if (!entryPath.empty())
1258 archiveTopDir = entryPath.begin()->string();
1260 for (
const auto& component : fs::path(entry))
1262 if (component ==
"..")
1264 Logger::error(
"[PrefixSnapshot] Archive contains path traversal entry: {}",
1266 traversalFound =
true;
1277 const int listStatus = listPipe.
close();
1279 return std::nullopt;
1280 if (listStatus == -1 || (WIFEXITED(listStatus) && WEXITSTATUS(listStatus) != 0))
1282 Logger::error(
"[PrefixSnapshot] tar listing failed (status={}); refusing to extract",
1284 return std::nullopt;
1288 if (archiveTopDir.empty())
1290 Logger::error(
"[PrefixSnapshot] Archive appears to be empty");
1291 return std::nullopt;
1295 " --no-absolute-filenames" +
" -C " +
1298 int result = std::system(cmd.c_str());
1299 if (result == -1 || !WIFEXITED(result) || WEXITSTATUS(result) != 0)
1301 Logger::error(
"[PrefixSnapshot] Failed to extract archive");
1302 return std::nullopt;
1306 fs::path extractedDir = impl_->snapshotDir / archiveTopDir;
1307 if (extractedDir != destDir)
1309 std::error_code renameEc;
1310 fs::rename(extractedDir, destDir, renameEc);
1313 Logger::error(
"[PrefixSnapshot] Failed to rename extracted dir '{}' to '{}': {}",
1314 extractedDir.string(), destDir.string(), renameEc.message());
1315 fs::remove_all(extractedDir, renameEc);
1316 return std::nullopt;
1329 std::error_code sweepEc;
1330 std::error_code canonRootEc;
1331 const fs::path canonRoot = fs::weakly_canonical(destDir, canonRootEc);
1332 for (
auto it = fs::recursive_directory_iterator(
1333 destDir, fs::directory_options::skip_permission_denied, sweepEc);
1334 it != fs::recursive_directory_iterator(); it.increment(sweepEc))
1338 std::error_code linkEc;
1339 if (!it->is_symlink(linkEc) || linkEc)
1341 const fs::path linkPath = it->path();
1342 fs::path target = fs::read_symlink(linkPath, linkEc);
1345 fs::remove(linkPath, linkEc);
1350 fs::path resolved = target.is_absolute() ? target : (linkPath.parent_path() / target);
1351 std::error_code canonEc;
1352 const fs::path canonTarget = fs::weakly_canonical(resolved, canonEc);
1353 std::error_code relEc;
1354 const fs::path rel =
1355 (canonRootEc || canonEc) ? fs::path() : fs::relative(canonTarget, canonRoot, relEc);
1356 const bool escapes =
1357 canonRootEc || canonEc || relEc || rel.empty() || *rel.begin() ==
"..";
1360 Logger::warn(
"[PrefixSnapshot] Removing imported symlink escaping the prefix: "
1362 linkPath.string(), target.string());
1363 std::error_code rmEc;
1364 fs::remove(linkPath, rmEc);
1372 snapshot.
name =
"Imported: " + archivePath.filename().string();
1374 snapshot.
created = std::chrono::system_clock::now();
1378 for (
const auto& entry : fs::recursive_directory_iterator(destDir, ec))
1380 if (entry.is_regular_file())
1383 snapshot.
sizeBytes +=
static_cast<int64_t
>(entry.file_size());
1388 Logger::error(
"[PrefixSnapshot] Directory iteration error: {}", ec.message());
1392 std::lock_guard<std::mutex> lock(impl_->mutex);
1393 impl_->snapshots[snapshot.
id] = snapshot;
1394 saveSnapshotIndex();
1397 Logger::info(
"[PrefixSnapshot] Imported snapshot {}", snapshot.
id);
1407 std::lock_guard<std::mutex> lock(impl_->mutex);
1408 impl_->snapshotDir = path;
1411 fs::create_directories(path, ec);
1416 std::lock_guard<std::mutex> lock(impl_->mutex);
1417 return impl_->snapshotDir;
1422 std::lock_guard<std::mutex> lock(impl_->mutex);
1423 impl_->compressionEnabled = enabled;
1428 std::lock_guard<std::mutex> lock(impl_->mutex);
1429 impl_->maxStorageBytes = maxBytes;
1434 std::lock_guard<std::mutex> lk(impl_->cancelMutex);
1435 for (
auto& wp : impl_->activeCancelFlags)
1437 if (
auto sp = wp.lock())
static void info(const std::string &message, Args &&... args)
Log info message (general information).
static void error(const std::string &message, Args &&... args)
Log error message (recoverable failure).
static void warn(const std::string &message, Args &&... args)
Log warning message (non-critical issue).
RAII wrapper for FILE* from popen().
int close()
Close the pipe early and return pclose's exit status.
FILE * get() const
Get raw FILE* pointer.
std::vector< PrefixSnapshot > getSnapshotsForGame(const std::string &gameId) const
Get snapshots for a specific game.
bool updateDescription(const std::string &snapshotId, const std::string &description)
Update snapshot description.
std::optional< PrefixSnapshot > importSnapshot(const std::filesystem::path &archivePath, const ProgressCallback &progressCallback=nullptr)
Import a snapshot from archive.
bool restoreSnapshot(const std::string &snapshotId, const ProgressCallback &progressCallback=nullptr)
Restore a prefix from snapshot.
bool exportSnapshot(const std::string &snapshotId, const std::filesystem::path &outputPath, const ProgressCallback &progressCallback=nullptr)
Export a snapshot to a portable archive.
void cancelOperation()
Cancel ongoing operation.
bool restoreFiles(const std::string &snapshotId, const std::vector< std::filesystem::path > &files)
Restore specific files from a snapshot.
PrefixDiff compareSnapshots(const std::string &snapshotId1, const std::string &snapshotId2) const
Compare two snapshots.
bool deleteSnapshot(const std::string &snapshotId)
Delete a snapshot.
std::vector< PrefixSnapshot > getSnapshotsForPrefix(const std::filesystem::path &prefixPath) const
Get snapshots for a specific prefix.
std::optional< PrefixSnapshot > getSnapshot(const std::string &snapshotId) const
Get a specific snapshot by ID.
std::optional< PrefixSnapshot > createAutoSnapshot(const std::filesystem::path &prefixPath, const std::string &gameId, const std::string &gameName, const std::string &reason)
Create automatic snapshot for a game.
PrefixDiff compareWithCurrent(const std::filesystem::path &prefixPath, const std::string &snapshotId) const
Compare current prefix state with a snapshot.
std::optional< PrefixSnapshot > createSnapshot(const std::filesystem::path &prefixPath, const std::string &name, const std::string &description="", const ProgressCallback &progressCallback=nullptr)
Create a full snapshot of a prefix.
std::future< std::optional< PrefixSnapshot > > createSnapshotAsync(const std::filesystem::path &prefixPath, const std::string &name, const std::string &description="", const ProgressCallback &progressCallback=nullptr)
Create a snapshot asynchronously.
std::vector< std::filesystem::path > listSnapshotFiles(const std::string &snapshotId) const
List files in a snapshot.
std::optional< PrefixSnapshot > createIncrementalSnapshot(const std::filesystem::path &prefixPath, const std::string &parentSnapshotId, const std::string &name, const ProgressCallback &progressCallback=nullptr)
Create an incremental snapshot (only changes since parent).
std::vector< PrefixSnapshot > getAllSnapshots() const
Get all snapshots.
int deleteSnapshotsForPrefix(const std::filesystem::path &prefixPath)
Delete all snapshots for a prefix.
void setCompressionEnabled(bool enabled)
Enable/disable compression for snapshots.
std::filesystem::path getSnapshotDirectory() const
Get current snapshot directory.
int64_t getTotalSnapshotSize() const
Get total space used by all snapshots.
void setSnapshotDirectory(const std::filesystem::path &path)
Set snapshot storage directory.
std::function< void(const SnapshotProgress &)> ProgressCallback
bool renameSnapshot(const std::string &snapshotId, const std::string &newName)
Rename a snapshot.
int cleanupOldAutoSnapshots(int maxAgeDays=30)
Clean up auto-snapshots older than specified days.
void setMaxStorageSize(int64_t maxBytes)
Set maximum total snapshot storage size.
int cleanupOldSnapshots(int keepCount=5)
Clean up old snapshots, keeping newest N per prefix.
std::future< bool > restoreSnapshotAsync(const std::string &snapshotId, const ProgressCallback &progressCallback=nullptr)
Restore a snapshot asynchronously.
static std::string escapeArg(const std::string &arg)
Escape a string for safe use as a shell argument.
Differences between two prefix states.
std::vector< std::filesystem::path > addedFiles
std::vector< std::filesystem::path > modifiedFiles
std::vector< std::filesystem::path > removedFiles
std::map< std::string, PrefixSnapshot > snapshots
std::vector< std::weak_ptr< std::atomic< bool > > > activeCancelFlags
Information about a saved prefix snapshot.
std::string description
Optional description.
std::filesystem::path prefixPath
Original prefix path.
std::string getFormattedDate() const
std::string parentSnapshotId
Parent snapshot for incremental.
int64_t sizeBytes
Snapshot size on disk.
std::string architecture
win32 or win64
std::string wineVersion
Wine/Proton version at snapshot time.
std::string getFormattedSize() const
std::string gameName
Associated game name.
bool isIncremental
Whether this is incremental.
std::string gameId
Associated game ID (if any).
std::chrono::system_clock::time_point created
std::string name
User-friendly name.
int fileCount
Number of files in snapshot.
std::filesystem::path snapshotPath
Where snapshot is stored.
std::string id
Unique snapshot ID.
Progress information during snapshot operations.
std::string getStateStr() const