Browse Source

Improve SftpClient Exists method

olegkap_cp 12 years ago
parent
commit
759aa4c9ee

+ 14 - 23
Renci.SshClient/Renci.SshNet/Sftp/SftpSession.cs

@@ -93,19 +93,7 @@ namespace Renci.SshNet.Sftp
         /// <returns>Absolute path</returns>
         internal string GetCanonicalPath(string path)
         {
-            var fullPath = path;
-
-            if (!string.IsNullOrEmpty(path) && path[0] != '/' && this.WorkingDirectory != null)
-            {
-                if (this.WorkingDirectory[this.WorkingDirectory.Length - 1] == '/')
-                {
-                    fullPath = string.Format(CultureInfo.InvariantCulture, "{0}{1}", this.WorkingDirectory, path);
-                }
-                else
-                {
-                    fullPath = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", this.WorkingDirectory, path);
-                }
-            }
+            var fullPath = GetFullRemotePath(path);
 
             var canonizedPath = string.Empty;
 
@@ -153,19 +141,22 @@ namespace Renci.SshNet.Sftp
             }
         }
 
-        internal bool FileExistsCommand(string path, Flags flags)
+        internal string GetFullRemotePath(string path)
         {
-            var handle = this.RequestOpen(path, flags, true);
-            if (handle == null)
-            {
-                return false;
-            }
-            else
-            {
-                this.RequestClose(handle);
+            var fullPath = path;
 
-                return true;
+            if (!string.IsNullOrEmpty(path) && path[0] != '/' && this.WorkingDirectory != null)
+            {
+                if (this.WorkingDirectory[this.WorkingDirectory.Length - 1] == '/')
+                {
+                    fullPath = string.Format(CultureInfo.InvariantCulture, "{0}{1}", this.WorkingDirectory, path);
+                }
+                else
+                {
+                    fullPath = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", this.WorkingDirectory, path);
+                }
             }
+            return fullPath;
         }
 
         protected override void OnChannelOpen()

+ 2 - 11
Renci.SshClient/Renci.SshNet/SftpClient.cs

@@ -397,23 +397,14 @@ namespace Renci.SshNet
             if (path.IsNullOrWhiteSpace())
                 throw new ArgumentException("path");
 
-            var fullPath = this._sftpSession.GetCanonicalPath(path);
-
-            //  Try to open as a file
-            var handle = this._sftpSession.RequestOpen(fullPath, Flags.Read, true);
-
-            if (handle == null)
-            {
-                handle = this._sftpSession.RequestOpenDir(fullPath, true);
-            }
+            var fullPath = this._sftpSession.GetFullRemotePath(path);
 
-            if (handle == null)
+            if (this._sftpSession.RequestRealPath(fullPath, true) == null)
             {
                 return false;
             }
             else
             {
-                this._sftpSession.RequestClose(handle);
                 return true;
             }
         }