forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInProcessMount.cs
More file actions
574 lines (481 loc) · 24.5 KB
/
InProcessMount.cs
File metadata and controls
574 lines (481 loc) · 24.5 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
using GVFS.Common;
using GVFS.Common.FileSystem;
using GVFS.Common.Git;
using GVFS.Common.Http;
using GVFS.Common.NamedPipes;
using GVFS.Common.Prefetch;
using GVFS.Common.Tracing;
using GVFS.PlatformLoader;
using GVFS.Virtualization;
using GVFS.Virtualization.FileSystem;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace GVFS.Mount
{
public class InProcessMount
{
// Tests show that 250 is the max supported pipe name length
private const int MaxPipeNameLength = 250;
private const int MutexMaxWaitTimeMS = 500;
private const string ModifiedPathsVersion = "1";
private readonly bool showDebugWindow;
private FileSystemCallbacks fileSystemCallbacks;
private GVFSEnlistment enlistment;
private ITracer tracer;
private BackgroundPrefetcher prefetcher;
private CacheServerInfo cacheServer;
private RetryConfig retryConfig;
private GitStatusCacheConfig gitStatusCacheConfig;
private GVFSContext context;
private GVFSGitObjects gitObjects;
private MountState currentState;
private HeartbeatThread heartbeat;
private ManualResetEvent unmountEvent;
public InProcessMount(ITracer tracer, GVFSEnlistment enlistment, CacheServerInfo cacheServer, RetryConfig retryConfig, GitStatusCacheConfig gitStatusCacheConfig, bool showDebugWindow)
{
this.tracer = tracer;
this.retryConfig = retryConfig;
this.gitStatusCacheConfig = gitStatusCacheConfig;
this.cacheServer = cacheServer;
this.enlistment = enlistment;
this.showDebugWindow = showDebugWindow;
this.unmountEvent = new ManualResetEvent(false);
}
private enum MountState
{
Invalid = 0,
Mounting,
Ready,
Unmounting,
MountFailed
}
public void Mount(EventLevel verbosity, Keywords keywords)
{
this.currentState = MountState.Mounting;
// We must initialize repo metadata before starting the pipe server so it
// can immediately handle status requests
string error;
if (!RepoMetadata.TryInitialize(this.tracer, this.enlistment.DotGVFSRoot, out error))
{
this.FailMountAndExit("Failed to load repo metadata: " + error);
}
string gitObjectsRoot;
if (!RepoMetadata.Instance.TryGetGitObjectsRoot(out gitObjectsRoot, out error))
{
this.FailMountAndExit("Failed to determine git objects root from repo metadata: " + error);
}
string localCacheRoot;
if (!RepoMetadata.Instance.TryGetLocalCacheRoot(out localCacheRoot, out error))
{
this.FailMountAndExit("Failed to determine local cache path from repo metadata: " + error);
}
string blobSizesRoot;
if (!RepoMetadata.Instance.TryGetBlobSizesRoot(out blobSizesRoot, out error))
{
this.FailMountAndExit("Failed to determine blob sizes root from repo metadata: " + error);
}
this.tracer.RelatedEvent(
EventLevel.Informational,
"CachePathsLoaded",
new EventMetadata
{
{ "gitObjectsRoot", gitObjectsRoot },
{ "localCacheRoot", localCacheRoot },
{ "blobSizesRoot", blobSizesRoot },
});
this.enlistment.InitializeCachePaths(localCacheRoot, gitObjectsRoot, blobSizesRoot);
using (NamedPipeServer pipeServer = this.StartNamedPipe())
{
this.tracer.RelatedEvent(
EventLevel.Informational,
$"{nameof(this.Mount)}_StartedNamedPipe",
new EventMetadata { { "NamedPipeName", this.enlistment.NamedPipeName } });
this.context = this.CreateContext();
if (this.context.Unattended)
{
this.tracer.RelatedEvent(EventLevel.Critical, GVFSConstants.UnattendedEnvironmentVariable, null);
}
this.ValidateMountPoints();
string errorMessage;
if (!HooksInstaller.TryUpdateHooks(this.context, out errorMessage))
{
this.FailMountAndExit(errorMessage);
}
GVFSPlatform.Instance.ConfigureVisualStudio(this.enlistment.GitBinPath, this.tracer);
this.MountAndStartWorkingDirectoryCallbacks(this.cacheServer);
Console.Title = "GVFS " + ProcessHelper.GetCurrentProcessVersion() + " - " + this.enlistment.EnlistmentRoot;
this.tracer.RelatedEvent(
EventLevel.Informational,
"Mount",
new EventMetadata
{
// Use TracingConstants.MessageKey.InfoMessage rather than TracingConstants.MessageKey.CriticalMessage
// as this message should not appear as an error
{ TracingConstants.MessageKey.InfoMessage, "Virtual repo is ready" },
},
Keywords.Telemetry);
this.currentState = MountState.Ready;
this.unmountEvent.WaitOne();
}
}
private GVFSContext CreateContext()
{
PhysicalFileSystem fileSystem = new PhysicalFileSystem();
GitRepo gitRepo = this.CreateOrReportAndExit(
() => new GitRepo(
this.tracer,
this.enlistment,
fileSystem),
"Failed to read git repo");
return new GVFSContext(this.tracer, fileSystem, gitRepo, this.enlistment);
}
private void ValidateMountPoints()
{
DirectoryInfo workingDirectoryRootInfo = new DirectoryInfo(this.enlistment.WorkingDirectoryRoot);
if (!workingDirectoryRootInfo.Exists)
{
this.FailMountAndExit("Failed to initialize file system callbacks. Directory \"{0}\" must exist.", this.enlistment.WorkingDirectoryRoot);
}
string dotGitPath = Path.Combine(this.enlistment.WorkingDirectoryRoot, GVFSConstants.DotGit.Root);
DirectoryInfo dotGitPathInfo = new DirectoryInfo(dotGitPath);
if (!dotGitPathInfo.Exists)
{
this.FailMountAndExit("Failed to mount. Directory \"{0}\" must exist.", dotGitPathInfo);
}
}
private NamedPipeServer StartNamedPipe()
{
try
{
return NamedPipeServer.StartNewServer(this.enlistment.NamedPipeName, this.tracer, this.HandleRequest);
}
catch (PipeNameLengthException)
{
this.FailMountAndExit("Failed to create mount point. Mount path exceeds the maximum number of allowed characters");
return null;
}
}
private void FailMountAndExit(string error, params object[] args)
{
this.currentState = MountState.MountFailed;
this.tracer.RelatedError(error, args);
if (this.showDebugWindow)
{
Console.WriteLine("\nPress Enter to Exit");
Console.ReadLine();
}
if (this.fileSystemCallbacks != null)
{
this.fileSystemCallbacks.Dispose();
this.fileSystemCallbacks = null;
}
Environment.Exit((int)ReturnCode.GenericError);
}
private T CreateOrReportAndExit<T>(Func<T> factory, string reportMessage)
{
try
{
return factory();
}
catch (Exception e)
{
this.FailMountAndExit(reportMessage + " " + e.ToString());
throw;
}
}
private void HandleRequest(ITracer tracer, string request, NamedPipeServer.Connection connection)
{
NamedPipeMessages.Message message = NamedPipeMessages.Message.FromString(request);
switch (message.Header)
{
case NamedPipeMessages.GetStatus.Request:
this.HandleGetStatusRequest(connection);
break;
case NamedPipeMessages.Unmount.Request:
this.HandleUnmountRequest(connection);
break;
case NamedPipeMessages.AcquireLock.AcquireRequest:
this.HandleLockRequest(message.Body, connection);
break;
case NamedPipeMessages.ReleaseLock.Request:
this.HandleReleaseLockRequest(message.Body, connection);
break;
case NamedPipeMessages.DownloadObject.DownloadRequest:
this.HandleDownloadObjectRequest(message, connection);
break;
case NamedPipeMessages.ModifiedPaths.ListRequest:
this.HandleModifiedPathsListRequest(message, connection);
break;
case NamedPipeMessages.RunPostFetchJob.PostFetchJob:
this.HandlePostFetchJobRequest(message, connection);
break;
default:
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", "Mount");
metadata.Add("Header", message.Header);
this.tracer.RelatedError(metadata, "HandleRequest: Unknown request");
connection.TrySendResponse(NamedPipeMessages.UnknownRequest);
break;
}
}
private void HandleLockRequest(string messageBody, NamedPipeServer.Connection connection)
{
NamedPipeMessages.AcquireLock.Response response;
NamedPipeMessages.LockRequest request = new NamedPipeMessages.LockRequest(messageBody);
NamedPipeMessages.LockData requester = request.RequestData;
if (this.currentState == MountState.Unmounting)
{
response = new NamedPipeMessages.AcquireLock.Response(NamedPipeMessages.AcquireLock.UnmountInProgressResult);
EventMetadata metadata = new EventMetadata();
metadata.Add("LockRequest", requester.ToString());
metadata.Add(TracingConstants.MessageKey.InfoMessage, "Request denied, unmount in progress");
this.tracer.RelatedEvent(EventLevel.Informational, "HandleLockRequest_UnmountInProgress", metadata);
}
else if (this.currentState != MountState.Ready)
{
response = new NamedPipeMessages.AcquireLock.Response(NamedPipeMessages.AcquireLock.MountNotReadyResult);
}
else
{
bool lockAcquired = false;
NamedPipeMessages.LockData existingExternalHolder = null;
string denyGVFSMessage = null;
bool lockAvailable = this.context.Repository.GVFSLock.IsLockAvailableForExternalRequestor(out existingExternalHolder);
bool isReadyForExternalLockRequests = this.fileSystemCallbacks.IsReadyForExternalAcquireLockRequests(requester, out denyGVFSMessage);
if (!requester.CheckAvailabilityOnly && isReadyForExternalLockRequests)
{
lockAcquired = this.context.Repository.GVFSLock.TryAcquireLockForExternalRequestor(requester, out existingExternalHolder);
}
if (requester.CheckAvailabilityOnly && lockAvailable && isReadyForExternalLockRequests)
{
response = new NamedPipeMessages.AcquireLock.Response(NamedPipeMessages.AcquireLock.AvailableResult);
}
else if (lockAcquired)
{
response = new NamedPipeMessages.AcquireLock.Response(NamedPipeMessages.AcquireLock.AcceptResult);
}
else if (existingExternalHolder == null)
{
response = new NamedPipeMessages.AcquireLock.Response(NamedPipeMessages.AcquireLock.DenyGVFSResult, responseData: null, denyGVFSMessage: denyGVFSMessage);
}
else
{
response = new NamedPipeMessages.AcquireLock.Response(NamedPipeMessages.AcquireLock.DenyGitResult, existingExternalHolder);
}
}
connection.TrySendResponse(response.CreateMessage());
}
private void HandleReleaseLockRequest(string messageBody, NamedPipeServer.Connection connection)
{
NamedPipeMessages.LockRequest request = new NamedPipeMessages.LockRequest(messageBody);
if (request.RequestData == null)
{
this.tracer.RelatedError($"{nameof(HandleReleaseLockRequest)} received invalid lock request with body '{messageBody}'");
this.UnmountAndStopWorkingDirectoryCallbacks();
Environment.Exit((int)ReturnCode.NullRequestData);
}
NamedPipeMessages.ReleaseLock.Response response = this.fileSystemCallbacks.TryReleaseExternalLock(request.RequestData.PID);
connection.TrySendResponse(response.CreateMessage());
}
private void HandleModifiedPathsListRequest(NamedPipeMessages.Message message, NamedPipeServer.Connection connection)
{
NamedPipeMessages.ModifiedPaths.Response response;
NamedPipeMessages.ModifiedPaths.Request request = new NamedPipeMessages.ModifiedPaths.Request(message);
if (request == null)
{
response = new NamedPipeMessages.ModifiedPaths.Response(NamedPipeMessages.UnknownRequest);
}
else if (this.currentState != MountState.Ready)
{
response = new NamedPipeMessages.ModifiedPaths.Response(NamedPipeMessages.MountNotReadyResult);
}
else
{
if (request.Version != ModifiedPathsVersion)
{
response = new NamedPipeMessages.ModifiedPaths.Response(NamedPipeMessages.ModifiedPaths.InvalidVersion);
}
else
{
string data = string.Join("\0", this.fileSystemCallbacks.GetAllModifiedPaths()) + "\0";
response = new NamedPipeMessages.ModifiedPaths.Response(NamedPipeMessages.ModifiedPaths.SuccessResult, data);
}
}
connection.TrySendResponse(response.CreateMessage());
}
private void HandleDownloadObjectRequest(NamedPipeMessages.Message message, NamedPipeServer.Connection connection)
{
NamedPipeMessages.DownloadObject.Response response;
NamedPipeMessages.DownloadObject.Request request = new NamedPipeMessages.DownloadObject.Request(message);
string objectSha = request.RequestSha;
if (this.currentState != MountState.Ready)
{
response = new NamedPipeMessages.DownloadObject.Response(NamedPipeMessages.MountNotReadyResult);
}
else
{
if (!SHA1Util.IsValidShaFormat(objectSha))
{
response = new NamedPipeMessages.DownloadObject.Response(NamedPipeMessages.DownloadObject.InvalidSHAResult);
}
else
{
Stopwatch downloadTime = Stopwatch.StartNew();
if (this.gitObjects.TryDownloadAndSaveObject(objectSha, GVFSGitObjects.RequestSource.NamedPipeMessage) == GitObjects.DownloadAndSaveObjectResult.Success)
{
response = new NamedPipeMessages.DownloadObject.Response(NamedPipeMessages.DownloadObject.SuccessResult);
}
else
{
response = new NamedPipeMessages.DownloadObject.Response(NamedPipeMessages.DownloadObject.DownloadFailed);
}
bool isBlob;
this.context.Repository.TryGetIsBlob(objectSha, out isBlob);
this.context.Repository.GVFSLock.Stats.RecordObjectDownload(isBlob, downloadTime.ElapsedMilliseconds);
}
}
connection.TrySendResponse(response.CreateMessage());
}
private void HandlePostFetchJobRequest(NamedPipeMessages.Message message, NamedPipeServer.Connection connection)
{
NamedPipeMessages.RunPostFetchJob.Request request = new NamedPipeMessages.RunPostFetchJob.Request(message);
this.tracer.RelatedInfo("Received post-fetch job request with body {0}", message.Body);
NamedPipeMessages.RunPostFetchJob.Response response;
if (this.currentState == MountState.Ready)
{
List<string> packIndexes = JsonConvert.DeserializeObject<List<string>>(message.Body);
this.fileSystemCallbacks.LaunchPostFetchJob(packIndexes);
response = new NamedPipeMessages.RunPostFetchJob.Response(NamedPipeMessages.RunPostFetchJob.QueuedResult);
}
else
{
response = new NamedPipeMessages.RunPostFetchJob.Response(NamedPipeMessages.RunPostFetchJob.MountNotReadyResult);
}
connection.TrySendResponse(response.CreateMessage());
}
private void HandleGetStatusRequest(NamedPipeServer.Connection connection)
{
NamedPipeMessages.GetStatus.Response response = new NamedPipeMessages.GetStatus.Response();
response.EnlistmentRoot = this.enlistment.EnlistmentRoot;
response.LocalCacheRoot = !string.IsNullOrWhiteSpace(this.enlistment.LocalCacheRoot) ? this.enlistment.LocalCacheRoot : this.enlistment.GitObjectsRoot;
response.RepoUrl = this.enlistment.RepoUrl;
response.CacheServer = this.cacheServer.ToString();
response.LockStatus = this.context?.Repository.GVFSLock != null ? this.context.Repository.GVFSLock.GetStatus() : "Unavailable";
response.DiskLayoutVersion = RepoMetadata.Instance.GetCurrentDiskLayoutVersion();
switch (this.currentState)
{
case MountState.Mounting:
response.MountStatus = NamedPipeMessages.GetStatus.Mounting;
break;
case MountState.Ready:
response.MountStatus = NamedPipeMessages.GetStatus.Ready;
response.BackgroundOperationCount = this.fileSystemCallbacks.BackgroundOperationCount;
break;
case MountState.Unmounting:
response.MountStatus = NamedPipeMessages.GetStatus.Unmounting;
break;
case MountState.MountFailed:
response.MountStatus = NamedPipeMessages.GetStatus.MountFailed;
break;
default:
response.MountStatus = NamedPipeMessages.UnknownGVFSState;
break;
}
connection.TrySendResponse(response.ToJson());
}
private void HandleUnmountRequest(NamedPipeServer.Connection connection)
{
switch (this.currentState)
{
case MountState.Mounting:
connection.TrySendResponse(NamedPipeMessages.Unmount.NotMounted);
break;
// Even if the previous mount failed, attempt to unmount anyway. Otherwise the user has no
// recourse but to kill the process.
case MountState.MountFailed:
goto case MountState.Ready;
case MountState.Ready:
this.currentState = MountState.Unmounting;
connection.TrySendResponse(NamedPipeMessages.Unmount.Acknowledged);
this.UnmountAndStopWorkingDirectoryCallbacks();
connection.TrySendResponse(NamedPipeMessages.Unmount.Completed);
this.unmountEvent.Set();
Environment.Exit((int)ReturnCode.Success);
break;
case MountState.Unmounting:
connection.TrySendResponse(NamedPipeMessages.Unmount.AlreadyUnmounting);
break;
default:
connection.TrySendResponse(NamedPipeMessages.UnknownGVFSState);
break;
}
}
private void MountAndStartWorkingDirectoryCallbacks(CacheServerInfo cache)
{
string error;
if (!this.context.Enlistment.Authentication.TryRefreshCredentials(this.context.Tracer, out error))
{
this.FailMountAndExit("Failed to obtain git credentials: " + error);
}
GitObjectsHttpRequestor objectRequestor = new GitObjectsHttpRequestor(this.context.Tracer, this.context.Enlistment, cache, this.retryConfig);
this.gitObjects = new GVFSGitObjects(this.context, objectRequestor);
FileSystemVirtualizer virtualizer = this.CreateOrReportAndExit(() => GVFSPlatformLoader.CreateFileSystemVirtualizer(this.context, this.gitObjects), "Failed to create src folder virtualizer");
GitStatusCache gitStatusCache = (!this.context.Unattended && GVFSPlatform.Instance.IsGitStatusCacheSupported()) ? new GitStatusCache(this.context, this.gitStatusCacheConfig) : null;
if (gitStatusCache != null)
{
this.tracer.RelatedInfo("Git status cache enabled. Backoff time: {0}ms", this.gitStatusCacheConfig.BackoffTime.TotalMilliseconds);
}
this.fileSystemCallbacks = this.CreateOrReportAndExit(() => new FileSystemCallbacks(this.context, this.gitObjects, RepoMetadata.Instance, virtualizer, gitStatusCache), "Failed to create src folder callback listener");
this.prefetcher = this.CreateOrReportAndExit(() => new BackgroundPrefetcher(this.tracer, this.enlistment, this.context.FileSystem, this.gitObjects), "Failed to start background prefetcher");
int majorVersion;
int minorVersion;
if (!RepoMetadata.Instance.TryGetOnDiskLayoutVersion(out majorVersion, out minorVersion, out error))
{
this.FailMountAndExit("Error: {0}", error);
}
if (majorVersion != RepoMetadata.DiskLayoutVersion.CurrentMajorVersion)
{
this.FailMountAndExit(
"Error: On disk version ({0}) does not match current version ({1})",
majorVersion,
RepoMetadata.DiskLayoutVersion.CurrentMajorVersion);
}
try
{
if (!this.fileSystemCallbacks.TryStart(out error))
{
this.FailMountAndExit("Error: {0}. \r\nPlease confirm that gvfs clone completed without error.", error);
}
}
catch (Exception e)
{
this.FailMountAndExit("Failed to initialize src folder callbacks. {0}", e.ToString());
}
this.heartbeat = new HeartbeatThread(this.tracer, this.fileSystemCallbacks);
this.heartbeat.Start();
}
private void UnmountAndStopWorkingDirectoryCallbacks()
{
if (this.prefetcher != null)
{
this.prefetcher.Dispose();
this.prefetcher = null;
}
if (this.heartbeat != null)
{
this.heartbeat.Stop();
this.heartbeat = null;
}
if (this.fileSystemCallbacks != null)
{
this.fileSystemCallbacks.Stop();
this.fileSystemCallbacks.Dispose();
this.fileSystemCallbacks = null;
}
}
}
}