game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
prefix_snapshot_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// prefix_snapshot_service.h
4// ==============================================================================
5// Wine prefix snapshot and restore service
6// ==============================================================================
7// Provides functionality for managing Wine prefix snapshots:
8// - Create snapshots of working Wine prefixes
9// - Restore prefixes to previous working state
10// - Compare prefix states for debugging
11// - Incremental snapshots for space efficiency
12//
13// ## Use Cases
14// - Backup working prefix before installing problematic software
15// - Restore after failed game patches
16// - Share working configurations between machines
17// - Debug prefix issues by comparing states
18//
19// ## Thread Safety
20// All public methods are thread-safe.
21// ==============================================================================
22
23#pragma once
24
25#include <chrono>
26#include <filesystem>
27#include <functional>
28#include <future>
29#include <memory>
30#include <optional>
31#include <string>
32#include <vector>
33
34// ==============================================================================
35// Data Structures
36// ==============================================================================
37
38/**
39 * @struct PrefixSnapshot
40 * @brief Information about a saved prefix snapshot
41 */
43{
44 std::string id; ///< Unique snapshot ID
45 std::string name; ///< User-friendly name
46 std::string description; ///< Optional description
47 std::filesystem::path prefixPath; ///< Original prefix path
48 std::filesystem::path snapshotPath; ///< Where snapshot is stored
49 std::chrono::system_clock::time_point created;
50 int64_t sizeBytes = 0; ///< Snapshot size on disk
51 int fileCount = 0; ///< Number of files in snapshot
52 bool isIncremental = false; ///< Whether this is incremental
53 std::string parentSnapshotId; ///< Parent snapshot for incremental
54 std::string gameId; ///< Associated game ID (if any)
55 std::string gameName; ///< Associated game name
56
57 // Metadata
58 std::string wineVersion; ///< Wine/Proton version at snapshot time
59 std::string architecture; ///< win32 or win64
60
61 std::string getFormattedSize() const;
62 std::string getFormattedDate() const;
63};
64
65/**
66 * @struct SnapshotProgress
67 * @brief Progress information during snapshot operations
68 */
70{
81
83 std::string currentFile;
85 int totalFiles = 0;
86 int64_t bytesProcessed = 0;
87 int64_t totalBytes = 0;
89 std::string errorMessage;
90
91 std::string getStateStr() const;
92};
93
94/**
95 * @struct PrefixDiff
96 * @brief Differences between two prefix states
97 */
99{
100 std::vector<std::filesystem::path> addedFiles;
101 std::vector<std::filesystem::path> removedFiles;
102 std::vector<std::filesystem::path> modifiedFiles;
103 std::vector<std::pair<std::string, std::string>> registryChanges; ///< Key, old->new value
104
105 bool hasChanges() const
106 {
107 return !addedFiles.empty() || !removedFiles.empty() || !modifiedFiles.empty() ||
108 !registryChanges.empty();
109 }
110
111 int totalChanges() const
112 {
113 return static_cast<int>(addedFiles.size() + removedFiles.size() + modifiedFiles.size() +
114 registryChanges.size());
115 }
116};
117
118// ==============================================================================
119// PrefixSnapshotService Class
120// ==============================================================================
121
122/**
123 * @class PrefixSnapshotService
124 * @brief Service for managing Wine prefix snapshots
125 *
126 * Provides methods for creating, restoring, and managing prefix snapshots
127 * for Wine and Proton game prefixes.
128 *
129 * ## Usage Example
130 * @code
131 * PrefixSnapshotService service;
132 *
133 * // Create snapshot before risky operation
134 * auto snapshot = service.createSnapshot(prefixPath, "Before patch 1.5");
135 *
136 * // ... something goes wrong ...
137 *
138 * // Restore to working state
139 * service.restoreSnapshot(snapshot.id);
140 * @endcode
141 */
143{
144 public:
145 using ProgressCallback = std::function<void(const SnapshotProgress&)>;
146
149
150 // ==========================================================================
151 // Snapshot Creation
152 // ==========================================================================
153
154 /**
155 * @brief Create a full snapshot of a prefix
156 * @param prefixPath Path to Wine prefix
157 * @param name Snapshot name
158 * @param description Optional description
159 * @param progressCallback Progress updates
160 * @return Created snapshot info, or nullopt on failure
161 */
162 std::optional<PrefixSnapshot>
163 createSnapshot(const std::filesystem::path& prefixPath, const std::string& name,
164 const std::string& description = "",
165 const ProgressCallback& progressCallback = nullptr);
166
167 /**
168 * @brief Create a snapshot asynchronously
169 */
170 std::future<std::optional<PrefixSnapshot>>
171 createSnapshotAsync(const std::filesystem::path& prefixPath, const std::string& name,
172 const std::string& description = "",
173 const ProgressCallback& progressCallback = nullptr);
174
175 /**
176 * @brief Create an incremental snapshot (only changes since parent)
177 * @param prefixPath Path to Wine prefix
178 * @param parentSnapshotId ID of parent snapshot
179 * @param name Snapshot name
180 * @param progressCallback Progress updates
181 * @return Created snapshot info, or nullopt on failure
182 */
183 std::optional<PrefixSnapshot>
184 createIncrementalSnapshot(const std::filesystem::path& prefixPath,
185 const std::string& parentSnapshotId, const std::string& name,
186 const ProgressCallback& progressCallback = nullptr);
187
188 /**
189 * @brief Create automatic snapshot for a game
190 * @param prefixPath Path to Wine prefix
191 * @param gameId Game ID
192 * @param gameName Game name
193 * @param reason Reason for snapshot (e.g., "pre-launch", "post-install")
194 * @return Created snapshot info
195 */
196 std::optional<PrefixSnapshot> createAutoSnapshot(const std::filesystem::path& prefixPath,
197 const std::string& gameId,
198 const std::string& gameName,
199 const std::string& reason);
200
201 // ==========================================================================
202 // Snapshot Restoration
203 // ==========================================================================
204
205 /**
206 * @brief Restore a prefix from snapshot
207 * @param snapshotId Snapshot ID to restore
208 * @param progressCallback Progress updates
209 * @return True on success
210 *
211 * Creates a backup of current state before restoring.
212 */
213 bool restoreSnapshot(const std::string& snapshotId,
214 const ProgressCallback& progressCallback = nullptr);
215
216 /**
217 * @brief Restore a snapshot asynchronously
218 */
219 std::future<bool> restoreSnapshotAsync(const std::string& snapshotId,
220 const ProgressCallback& progressCallback = nullptr);
221
222 /**
223 * @brief Restore specific files from a snapshot
224 * @param snapshotId Snapshot ID
225 * @param files List of files to restore (relative paths)
226 * @return True on success
227 */
228 bool restoreFiles(const std::string& snapshotId,
229 const std::vector<std::filesystem::path>& files);
230
231 // ==========================================================================
232 // Snapshot Management
233 // ==========================================================================
234
235 /**
236 * @brief Get all snapshots
237 * @return Vector of all snapshots
238 */
239 std::vector<PrefixSnapshot> getAllSnapshots() const;
240
241 /**
242 * @brief Get snapshots for a specific prefix
243 * @param prefixPath Prefix path
244 * @return Vector of snapshots for that prefix
245 */
246 std::vector<PrefixSnapshot>
247 getSnapshotsForPrefix(const std::filesystem::path& prefixPath) const;
248
249 /**
250 * @brief Get snapshots for a specific game
251 * @param gameId Game ID
252 * @return Vector of snapshots for that game
253 */
254 std::vector<PrefixSnapshot> getSnapshotsForGame(const std::string& gameId) const;
255
256 /**
257 * @brief Get a specific snapshot by ID
258 * @param snapshotId Snapshot ID
259 * @return Snapshot if found
260 */
261 std::optional<PrefixSnapshot> getSnapshot(const std::string& snapshotId) const;
262
263 /**
264 * @brief Delete a snapshot
265 * @param snapshotId Snapshot ID to delete
266 * @return True on success
267 */
268 bool deleteSnapshot(const std::string& snapshotId);
269
270 /**
271 * @brief Delete all snapshots for a prefix
272 * @param prefixPath Prefix path
273 * @return Number of snapshots deleted
274 */
275 int deleteSnapshotsForPrefix(const std::filesystem::path& prefixPath);
276
277 /**
278 * @brief Rename a snapshot
279 * @param snapshotId Snapshot ID
280 * @param newName New name
281 * @return True on success
282 */
283 bool renameSnapshot(const std::string& snapshotId, const std::string& newName);
284
285 /**
286 * @brief Update snapshot description
287 * @param snapshotId Snapshot ID
288 * @param description New description
289 * @return True on success
290 */
291 bool updateDescription(const std::string& snapshotId, const std::string& description);
292
293 // ==========================================================================
294 // Comparison and Analysis
295 // ==========================================================================
296
297 /**
298 * @brief Compare two snapshots
299 * @param snapshotId1 First snapshot ID
300 * @param snapshotId2 Second snapshot ID
301 * @return Differences between snapshots
302 */
303 PrefixDiff compareSnapshots(const std::string& snapshotId1,
304 const std::string& snapshotId2) const;
305
306 /**
307 * @brief Compare current prefix state with a snapshot
308 * @param prefixPath Current prefix path
309 * @param snapshotId Snapshot to compare against
310 * @return Differences from snapshot
311 */
312 PrefixDiff compareWithCurrent(const std::filesystem::path& prefixPath,
313 const std::string& snapshotId) const;
314
315 /**
316 * @brief List files in a snapshot
317 * @param snapshotId Snapshot ID
318 * @return List of files in snapshot
319 */
320 std::vector<std::filesystem::path> listSnapshotFiles(const std::string& snapshotId) const;
321
322 // ==========================================================================
323 // Space Management
324 // ==========================================================================
325
326 /**
327 * @brief Get total space used by all snapshots
328 * @return Total size in bytes
329 */
330 int64_t getTotalSnapshotSize() const;
331
332 /**
333 * @brief Clean up old snapshots, keeping newest N per prefix
334 * @param keepCount Number of snapshots to keep per prefix
335 * @return Number of snapshots deleted
336 */
337 int cleanupOldSnapshots(int keepCount = 5);
338
339 /**
340 * @brief Clean up auto-snapshots older than specified days
341 * @param maxAgeDays Maximum age in days
342 * @return Number of snapshots deleted
343 */
344 int cleanupOldAutoSnapshots(int maxAgeDays = 30);
345
346 /**
347 * @brief Export a snapshot to a portable archive
348 * @param snapshotId Snapshot ID
349 * @param outputPath Output archive path
350 * @param progressCallback Progress updates
351 * @return True on success
352 */
353 bool exportSnapshot(const std::string& snapshotId, const std::filesystem::path& outputPath,
354 const ProgressCallback& progressCallback = nullptr);
355
356 /**
357 * @brief Import a snapshot from archive
358 * @param archivePath Archive file path
359 * @param progressCallback Progress updates
360 * @return Imported snapshot info, or nullopt on failure
361 */
362 std::optional<PrefixSnapshot>
363 importSnapshot(const std::filesystem::path& archivePath,
364 const ProgressCallback& progressCallback = nullptr);
365
366 // ==========================================================================
367 // Configuration
368 // ==========================================================================
369
370 /**
371 * @brief Set snapshot storage directory
372 * @param path Directory path
373 */
374 void setSnapshotDirectory(const std::filesystem::path& path);
375
376 /**
377 * @brief Get current snapshot directory
378 */
379 std::filesystem::path getSnapshotDirectory() const;
380
381 /**
382 * @brief Enable/disable compression for snapshots
383 * @param enabled Whether to compress
384 */
385 void setCompressionEnabled(bool enabled);
386
387 /**
388 * @brief Set maximum total snapshot storage size
389 * @param maxBytes Maximum size in bytes (0 = unlimited)
390 */
391 void setMaxStorageSize(int64_t maxBytes);
392
393 /**
394 * @brief Cancel ongoing operation
395 */
396 void cancelOperation();
397
398 private:
399 struct Impl;
400 std::unique_ptr<Impl> impl_;
401
402 // Internal helpers
403 bool deleteSnapshotUnlocked(const std::string& snapshotId); // caller must hold impl_->mutex
404 std::string generateSnapshotId() const;
405 bool copyPrefix(const std::filesystem::path& src, const std::filesystem::path& dest,
406 const ProgressCallback& callback, std::atomic<bool>& cancelled);
407 void saveSnapshotMetadata(const PrefixSnapshot& snapshot);
408 void loadSnapshotIndex();
409 void saveSnapshotIndex();
410 std::string detectWineVersion(const std::filesystem::path& prefixPath) const;
411 std::string detectArchitecture(const std::filesystem::path& prefixPath) const;
412};
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.
Differences between two prefix states.
bool hasChanges() const
std::vector< std::filesystem::path > addedFiles
std::vector< std::filesystem::path > modifiedFiles
std::vector< std::pair< std::string, std::string > > registryChanges
Key, old->new value.
std::vector< std::filesystem::path > removedFiles
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