소스 검색

RemotePath.TranslateLocalPathToRemote and RemotePath.TranslateRemotePathToLocal can accept a directory source path equal to the source root path with a missing trailing slash

Source commit: aaa9832a1dfb540c331bb8f56fd610346b0e61a1
Martin Prikryl 5 년 전
부모
커밋
b1e5af5132
1개의 변경된 파일16개의 추가작업 그리고 22개의 파일을 삭제
  1. 16 22
      dotnet/RemotePath.cs

+ 16 - 22
dotnet/RemotePath.cs

@@ -75,20 +75,12 @@ namespace WinSCP
                 throw new ArgumentNullException(nameof(localRoot));
             }
 
-            if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
-            {
-                localRoot += "\\";
-            }
-
-            // not adding to empty root paths, because the path may not even start with slash
-            if ((remoteRoot.Length > 0) && !remoteRoot.EndsWith("/", StringComparison.Ordinal))
-            {
-                remoteRoot += "/";
-            }
+            localRoot = AddSeparator(localRoot, @"\");
+            remoteRoot = AddSeparator(remoteRoot, "/");
 
             string localPath;
             // special case
-            if (remotePath == remoteRoot)
+            if (AddSeparator(remotePath, "/") == remoteRoot)
             {
                 localPath = localRoot;
             }
@@ -111,6 +103,16 @@ namespace WinSCP
             return localPath;
         }
 
+        private static string AddSeparator(string path, string separator)
+        {
+            // not adding to empty root paths, because the path may not even start with slash
+            if ((path.Length > 0) && !path.EndsWith(separator, StringComparison.Ordinal))
+            {
+                path += separator;
+            }
+            return path;
+        }
+
         public static string TranslateLocalPathToRemote(string localPath, string localRoot, string remoteRoot)
         {
             if (localPath == null)
@@ -128,20 +130,12 @@ namespace WinSCP
                 throw new ArgumentNullException(nameof(remoteRoot));
             }
 
-            if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
-            {
-                localRoot += "\\";
-            }
-
-            // not adding to empty root paths, because the path may not even start with slash
-            if ((remoteRoot.Length > 0) && !remoteRoot.EndsWith("/", StringComparison.Ordinal))
-            {
-                remoteRoot += "/";
-            }
+            localRoot = AddSeparator(localRoot, @"\");
+            remoteRoot = AddSeparator(remoteRoot, "/");
 
             string remotePath;
             // special case
-            if (localPath == localRoot)
+            if (AddSeparator(localPath, @"\") == localRoot)
             {
                 remotePath = remoteRoot;
             }