/Mercurial.Net/Versions/MercurialVersionPre18.cs

# · C# · 71 lines · 53 code · 3 blank · 15 comment · 3 complexity · 20205fd713b7ffb61afde75e9cc981f3 MD5 · raw file

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