ShareCollection.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 2014 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace SMBLibrary.Server
  12. {
  13. public class ShareCollection : List<FileSystemShare>
  14. {
  15. public void Add(string shareName, List<string> readAccess, List<string> writeAccess, IFileSystem fileSystem)
  16. {
  17. FileSystemShare share = new FileSystemShare();
  18. share.Name = shareName;
  19. share.ReadAccess = readAccess;
  20. share.WriteAccess = writeAccess;
  21. share.FileSystem = fileSystem;
  22. this.Add(share);
  23. }
  24. public bool Contains(string shareName, StringComparison comparisonType)
  25. {
  26. return (this.IndexOf(shareName, comparisonType) != -1);
  27. }
  28. public int IndexOf(string shareName, StringComparison comparisonType)
  29. {
  30. for (int index = 0; index < this.Count; index++)
  31. {
  32. if (this[index].Name.Equals(shareName, comparisonType))
  33. {
  34. return index;
  35. }
  36. }
  37. return -1;
  38. }
  39. public List<string> ListShares()
  40. {
  41. List<string> result = new List<string>();
  42. foreach (FileSystemShare share in this)
  43. {
  44. result.Add(share.Name);
  45. }
  46. return result;
  47. }
  48. /// <param name="relativePath">e.g. \Shared</param>
  49. public FileSystemShare GetShareFromRelativePath(string relativePath)
  50. {
  51. if (relativePath.StartsWith(@"\"))
  52. {
  53. relativePath = relativePath.Substring(1);
  54. }
  55. int indexOfSeparator = relativePath.IndexOf(@"\");
  56. if (indexOfSeparator >= 0)
  57. {
  58. relativePath = relativePath.Substring(0, indexOfSeparator);
  59. }
  60. int index = IndexOf(relativePath, StringComparison.InvariantCultureIgnoreCase);
  61. if (index >= 0)
  62. {
  63. return this[index];
  64. }
  65. else
  66. {
  67. return null;
  68. }
  69. }
  70. }
  71. }