PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Mercurial.Net/Versions/MercurialVersionPre18.cs

#
C# | 71 lines | 53 code | 3 blank | 15 comment | 3 complexity | 20205fd713b7ffb61afde75e9cc981f3 MD5 | raw file
 1using System;
 2using System.Diagnostics;
 3using System.Diagnostics.CodeAnalysis;
 4using System.IO;
 5
 6namespace Mercurial.Versions
 7{
 8    /// <summary>
 9    /// This base class handles Mercurial &lt; 1.8.
10    /// </summary>
11    [SuppressMessage("Microsoft.Usage", "CA2243:AttributeStringLiteralsShouldParseCorrectly", Justification = "There is more advanced parsing here to handle version ranges, so this is not necessary.")]
12    [MercurialVersion("", "1.7")]
13    public class MercurialVersionPre18 : MercurialVersionBase
14    {
15        /// <summary>
16        /// This method will wait for lingering POSIX-style file locks to dissipate before
17        /// continuing, to get around problems with such locks in pre-1.8 Mercurial.
18        /// </summary>
19        /// <param name="repositoryPath">
20        /// The path to the repository that the locks exists in.
21        /// </param>
22        /// <exception cref="ArgumentNullException">
23        /// <para><paramref name="repositoryPath"/> is <c>null</c> or empty.</para>
24        /// </exception>
25        public override void WaitForLocksToDissipate(string repositoryPath)
26        {
27            string repositoryFolderPath = Path.Combine(repositoryPath, ".hg");
28            if (!Directory.Exists(repositoryFolderPath))
29                return;
30
31            string lockPath = Path.Combine(repositoryFolderPath, "lock");
32            string wlockPath = Path.Combine(repositoryFolderPath, "wlock");
33
34            foreach (string path in new[] { lockPath, wlockPath })
35            {
36                Stopwatch sw = Stopwatch.StartNew();
37                try
38                {
39                    while (sw.ElapsedMilliseconds < 1000)
40                    {
41                        try
42                        {
43                            File.Create(path).Dispose();
44                            break;
45                        }
46                        catch (IOException)
47                        {
48                            // swallow this
49                        }
50                    }
51                }
52                finally
53                {
54                    sw = Stopwatch.StartNew();
55                    while (sw.ElapsedMilliseconds < 1000)
56                    {
57                        try
58                        {
59                            File.Delete(path);
60                            break;
61                        }
62                        catch (IOException)
63                        {
64                            // swallow this
65                        }
66                    }
67                }
68            }
69        }
70    }
71}