RemotePath.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. namespace WinSCP
  6. {
  7. [Guid("70253534-C5DC-4EF3-9C98-65C57D79C324")]
  8. [ClassInterface(Constants.ClassInterface)]
  9. [ComVisible(true)]
  10. public sealed class RemotePath : IReflect
  11. {
  12. public static string EscapeFileMask(string fileMask)
  13. {
  14. if (fileMask == null)
  15. {
  16. throw new ArgumentNullException(nameof(fileMask));
  17. }
  18. int lastSlash = fileMask.LastIndexOf('/');
  19. string path = lastSlash > 0 ? fileMask.Substring(0, lastSlash + 1) : string.Empty;
  20. string mask = lastSlash > 0 ? fileMask.Substring(lastSlash + 1) : fileMask;
  21. // Keep in sync with EscapeFileMask in GenerateUrl.cpp
  22. mask = mask.Replace("[", "[[]").Replace("*", "[*]").Replace("?", "[?]").Replace("<", "<<").Replace(">", ">>");
  23. return path + mask;
  24. }
  25. [Obsolete("Use RemotePath.Combine method")]
  26. public static string CombinePaths(string path1, string path2)
  27. {
  28. return Combine(path1, path2);
  29. }
  30. public static string Combine(string path1, string path2)
  31. {
  32. if (path1 == null)
  33. {
  34. throw new ArgumentNullException(nameof(path1));
  35. }
  36. if (path2 == null)
  37. {
  38. throw new ArgumentNullException(nameof(path2));
  39. }
  40. string result;
  41. if (path2.StartsWith("/", StringComparison.Ordinal))
  42. {
  43. result = path2;
  44. }
  45. else
  46. {
  47. result =
  48. path1 +
  49. ((path1.Length == 0) || (path2.Length == 0) || path1.EndsWith("/", StringComparison.Ordinal) ? string.Empty : "/") +
  50. path2;
  51. }
  52. return result;
  53. }
  54. public static string TranslateRemotePathToLocal(string remotePath, string remoteRoot, string localRoot)
  55. {
  56. if (remotePath == null)
  57. {
  58. throw new ArgumentNullException(nameof(remotePath));
  59. }
  60. if (remoteRoot == null)
  61. {
  62. throw new ArgumentNullException(nameof(remoteRoot));
  63. }
  64. if (localRoot == null)
  65. {
  66. throw new ArgumentNullException(nameof(localRoot));
  67. }
  68. localRoot = AddSeparator(localRoot, @"\");
  69. remoteRoot = AddSeparator(remoteRoot, "/");
  70. string localPath;
  71. // special case
  72. if (AddSeparator(remotePath, "/") == remoteRoot)
  73. {
  74. localPath = localRoot;
  75. }
  76. else
  77. {
  78. if (!remotePath.StartsWith(remoteRoot, StringComparison.Ordinal))
  79. {
  80. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "{0} does not start with {1}", remotePath, remoteRoot));
  81. }
  82. string subPath = remotePath.Substring(remoteRoot.Length);
  83. // can happen only when remoteRoot is empty
  84. if (subPath.StartsWith("/", StringComparison.Ordinal))
  85. {
  86. subPath = subPath.Substring(1);
  87. }
  88. subPath = subPath.Replace('/', '\\');
  89. localPath = localRoot + subPath;
  90. }
  91. return localPath;
  92. }
  93. private static string AddSeparator(string path, string separator)
  94. {
  95. // not adding to empty root paths, because the path may not even start with slash
  96. if ((path.Length > 0) && !path.EndsWith(separator, StringComparison.Ordinal))
  97. {
  98. path += separator;
  99. }
  100. return path;
  101. }
  102. public static string TranslateLocalPathToRemote(string localPath, string localRoot, string remoteRoot)
  103. {
  104. if (localPath == null)
  105. {
  106. throw new ArgumentNullException(nameof(localPath));
  107. }
  108. if (localRoot == null)
  109. {
  110. throw new ArgumentNullException(nameof(localRoot));
  111. }
  112. if (remoteRoot == null)
  113. {
  114. throw new ArgumentNullException(nameof(remoteRoot));
  115. }
  116. localRoot = AddSeparator(localRoot, @"\");
  117. remoteRoot = AddSeparator(remoteRoot, "/");
  118. string remotePath;
  119. // special case
  120. if (AddSeparator(localPath, @"\") == localRoot)
  121. {
  122. remotePath = remoteRoot;
  123. }
  124. else
  125. {
  126. if (!localPath.StartsWith(localRoot, StringComparison.Ordinal))
  127. {
  128. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "{0} does not start with {1}", localPath, localRoot));
  129. }
  130. string subPath = localPath.Substring(localRoot.Length);
  131. // can happen only when localRoot is empty
  132. if (subPath.StartsWith("\\", StringComparison.Ordinal))
  133. {
  134. subPath = subPath.Substring(1);
  135. }
  136. subPath = subPath.Replace('\\', '/');
  137. remotePath = remoteRoot + subPath;
  138. }
  139. return remotePath;
  140. }
  141. public static string GetDirectoryName(string path)
  142. {
  143. string result;
  144. if (path == null)
  145. {
  146. result = null;
  147. }
  148. else if (path.Length == 0)
  149. {
  150. throw new ArgumentException("Path cannot be empty", nameof(path));
  151. }
  152. else
  153. {
  154. int i = path.LastIndexOf('/');
  155. if (i < 0)
  156. {
  157. result = string.Empty;
  158. }
  159. else if (i == 0)
  160. {
  161. if (path.Length == 1)
  162. {
  163. result = null;
  164. }
  165. else
  166. {
  167. result = "/";
  168. }
  169. }
  170. else
  171. {
  172. result = path.Substring(0, i);
  173. }
  174. }
  175. return result;
  176. }
  177. public static string AddDirectorySeparator(string path)
  178. {
  179. if (string.IsNullOrEmpty(path))
  180. {
  181. throw new ArgumentException("Path cannot be empty", nameof(path));
  182. }
  183. if (!path.EndsWith("/", StringComparison.Ordinal))
  184. {
  185. path += "/";
  186. }
  187. return path;
  188. }
  189. public static string GetFileName(string path)
  190. {
  191. string result;
  192. if (string.IsNullOrEmpty(path))
  193. {
  194. result = null;
  195. }
  196. else
  197. {
  198. int i = path.LastIndexOf('/');
  199. if (i >= 0)
  200. {
  201. result = path.Substring(i + 1);
  202. }
  203. else
  204. {
  205. result = path;
  206. }
  207. }
  208. return result;
  209. }
  210. FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
  211. {
  212. return GetType().GetField(name, bindingAttr);
  213. }
  214. FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
  215. {
  216. return GetType().GetFields(bindingAttr);
  217. }
  218. MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
  219. {
  220. return GetType().GetMember(name, bindingAttr);
  221. }
  222. MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
  223. {
  224. return GetType().GetMembers(bindingAttr);
  225. }
  226. MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
  227. {
  228. return GetType().GetMethod(name, bindingAttr);
  229. }
  230. MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
  231. {
  232. return GetType().GetMethod(name, bindingAttr, binder, types, modifiers);
  233. }
  234. MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
  235. {
  236. return GetType().GetMethods(bindingAttr);
  237. }
  238. PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
  239. {
  240. return GetType().GetProperties(bindingAttr);
  241. }
  242. PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
  243. {
  244. return GetType().GetProperty(name, bindingAttr, binder, returnType, types, modifiers);
  245. }
  246. PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
  247. {
  248. return GetType().GetProperty(name, bindingAttr);
  249. }
  250. object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
  251. {
  252. if (target == null)
  253. {
  254. throw new ArgumentNullException(nameof(target));
  255. }
  256. Type type = target.GetType();
  257. // This trivial implementation allows calling static methods over COM
  258. return type.InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
  259. }
  260. Type IReflect.UnderlyingSystemType
  261. {
  262. get { return GetType(); }
  263. }
  264. }
  265. }