Esempio n. 1
0
        public override void Build(IBounce bounce)
        {
            var fromPath = FromPath.Value;
            var toPath   = _toPath.Value;

            bounce.Log.Debug("copying from: `{0}', to: `{1}'", fromPath, toPath);

            FileSystemCopier.Copy(fromPath, toPath, GetValueOf(Excludes), GetValueOf(Includes), DeleteToDirectory.Value);
        }
Esempio n. 2
0
        public void RunOperations(IList <SyncOperation> operations)
        {
            if (operations == null)
            {
                throw new ArgumentNullException("operations");
            }

            try
            {
                int percentageProgress = 0;
                _log.InfoFormat("Running {0} operations.", operations.Count.ToString("#,0"));
                OnActionChanged("Performing sync ...");

                // Delete file operations
                _log.Debug("Deleting files ...");
                int allocatedPercentage = 5;
                var deleteFiles         =
                    operations.Where(o => o.OperationType == SyncOperationType.DeleteSource && o.IsFile).Select(o => o.Source.FullName).ToList();
                deleteFiles.AddRange(operations.Where(o => o.OperationType == SyncOperationType.DeleteTarget && o.IsFile).Select(o => o.Target.FullName).ToList());
                RunDeleteOperations(allocatedPercentage, percentageProgress, deleteFiles, true);
                percentageProgress += allocatedPercentage;

                // Delete directory operations
                _log.Debug("Deleting directories ...");
                allocatedPercentage = 4;
                var deleteDirectories =
                    operations.Where(o => o.OperationType == SyncOperationType.DeleteSource && !o.IsFile).Select(o => o.Source.FullName).ToList();
                deleteDirectories.AddRange(operations.Where(o => o.OperationType == SyncOperationType.DeleteTarget && !o.IsFile).Select(o => o.Target.FullName).ToList());
                RunDeleteOperations(allocatedPercentage, percentageProgress, deleteDirectories, false);
                percentageProgress += allocatedPercentage;

                // Create directories
                _log.Debug("Creating directories ...");
                allocatedPercentage = 1;
                var createDirectories =
                    operations.Where(o => (o.OperationType == SyncOperationType.CopyToSource || o.OperationType == SyncOperationType.CopyToTarget) && !o.IsFile).ToList();
                for (var i = 0; i < createDirectories.Count; i++)
                {
                    CheckStopped();
                    try
                    {
                        var sourceDirectory = (IDirectoryInfo)createDirectories[i].Source;
                        var targetDirectory = (IDirectoryInfo)createDirectories[i].Target;
                        if (createDirectories[i].OperationType == SyncOperationType.CopyToSource)
                        {
                            sourceDirectory = (IDirectoryInfo)createDirectories[i].Target;
                            targetDirectory = (IDirectoryInfo)createDirectories[i].Source;
                        }
                        var actionName = string.Format("Creating directory {0}", createDirectories[i]);
                        _log.Info(actionName);
                        OnActionChanged(actionName);
                        targetDirectory.Create();
                        targetDirectory.Attributes    = sourceDirectory.Attributes;
                        targetDirectory.LastWriteTime = sourceDirectory.LastWriteTime;
                    }
                    catch (Exception ex)
                    {
                        var message = string.Format("Error creating directory file {0}", createDirectories[i]);
                        _log.Error(message);
                        _log.Error(ex.Message, ex);
                        OnErrorOccured(message, ex);
                    }
                    OnProgressChanged(percentageProgress + (i + 1) * allocatedPercentage / createDirectories.Count);
                }
                percentageProgress += allocatedPercentage;

                // Copy files
                _log.Debug("Copying files ...");
                allocatedPercentage = 90;
                var copyFiles =
                    operations.Where(o => (o.OperationType == SyncOperationType.CopyToTarget || o.OperationType == SyncOperationType.CopyToSource) && o.IsFile).ToList();
                var copyToTargetFiles = copyFiles.Where(o => o.OperationType == SyncOperationType.CopyToTarget).ToList();
                var copyToSourceFiles = copyFiles.Where(o => o.OperationType == SyncOperationType.CopyToSource).ToList();
                BytesToCopy = copyToTargetFiles.Sum(o => o.Source.Size) + copyToSourceFiles.Sum(o => o.Target.Size);
                BytesCopied = 0;
                foreach (var operation in copyFiles)
                {
                    CheckStopped();
                    string sourceFile, targetFile;
                    long   operationSize;
                    if (operation.OperationType == SyncOperationType.CopyToTarget)
                    {
                        sourceFile    = operation.Source.FullName;
                        targetFile    = operation.Target.FullName;
                        operationSize = operation.Source.Size;
                    }
                    else
                    {
                        sourceFile    = operation.Target.FullName;
                        targetFile    = operation.Source.FullName;
                        operationSize = operation.Target.Size;
                    }
                    try
                    {
                        var actionName = string.Format("Copying {0} to {1}", sourceFile, targetFile);
                        _log.Info(actionName);
                        OnActionChanged(actionName);
                        _fileSystemCopier.Copy(sourceFile, targetFile);
                    }
                    catch (Exception ex)
                    {
                        var message = string.Format("Error copying {0} to {1}", sourceFile, targetFile);
                        _log.Error(message);
                        _log.Error(ex.Message, ex);
                        OnErrorOccured(message, ex);
                    }
                    BytesCopied += operationSize;
                    if (BytesToCopy != 0)
                    {
                        OnProgressChanged(percentageProgress + (int)(BytesCopied * allocatedPercentage / BytesToCopy));
                    }
                }
                OnProgressChanged(100);
            }
            catch (StoppedException)
            {
                _log.Info("Operation stopped.");
                OnActionChanged("Stopped.");
                StopPending = false;
            }
        }