Repository navigation
Expand file tree
/
Copy pathRecoverySource.cs
More file actions
165 lines (156 loc) · 7.07 KB
/
Copy pathRecoverySource.cs
File metadata and controls
165 lines (156 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.ComponentModel;
using OpenClaw.Launcher.Session;
namespace OpenClaw.Launcher.StateTransfer;
internal sealed class RecoverySource : IDisposable
{
private readonly TrustedPath.ValidatedDirectory _profile;
private readonly Dictionary<string, FileStream> _files = new(StringComparer.OrdinalIgnoreCase);
private readonly List<TrustedPath.ValidatedDirectory> _directories = [];
private readonly Dictionary<string, string[]> _inventories = new(StringComparer.OrdinalIgnoreCase);
private readonly HashSet<string> _copied = new(StringComparer.OrdinalIgnoreCase);
public RecoverySource(string profile)
{
if (!Path.IsPathFullyQualified(profile))
{
throw new SessionException("Recovery requires an explicitly named absolute profile path.");
}
ProfilePath = Path.TrimEndingDirectorySeparator(Path.GetFullPath(profile));
if (string.Equals(ProfilePath, Path.GetPathRoot(ProfilePath), StringComparison.OrdinalIgnoreCase))
{
throw new SessionException("A filesystem root is not a recovery profile.");
}
try
{
TrustedPath.EnsureNoReparsePoints(Path.GetPathRoot(ProfilePath)!, ProfilePath);
_profile = TrustedPath.TryOpenValidatedDirectory(ProfilePath, expectedIdentity: null)
?? throw new SessionException(
"The selected profile is missing, inaccessible, or redirected. " +
"If access is denied, rerun recovery explicitly from an elevated terminal; no ACLs were changed.");
}
catch (Exception exception) when (IsAccessDenied(exception))
{
throw UnreadableSource(exception);
}
}
public string ProfilePath { get; }
public async Task<int> CopyAsync(
IEnumerable<string> paths,
string destination,
SessionWorkspaceOperation operation,
CancellationToken cancellationToken)
{
int copied = 0;
foreach (string path in paths)
{
cancellationToken.ThrowIfCancellationRequested();
string absolute = Path.GetFullPath(path);
string relative = StateTransferFiles.Relative(ProfilePath, absolute);
StateTransferFiles.ValidateProfileAsset(relative);
if (_copied.Contains(absolute))
{
continue;
}
// The profile may genuinely lack a configured directory; the helper decides whether that matters.
// Only a definite not-found counts as absent: an access error must surface, never read as absence.
try
{
FileAttributes attributes;
try
{
attributes = File.GetAttributes(absolute);
}
catch (Exception exception) when (exception is FileNotFoundException or DirectoryNotFoundException)
{
continue;
}
TrustedPath.EnsureNoReparsePoints(ProfilePath, absolute);
List<string> entries = [absolute];
if ((attributes & FileAttributes.Directory) != 0)
{
entries.AddRange(StateTransferFiles.Entries(absolute));
_inventories.Add(absolute, [.. entries]);
}
foreach (string entry in entries)
{
cancellationToken.ThrowIfCancellationRequested();
if (_copied.Contains(entry))
{
continue;
}
string target = Path.Combine(destination, StateTransferFiles.Relative(ProfilePath, entry));
if ((File.GetAttributes(entry) & FileAttributes.Directory) != 0)
{
TrustedPath.ValidatedDirectory directory =
TrustedPath.TryOpenValidatedDirectory(entry, expectedIdentity: null)
?? throw new SessionException("An offline source directory changed or became inaccessible.");
_directories.Add(directory);
operation.EnsureDirectory(target);
}
else
{
FileStream input = TrustedPath.OpenRead(ProfilePath, entry, protectContents: true);
_files.Add(entry, input);
operation.EnsureDirectory(Path.GetDirectoryName(target)!);
using Stream output = operation.CreateNew(target);
await input.CopyToAsync(output, cancellationToken).ConfigureAwait(false);
await output.FlushAsync(cancellationToken).ConfigureAwait(false);
}
_copied.Add(entry);
copied++;
}
}
catch (Exception exception) when (IsAccessDenied(exception))
{
throw UnreadableSource(exception);
}
catch (IOException exception) when (
exception.InnerException is Win32Exception { NativeErrorCode: 32 or 33 })
{
throw new SessionException(
"The selected profile is still in use. Stop its writers explicitly, then retry offline recovery. " +
"No source process was terminated.", exception);
}
}
EnsureUnchanged();
return copied;
}
public void EnsureUnchanged()
{
try
{
using TrustedPath.ValidatedDirectory current =
TrustedPath.TryOpenValidatedDirectory(ProfilePath, _profile.Identity) ??
throw new SessionException("The selected source profile changed during recovery.");
foreach ((string root, string[] expected) in _inventories)
{
string[] currentEntries = [root, .. StateTransferFiles.Entries(root)];
if (!expected.SequenceEqual(currentEntries, StringComparer.OrdinalIgnoreCase))
{
throw new SessionException("The offline source inventory changed during recovery. Stop its writers before retrying.");
}
}
}
catch (Exception exception) when (IsAccessDenied(exception))
{
throw UnreadableSource(exception);
}
}
public void Dispose()
{
foreach (FileStream file in _files.Values)
{
file.Dispose();
}
foreach (TrustedPath.ValidatedDirectory directory in _directories)
{
directory.Dispose();
}
_profile.Dispose();
}
private static bool IsAccessDenied(Exception exception) =>
exception is UnauthorizedAccessException ||
exception is IOException { InnerException: Win32Exception { NativeErrorCode: 5 } };
private static SessionException UnreadableSource(Exception exception) => new(
"The selected old profile is not readable. Rerun recovery explicitly from an elevated terminal. " +
"The source and its ACLs were not changed.", exception);
}