forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileSystemCallbacks.cs
More file actions
1038 lines (886 loc) · 45.9 KB
/
FileSystemCallbacks.cs
File metadata and controls
1038 lines (886 loc) · 45.9 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using GVFS.Common;
using GVFS.Common.FileSystem;
using GVFS.Common.Git;
using GVFS.Common.NamedPipes;
using GVFS.Common.Tracing;
using GVFS.Virtualization.Background;
using GVFS.Virtualization.BlobSize;
using GVFS.Virtualization.FileSystem;
using GVFS.Virtualization.Projection;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
namespace GVFS.Virtualization
{
public class FileSystemCallbacks : IDisposable, IHeartBeatMetadataProvider
{
private const string EtwArea = nameof(FileSystemCallbacks);
private const string PostFetchLock = "post-fetch.lock";
private const string CommitGraphLock = "commit-graph.lock";
private const string MultiPackIndexLock = "multi-pack-index.lock";
private const string PostFetchTelemetryKey = "post-fetch";
private static readonly GitCommandLineParser.Verbs LeavesProjectionUnchangedVerbs =
GitCommandLineParser.Verbs.AddOrStage |
GitCommandLineParser.Verbs.Commit |
GitCommandLineParser.Verbs.Status |
GitCommandLineParser.Verbs.UpdateIndex;
private readonly string logsHeadPath;
private GVFSContext context;
private GVFSGitObjects gitObjects;
private ModifiedPathsDatabase modifiedPaths;
private ConcurrentHashSet<string> newlyCreatedFileAndFolderPaths;
private ConcurrentDictionary<string, PlaceHolderCreateCounter> placeHolderCreationCount;
private BackgroundFileSystemTaskRunner backgroundFileSystemTaskRunner;
private FileSystemVirtualizer fileSystemVirtualizer;
private FileProperties logsHeadFileProperties;
private Thread postFetchJobThread;
private GitProcess postFetchGitProcess;
private object postFetchJobLock;
private bool stopping;
private GitStatusCache gitStatusCache;
private bool enableGitStatusCache;
public FileSystemCallbacks(GVFSContext context, GVFSGitObjects gitObjects, RepoMetadata repoMetadata, FileSystemVirtualizer fileSystemVirtualizer, GitStatusCache gitStatusCache)
: this(
context,
gitObjects,
repoMetadata,
new BlobSizes(context.Enlistment.BlobSizesRoot, context.FileSystem, context.Tracer),
gitIndexProjection: null,
backgroundFileSystemTaskRunner: null,
fileSystemVirtualizer: fileSystemVirtualizer,
gitStatusCache: gitStatusCache)
{
}
public FileSystemCallbacks(
GVFSContext context,
GVFSGitObjects gitObjects,
RepoMetadata repoMetadata,
BlobSizes blobSizes,
GitIndexProjection gitIndexProjection,
BackgroundFileSystemTaskRunner backgroundFileSystemTaskRunner,
FileSystemVirtualizer fileSystemVirtualizer,
GitStatusCache gitStatusCache = null)
{
this.logsHeadFileProperties = null;
this.postFetchJobLock = new object();
this.context = context;
this.gitObjects = gitObjects;
this.fileSystemVirtualizer = fileSystemVirtualizer;
this.placeHolderCreationCount = new ConcurrentDictionary<string, PlaceHolderCreateCounter>(StringComparer.OrdinalIgnoreCase);
this.newlyCreatedFileAndFolderPaths = new ConcurrentHashSet<string>(StringComparer.OrdinalIgnoreCase);
string error;
if (!ModifiedPathsDatabase.TryLoadOrCreate(
this.context.Tracer,
Path.Combine(this.context.Enlistment.DotGVFSRoot, GVFSConstants.DotGVFS.Databases.ModifiedPaths),
this.context.FileSystem,
out this.modifiedPaths,
out error))
{
throw new InvalidRepoException(error);
}
this.BlobSizes = blobSizes;
this.BlobSizes.Initialize();
PlaceholderListDatabase placeholders;
if (!PlaceholderListDatabase.TryCreate(
this.context.Tracer,
Path.Combine(this.context.Enlistment.DotGVFSRoot, GVFSConstants.DotGVFS.Databases.PlaceholderList),
this.context.FileSystem,
out placeholders,
out error))
{
throw new InvalidRepoException(error);
}
this.GitIndexProjection = gitIndexProjection ?? new GitIndexProjection(
context,
gitObjects,
this.BlobSizes,
repoMetadata,
fileSystemVirtualizer,
placeholders,
this.modifiedPaths);
if (backgroundFileSystemTaskRunner != null)
{
this.backgroundFileSystemTaskRunner = backgroundFileSystemTaskRunner;
this.backgroundFileSystemTaskRunner.SetCallbacks(
this.PreBackgroundOperation,
this.ExecuteBackgroundOperation,
this.PostBackgroundOperation);
}
else
{
this.backgroundFileSystemTaskRunner = new BackgroundFileSystemTaskRunner(
this.context,
this.PreBackgroundOperation,
this.ExecuteBackgroundOperation,
this.PostBackgroundOperation,
Path.Combine(context.Enlistment.DotGVFSRoot, GVFSConstants.DotGVFS.Databases.BackgroundFileSystemTasks));
}
this.enableGitStatusCache = gitStatusCache != null;
// If the status cache is not enabled, create a dummy GitStatusCache that will never be initialized
// This lets us from having to add null checks to callsites into GitStatusCache.
this.gitStatusCache = gitStatusCache ?? new GitStatusCache(context, TimeSpan.Zero);
this.logsHeadPath = Path.Combine(this.context.Enlistment.WorkingDirectoryRoot, GVFSConstants.DotGit.Logs.Head);
EventMetadata metadata = new EventMetadata();
metadata.Add("placeholders.Count", placeholders.EstimatedCount);
metadata.Add("background.Count", this.backgroundFileSystemTaskRunner.Count);
metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(FileSystemCallbacks)} created");
this.context.Tracer.RelatedEvent(EventLevel.Informational, $"{nameof(FileSystemCallbacks)}_Constructor", metadata);
}
public IProfilerOnlyIndexProjection GitIndexProjectionProfiler
{
get { return this.GitIndexProjection; }
}
public int BackgroundOperationCount
{
get { return this.backgroundFileSystemTaskRunner.Count; }
}
public BlobSizes BlobSizes { get; private set; }
public GitIndexProjection GitIndexProjection { get; private set; }
public bool IsMounted { get; private set; }
/// <summary>
/// Returns true for paths that begin with ".git\" (regardless of case)
/// </summary>
public static bool IsPathInsideDotGit(string relativePath)
{
return relativePath.StartsWith(GVFSConstants.DotGit.Root + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase);
}
public bool TryStart(out string error)
{
this.modifiedPaths.RemoveEntriesWithParentFolderEntry(this.context.Tracer);
this.modifiedPaths.WriteAllEntriesAndFlush();
if (!this.fileSystemVirtualizer.TryStart(this, out error))
{
return false;
}
this.GitIndexProjection.Initialize(this.backgroundFileSystemTaskRunner);
if (this.enableGitStatusCache)
{
this.gitStatusCache.Initialize();
}
this.backgroundFileSystemTaskRunner.Start();
this.IsMounted = true;
return true;
}
public void Stop()
{
this.stopping = true;
lock (this.postFetchJobLock)
{
GitProcess process = this.postFetchGitProcess;
if (process != null)
{
if (process.TryKillRunningProcess())
{
this.context.Tracer.RelatedEvent(
EventLevel.Informational,
PostFetchTelemetryKey + ": killed background Git process during " + nameof(this.Stop),
metadata: null);
}
else
{
this.context.Tracer.RelatedEvent(
EventLevel.Informational,
PostFetchTelemetryKey + ": failed to kill background Git process during " + nameof(this.Stop),
metadata: null);
}
}
}
// Shutdown the GitStatusCache before other
// components that it depends on.
this.gitStatusCache.Shutdown();
this.fileSystemVirtualizer.PrepareToStop();
this.backgroundFileSystemTaskRunner.Shutdown();
this.GitIndexProjection.Shutdown();
this.BlobSizes.Shutdown();
this.fileSystemVirtualizer.Stop();
this.IsMounted = false;
}
public void Dispose()
{
if (this.BlobSizes != null)
{
this.BlobSizes.Dispose();
this.BlobSizes = null;
}
if (this.fileSystemVirtualizer != null)
{
this.fileSystemVirtualizer.Dispose();
this.fileSystemVirtualizer = null;
}
if (this.GitIndexProjection != null)
{
this.GitIndexProjection.Dispose();
this.GitIndexProjection = null;
}
if (this.modifiedPaths != null)
{
this.modifiedPaths.Dispose();
this.modifiedPaths = null;
}
if (this.gitStatusCache != null)
{
this.gitStatusCache.Dispose();
this.gitStatusCache = null;
}
if (this.backgroundFileSystemTaskRunner != null)
{
this.backgroundFileSystemTaskRunner.Dispose();
this.backgroundFileSystemTaskRunner = null;
}
if (this.context != null)
{
this.context.Dispose();
this.context = null;
}
}
public bool IsReadyForExternalAcquireLockRequests(NamedPipeMessages.LockData requester, out string denyMessage)
{
if (!this.IsMounted)
{
denyMessage = "Waiting for mount to complete";
return false;
}
if (this.BackgroundOperationCount != 0)
{
denyMessage = "Waiting for GVFS to release the lock";
return false;
}
if (!this.GitIndexProjection.IsProjectionParseComplete())
{
denyMessage = "Waiting for GVFS to parse index and update placeholder files";
return false;
}
if (!this.gitStatusCache.IsReadyForExternalAcquireLockRequests(requester, out denyMessage))
{
return false;
}
// Even though we're returning true and saying it's safe to ask for the lock
// there is no guarantee that the lock will be acquired, because GVFS itself
// could obtain the lock before the external holder gets it. Setting up an
// appropriate error message in case that happens
denyMessage = "Waiting for GVFS to release the lock";
return true;
}
public EventMetadata GetMetadataForHeartBeat(ref EventLevel eventLevel)
{
EventMetadata metadata = new EventMetadata();
if (this.placeHolderCreationCount.Count > 0)
{
ConcurrentDictionary<string, PlaceHolderCreateCounter> collectedData = this.placeHolderCreationCount;
this.placeHolderCreationCount = new ConcurrentDictionary<string, PlaceHolderCreateCounter>(StringComparer.OrdinalIgnoreCase);
int count = 0;
foreach (KeyValuePair<string, PlaceHolderCreateCounter> processCount in
collectedData.OrderByDescending((KeyValuePair<string, PlaceHolderCreateCounter> kvp) => kvp.Value.Count))
{
++count;
if (count > 10)
{
break;
}
metadata.Add("ProcessName" + count, processCount.Key);
metadata.Add("ProcessCount" + count, processCount.Value.Count);
}
eventLevel = EventLevel.Informational;
}
metadata.Add("ModifiedPathsCount", this.modifiedPaths.Count);
metadata.Add("PlaceholderCount", this.GitIndexProjection.EstimatedPlaceholderCount);
if (this.gitStatusCache.WriteTelemetryandReset(metadata))
{
eventLevel = EventLevel.Informational;
}
metadata.Add(nameof(RepoMetadata.Instance.EnlistmentId), RepoMetadata.Instance.EnlistmentId);
metadata.Add(
"PhysicalDiskInfo",
GVFSPlatform.Instance.GetPhysicalDiskInfo(
this.context.Enlistment.WorkingDirectoryRoot,
sizeStatsOnly: true));
return metadata;
}
public NamedPipeMessages.ReleaseLock.Response TryReleaseExternalLock(int pid)
{
return this.GitIndexProjection.TryReleaseExternalLock(pid);
}
public IEnumerable<string> GetAllModifiedPaths()
{
return this.modifiedPaths.GetAllModifiedPaths();
}
public virtual void OnIndexFileChange()
{
string lockedGitCommand = this.context.Repository.GVFSLock.GetLockedGitCommand();
GitCommandLineParser gitCommand = new GitCommandLineParser(lockedGitCommand);
if (!gitCommand.IsValidGitCommand)
{
// Something wrote to the index without holding the GVFS lock, so we invalidate the projection
this.GitIndexProjection.InvalidateProjection();
this.InvalidateGitStatusCache();
// But this isn't something we expect to see, so log a warning
EventMetadata metadata = new EventMetadata
{
{ "Area", EtwArea },
{ TracingConstants.MessageKey.WarningMessage, "Index modified without git holding GVFS lock" },
};
this.context.Tracer.RelatedEvent(EventLevel.Warning, $"{nameof(this.OnIndexFileChange)}_NoLock", metadata);
}
else if (this.GitCommandLeavesProjectionUnchanged(gitCommand))
{
if (this.GitCommandRequiresModifiedPathValidationAfterIndexChange(gitCommand))
{
this.GitIndexProjection.InvalidateModifiedFiles();
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnIndexWriteRequiringModifiedPathsValidation());
}
this.InvalidateGitStatusCache();
}
else
{
this.GitIndexProjection.InvalidateProjection();
this.InvalidateGitStatusCache();
}
this.newlyCreatedFileAndFolderPaths.Clear();
}
public void InvalidateGitStatusCache()
{
this.gitStatusCache.Invalidate();
// If there are background tasks queued up, then it will be
// refreshed after they have been processed.
if (this.backgroundFileSystemTaskRunner.Count == 0)
{
this.gitStatusCache.RefreshAsynchronously();
}
}
public virtual void OnLogsHeadChange()
{
// Don't open the .git\logs\HEAD file here to check its attributes as we're in a callback for the .git folder
this.logsHeadFileProperties = null;
}
public void OnHeadOrRefChanged()
{
this.InvalidateGitStatusCache();
}
/// <summary>
/// This method signals that the repository git exclude file
/// has been modified (i.e. .git/info/exclude)
/// </summary>
public void OnExcludeFileChanged()
{
this.InvalidateGitStatusCache();
}
public void OnFileCreated(string relativePath)
{
this.newlyCreatedFileAndFolderPaths.Add(relativePath);
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFileCreated(relativePath));
}
public void OnFileOverwritten(string relativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFileOverwritten(relativePath));
}
public void OnFileSuperseded(string relativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFileSuperseded(relativePath));
}
public void OnFileConvertedToFull(string relativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFileConvertedToFull(relativePath));
}
public virtual void OnFileRenamed(string oldRelativePath, string newRelativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFileRenamed(oldRelativePath, newRelativePath));
}
public virtual void OnFileHardLinkCreated(string newLinkRelativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFileHardLinkCreated(newLinkRelativePath));
}
public virtual void OnFileSymLinkCreated(string newLinkRelativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFileSymLinkCreated(newLinkRelativePath));
}
public void OnFileDeleted(string relativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFileDeleted(relativePath));
}
public void OnFilePreDelete(string relativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFilePreDelete(relativePath));
}
public void OnFolderCreated(string relativePath)
{
this.newlyCreatedFileAndFolderPaths.Add(relativePath);
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFolderCreated(relativePath));
}
public virtual void OnFolderRenamed(string oldRelativePath, string newRelativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFolderRenamed(oldRelativePath, newRelativePath));
}
public void OnFolderDeleted(string relativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFolderDeleted(relativePath));
}
public void OnFolderPreDelete(string relativePath)
{
this.backgroundFileSystemTaskRunner.Enqueue(FileSystemTask.OnFolderPreDelete(relativePath));
}
public void OnPlaceholderFileCreated(string relativePath, string sha, string triggeringProcessImageFileName)
{
this.GitIndexProjection.OnPlaceholderFileCreated(relativePath, sha);
// Note: Because OnPlaceholderFileCreated is not synchronized on all platforms it is possible that GVFS will double count
// the creation of file placeholders if multiple requests for the same file are received at the same time on different
// threads.
this.placeHolderCreationCount.AddOrUpdate(
triggeringProcessImageFileName,
(imageName) => { return new PlaceHolderCreateCounter(); },
(key, oldCount) => { oldCount.Increment(); return oldCount; });
}
public void OnPlaceholderCreateBlockedForGit()
{
this.GitIndexProjection.OnPlaceholderCreateBlockedForGit();
}
public void OnPlaceholderFolderCreated(string relativePath)
{
this.GitIndexProjection.OnPlaceholderFolderCreated(relativePath);
}
public void OnPlaceholderFolderExpanded(string relativePath)
{
this.GitIndexProjection.OnPlaceholderFolderExpanded(relativePath);
}
public FileProperties GetLogsHeadFileProperties()
{
// Use a temporary FileProperties in case another thread sets this.logsHeadFileProperties before this
// method returns
FileProperties properties = this.logsHeadFileProperties;
if (properties == null)
{
try
{
properties = this.context.FileSystem.GetFileProperties(this.logsHeadPath);
this.logsHeadFileProperties = properties;
}
catch (Exception e)
{
EventMetadata metadata = this.CreateEventMetadata(relativePath: null, exception: e);
this.context.Tracer.RelatedWarning(metadata, "GetLogsHeadFileProperties: Exception thrown from GetFileProperties", Keywords.Telemetry);
properties = FileProperties.DefaultFile;
// Leave logsHeadFileProperties null to indicate that it is still needs to be refreshed
this.logsHeadFileProperties = null;
}
}
return properties;
}
public void LaunchPostFetchJob(List<string> packIndexes)
{
lock (this.postFetchJobLock)
{
if (this.postFetchJobThread?.IsAlive == true)
{
this.context.Tracer.RelatedWarning("Dropping post-fetch job since previous job is still running");
return;
}
if (this.stopping)
{
this.context.Tracer.RelatedWarning("Dropping post-fetch job since attempting to unmount");
return;
}
this.postFetchJobThread = new Thread(() => this.PostFetchJob(packIndexes));
this.postFetchJobThread.IsBackground = true;
this.postFetchJobThread.Start();
}
}
private void PostFetchJob(List<string> packIndexes)
{
try
{
using (FileBasedLock postFetchFileLock = GVFSPlatform.Instance.CreateFileBasedLock(
this.context.FileSystem,
this.context.Tracer,
Path.Combine(this.context.Enlistment.GitObjectsRoot, PostFetchLock)))
{
if (!postFetchFileLock.TryAcquireLock())
{
this.context.Tracer.RelatedInfo(PostFetchTelemetryKey + ": Skipping post-fetch work since another process holds the lock");
return;
}
this.postFetchGitProcess = new GitProcess(this.context.Enlistment);
using (ITracer activity = this.context.Tracer.StartActivity("TryWriteMultiPackIndex", EventLevel.Informational, Keywords.Telemetry, metadata: null))
{
string midxLockFile = Path.Combine(this.context.Enlistment.GitPackRoot, MultiPackIndexLock);
this.context.FileSystem.TryDeleteFile(midxLockFile);
if (this.stopping)
{
this.context.Tracer.RelatedWarning(
metadata: null,
message: PostFetchTelemetryKey + ": Not launching 'git multi-pack-index' because the mount is stopping",
keywords: Keywords.Telemetry);
return;
}
GitProcess.Result result = this.postFetchGitProcess.WriteMultiPackIndex(this.context.Enlistment.GitObjectsRoot);
if (!this.stopping && result.ExitCodeIsFailure)
{
this.context.Tracer.RelatedWarning(
metadata: null,
message: PostFetchTelemetryKey + ": Failed to generate multi-pack-index for new packfiles:" + result.Errors,
keywords: Keywords.Telemetry);
return;
}
}
if (packIndexes == null || packIndexes.Count == 0)
{
this.context.Tracer.RelatedInfo(PostFetchTelemetryKey + ": Skipping commit-graph write due to no new packfiles");
return;
}
using (ITracer activity = this.context.Tracer.StartActivity("TryWriteGitCommitGraph", EventLevel.Informational, Keywords.Telemetry, metadata: null))
{
string graphLockFile = Path.Combine(this.context.Enlistment.GitObjectsRoot, "info", CommitGraphLock);
this.context.FileSystem.TryDeleteFile(graphLockFile);
if (this.stopping)
{
this.context.Tracer.RelatedWarning(
metadata: null,
message: PostFetchTelemetryKey + ": Not launching 'git commit-graph' because the mount is stopping",
keywords: Keywords.Telemetry);
return;
}
GitProcess.Result result = this.postFetchGitProcess.WriteCommitGraph(this.context.Enlistment.GitObjectsRoot, packIndexes);
if (!this.stopping && result.ExitCodeIsFailure)
{
this.context.Tracer.RelatedWarning(
metadata: null,
message: PostFetchTelemetryKey + ": Failed to generate commit-graph for new packfiles:" + result.Errors,
keywords: Keywords.Telemetry);
return;
}
}
}
}
catch (IOException e)
{
this.context.Tracer.RelatedWarning(
metadata: this.CreateEventMetadata(null, e),
message: PostFetchTelemetryKey + ": IOException while running post-fetch job: " + e.Message,
keywords: Keywords.Telemetry);
}
catch (Exception e)
{
this.context.Tracer.RelatedError(
metadata: this.CreateEventMetadata(null, e),
message: PostFetchTelemetryKey + ": Exception while running post-fetch job: " + e.Message,
keywords: Keywords.Telemetry);
Environment.Exit((int)ReturnCode.GenericError);
}
}
private bool GitCommandLeavesProjectionUnchanged(GitCommandLineParser gitCommand)
{
return
gitCommand.IsVerb(LeavesProjectionUnchangedVerbs) ||
gitCommand.IsResetSoftOrMixed() ||
gitCommand.IsCheckoutWithFilePaths();
}
private bool GitCommandRequiresModifiedPathValidationAfterIndexChange(GitCommandLineParser gitCommand)
{
return
gitCommand.IsVerb(GitCommandLineParser.Verbs.UpdateIndex) ||
gitCommand.IsResetMixed();
}
private FileSystemTaskResult PreBackgroundOperation()
{
return this.GitIndexProjection.OpenIndexForRead();
}
private FileSystemTaskResult ExecuteBackgroundOperation(FileSystemTask gitUpdate)
{
EventMetadata metadata = new EventMetadata();
FileSystemTaskResult result;
switch (gitUpdate.Operation)
{
case FileSystemTask.OperationType.OnFileCreated:
case FileSystemTask.OperationType.OnFailedPlaceholderDelete:
case FileSystemTask.OperationType.OnFileHardLinkCreated:
case FileSystemTask.OperationType.OnFileSymLinkCreated:
metadata.Add("virtualPath", gitUpdate.VirtualPath);
result = this.AddModifiedPathAndRemoveFromPlaceholderList(gitUpdate.VirtualPath);
break;
case FileSystemTask.OperationType.OnFileRenamed:
metadata.Add("oldVirtualPath", gitUpdate.OldVirtualPath);
metadata.Add("virtualPath", gitUpdate.VirtualPath);
result = FileSystemTaskResult.Success;
if (!string.IsNullOrEmpty(gitUpdate.OldVirtualPath) && !IsPathInsideDotGit(gitUpdate.OldVirtualPath))
{
if (this.newlyCreatedFileAndFolderPaths.Contains(gitUpdate.OldVirtualPath))
{
result = this.TryRemoveModifiedPath(gitUpdate.OldVirtualPath, isFolder: false);
}
else
{
result = this.AddModifiedPathAndRemoveFromPlaceholderList(gitUpdate.OldVirtualPath);
}
}
if (result == FileSystemTaskResult.Success &&
!string.IsNullOrEmpty(gitUpdate.VirtualPath) &&
!IsPathInsideDotGit(gitUpdate.VirtualPath))
{
result = this.AddModifiedPathAndRemoveFromPlaceholderList(gitUpdate.VirtualPath);
}
break;
case FileSystemTask.OperationType.OnFilePreDelete:
// This code assumes that the current implementations of FileSystemVirtualizer will call either
// the PreDelete or the Delete not both so if a new implementation starts calling both
// this will need to be cleaned up to not duplicate the work that is being done.
metadata.Add("virtualPath", gitUpdate.VirtualPath);
if (this.newlyCreatedFileAndFolderPaths.Contains(gitUpdate.VirtualPath))
{
string fullPathToFolder = Path.Combine(this.context.Enlistment.WorkingDirectoryRoot, gitUpdate.VirtualPath);
if (!this.context.FileSystem.FileExists(fullPathToFolder))
{
result = this.TryRemoveModifiedPath(gitUpdate.VirtualPath, isFolder: false);
}
else
{
result = FileSystemTaskResult.Success;
}
}
else
{
result = this.AddModifiedPathAndRemoveFromPlaceholderList(gitUpdate.VirtualPath);
}
break;
case FileSystemTask.OperationType.OnFileDeleted:
// This code assumes that the current implementations of FileSystemVirtualizer will call either
// the PreDelete or the Delete not both so if a new implementation starts calling both
// this will need to be cleaned up to not duplicate the work that is being done.
metadata.Add("virtualPath", gitUpdate.VirtualPath);
if (this.newlyCreatedFileAndFolderPaths.Contains(gitUpdate.VirtualPath))
{
result = this.TryRemoveModifiedPath(gitUpdate.VirtualPath, isFolder: false);
}
else
{
result = this.AddModifiedPathAndRemoveFromPlaceholderList(gitUpdate.VirtualPath);
}
break;
case FileSystemTask.OperationType.OnFileOverwritten:
case FileSystemTask.OperationType.OnFileSuperseded:
case FileSystemTask.OperationType.OnFileConvertedToFull:
case FileSystemTask.OperationType.OnFailedPlaceholderUpdate:
metadata.Add("virtualPath", gitUpdate.VirtualPath);
result = this.AddModifiedPathAndRemoveFromPlaceholderList(gitUpdate.VirtualPath);
break;
case FileSystemTask.OperationType.OnFolderCreated:
metadata.Add("virtualPath", gitUpdate.VirtualPath);
result = this.TryAddModifiedPath(gitUpdate.VirtualPath, isFolder: true);
break;
case FileSystemTask.OperationType.OnFolderRenamed:
result = FileSystemTaskResult.Success;
metadata.Add("oldVirtualPath", gitUpdate.OldVirtualPath);
metadata.Add("virtualPath", gitUpdate.VirtualPath);
if (!string.IsNullOrEmpty(gitUpdate.OldVirtualPath) &&
this.newlyCreatedFileAndFolderPaths.Contains(gitUpdate.OldVirtualPath))
{
result = this.TryRemoveModifiedPath(gitUpdate.OldVirtualPath, isFolder: true);
}
// An empty destination path means the folder was renamed to somewhere outside of the repo
// Note that only full folders can be moved\renamed, and so there will already be a recursive
// sparse-checkout entry for the virtualPath of the folder being moved (meaning that no
// additional work is needed for any files\folders inside the folder being moved)
if (result == FileSystemTaskResult.Success && !string.IsNullOrEmpty(gitUpdate.VirtualPath))
{
result = this.TryAddModifiedPath(gitUpdate.VirtualPath, isFolder: true);
if (result == FileSystemTaskResult.Success)
{
this.newlyCreatedFileAndFolderPaths.Add(gitUpdate.VirtualPath);
Queue<string> relativeFolderPaths = new Queue<string>();
relativeFolderPaths.Enqueue(gitUpdate.VirtualPath);
// Remove old paths from modified paths if in the newly created list
while (relativeFolderPaths.Count > 0)
{
string folderPath = relativeFolderPaths.Dequeue();
if (result == FileSystemTaskResult.Success)
{
try
{
foreach (DirectoryItemInfo itemInfo in this.context.FileSystem.ItemsInDirectory(Path.Combine(this.context.Enlistment.WorkingDirectoryRoot, folderPath)))
{
string itemVirtualPath = Path.Combine(folderPath, itemInfo.Name);
string oldItemVirtualPath = gitUpdate.OldVirtualPath + itemVirtualPath.Substring(gitUpdate.VirtualPath.Length);
this.newlyCreatedFileAndFolderPaths.Add(itemVirtualPath);
if (this.newlyCreatedFileAndFolderPaths.Contains(oldItemVirtualPath))
{
result = this.TryRemoveModifiedPath(oldItemVirtualPath, isFolder: itemInfo.IsDirectory);
}
if (itemInfo.IsDirectory)
{
relativeFolderPaths.Enqueue(itemVirtualPath);
}
}
}
catch (DirectoryNotFoundException)
{
// DirectoryNotFoundException can occur when the renamed folder (or one of its children) is
// deleted prior to the background thread running
EventMetadata exceptionMetadata = new EventMetadata();
exceptionMetadata.Add("Area", "ExecuteBackgroundOperation");
exceptionMetadata.Add("Operation", gitUpdate.Operation.ToString());
exceptionMetadata.Add("oldVirtualPath", gitUpdate.OldVirtualPath);
exceptionMetadata.Add("virtualPath", gitUpdate.VirtualPath);
exceptionMetadata.Add(TracingConstants.MessageKey.InfoMessage, "DirectoryNotFoundException while traversing folder path");
exceptionMetadata.Add("folderPath", folderPath);
this.context.Tracer.RelatedEvent(EventLevel.Informational, "DirectoryNotFoundWhileUpdatingModifiedPaths", exceptionMetadata);
}
catch (IOException e)
{
metadata.Add("Details", "IOException while traversing folder path");
metadata.Add("folderPath", folderPath);
metadata.Add("Exception", e.ToString());
result = FileSystemTaskResult.RetryableError;
break;
}
catch (UnauthorizedAccessException e)
{
metadata.Add("Details", "UnauthorizedAccessException while traversing folder path");
metadata.Add("folderPath", folderPath);
metadata.Add("Exception", e.ToString());
result = FileSystemTaskResult.RetryableError;
break;
}
}
else
{
break;
}
}
}
}
break;
case FileSystemTask.OperationType.OnFolderPreDelete:
// This code assumes that the current implementations of FileSystemVirtualizer will call either
// the PreDelete or the Delete not both so if a new implementation starts calling both
// this will need to be cleaned up to not duplicate the work that is being done.
metadata.Add("virtualPath", gitUpdate.VirtualPath);
if (this.newlyCreatedFileAndFolderPaths.Contains(gitUpdate.VirtualPath))
{
string fullPathToFolder = Path.Combine(this.context.Enlistment.WorkingDirectoryRoot, gitUpdate.VirtualPath);
if (!this.context.FileSystem.DirectoryExists(fullPathToFolder))
{
result = this.TryRemoveModifiedPath(gitUpdate.VirtualPath, isFolder: true);
}
else
{
result = FileSystemTaskResult.Success;
}
}
else
{
result = this.TryAddModifiedPath(gitUpdate.VirtualPath, isFolder: true);
}
break;
case FileSystemTask.OperationType.OnFolderDeleted:
// This code assumes that the current implementations of FileSystemVirtualizer will call either
// the PreDelete or the Delete not both so if a new implementation starts calling both
// this will need to be cleaned up to not duplicate the work that is being done.
metadata.Add("virtualPath", gitUpdate.VirtualPath);
if (this.newlyCreatedFileAndFolderPaths.Contains(gitUpdate.VirtualPath))
{
result = this.TryRemoveModifiedPath(gitUpdate.VirtualPath, isFolder: true);
}
else
{
result = this.TryAddModifiedPath(gitUpdate.VirtualPath, isFolder: true);
}
break;
case FileSystemTask.OperationType.OnFolderFirstWrite:
result = FileSystemTaskResult.Success;
break;
case FileSystemTask.OperationType.OnIndexWriteRequiringModifiedPathsValidation:
result = this.GitIndexProjection.AddMissingModifiedFiles();
break;
case FileSystemTask.OperationType.OnPlaceholderCreationsBlockedForGit:
this.GitIndexProjection.ClearNegativePathCacheIfPollutedByGit();
result = FileSystemTaskResult.Success;
break;
default:
throw new InvalidOperationException("Invalid background operation");
}
if (result != FileSystemTaskResult.Success)
{
metadata.Add("Area", "ExecuteBackgroundOperation");
metadata.Add("Operation", gitUpdate.Operation.ToString());
metadata.Add(TracingConstants.MessageKey.WarningMessage, "Background operation failed");
metadata.Add(nameof(result), result.ToString());
this.context.Tracer.RelatedEvent(EventLevel.Warning, "FailedBackgroundOperation", metadata);
}
return result;
}
private FileSystemTaskResult TryRemoveModifiedPath(string virtualPath, bool isFolder)
{
if (!this.modifiedPaths.TryRemove(virtualPath, isFolder, out bool isRetryable))
{
return isRetryable ? FileSystemTaskResult.RetryableError : FileSystemTaskResult.FatalError;
}
this.newlyCreatedFileAndFolderPaths.TryRemove(virtualPath);
this.InvalidateGitStatusCache();
return FileSystemTaskResult.Success;
}
private FileSystemTaskResult TryAddModifiedPath(string virtualPath, bool isFolder)
{
if (!this.modifiedPaths.TryAdd(virtualPath, isFolder, out bool isRetryable))
{
return isRetryable ? FileSystemTaskResult.RetryableError : FileSystemTaskResult.FatalError;
}
this.InvalidateGitStatusCache();
return FileSystemTaskResult.Success;
}
private FileSystemTaskResult AddModifiedPathAndRemoveFromPlaceholderList(string virtualPath)
{
FileSystemTaskResult result = this.TryAddModifiedPath(virtualPath, isFolder: false);
if (result != FileSystemTaskResult.Success)
{
return result;
}
bool isFolder;
string fileName;
// We don't want to fill the placeholder list with deletes for files that are
// not in the projection so we make sure it is in the projection before removing.
if (this.GitIndexProjection.IsPathProjected(virtualPath, out fileName, out isFolder))
{
this.GitIndexProjection.RemoveFromPlaceholderList(virtualPath);
}
return result;
}
private FileSystemTaskResult PostBackgroundOperation()
{
this.modifiedPaths.WriteAllEntriesAndFlush();
this.gitStatusCache.RefreshAsynchronously();
return this.GitIndexProjection.CloseIndex();
}
private EventMetadata CreateEventMetadata(
string relativePath = null,
Exception exception = null)