@@ -1996,6 +1996,7 @@ public void SetAttributes(string path, SftpFileAttributes fileAttributes)
19961996 /// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <c>null</c>.</exception>
19971997 /// <exception cref="ArgumentException"><paramref name="destinationPath"/> is <c>null</c> or contains only whitespace.</exception>
19981998 /// <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>
19992000 public IEnumerable < FileInfo > SynchronizeDirectories ( string sourcePath , string destinationPath , string searchPattern )
20002001 {
20012002 if ( sourcePath == null )
@@ -2019,6 +2020,7 @@ public IEnumerable<FileInfo> SynchronizeDirectories(string sourcePath, string de
20192020 /// </returns>
20202021 /// <exception cref="ArgumentNullException"><paramref name="sourcePath"/> is <c>null</c>.</exception>
20212022 /// <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>
20222024 public IAsyncResult BeginSynchronizeDirectories ( string sourcePath , string destinationPath , string searchPattern , AsyncCallback asyncCallback , object state )
20232025 {
20242026 if ( sourcePath == null )
@@ -2074,60 +2076,72 @@ private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath,
20742076
20752077 var sourceDirectory = new DirectoryInfo ( sourcePath ) ;
20762078
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+ }
20802085
2081- #region Existing Files at The Destination
2086+ #region Existing Files at The Destination
20822087
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+ }
20912096
2092- #endregion
2097+ destDict . Add ( destFile . Name , destFile ) ;
2098+ }
20932099
2094- #region Upload the difference
2100+ #endregion
20952101
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
21002103
2101- if ( ! isDifferent )
2104+ const Flags uploadFlag = Flags . Write | Flags . Truncate | Flags . CreateNewOrOpen ;
2105+ do
21022106 {
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+ }
21082112
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 ) )
21132115 {
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
21152125 {
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+ }
21182130
2119- uploadedFiles . Add ( localFile ) ;
2131+ uploadedFiles . Add ( localFile ) ;
21202132
2121- if ( asynchResult != null )
2133+ if ( asynchResult != null )
2134+ {
2135+ asynchResult . Update ( uploadedFiles . Count ) ;
2136+ }
2137+ }
2138+ catch ( Exception ex )
21222139 {
2123- asynchResult . Update ( uploadedFiles . Count ) ;
2140+ throw new SshException ( $ "Failed to upload { localFile . FullName } to { remoteFileName } " , ex ) ;
21242141 }
21252142 }
2126- catch ( Exception ex )
2127- {
2128- throw new Exception ( string . Format ( "Failed to upload {0} to {1}" , localFile . FullName , remoteFileName ) , ex ) ;
2129- }
21302143 }
2144+ while ( sourceFiles . MoveNext ( ) ) ;
21312145 }
21322146
21332147 #endregion
0 commit comments