示例#1
0
        private void BtnStartSync_Click(object sender, EventArgs e)
        {
            BtnStartSync.Enabled = false;
            BtnEndSync.Enabled   = true;

            InitRoots();
            Logger.Info("Building replication dictionary...");
            _replDictionary = ReplicationDictionary.BuildDictionary(_roots, Rules);

            Logger.Info("Queueing initial sync operations...");
            var count = 0;

            PbOpStatus.Maximum = _replDictionary.Count;
            PbOpStatus.Value   = 0;

            foreach (var item in _replDictionary)
            {
                count++;
                _changeQueue.Queue(new FileChange(ChangeAction.Sync, item.Key, MaxRetries));
                PbOpStatus.Value = count;
                Application.DoEvents();
            }

            Logger.Info("Creating file system watchers...");
            for (var xx = 0; xx < _roots.Count; xx++)
            {
                var watcher = new FileSystemWatcher();
                var root    = _roots[xx];

                watcher.Path = root;
                watcher.IncludeSubdirectories = true;

                // watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;

                watcher.Filter   = "*.*";
                watcher.Changed += new FileSystemEventHandler(OnChanged);
                watcher.Created += new FileSystemEventHandler(OnChanged);
                watcher.Deleted += new FileSystemEventHandler(OnChanged);
                watcher.Renamed += new RenamedEventHandler(OnRenamed);

                watcher.EnableRaisingEvents = true;

                _rootWatchers.Add(watcher);
            }

            QueueTimer.Enabled = true;
        }
示例#2
0
        public static ReplicationDictionary BuildDictionary(List <string> roots, SyncRules rules)
        {
            var replDict = new ReplicationDictionary();

            replDict.Roots = roots;
            replDict.Rules = rules;

            var dirsToDo = new Stack <string>();

            for (var xx = 0; xx < roots.Count; xx++)
            {
                dirsToDo.Push(roots[xx]);
            }

            while (dirsToDo.Count > 0)
            {
                Application.DoEvents();
                var curDir = dirsToDo.Pop();

                var ruleResult = rules.ProcessPath(curDir);
                if (ruleResult == RuleResult.Reject)
                {
                    Logger.Verbose("Path ignored due to rule; " + curDir);
                    continue;
                }

                string curRoot = replDict.ExtractRootFromPath(curDir);

                var dirs = Directory.GetDirectories(curDir);

                for (var xx = 0; xx < dirs.Length; xx++)
                {
                    dirsToDo.Push(dirs[xx]);
                    Application.DoEvents();
                }

                var files = Directory.GetFiles(curDir);

                for (var xx = 0; xx < files.Length; xx++)
                {
                    var file = replDict.RemoveRoot(files[xx], curRoot);
                    Application.DoEvents();

                    ruleResult = rules.ProcessPath(file);

                    if (ruleResult == RuleResult.Reject)
                    {
                        Logger.Info("File rejected due to rule; " + file);
                        continue;
                    }

                    if (replDict.ContainsKey(file))
                    {
                        continue;
                    }

                    var replInfo = new ReplInfo(file, roots);

                    replDict.Add(file, replInfo);
                }
            }

            return(replDict);
        }