Example #1
0
        /// <summary>Starts the actual (non-simulation) Robocopy process.</summary>
        private void StartProcess(string sourceFolder)
        {
            _process         = new RobocopyProcess(Task, sourceFolder, DestinationFolder, _simulationProcessOutputLinesCount);
            _process.Exited += Process_Exited;
            if (_simulationProcessOutputLinesCount > 0)
            {
                _process.ProgressChanged += Process_ProgressChanged;
            }

            if (!TryStartRobocopy(_process))
            {
                return;
            }

            _status.OnEnterNewStage("Mirroring...", string.Format("to {0}", PathHelper.Quote(DestinationFolder)));
            _status.IsAbortingSupported = true;
        }
Example #2
0
        /// <summary>
        /// Creates/overwrites the scheduled task for the specified mirror task.
        /// </summary>
        public Task Save(MirrorTask mirrorTask, Trigger trigger)
        {
            if (mirrorTask == null)
            {
                throw new ArgumentNullException("mirrorTask");
            }
            if (trigger == null)
            {
                throw new ArgumentNullException("trigger");
            }

            var definition = _service.NewTask();

            definition.RegistrationInfo.Description = string.Format("Mirrors {0} to {1}.",
                                                                    PathHelper.Quote(mirrorTask.Source), PathHelper.Quote(mirrorTask.Target));

            definition.Actions.Add(new ExecAction(Application.ExecutablePath,
                                                  mirrorTask.Guid, Application.StartupPath));

            definition.Triggers.Add(trigger);

            definition.Principal.LogonType = TaskLogonType.InteractiveToken;

            // set some advanced settings under Vista+
            if (!_v1Mode)
            {
                definition.Settings.AllowHardTerminate     = false;
                definition.Settings.StopIfGoingOnBatteries = false;
                definition.Settings.StartWhenAvailable     = true;

                if (UacHelper.IsInAdminRole())
                {
                    definition.Principal.RunLevel = TaskRunLevel.Highest;
                }
            }

            Delete(mirrorTask);

            return(_folder.RegisterTaskDefinition(GetName(mirrorTask), definition,
                                                  TaskCreation.Create, null, null, TaskLogonType.InteractiveToken, null));
        }
Example #3
0
        private void StartInVscSession()
        {
            string sourceVolume = PathHelper.RemoveTrailingSeparator(Path.GetPathRoot(SourceFolder));

            _vscSession        = new VolumeShadowCopySession();
            _vscSession.Error += VscSession_Error;
            _vscSession.Ready += VscSession_Ready;

            _status.OnEnterNewStage("Preparing...", string.Format("Creating shadow copy of volume {0} ...", PathHelper.Quote(sourceVolume)));

            // create and mount the shadow copy
            _vscSession.Start(SourceFolder);

            _status.IsAbortingSupported = true;
        }
Example #4
0
 public FileLockedException(string path, Exception innerException)
     : base(string.Format("The file {0} is locked.", PathHelper.Quote(path)), innerException)
 {
 }
Example #5
0
        /// <summary>Logs a Robocopy run.</summary>
        /// <param name="process">Terminated Robocopy process.</param>
        public static LogEntry LogRun(string taskGuid, RobocopyProcess process, string sourceFolder,
                                      string destinationFolder, bool updateLastSuccessTimeStamp)
        {
            if (string.IsNullOrEmpty(taskGuid))
            {
                throw new ArgumentNullException("taskGuid");
            }
            if (process == null)
            {
                throw new ArgumentNullException("process");
            }
            if (string.IsNullOrEmpty(sourceFolder))
            {
                throw new ArgumentNullException("sourceFolder");
            }
            if (string.IsNullOrEmpty(destinationFolder))
            {
                throw new ArgumentNullException("destinationFolder");
            }

            EventLogEntryType type;
            string            messageFormat;

            if (process.ExitCode == -1)
            {
                type          = EventLogEntryType.Error;
                messageFormat = "Operation aborted while mirroring {0} to {1}.";
            }
            else if (process.ExitCode == 0)
            {
                type          = EventLogEntryType.Information;
                messageFormat = "Already in sync: {0} and {1}";
            }
            else if (process.IsAnyExitFlagSet(RobocopyExitCodes.FatalError))
            {
                type          = EventLogEntryType.Error;
                messageFormat = "A fatal error occurred while trying to mirror {0} to {1}.";
            }
            else if (process.IsAnyExitFlagSet(RobocopyExitCodes.CopyErrors))
            {
                type          = EventLogEntryType.Error;
                messageFormat = "Some items could not be mirrored from {0} to {1}.";
            }
            else if (process.IsAnyExitFlagSet(RobocopyExitCodes.MismatchedItems))
            {
                type          = EventLogEntryType.Warning;
                messageFormat = "Some file <-> folder mismatches while mirroring {0} to {1}.";
            }
            else
            {
                type          = EventLogEntryType.Information;
                messageFormat = "Success: {0} mirrored to {1}";
            }

            string message = string.Format(messageFormat,
                                           PathHelper.Quote(sourceFolder),
                                           PathHelper.Quote(destinationFolder));

            return(WriteEntry(taskGuid, type, message, process.FullOutput,
                              updateLastSuccessTimeStamp));
        }
Example #6
0
        /// <exception cref="FileLockedException"></exception>
        protected XmlFileManager(string path, string rootTag, bool readOnly)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }
            if (string.IsNullOrEmpty(rootTag))
            {
                throw new ArgumentNullException("rootTag");
            }

            FilePath = path;

            // make sure the folder exists
            string folder = Path.GetDirectoryName(FilePath);

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            // open/create the file
            try
            {
                _file = File.Open(FilePath, FileMode.OpenOrCreate,
                                  readOnly ? FileAccess.Read : FileAccess.ReadWrite,
                                  readOnly ? FileShare.ReadWrite : FileShare.Read);
            }
            catch (IOException e)
            {
                throw new FileLockedException(FilePath, e);
            }

            // parse it
            var document = new XDocument();

            try
            {
                using (var fileLock = new FileLock(_file))
                {
                    if (_file.Length > 0)
                    {
                        document = XDocument.Load(_file);
                    }
                    else                     // initialize the XML document with the root element
                    {
                        document = new XDocument(new XElement(rootTag));
                    }
                }
            }
            catch (FileLockedException)
            {
                Dispose();
                throw;
            }
            catch (Exception e)
            {
                Dispose();
                throw new InvalidDataException(string.Format("{0} is corrupt.", PathHelper.Quote(FilePath)), e);
            }

            RootElement = document.Root;

            if (readOnly)
            {
                Dispose();
            }
        }
Example #7
0
        /// <param name="task">Task to be backed up/restored.</param>
        /// <param name="expectedNumOutputLines">
        /// Expected total number of output lines for progress estimation, e.g.,
        /// obtained by a prior simulation run.
        /// Required for the ProgressChanged event to be fired.
        /// </param>
        public RobocopyProcess(MirrorTask task, string sourceFolder, string destinationFolder, int expectedNumOutputLines = -1)
        {
            if (task == null)
            {
                throw new ArgumentNullException("task");
            }
            if (string.IsNullOrEmpty(sourceFolder))
            {
                throw new ArgumentNullException("sourceFolder");
            }
            if (string.IsNullOrEmpty(destinationFolder))
            {
                throw new ArgumentNullException("destinationFolder");
            }

            if (!Directory.Exists(sourceFolder))
            {
                throw new InvalidOperationException(string.Format("The source folder {0} does not exist.", PathHelper.Quote(sourceFolder)));
            }
            if (!Directory.Exists(destinationFolder))
            {
                throw new InvalidOperationException(string.Format("The destination folder {0} does not exist.", PathHelper.Quote(destinationFolder)));
            }

            // only use the bundled Robocopy version if the system does not ship with one
            string exePath = Path.Combine(Environment.SystemDirectory, "Robocopy.exe");

            if (!File.Exists(exePath))
            {
                exePath = Path.Combine(System.Windows.Forms.Application.StartupPath, @"Tools\Robocopy.exe");

                if (!File.Exists(exePath))
                {
                    throw new InvalidOperationException(string.Format("{0} does not exist.", PathHelper.Quote(exePath)));
                }
            }

#if DEBUG
            exePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "DummyConsoleProcess.exe");
#endif

            SourceFolder      = sourceFolder;
            DestinationFolder = destinationFolder;
            PurgeExtraItems   = task.DeleteExtraItems;

            _expectedNumOutputLines = expectedNumOutputLines;

            StartInfo.FileName  = exePath;
            StartInfo.Arguments = string.Format("{0} {1} {2}", PathHelper.QuoteForRobocopy(SourceFolder),
                                                PathHelper.QuoteForRobocopy(DestinationFolder), BuildSwitches(task, SourceFolder));
        }