1
0

RemotePath.cs 10 KB

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