Exemplo n.º 1
0
        private static void AcquireLock(string enlistmentPipename)
        {
            using (NamedPipeClient pipeClient = new NamedPipeClient(enlistmentPipename))
            {
                if (!pipeClient.Connect())
                {
                    throw new Exception("The repo does not appear to be mounted. Use 'gvfs status' to check.");
                }

                int pid = Process.GetCurrentProcess().Id;

                string result;
                if (!GVFSLock.TryAcquireGVFSLockForProcess(
                        unattended: false,
                        pipeClient: pipeClient,
                        fullCommand: AcquireGVFSLockVerb.fullCommand,
                        pid: pid,
                        isElevated: false,
                        isConsoleOutputRedirectedToFile: false,
                        checkAvailabilityOnly: false,
                        gvfsEnlistmentRoot: null,
                        gitCommandSessionId: string.Empty,
                        result: out result))
                {
                    throw new Exception(result);
                }
            }
        }
Exemplo n.º 2
0
        public void Mount(EventLevel verbosity, Keywords keywords)
        {
            this.currentState = MountState.Mounting;
            if (Environment.CurrentDirectory != this.enlistment.EnlistmentRoot)
            {
                Environment.CurrentDirectory = this.enlistment.EnlistmentRoot;
            }

            using (NamedPipeServer pipeServer = this.StartNamedPipe())
            {
                this.AcquireRepoMutex();

                GVFSContext context = this.CreateContext();

                this.ValidateMountPoints();
                this.UpdateHooks();

                this.gvfsLock = context.Repository.GVFSLock;
                this.MountAndStartWorkingDirectoryCallbacks(context);

                Console.Title = "GVFS " + ProcessHelper.GetCurrentProcessVersion() + " - " + this.enlistment.EnlistmentRoot;

                this.tracer.RelatedEvent(
                    EventLevel.Critical,
                    "Mount",
                    new EventMetadata
                {
                    { "Message", "Virtual repo is ready" },
                });

                this.currentState = MountState.Ready;

                this.unmountEvent.WaitOne();
            }
        }
Exemplo n.º 3
0
        public void ReleaseLockHeldByGVFS_WhenExternalHasLockShouldThrow()
        {
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform);

            Assert.Throws <InvalidOperationException>(() => gvfsLock.ReleaseLockHeldByGVFS());
        }
Exemplo n.º 4
0
        public void ReleaseLockHeldByGVFS_WhenNoLock()
        {
            GVFSLock gvfsLock = new GVFSLock(new MockTracer());

            this.ValidateLockIsFree(gvfsLock);
            Assert.Throws <InvalidOperationException>(() => gvfsLock.ReleaseLockHeldByGVFS());
        }
Exemplo n.º 5
0
        public void ReleaseLockHeldByExternalProcess_WhenGVFSHasLock()
        {
            GVFSLock gvfsLock = this.AcquireGVFSLock();

            gvfsLock.ReleaseLockHeldByExternalProcess(DefaultLockData.PID).ShouldBeFalse();
            this.ValidateLockHeldByGVFS(gvfsLock);
        }
Exemplo n.º 6
0
        private void AcquireLock(string enlistmentRoot)
        {
            string pipeName = Paths.GetNamedPipeName(enlistmentRoot);

            using (NamedPipeClient pipeClient = new NamedPipeClient(pipeName))
            {
                try
                {
                    if (!pipeClient.Connect())
                    {
                        this.ReportErrorAndExit("Unable to connect to GVFS while acquiring lock to unmount.  Try 'gvfs status' to verify if the repo is mounted.");
                        return;
                    }

                    Process currentProcess = Process.GetCurrentProcess();
                    string  result         = null;
                    if (!GVFSLock.TryAcquireGVFSLockForProcess(
                            pipeClient,
                            "gvfs unmount",
                            currentProcess.Id,
                            currentProcess,
                            enlistmentRoot,
                            out result))
                    {
                        this.ReportErrorAndExit("Unable to acquire the lock prior to unmount. " + result);
                    }
                }
                catch (BrokenPipeException)
                {
                    this.ReportErrorAndExit("Unable to acquire the lock prior to unmount.  Try 'gvfs status' to verify if the repo is mounted.");
                }
            }
        }
Exemplo n.º 7
0
        private void AcquireLock(string enlistmentRoot)
        {
            string pipeName = Paths.GetNamedPipeName(enlistmentRoot);

            using (NamedPipeClient pipeClient = new NamedPipeClient(pipeName))
            {
                try
                {
                    if (!pipeClient.Connect())
                    {
                        this.ReportErrorAndExit("Unable to connect to GVFS while acquiring lock to unmount.  Try 'gvfs status' to verify if the repo is mounted.");
                        return;
                    }

                    Process currentProcess = Process.GetCurrentProcess();
                    string  result         = null;
                    if (!GVFSLock.TryAcquireGVFSLockForProcess(
                            this.Unattended,
                            pipeClient,
                            "gvfs unmount",
                            currentProcess.Id,
                            GVFSPlatform.Instance.IsElevated(),
                            checkAvailabilityOnly: false,
                            gvfsEnlistmentRoot: enlistmentRoot,
                            result: out result))
                    {
                        this.ReportErrorAndExit("Unable to acquire the lock prior to unmount. " + result);
                    }
                }
                catch (BrokenPipeException)
                {
                    this.ReportErrorAndExit("Unable to acquire the lock prior to unmount.  Try 'gvfs status' to verify if the repo is mounted.");
                }
            }
        }
Exemplo n.º 8
0
        public void TryAcquireLockForGVFS_WhenExternalLock()
        {
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform);

            gvfsLock.TryAcquireLockForGVFS().ShouldBeFalse();
            mockPlatform.ActiveProcesses.Remove(DefaultLockData.PID);
        }
Exemplo n.º 9
0
        public void ReleaseLockHeldByExternalProcess_WhenNoLock()
        {
            GVFSLock gvfsLock = new GVFSLock(new MockTracer());

            this.ValidateLockIsFree(gvfsLock);
            gvfsLock.ReleaseLockHeldByExternalProcess(DefaultLockData.PID).ShouldBeFalse();
            this.ValidateLockIsFree(gvfsLock);
        }
Exemplo n.º 10
0
        public void ReleaseLockHeldByExternalProcess_DifferentPID()
        {
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform);

            gvfsLock.ReleaseLockHeldByExternalProcess(4321).ShouldBeFalse();
            this.ValidateLockHeld(gvfsLock, DefaultLockData);
        }
Exemplo n.º 11
0
        public void TryAcquireLockForExternalRequestor_WhenGVFSLock()
        {
            GVFSLock gvfsLock = this.AcquireGVFSLock();

            NamedPipeMessages.LockData existingExternalHolder;
            gvfsLock.TryAcquireLockForExternalRequestor(DefaultLockData, out existingExternalHolder).ShouldBeFalse();
            this.ValidateLockHeldByGVFS(gvfsLock);
            existingExternalHolder.ShouldBeNull();
        }
Exemplo n.º 12
0
        private GVFSLock AcquireGVFSLock(ITracer mockTracer)
        {
            GVFSLock gvfsLock = new GVFSLock(mockTracer);

            this.ValidateLockIsFree(gvfsLock);
            gvfsLock.TryAcquireLockForGVFS().ShouldBeTrue();
            this.ValidateLockHeldByGVFS(gvfsLock);
            return(gvfsLock);
        }
Exemplo n.º 13
0
        public void ReleaseLockHeldByGVFS_WhenNoLock()
        {
            Mock <ITracer> mockTracer = new Mock <ITracer>(MockBehavior.Strict);
            GVFSLock       gvfsLock   = new GVFSLock(mockTracer.Object);

            this.ValidateLockIsFree(gvfsLock);
            Assert.Throws <InvalidOperationException>(() => gvfsLock.ReleaseLockHeldByGVFS());
            mockTracer.VerifyAll();
        }
Exemplo n.º 14
0
        public void TryAcquireAndReleaseLockForExternalRequestor()
        {
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform);

            mockPlatform.ActiveProcesses.Remove(DefaultLockData.PID);
            gvfsLock.ReleaseLockHeldByExternalProcess(DefaultLockData.PID);
            this.ValidateLockIsFree(gvfsLock);
        }
Exemplo n.º 15
0
        public void TryAcquireLockForGVFS()
        {
            GVFSLock gvfsLock = this.AcquireGVFSLock();

            // Should be able to call again when GVFS has the lock
            gvfsLock.TryAcquireLockForGVFS().ShouldBeTrue();
            this.ValidateLockHeldByGVFS(gvfsLock);

            gvfsLock.ReleaseLockHeldByGVFS();
            this.ValidateLockIsFree(gvfsLock);
        }
Exemplo n.º 16
0
        public void ReleaseLockHeldByGVFS_WhenExternalHasLockShouldThrow()
        {
            Mock <ITracer> mockTracer = new Mock <ITracer>(MockBehavior.Strict);

            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Informational, "TryAcquireLockExternal", It.IsAny <EventMetadata>()));
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform, mockTracer.Object);

            Assert.Throws <InvalidOperationException>(() => gvfsLock.ReleaseLockHeldByGVFS());
            mockTracer.VerifyAll();
        }
Exemplo n.º 17
0
        private GVFSLock AcquireDefaultLock(MockPlatform mockPlatform, ITracer mockTracer)
        {
            GVFSLock gvfsLock = new GVFSLock(mockTracer);

            this.ValidateLockIsFree(gvfsLock);
            NamedPipeMessages.LockData existingExternalHolder;
            gvfsLock.TryAcquireLockForExternalRequestor(DefaultLockData, out existingExternalHolder).ShouldBeTrue();
            existingExternalHolder.ShouldBeNull();
            mockPlatform.ActiveProcesses.Add(DefaultLockData.PID);
            this.ValidateLockHeld(gvfsLock, DefaultLockData);
            return(gvfsLock);
        }
Exemplo n.º 18
0
        public void ReleaseLockHeldByExternalProcess_WhenGVFSHasLock()
        {
            Mock <ITracer> mockTracer = new Mock <ITracer>(MockBehavior.Strict);

            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Verbose, "TryAcquireLockInternal", It.IsAny <EventMetadata>()));
            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Informational, "ReleaseLockHeldByExternalProcess", It.IsAny <EventMetadata>(), Keywords.Telemetry));
            GVFSLock gvfsLock = this.AcquireGVFSLock(mockTracer.Object);

            gvfsLock.ReleaseLockHeldByExternalProcess(DefaultLockData.PID).ShouldBeFalse();
            this.ValidateLockHeldByGVFS(gvfsLock);
            mockTracer.VerifyAll();
        }
Exemplo n.º 19
0
        public void TryAcquireLockForExternalRequestor_WhenExternalLock()
        {
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform);

            NamedPipeMessages.LockData newLockData = new NamedPipeMessages.LockData(4321, false, false, "git new");
            NamedPipeMessages.LockData existingExternalHolder;
            gvfsLock.TryAcquireLockForExternalRequestor(newLockData, out existingExternalHolder).ShouldBeFalse();
            this.ValidateLockHeld(gvfsLock, DefaultLockData);
            this.ValidateExistingExternalHolder(DefaultLockData, existingExternalHolder);
            mockPlatform.ActiveProcesses.Remove(DefaultLockData.PID);
        }
Exemplo n.º 20
0
        public void TryAcquireLockForGVFS_WhenExternalLock()
        {
            Mock <ITracer> mockTracer = new Mock <ITracer>(MockBehavior.Strict);

            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Informational, "TryAcquireLockExternal", It.IsAny <EventMetadata>()));
            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Verbose, "TryAcquireLockInternal", It.IsAny <EventMetadata>()));
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform, mockTracer.Object);

            gvfsLock.TryAcquireLockForGVFS().ShouldBeFalse();
            mockPlatform.ActiveProcesses.Remove(DefaultLockData.PID);
            mockTracer.VerifyAll();
        }
Exemplo n.º 21
0
        public void ReleaseLockHeldByExternalProcess_DifferentPID()
        {
            Mock <ITracer> mockTracer = new Mock <ITracer>(MockBehavior.Strict);

            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Informational, "ReleaseLockHeldByExternalProcess", It.IsAny <EventMetadata>(), Keywords.Telemetry));
            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Informational, "TryAcquireLockExternal", It.IsAny <EventMetadata>()));
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform, mockTracer.Object);

            gvfsLock.ReleaseLockHeldByExternalProcess(4321).ShouldBeFalse();
            this.ValidateLockHeld(gvfsLock, DefaultLockData);
            mockTracer.VerifyAll();
        }
Exemplo n.º 22
0
        public void TryAcquireAndReleaseLockForExternalRequestor()
        {
            Mock <ITracer> mockTracer = new Mock <ITracer>(MockBehavior.Strict);

            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Informational, "TryAcquireLockExternal", It.IsAny <EventMetadata>()));
            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Informational, "ReleaseLockHeldByExternalProcess", It.IsAny <EventMetadata>(), Keywords.Telemetry));
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform, mockTracer.Object);

            mockPlatform.ActiveProcesses.Remove(DefaultLockData.PID);
            gvfsLock.ReleaseLockHeldByExternalProcess(DefaultLockData.PID);
            this.ValidateLockIsFree(gvfsLock);
            mockTracer.VerifyAll();
        }
Exemplo n.º 23
0
        public void Dispose()
        {
            if (this.libgit2RepoPool != null)
            {
                this.libgit2RepoPool.Dispose();
                this.libgit2RepoPool = null;
            }

            if (this.GVFSLock != null)
            {
                this.GVFSLock.Dispose();
                this.GVFSLock = null;
            }
        }
Exemplo n.º 24
0
        public void TryAcquireLockForExternalRequestor_WhenGVFSLock()
        {
            Mock <ITracer> mockTracer = new Mock <ITracer>(MockBehavior.Strict);

            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Verbose, "TryAcquireLockInternal", It.IsAny <EventMetadata>()));
            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Verbose, "TryAcquireLockExternal", It.IsAny <EventMetadata>()));
            GVFSLock gvfsLock = this.AcquireGVFSLock(mockTracer.Object);

            NamedPipeMessages.LockData existingExternalHolder;
            gvfsLock.TryAcquireLockForExternalRequestor(DefaultLockData, out existingExternalHolder).ShouldBeFalse();
            this.ValidateLockHeldByGVFS(gvfsLock);
            existingExternalHolder.ShouldBeNull();
            mockTracer.VerifyAll();
        }
Exemplo n.º 25
0
 private void ValidateLock(
     GVFSLock gvfsLock,
     NamedPipeMessages.LockData expected,
     string expectedStatus,
     string expectedGitCommand,
     bool expectedIsAvailable)
 {
     gvfsLock.GetStatus().ShouldEqual(expectedStatus);
     NamedPipeMessages.LockData existingHolder;
     gvfsLock.IsLockAvailableForExternalRequestor(out existingHolder).ShouldEqual(expectedIsAvailable);
     this.ValidateExistingExternalHolder(expected, existingHolder);
     gvfsLock.GetLockedGitCommand().ShouldEqual(expectedGitCommand);
     NamedPipeMessages.LockData externalHolder = gvfsLock.GetExternalHolder();
     this.ValidateExistingExternalHolder(expected, externalHolder);
 }
Exemplo n.º 26
0
        private static void AcquireGVFSLockForProcess(string fullCommand, int pid, Process parentProcess, NamedPipeClient pipeClient)
        {
            string result;

            if (!GVFSLock.TryAcquireGVFSLockForProcess(
                    pipeClient,
                    fullCommand,
                    pid,
                    parentProcess,
                    null, // gvfsEnlistmentRoot
                    out result))
            {
                ExitWithError(result);
            }
        }
Exemplo n.º 27
0
        public void TryAcquireLockForGVFS()
        {
            Mock <ITracer> mockTracer = new Mock <ITracer>(MockBehavior.Strict);

            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Verbose, "TryAcquireLockInternal", It.IsAny <EventMetadata>()));
            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Verbose, "ReleaseLockHeldByGVFS", It.IsAny <EventMetadata>()));
            GVFSLock gvfsLock = this.AcquireGVFSLock(mockTracer.Object);

            // Should be able to call again when GVFS has the lock
            gvfsLock.TryAcquireLockForGVFS().ShouldBeTrue();
            this.ValidateLockHeldByGVFS(gvfsLock);

            gvfsLock.ReleaseLockHeldByGVFS();
            this.ValidateLockIsFree(gvfsLock);
            mockTracer.VerifyAll();
        }
Exemplo n.º 28
0
        public void TryAcquireLockForExternalRequestor_WhenExternalLock()
        {
            Mock <ITracer> mockTracer = new Mock <ITracer>(MockBehavior.Strict);

            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Informational, "TryAcquireLockExternal", It.IsAny <EventMetadata>()));
            mockTracer.Setup(x => x.RelatedEvent(EventLevel.Verbose, "TryAcquireLockExternal", It.IsAny <EventMetadata>()));
            MockPlatform mockPlatform = (MockPlatform)GVFSPlatform.Instance;
            GVFSLock     gvfsLock     = this.AcquireDefaultLock(mockPlatform, mockTracer.Object);

            NamedPipeMessages.LockData newLockData = new NamedPipeMessages.LockData(4321, false, false, "git new", "123");
            NamedPipeMessages.LockData existingExternalHolder;
            gvfsLock.TryAcquireLockForExternalRequestor(newLockData, out existingExternalHolder).ShouldBeFalse();
            this.ValidateLockHeld(gvfsLock, DefaultLockData);
            this.ValidateExistingExternalHolder(DefaultLockData, existingExternalHolder);
            mockPlatform.ActiveProcesses.Remove(DefaultLockData.PID);
            mockTracer.VerifyAll();
        }
Exemplo n.º 29
0
        private static void AcquireGVFSLockForProcess(bool unattended, string fullCommand, int pid, Process parentProcess, NamedPipeClient pipeClient)
        {
            string result;

            if (!GVFSLock.TryAcquireGVFSLockForProcess(
                    unattended,
                    pipeClient,
                    fullCommand,
                    pid,
                    ProcessHelper.IsAdminElevated(),
                    parentProcess,
                    null, // gvfsEnlistmentRoot
                    out result))
            {
                ExitWithError(result);
            }
        }
Exemplo n.º 30
0
        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: {0}", error);
            }

            using (NamedPipeServer pipeServer = this.StartNamedPipe())
            {
                GVFSContext context = this.CreateContext();

                if (context.Unattended)
                {
                    this.tracer.RelatedEvent(EventLevel.Critical, GVFSConstants.UnattendedEnvironmentVariable, null);
                }

                this.ValidateMountPoints();
                this.UpdateHooks();
                this.SetVisualStudioRegistryKey();

                this.gvfsLock = context.Repository.GVFSLock;
                this.MountAndStartWorkingDirectoryCallbacks(context, this.cacheServer);

                Console.Title = "GVFS " + ProcessHelper.GetCurrentProcessVersion() + " - " + this.enlistment.EnlistmentRoot;

                this.tracer.RelatedEvent(
                    EventLevel.Critical,
                    "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" },
                });

                this.currentState = MountState.Ready;

                this.unmountEvent.WaitOne();
            }
        }