public void SftpSynchronizeDirectoriesAsyncResultConstructorTest()
 {
     AsyncCallback asyncCallback = null; // TODO: Initialize to an appropriate value
     object state = null; // TODO: Initialize to an appropriate value
     SftpSynchronizeDirectoriesAsyncResult target = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state);
     Assert.Inconclusive("TODO: Implement code to verify target");
 }
        /// <summary>
        /// Begins the synchronize directories.
        /// </summary>
        /// <param name="sourcePath">The source path.</param>
        /// <param name="destinationPath">The destination path.</param>
        /// <param name="searchPattern">The search pattern.</param>
        /// <param name="asyncCallback">The async callback.</param>
        /// <param name="state">The state.</param>
        /// <returns>
        /// An <see cref="System.IAsyncResult" /> that represents the asynchronous directory synchronization.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">sourceDir</exception>
        /// <exception cref="System.ArgumentException">destDir</exception>
        public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback asyncCallback, object state)
        {
            if (sourcePath == null)
            {
                throw new ArgumentNullException("sourceDir");
            }

            if (destinationPath.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("destDir");
            }

            var asyncResult = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state);

            this.ExecuteThread(() =>
            {
                try
                {
                    var result = this.InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncResult);

                    asyncResult.SetAsCompleted(result, false);
                }
                catch (Exception exp)
                {
                    asyncResult.SetAsCompleted(exp, false);
                }
            });

            return(asyncResult);
        }
        /// <summary>
        /// Begins the synchronize directories.
        /// </summary>
        /// <param name="sourcePath">The source path.</param>
        /// <param name="destinationPath">The destination path.</param>
        /// <param name="searchPattern">The search pattern.</param>
        /// <param name="asyncCallback">The async callback.</param>
        /// <param name="state">The state.</param>
        /// <returns>
        /// An <see cref="System.IAsyncResult" /> that represents the asynchronous directory synchronization.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">sourceDir</exception>
        /// <exception cref="System.ArgumentException">destDir</exception>
        public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback asyncCallback, object state)
        {
            if (sourcePath == null)
                throw new ArgumentNullException("sourceDir");

            if (destinationPath.IsNullOrWhiteSpace())
                throw new ArgumentException("destDir");

            var asyncResult = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state);

            this.ExecuteThread(() =>
            {
                try
                {
                    var result = this.InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncResult);

                    asyncResult.SetAsCompleted(result, false);
                }
                catch (Exception exp)
                {
                    asyncResult.SetAsCompleted(exp, false);
                }
            });

            return asyncResult;
        }
        public void SftpSynchronizeDirectoriesAsyncResultConstructorTest()
        {
            AsyncCallback asyncCallback = null; // TODO: Initialize to an appropriate value
            object        state         = null; // TODO: Initialize to an appropriate value
            SftpSynchronizeDirectoriesAsyncResult target = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state);

            Assert.Inconclusive("TODO: Implement code to verify target");
        }
        private IEnumerable <FileInfo> InternalSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, SftpSynchronizeDirectoriesAsyncResult asynchResult)
        {
            if (destinationPath.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("destinationPath");
            }

            if (!Directory.Exists(sourcePath))
            {
                throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath));
            }

            IList <FileInfo> uploadedFiles = new List <FileInfo>();

            DirectoryInfo sourceDirectory = new DirectoryInfo(sourcePath);

#if SILVERLIGHT
            var sourceFiles = sourceDirectory.EnumerateFiles(searchPattern);
#else
            var sourceFiles = sourceDirectory.GetFiles(searchPattern);
#endif

            if (sourceFiles == null || sourceFiles.Count() <= 0)
            {
                return(uploadedFiles);
            }

            try
            {
                #region Existing Files at The Destination

                var destFiles = InternalListDirectory(destinationPath, null);
                Dictionary <string, SftpFile> destDict = new Dictionary <string, SftpFile>();
                foreach (var destFile in destFiles)
                {
                    if (destFile.IsDirectory)
                    {
                        continue;
                    }
                    destDict.Add(destFile.Name, destFile);
                }

                #endregion

                #region Upload the difference

                bool  isDifferent = false;
                Flags uploadFlag  = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;
                foreach (var localFile in sourceFiles)
                {
                    isDifferent = !destDict.ContainsKey(localFile.Name);

                    if (!isDifferent)
                    {
                        SftpFile 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;
                    }

                    if (isDifferent)
                    {
                        var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);
                        try
                        {
                            using (var file = File.OpenRead(localFile.FullName))
                            {
                                this.InternalUploadFile(file, remoteFileName, uploadFlag, null, null);
                            }

                            uploadedFiles.Add(localFile);

                            if (asynchResult != null)
                            {
                                asynchResult.Update(uploadedFiles.Count);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("Failed to upload {0} to {1}", localFile.FullName, remoteFileName), ex);
                        }
                    }
                }

                #endregion
            }
            catch
            {
                throw;
            }
            return(uploadedFiles);
        }
        private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, SftpSynchronizeDirectoriesAsyncResult asynchResult)
        {
            if (destinationPath.IsNullOrWhiteSpace())
                throw new ArgumentException("destinationPath");

            if (!Directory.Exists(sourcePath))
                throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath));

            IList<FileInfo> uploadedFiles = new List<FileInfo>();

            DirectoryInfo sourceDirectory = new DirectoryInfo(sourcePath);

#if SILVERLIGHT
            var sourceFiles = sourceDirectory.EnumerateFiles(searchPattern);
#else
            var sourceFiles = sourceDirectory.GetFiles(searchPattern);
#endif

            if (sourceFiles == null || sourceFiles.Count() <= 0)
                return uploadedFiles;

            try
            {
                #region Existing Files at The Destination

                var destFiles = InternalListDirectory(destinationPath, null);
                Dictionary<string, SftpFile> destDict = new Dictionary<string, SftpFile>();
                foreach (var destFile in destFiles)
                {
                    if (destFile.IsDirectory)
                        continue;
                    destDict.Add(destFile.Name, destFile);
                }

                #endregion

                #region Upload the difference

                bool isDifferent = false;
                Flags uploadFlag = Flags.Write | Flags.Truncate | Flags.CreateNewOrOpen;
                foreach (var localFile in sourceFiles)
                {
                    isDifferent = !destDict.ContainsKey(localFile.Name);

                    if (!isDifferent)
                    {
                        SftpFile temp = destDict[localFile.Name];
                        //ltang: File exists at the destination => Using filesize to detect the difference
                        isDifferent = localFile.Length != temp.Length;
                    }

                    if (isDifferent)
                    {
                        var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);
                        try
                        {
                            using (var file = File.OpenRead(localFile.FullName))
                            {
                                this.InternalUploadFile(file, remoteFileName, uploadFlag, null, null);
                            }

                            uploadedFiles.Add(localFile);

                            if (asynchResult != null)
                            {
                                asynchResult.Update(uploadedFiles.Count);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("Failed to upload {0} to {1}", localFile.FullName, remoteFileName), ex);
                        }
                    }
                }

                #endregion
            }
            catch
            {
                throw;
            }
            return uploadedFiles;
        }
Esempio n. 7
0
        private IEnumerable<FileInfo> InternalSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, SftpSynchronizeDirectoriesAsyncResult asynchResult)
        {
            if (!Directory.Exists(sourcePath))
                throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath));

            var uploadedFiles = new List<FileInfo>();

            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, SftpFile>();
            foreach (var destFile in destFiles)
            {
                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)
                {
                    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;
                }

                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 Exception(string.Format("Failed to upload {0} to {1}", localFile.FullName, remoteFileName), ex);
                    }
                }
            }

            #endregion

            return uploadedFiles;
        }
        private async Task <IEnumerable <IStorageFile> > InternalSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, SftpSynchronizeDirectoriesAsyncResult asynchResult)
        {
            if (destinationPath.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("destinationPath");
            }

            StorageFolder sourceDirectory;

            try
            {
                sourceDirectory = await StorageFolder.GetFolderFromPathAsync(sourcePath);
            }
            catch (FileNotFoundException)
            {
                throw new FileNotFoundException(string.Format("Source directory not found: {0}", sourcePath));
            }

            var uploadedFiles = new List <IStorageFile>();

            var searchRegex = new Regex(string.Join(string.Empty, searchPattern.Select(c => c == '*' ? ".*" : c == '?' ? ".?" : Regex.Escape(c.ToString()))));

            var sourceFiles = (await sourceDirectory.GetFilesAsync()).Where <StorageFile>(file => searchRegex.IsMatch(file.Name)).ToList();

            if (!sourceFiles.Any())
            {
                return(uploadedFiles);
            }

            #region Existing Files at The Destination

            var destFiles = InternalListDirectory(destinationPath, null);
            var destDict  = new Dictionary <string, SftpFile>();
            foreach (var destFile in destFiles)
            {
                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)
                {
                    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 = (await localFile.GetBasicPropertiesAsync()).Size != (ulong)temp.Length;
                }

                if (isDifferent)
                {
                    var remoteFileName = string.Format(CultureInfo.InvariantCulture, @"{0}/{1}", destinationPath, localFile.Name);
                    try
                    {
                        using (var file = await localFile.OpenStreamForReadAsync())
                        {
                            InternalUploadFile(file, remoteFileName, uploadFlag, null, null);
                        }

                        uploadedFiles.Add(localFile);

                        if (asynchResult != null)
                        {
                            asynchResult.Update(uploadedFiles.Count);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("Failed to upload {0} to {1}", localFile.Path, remoteFileName), ex);
                    }
                }
            }

            #endregion

            return(uploadedFiles);
        }