mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-09-10 01:05:42 +00:00
Remove FEATURE_DIRECTORYINFO_ENUMERATEFILES (#1119)
* Remove FEATURE_DIRECTORYINFO_ENUMERATEFILES * Add exception documentation
This commit is contained in:
committed by
GitHub
parent
8c932fb1e8
commit
072ba7e013
@@ -1,32 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Renci.SshNet.Abstractions
|
||||
{
|
||||
internal class FileSystemAbstraction
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns an enumerable collection of file information that matches a search pattern.
|
||||
/// </summary>
|
||||
/// <param name="directoryInfo"></param>
|
||||
/// <param name="searchPattern">The search string to match against the names of files.</param>
|
||||
/// <returns>
|
||||
/// An enumerable collection of files that matches <paramref name="searchPattern"/>.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="directoryInfo"/> is <c>null</c>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="searchPattern"/> is <c>null</c>.</exception>
|
||||
/// <exception cref="DirectoryNotFoundException">The path represented by <paramref name="directoryInfo"/> does not exist or is not valid.</exception>
|
||||
public static IEnumerable<FileInfo> EnumerateFiles(DirectoryInfo directoryInfo, string searchPattern)
|
||||
{
|
||||
if (directoryInfo == null)
|
||||
throw new ArgumentNullException("directoryInfo");
|
||||
|
||||
#if FEATURE_DIRECTORYINFO_ENUMERATEFILES
|
||||
return directoryInfo.EnumerateFiles(searchPattern);
|
||||
#else
|
||||
return directoryInfo.GetFiles(searchPattern);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<AssemblyName>Renci.SshNet</AssemblyName>
|
||||
<AssemblyOriginatorKeyFile>../Renci.SshNet.snk</AssemblyOriginatorKeyFile>
|
||||
<LangVersion>6</LangVersion>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<TargetFrameworks>net462;netstandard2.0;net6.0;net7.0</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
@@ -19,6 +19,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' ">
|
||||
<DefineConstants>FEATURE_DIRECTORYINFO_ENUMERATEFILES;FEATURE_SOCKET_TAP;FEATURE_SOCKET_APM;FEATURE_SOCKET_EAP;FEATURE_DNS_SYNC;FEATURE_DNS_APM;FEATURE_DNS_TAP</DefineConstants>
|
||||
<DefineConstants>FEATURE_SOCKET_TAP;FEATURE_SOCKET_APM;FEATURE_SOCKET_EAP;FEATURE_DNS_SYNC;FEATURE_DNS_APM;FEATURE_DNS_TAP</DefineConstants>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1996,6 +1996,7 @@ namespace Renci.SshNet
|
||||
/// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <c>null</c>.</exception>
|
||||
/// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <c>null</c> or contains only whitespace.</exception>
|
||||
/// <exception cref="SftpPathNotFoundException"><paramref name="destinationPath"/> was not found on the remote host.</exception>
|
||||
/// <exception cref="SshException">If a problem occurs while copying the file</exception>
|
||||
public IEnumerable<FileInfo> SynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern)
|
||||
{
|
||||
if (sourcePath == null)
|
||||
@@ -2019,6 +2020,7 @@ namespace Renci.SshNet
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <c>null</c>.</exception>
|
||||
/// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <c>null</c> or contains only whitespace.</exception>
|
||||
/// <exception cref="SshException">If a problem occurs while copying the file</exception>
|
||||
public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback asyncCallback, object state)
|
||||
{
|
||||
if (sourcePath == null)
|
||||
@@ -2074,60 +2076,72 @@ namespace Renci.SshNet
|
||||
|
||||
var sourceDirectory = new DirectoryInfo(sourcePath);
|
||||
|
||||
var sourceFiles = FileSystemAbstraction.EnumerateFiles(sourceDirectory, searchPattern).ToList();
|
||||
if (sourceFiles.Count == 0)
|
||||
return uploadedFiles;
|
||||
|
||||
#region Existing Files at The Destination
|
||||
|
||||
var destFiles = InternalListDirectory(destinationPath, null);
|
||||
var destDict = new Dictionary<string, ISftpFile>();
|
||||
foreach (var destFile in destFiles)
|
||||
using (var sourceFiles = sourceDirectory.EnumerateFiles(searchPattern).GetEnumerator())
|
||||
{
|
||||
if (destFile.IsDirectory)
|
||||
continue;
|
||||
destDict.Add(destFile.Name, destFile);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Upload the difference
|
||||
|
||||
const Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;
|
||||
foreach (var localFile in sourceFiles)
|
||||
{
|
||||
var isDifferent = !destDict.ContainsKey(localFile.Name);
|
||||
|
||||
if (!isDifferent)
|
||||
if (!sourceFiles.MoveNext())
|
||||
{
|
||||
var temp = destDict[localFile.Name];
|
||||
// TODO: Use md5 to detect a difference
|
||||
//ltang: File exists at the destination => Using filesize to detect the difference
|
||||
isDifferent = localFile.Length != temp.Length;
|
||||
return uploadedFiles;
|
||||
}
|
||||
|
||||
if (isDifferent)
|
||||
#region Existing Files at The Destination
|
||||
|
||||
var destFiles = InternalListDirectory(destinationPath, null);
|
||||
var destDict = new Dictionary<string, ISftpFile>();
|
||||
foreach (var destFile in destFiles)
|
||||
{
|
||||
var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);
|
||||
try
|
||||
if (destFile.IsDirectory)
|
||||
{
|
||||
using (var file = File.OpenRead(localFile.FullName))
|
||||
{
|
||||
InternalUploadFile(file, remoteFileName, uploadFlag, null, null);
|
||||
}
|
||||
|
||||
uploadedFiles.Add(localFile);
|
||||
|
||||
if (asynchResult != null)
|
||||
{
|
||||
asynchResult.Update(uploadedFiles.Count);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
destDict.Add(destFile.Name, destFile);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Upload the difference
|
||||
|
||||
const Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;
|
||||
do
|
||||
{
|
||||
var localFile = sourceFiles.Current;
|
||||
if (localFile == null)
|
||||
{
|
||||
throw new Exception(string.Format("Failed to upload {0} to {1}", localFile.FullName, remoteFileName), ex);
|
||||
continue;
|
||||
}
|
||||
|
||||
var isDifferent = true;
|
||||
if (destDict.TryGetValue(localFile.Name, out var remoteFile))
|
||||
{
|
||||
// TODO: Use md5 to detect a difference
|
||||
//ltang: File exists at the destination => Using filesize to detect the difference
|
||||
isDifferent = localFile.Length != remoteFile.Length;
|
||||
}
|
||||
|
||||
if (isDifferent)
|
||||
{
|
||||
var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);
|
||||
try
|
||||
{
|
||||
using (var file = File.OpenRead(localFile.FullName))
|
||||
{
|
||||
InternalUploadFile(file, remoteFileName, uploadFlag, null, null);
|
||||
}
|
||||
|
||||
uploadedFiles.Add(localFile);
|
||||
|
||||
if (asynchResult != null)
|
||||
{
|
||||
asynchResult.Update(uploadedFiles.Count);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new SshException($"Failed to upload {localFile.FullName} to {remoteFileName}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
while (sourceFiles.MoveNext());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user