1
0

RemotePath.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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("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("?", "[?]");
  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("path1");
  35. }
  36. if (path2 == null)
  37. {
  38. throw new ArgumentNullException("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("remotePath");
  59. }
  60. if (remoteRoot == null)
  61. {
  62. throw new ArgumentNullException("remoteRoot");
  63. }
  64. if (localRoot == null)
  65. {
  66. throw new ArgumentNullException("localRoot");
  67. }
  68. if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
  69. {
  70. localRoot += "\\";
  71. }
  72. // not adding to empty root paths, because the path may not even start with slash
  73. if ((remoteRoot.Length > 0) && !remoteRoot.EndsWith("/", StringComparison.Ordinal))
  74. {
  75. remoteRoot += "/";
  76. }
  77. string localPath;
  78. // special case
  79. if (remotePath == remoteRoot)
  80. {
  81. localPath = localRoot;
  82. }
  83. else
  84. {
  85. if (!remotePath.StartsWith(remoteRoot, StringComparison.Ordinal))
  86. {
  87. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "{0} does not start with {1}", remotePath, remoteRoot));
  88. }
  89. string subPath = remotePath.Substring(remoteRoot.Length);
  90. // can happen only when remoteRoot is empty
  91. if (subPath.StartsWith("/", StringComparison.Ordinal))
  92. {
  93. subPath = subPath.Substring(1);
  94. }
  95. subPath = subPath.Replace('/', '\\');
  96. localPath = localRoot + subPath;
  97. }
  98. return localPath;
  99. }
  100. public static string TranslateLocalPathToRemote(string localPath, string localRoot, string remoteRoot)
  101. {
  102. if (localPath == null)
  103. {
  104. throw new ArgumentNullException("localPath");
  105. }
  106. if (localRoot == null)
  107. {
  108. throw new ArgumentNullException("localRoot");
  109. }
  110. if (remoteRoot == null)
  111. {
  112. throw new ArgumentNullException("remoteRoot");
  113. }
  114. if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
  115. {
  116. localRoot += "\\";
  117. }
  118. // not adding to empty root paths, because the path may not even start with slash
  119. if ((remoteRoot.Length > 0) && !remoteRoot.EndsWith("/", StringComparison.Ordinal))
  120. {
  121. remoteRoot += "/";
  122. }
  123. string remotePath;
  124. // special case
  125. if (localPath == localRoot)
  126. {
  127. remotePath = remoteRoot;
  128. }
  129. else
  130. {
  131. if (!localPath.StartsWith(localRoot, StringComparison.Ordinal))
  132. {
  133. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "{0} does not start with {1}", localPath, localRoot));
  134. }
  135. string subPath = localPath.Substring(localRoot.Length);
  136. // can happen only when localRoot is empty
  137. if (subPath.StartsWith("\\", StringComparison.Ordinal))
  138. {
  139. subPath = subPath.Substring(1);
  140. }
  141. subPath = subPath.Replace('\\', '/');
  142. remotePath = remoteRoot + subPath;
  143. }
  144. return remotePath;
  145. }
  146. public static string GetDirectoryName(string path)
  147. {
  148. string result;
  149. if (path == null)
  150. {
  151. result = null;
  152. }
  153. else if (path.Length == 0)
  154. {
  155. throw new ArgumentException("Path cannot be empty", "path");
  156. }
  157. else
  158. {
  159. int i = path.LastIndexOf('/');
  160. if (i < 0)
  161. {
  162. result = string.Empty;
  163. }
  164. else if (i == 0)
  165. {
  166. if (path.Length == 1)
  167. {
  168. result = null;
  169. }
  170. else
  171. {
  172. result = "/";
  173. }
  174. }
  175. else
  176. {
  177. result = path.Substring(0, i);
  178. }
  179. }
  180. return result;
  181. }
  182. public static string AddDirectorySeparator(string path)
  183. {
  184. if (string.IsNullOrEmpty(path))
  185. {
  186. throw new ArgumentException("Path cannot be empty", "path");
  187. }
  188. if (!path.EndsWith("/", StringComparison.Ordinal))
  189. {
  190. path += "/";
  191. }
  192. return path;
  193. }
  194. public static string GetFileName(string path)
  195. {
  196. string result;
  197. if (string.IsNullOrEmpty(path))
  198. {
  199. result = null;
  200. }
  201. else
  202. {
  203. int i = path.LastIndexOf('/');
  204. if (i >= 0)
  205. {
  206. result = path.Substring(i + 1);
  207. }
  208. else
  209. {
  210. result = path;
  211. }
  212. }
  213. return result;
  214. }
  215. FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
  216. {
  217. return GetType().GetField(name, bindingAttr);
  218. }
  219. FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
  220. {
  221. return GetType().GetFields(bindingAttr);
  222. }
  223. MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
  224. {
  225. return GetType().GetMember(name, bindingAttr);
  226. }
  227. MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
  228. {
  229. return GetType().GetMembers(bindingAttr);
  230. }
  231. MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
  232. {
  233. return GetType().GetMethod(name, bindingAttr);
  234. }
  235. MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
  236. {
  237. return GetType().GetMethod(name, bindingAttr, binder, types, modifiers);
  238. }
  239. MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
  240. {
  241. return GetType().GetMethods(bindingAttr);
  242. }
  243. PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
  244. {
  245. return GetType().GetProperties(bindingAttr);
  246. }
  247. PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
  248. {
  249. return GetType().GetProperty(name, bindingAttr, binder, returnType, types, modifiers);
  250. }
  251. PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
  252. {
  253. return GetType().GetProperty(name, bindingAttr);
  254. }
  255. object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
  256. {
  257. if (target == null)
  258. {
  259. throw new ArgumentNullException("target");
  260. }
  261. Type type = target.GetType();
  262. // This trivial implementation allows calling static methods over COM
  263. return type.InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
  264. }
  265. Type IReflect.UnderlyingSystemType
  266. {
  267. get { return GetType(); }
  268. }
  269. }
  270. }