Esempio n. 1
0
        public void AppendActionListWithDeleteRenameMove(FileExtended firstFileExtended, FileExtended secondFileExtended,
                                                         CsvRow row)
        {
            var filePairAction = new FilePairAction(firstFileExtended, secondFileExtended);



            FileExtended sourceFile = WorkingWithFiles.GetSourceAndDestFile(firstFileExtended, secondFileExtended)[FileType.Source];
            FileExtended destFile   = WorkingWithFiles.GetSourceAndDestFile(firstFileExtended, secondFileExtended)[FileType.Destination];

            // handle deletions
            bool deletion = IdentifyDeletion(sourceFile, destFile, filePairAction);

            if (deletion)
            {
                //_actionList.Add(filePairAction);
                return;
            }

            // handle renaming and moving
            var oldFirstFileType     = (FileType)Enum.Parse(typeof(FileType), row[0]);
            var oldFirstBasePath     = row[1];
            var oldFirstFileFullPath = row[2];
            var oldFirstFileId       = row[3];
            var oldFirstFile         = new FileExtended(oldFirstFileType, oldFirstBasePath, oldFirstFileFullPath, oldFirstFileId);

            var oldSecondFileType     = (FileType)Enum.Parse(typeof(FileType), row[4]);
            var oldSecondBasePath     = row[5];
            var oldSecondFileFullPath = row[6];
            var oldSecondFileId       = row[7];
            var oldSecondFile         = new FileExtended(oldSecondFileType, oldSecondBasePath, oldSecondFileFullPath,
                                                         oldSecondFileId);

            var oldSourceFile = oldFirstFileType == FileType.Source ? oldFirstFile : oldSecondFile;
            var oldDestFile   = oldSecondFileType == FileType.Destination ? oldSecondFile : oldFirstFile;


            IdentifyRenameMove(sourceFile, oldSourceFile, destFile, oldDestFile, filePairAction);
            if (filePairAction.ActionType == ActionType.RenameMove
                ||
                filePairAction.ActionType == ActionType.Rename
                ||
                filePairAction.ActionType == ActionType.Move
                // ||
                //filePairAction.ActionType == ActionType.Delete
                )
            {
                AddFilePairWithCheck(filePairAction);
            }
        }
        private void CalculateSpaceNeeded()
        {
            long sourceDeletionSize = 0; // B
            long sourceCreationSize = 0;
            long destDeletionSize   = 0;
            long destCreationSize   = 0;

            var creationsAndDeletions =
                ActionsList.FindAll(x => x.ActionType == ActionType.Create
                                    //|| x.ActionType == ActionType.Delete
                                    );

            foreach (var action in creationsAndDeletions)
            {
                var files      = WorkingWithFiles.GetSourceAndDestFile(action.File1, action.File2);
                var sourceFile = files[FileType.Source];
                var destFile   = files[FileType.Destination];

                if (action.ActionType == ActionType.Create)
                {
                    if (action.ActionDirection == Direction.SourceToDestination)
                    {
                        destCreationSize += sourceFile.FileSize;
                    }
                    else
                    {
                        sourceCreationSize += destFile.FileSize;
                    }
                }

                //if (action.ActionType == ActionType.Delete)
                //{
                //    if (action.ActionDirection == Direction.SourceToDestination)
                //    {
                //        destDeletionSize += destFile.FileSize;
                //    }
                //    else
                //    {
                //        sourceDeletionSize += sourceFile.FileSize;
                //    }
                //}
            }

            _spaceNeededInSource      = (int)Math.Round((double)(sourceCreationSize - sourceDeletionSize) / (1024 * 1024));
            _spaceNeededInDestination = (int)Math.Round((double)(destCreationSize - destDeletionSize) / (1024 * 1024));
        }
        public void PerformActions()
        {
            Console.WriteLine();
            var actionsToPerform = _actionList.FindAll(x => x.ActionType != ActionType.None);

            if (actionsToPerform.Count == 0)
            {
                Console.WriteLine("No changes have been detected - no actions needed");
                return;
            }

            var syncWatch = new Stopwatch();

            syncWatch.Start();


            Console.WriteLine("Starting synchronization....");
            foreach (var action in actionsToPerform)
            {
                var          filesDict  = WorkingWithFiles.GetSourceAndDestFile(action.File1, action.File2);
                FileExtended sourceFile = filesDict[FileType.Source];
                FileExtended destFile   = filesDict[FileType.Destination];


                try
                {
                    switch (action.ActionType)
                    {
                    case ActionType.Create:
                        var creatingFileOp = action.ActionDirection == Direction.SourceToDestination
                                ? sourceFile.fullPath
                                : destFile.fullPath;
                        DisplaySyncProcessStats("Copying file " + creatingFileOp);
                        ActionCreate(sourceFile, destFile, action.ActionDirection);
                        _filesCreated++;

                        //Init.DisplayCompletionInfo("actions performed",
                        //    _filesCreated + _filesDeleted + _filesMoved + _filesRenamed + _filesRenamedMoved + _filesUpdated,
                        //    ActionsList.Count);
                        break;

                    case ActionType.Update:
                        var updatingFile = action.ActionDirection == Direction.SourceToDestination
                                ? destFile.fullPath
                                : sourceFile.fullPath;
                        DisplaySyncProcessStats("Updating file " + updatingFile);
                        ActionUpdate(sourceFile, destFile, action.ActionDirection);
                        _filesUpdated++;

                        break;

                    case ActionType.RenameMove:
                        var movingFile = action.ActionDirection == Direction.SourceToDestination
                                ? destFile.fullPath
                                : sourceFile.fullPath;
                        DisplaySyncProcessStats("Renaming and moving file " + movingFile);
                        ActionRenameMove(sourceFile, destFile, action.ActionDirection);
                        _filesRenamedMoved++;

                        break;

                    case ActionType.Rename:
                        var renamingFile = action.ActionDirection == Direction.SourceToDestination
                                ? destFile.fullPath
                                : sourceFile.fullPath;
                        DisplaySyncProcessStats("Renaming file " + renamingFile);
                        ActionRenameMove(sourceFile, destFile, action.ActionDirection);
                        _filesRenamed++;

                        break;

                    case ActionType.Move:
                        var movingFile2 = action.ActionDirection == Direction.SourceToDestination
                                ? destFile.fullPath
                                : sourceFile.fullPath;
                        DisplaySyncProcessStats("Renaming and moving file " + movingFile2);
                        ActionRenameMove(sourceFile, destFile, action.ActionDirection);
                        _filesMoved++;

                        break;

                        //case ActionType.Delete:
                        //    var archivingFile = action.ActionDirection == Direction.SourceToDestination
                        //        ? destFile.fullPath
                        //        : sourceFile.fullPath;
                        //    DisplaySyncProcessStats("Archiving file " + archivingFile);
                        //    ActionDelete(sourceFile, destFile, action.ActionDirection);
                        //    _filesDeleted++;

                        //    break;
                    }
                    action.SyncSuccess = true;
                }
                catch (Exception ex)
                {
                    action.SyncSuccess      = false;
                    action.ExceptionMessage = ex.Message;
                    _failedActions.Add(action);
                }
            }
            syncWatch.Stop();

            Console.WriteLine("\n\nSynchronization complete! Elapsed time: "
                              + Init.FormatTime(syncWatch.ElapsedMilliseconds));
        }