@@ -1996,6 +1996,7 @@ public void SetAttributes(string path, SftpFileAttributes fileAttributes)
1996
1996
/// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <c>null</c>.</exception>
1997
1997
/// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <c>null</c> or contains only whitespace.</exception>
1998
1998
/// <exception cref="SftpPathNotFoundException"><paramref name="destinationPath"/> was not found on the remote host.</exception>
1999
+ /// <exception cref="SshException">If a problem occurs while copying the file</exception>
1999
2000
public IEnumerable < FileInfo > SynchronizeDirectories ( string sourcePath , string destinationPath , string searchPattern )
2000
2001
{
2001
2002
if ( sourcePath == null )
@@ -2019,6 +2020,7 @@ public IEnumerable<FileInfo> SynchronizeDirectories(string sourcePath, string de
2019
2020
/// </returns>
2020
2021
/// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <c>null</c>.</exception>
2021
2022
/// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <c>null</c> or contains only whitespace.</exception>
2023
+ /// <exception cref="SshException">If a problem occurs while copying the file</exception>
2022
2024
public IAsyncResult BeginSynchronizeDirectories ( string sourcePath , string destinationPath , string searchPattern , AsyncCallback asyncCallback , object state )
2023
2025
{
2024
2026
if ( sourcePath == null )
@@ -2074,60 +2076,72 @@ private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath,
2074
2076
2075
2077
var sourceDirectory = new DirectoryInfo ( sourcePath ) ;
2076
2078
2077
- var sourceFiles = FileSystemAbstraction . EnumerateFiles ( sourceDirectory , searchPattern ) . ToList ( ) ;
2078
- if ( sourceFiles . Count == 0 )
2079
- return uploadedFiles ;
2079
+ using ( var sourceFiles = sourceDirectory . EnumerateFiles ( searchPattern ) . GetEnumerator ( ) )
2080
+ {
2081
+ if ( ! sourceFiles . MoveNext ( ) )
2082
+ {
2083
+ return uploadedFiles ;
2084
+ }
2080
2085
2081
- #region Existing Files at The Destination
2086
+ #region Existing Files at The Destination
2082
2087
2083
- var destFiles = InternalListDirectory ( destinationPath , null ) ;
2084
- var destDict = new Dictionary < string , ISftpFile > ( ) ;
2085
- foreach ( var destFile in destFiles )
2086
- {
2087
- if ( destFile . IsDirectory )
2088
- continue ;
2089
- destDict . Add ( destFile . Name , destFile ) ;
2090
- }
2088
+ var destFiles = InternalListDirectory ( destinationPath , null ) ;
2089
+ var destDict = new Dictionary < string , ISftpFile > ( ) ;
2090
+ foreach ( var destFile in destFiles )
2091
+ {
2092
+ if ( destFile . IsDirectory )
2093
+ {
2094
+ continue ;
2095
+ }
2091
2096
2092
- #endregion
2097
+ destDict . Add ( destFile . Name , destFile ) ;
2098
+ }
2093
2099
2094
- #region Upload the difference
2100
+ #endregion
2095
2101
2096
- const Flags uploadFlag = Flags . Write | Flags . Truncate | Flags . CreateNewOrOpen ;
2097
- foreach ( var localFile in sourceFiles )
2098
- {
2099
- var isDifferent = ! destDict . ContainsKey ( localFile . Name ) ;
2102
+ #region Upload the difference
2100
2103
2101
- if ( ! isDifferent )
2104
+ const Flags uploadFlag = Flags . Write | Flags . Truncate | Flags . CreateNewOrOpen ;
2105
+ do
2102
2106
{
2103
- var temp = destDict [ localFile . Name ] ;
2104
- // TODO: Use md5 to detect a difference
2105
- //ltang: File exists at the destination => Using filesize to detect the difference
2106
- isDifferent = localFile . Length != temp . Length ;
2107
- }
2107
+ var localFile = sourceFiles . Current ;
2108
+ if ( localFile == null )
2109
+ {
2110
+ continue ;
2111
+ }
2108
2112
2109
- if ( isDifferent )
2110
- {
2111
- var remoteFileName = string . Format ( CultureInfo . InvariantCulture , @"{0}/{1}" , destinationPath , localFile . Name ) ;
2112
- try
2113
+ var isDifferent = true ;
2114
+ if ( destDict . TryGetValue ( localFile . Name , out var remoteFile ) )
2113
2115
{
2114
- using ( var file = File . OpenRead ( localFile . FullName ) )
2116
+ // TODO: Use md5 to detect a difference
2117
+ //ltang: File exists at the destination => Using filesize to detect the difference
2118
+ isDifferent = localFile . Length != remoteFile . Length ;
2119
+ }
2120
+
2121
+ if ( isDifferent )
2122
+ {
2123
+ var remoteFileName = string . Format ( CultureInfo . InvariantCulture , @"{0}/{1}" , destinationPath , localFile . Name ) ;
2124
+ try
2115
2125
{
2116
- InternalUploadFile ( file , remoteFileName , uploadFlag , null , null ) ;
2117
- }
2126
+ using ( var file = File . OpenRead ( localFile . FullName ) )
2127
+ {
2128
+ InternalUploadFile ( file , remoteFileName , uploadFlag , null , null ) ;
2129
+ }
2118
2130
2119
- uploadedFiles . Add ( localFile ) ;
2131
+ uploadedFiles . Add ( localFile ) ;
2120
2132
2121
- if ( asynchResult != null )
2133
+ if ( asynchResult != null )
2134
+ {
2135
+ asynchResult . Update ( uploadedFiles . Count ) ;
2136
+ }
2137
+ }
2138
+ catch ( Exception ex )
2122
2139
{
2123
- asynchResult . Update ( uploadedFiles . Count ) ;
2140
+ throw new SshException ( $ "Failed to upload { localFile . FullName } to { remoteFileName } " , ex ) ;
2124
2141
}
2125
2142
}
2126
- catch ( Exception ex )
2127
- {
2128
- throw new Exception ( string . Format ( "Failed to upload {0} to {1}" , localFile . FullName , remoteFileName ) , ex ) ;
2129
- }
2130
2143
}
2144
+ while ( sourceFiles . MoveNext ( ) ) ;
2131
2145
}
2132
2146
2133
2147
#endregion
0 commit comments