Ejemplo n.º 1
0
        internal static void PreviewChangesStats(SyncExecution syncExec)
        {
            Console.WriteLine("\n");
            int filesToCreate     = syncExec.ActionsList.FindAll(x => x.ActionType == ActionType.Create).Count;
            int filesToUpdate     = syncExec.ActionsList.FindAll(x => x.ActionType == ActionType.Update).Count;
            int filesToRenameMove = syncExec.ActionsList.FindAll(x => x.ActionType == ActionType.RenameMove).Count;
            int filesToRename     = syncExec.ActionsList.FindAll(x => x.ActionType == ActionType.Rename).Count;
            int filesToMove       = syncExec.ActionsList.FindAll(x => x.ActionType == ActionType.Move).Count;

            //int filesToDelete = syncExec.ActionsList.FindAll(x => x.ActionType == ActionType.Delete).Count;



            Console.WriteLine("Files to create:            " + filesToCreate);
            Console.WriteLine("Files to update:            " + filesToUpdate);
            Console.WriteLine("Files to rename and move:   " + filesToRenameMove);
            Console.WriteLine("Files to rename:            " + filesToRename);
            Console.WriteLine("Files to move:              " + filesToMove);
            //Console.WriteLine("Files to delete:            " + filesToDelete);

            Console.WriteLine("For further details, please, see the actions log file -\n"
                              + syncExec.SyncConfig.ActionsPreviewLogFile);

            try
            {
                ShowSpaceInfo(syncExec);
            }
            catch (Exception e)
            {
                ErrorHandling.HandleException(syncExec.SyncConfig, e);
            }
        }
Ejemplo n.º 2
0
 internal static void WriteFailedActionsToLog(SyncExecution syncExec)
 {
     using (var logWriter = File.AppendText(syncExec.SyncConfig.ErrorLogFile))
     {
         WriteFailedActions(syncExec, logWriter);
     }
 }
Ejemplo n.º 3
0
        internal static void WriteFailedActions(SyncExecution syncExec, StreamWriter writer)
        {
            if (syncExec.FailedActions.Count > 0)
            {
                var failedActions = new List <FilePairAction>(syncExec.FailedActions);
                failedActions.Sort();

                writer.WriteLine("\n");
                writer.WriteLine(failedActions.Count +
                                 " actions could not be performed:");
                foreach (var action in failedActions)
                {
                    var sourceFile = WorkingWithFiles.GetSourceAndDestFile(action.File1, action.File2)[FileType.Source];
                    var destFile   = WorkingWithFiles.GetSourceAndDestFile(action.File1, action.File2)[FileType.Destination];
                    var sourcePath = sourceFile != null ? sourceFile.fullPath : "";
                    var destPath   = destFile != null ? destFile.fullPath : "";

                    writer.WriteLine("\taction:       " + action.ActionType);
                    writer.WriteLine("\tdirection:    " + action.ActionDirection);
                    writer.WriteLine("\tsource file:  " + sourcePath);
                    writer.WriteLine("\tdest file:    " + destPath);
                    writer.WriteLine("\treason:       " + action.ExceptionMessage);
                    writer.WriteLine();
                }
            }
        }
Ejemplo n.º 4
0
        internal static void WriteActionsListToLog(SyncExecution syncExec)
        {
            string logFile = syncExec.SyncConfig.ActionsPreviewLogFile;

            using (var writer = new StreamWriter(logFile))
            {
                WriteActionsList(syncExec, writer);
            }
        }
Ejemplo n.º 5
0
        private static void ShowSpaceInfo(SyncExecution syncExec)
        {
            string sourceVolume = DriveHelper.GetSourceVolume(syncExec.SyncConfig);
            string destVolume   = DriveHelper.GetDestVolume(syncExec.SyncConfig);

            int  sourceAvailableSpace;
            int  destAvailableSpace;
            bool networkTransfer = false;

            if (sourceVolume.Contains(@"\\"))
            {
                sourceAvailableSpace = 0;
                networkTransfer      = true;
            }
            else
            {
                sourceAvailableSpace = DriveHelper.GetVolumeAvailableSpace(sourceVolume);
            }
            if (destVolume.Contains(@"\\"))
            {
                destAvailableSpace = 0;
                networkTransfer    = true;
            }
            else
            {
                destAvailableSpace = DriveHelper.GetVolumeAvailableSpace(destVolume);
            }

            if (!networkTransfer)
            {
                Console.WriteLine("\nRequired space on disk " + sourceVolume + ": " + syncExec.SpaceNeededInSource +
                                  "MB. Available space: "
                                  + sourceAvailableSpace + "MB");
                Console.WriteLine("Required space on disk " + destVolume + ": " + syncExec.SpaceNeededInDestination +
                                  "MB. Available space: "
                                  + destAvailableSpace + "MB");
                if (sourceAvailableSpace < syncExec.SpaceNeededInSource)
                {
                    Console.WriteLine(
                        "WARNING: there is not enough available space for synchronization to complete. Disk " +
                        sourceVolume);
                }
                if (destAvailableSpace < syncExec.SpaceNeededInDestination)
                {
                    Console.WriteLine(
                        "WARNING: there is not enough available space for synchronization to complete. Disk " +
                        destVolume);
                }
            }
        }
Ejemplo n.º 6
0
        static void ListFilesLists(SyncExecution syncExec)
        {
            Console.WriteLine("source files:");
            foreach (var s in syncExec.SourceFiles.Values)
            {
                Console.WriteLine(s.fullPath);
                Console.WriteLine(s.fileID);
            }

            Console.WriteLine("\ndest files:");
            foreach (var d in syncExec.DestFiles.Values)
            {
                Console.WriteLine(d.fullPath);
                Console.WriteLine(d.fileID);
            }
        }
Ejemplo n.º 7
0
        internal static void WriteActionsList(SyncExecution syncExec, StreamWriter writer)
        {
            var    actionList = syncExec.ActionsList.Where(x => x.ActionType != ActionType.None);
            string direction;
            string source;
            string dest;


            writer.WriteLine("\nList of actions to perform: ");
            foreach (var action in actionList)
            {
                switch (action.ActionDirection)
                {
                case Direction.None:
                    direction = "==";
                    break;

                case Direction.SourceToDestination:
                    direction = "=>";
                    break;

                case Direction.DestinationToSource:
                    direction = "<=";
                    break;

                case Direction.Unknown:
                    direction = "??";
                    break;

                default:
                    throw new Exception("Ivalid direction");
                }

                var filesDict = WorkingWithFiles.GetSourceAndDestFile(action.File1, action.File2);
                source = filesDict[FileType.Source] != null ? filesDict[FileType.Source].fullPath : "";
                dest   = filesDict[FileType.Destination] != null ? filesDict[FileType.Destination].fullPath : "";

                writer.WriteLine(direction + action.ActionType + direction
                                 + source + "   -   " + dest);
            }
        }
Ejemplo n.º 8
0
        public Messenger(IConfiguration config, SyncConfigRepo configRepo, MessageRepo msgRepo, SyncExecutionRepo execRepo)
        {
            _config         = config;
            _syncConfigRepo = configRepo;
            _msgRepo        = msgRepo;
            _execRepo       = execRepo;

            _sqlClient = new SQLClientAdo();

            string configToExecute = _config["ExecuteOptions:JobName"];
            string smtpConfigName  = _config["ExecuteOptions:SmtpConfigName"];

            var syncConfig = _syncConfigRepo.GetByConfigName(configToExecute);


            //Create execution object to run job
            Execution = new SyncExecution()
            {
                SysUser   = _config["ExecuteOptions:Caller"],
                StartTime = DateTime.Now,
                Config    = syncConfig
            };
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            //TestSpaceSizes();
            Console.WriteLine("Starting the FileSync app... \n"
                              + "If you want to stop its execution at any time, please, press CTRL+C");

            Console.CancelKeyPress += (sender, e) =>
            {
                Console.WriteLine("\n\n\n\n\nExiting...");
                if (Console.ReadKey() != null)
                {
                    Environment.Exit(0);
                }
            };

            SyncConfig confInstance = null;

            try
            {
                Console.WriteLine("Initializing configuration...");
                confInstance = new SyncConfig();
                //string LogFolder = confInstance.Parameters["LogFolder"];
                //MappingCsvFileName = confInstance.Parameters["FileMappingFile"];
                Console.WriteLine("Folders for synchronization are following");
                foreach (var entry in confInstance.FolderMappings)
                {
                    Console.WriteLine(entry.Value.Item1 + "  <=>  " + entry.Value.Item2);
                }
                Console.WriteLine("Done");
            }

            catch (Exception ex)
            {
                string mes = ex.Message;
                ExitApp("Could not initialize configuration. Check error log file for details \n" + mes);
            }


            var syncExec = new SyncExecution(confInstance);

            if (syncExec.SyncConfig.FolderMappings.Count == 0)
            {
                ExitApp("No folder mappings found in config file, exiting");
            }

            // initialize source and destination files:
            try
            {
                Init.InitializeFiles(syncExec);
            }
            catch (Exception e)
            {
                ExitApp("Could not initialize files. \n" + e.Message);
            }



            // remove duplicates if this is configured:
            try
            {
                if (confInstance.Parameters["RemoveDuplicates"] == "yes")
                {
                    DuplicatesHandling.RemoveDuplicates(syncExec);
                }
            }
            catch (Exception e)
            {
                ExitApp("Could not remove duplicates. \n" + e.Message);
            }


            try
            {
                // retrieve existing file mapping from CSV
                // and, if necessary, create additional mapping from paths
                Init.MapFiles(syncExec);
            }
            catch (Exception e)
            {
                ExitApp("Could not map files. \n" + e.Message);
            }

            try
            {
                syncExec.AppendActionListWithUpdateCreateMove();
            }
            catch (Exception e)
            {
                ExitApp("Could not complete the stage of identifying updates, creations and moves. \n" + e.Message);
            }



            if (syncExec.AnyChangesNeeded)
            {
                SyncHelper.WriteActionsListToLog(syncExec);
                SyncHelper.PreviewChangesStats(syncExec);
                string proceedWithSync = "";
                while (proceedWithSync != "yes" && proceedWithSync != "no")
                {
                    Console.WriteLine("\nWould you like to perform synchronization (yes/no)?");
                    proceedWithSync = Console.ReadLine();
                    if (proceedWithSync == "yes")
                    {
                        syncExec.PerformActions();
                        SyncHelper.WriteFailedActionsToLog(syncExec);
                        CSVHelper.SaveFileMappingToCsv(syncExec);
                    }
                    else if (proceedWithSync == "no")
                    {
                        Console.WriteLine("The synchronization has been cancelled, exiting the app");
                        Thread.Sleep(1500);
                        Environment.Exit(0);
                    }
                }

                if (syncExec.FailedActions.Count > 0)
                {
                    Console.WriteLine("\nSome actions failed: for details, see the error log file -\n"
                                      + syncExec.SyncConfig.ErrorLogFile);
                }
                else
                {
                    if (proceedWithSync == "yes")
                    {
                        Console.WriteLine("\n\nExecution completed successfully.");
                    }
                }



                ExitApp("");
            }
            else
            {
                CSVHelper.SaveFileMappingToCsv(syncExec);
                ExitApp("No changes needed");
            }
        }