예제 #1
0
 private void AddDirectoryContents(string path)
 {
     try
     {
         var scanDirectory = new ScanDirectory(_sender._logger, _sender._excludeList,
                                               false, _cancellationTokenSource.Token);
         foreach (var srcEntry in scanDirectory.ScanPath(_sender._srcPath, path))
         {
             _sender.AddChange(FsChange.CreateChange(srcEntry.Path));
         }
     }
     catch (OperationCanceledException)
     {
     }
 }
예제 #2
0
파일: Sender.cs 프로젝트: lulzzz/DevSync
        private void OnWatcherRenamed(object source, RenamedEventArgs e)
        {
            // ignore event for srcPath (don't know why it occurs rarely)
            if (e.FullPath == _srcPath || e.OldFullPath == _srcPath)
            {
                return;
            }

            var path    = GetPath(e.FullPath);
            var oldPath = GetPath(e.OldFullPath);

            if (_gitIsBusy && oldPath == GIT_INDEX_LOCK_FILENAME)
            {
                SetGitIsBusy(false);
            }

            // is new file excluded?
            if (_excludeList.IsMatch(path))
            {
                // old file is not excluded -> delete it
                if (!_excludeList.IsMatch(oldPath))
                {
                    AddChange(FsChange.CreateRemove(oldPath));
                }

                // both files are excluded -> do nothing
            }
            else // new file is not excluded
            {
                // old file is excluded -> send change with withSubdirectories
                if (_excludeList.IsMatch(oldPath))
                {
                    AddChange(FsChange.CreateChange(path), withSubdirectories: true);
                }
                else
                {
                    // both files are not excluded -> send rename
                    AddChange(FsChange.CreateRename(path, oldPath));
                }
            }
        }
예제 #3
0
파일: Sender.cs 프로젝트: lulzzz/DevSync
        private void OnWatcherChanged(object source, FileSystemEventArgs e)
        {
            // ignore event for srcPath (don't know why it occurs rarely)
            if (e.FullPath == _srcPath)
            {
                return;
            }

            var path = GetPath(e.FullPath);

            if (!_gitIsBusy && e.ChangeType == WatcherChangeTypes.Created && path == GIT_INDEX_LOCK_FILENAME)
            {
                SetGitIsBusy(true);
            }

            if (_excludeList.IsMatch(path))
            {
                return;
            }
            AddChange(FsChange.CreateChange(path), withSubdirectories: e.ChangeType == WatcherChangeTypes.Created);
        }
예제 #4
0
파일: Sender.cs 프로젝트: lulzzz/DevSync
        private bool SendChanges()
        {
            // fetch changes
            lock (_changes)
            {
                if (_changes.Count == 0)
                {
                    return(true);
                }
                _applyRequest.SetChanges(_changes.Values);
            }

            if (!_isSending)
            {
                _logger.Log("Sending");
                _isSending = true;
            }

            var sw             = Stopwatch.StartNew();
            var response       = _agentStarter.SendCommand <ApplyResponse>(_applyRequest);
            var responseResult = response.Result.ToDictionary(x => x.Key, y => y);

            bool hasErrors = false;

            // process sent changes
            lock (_changes)
            {
                foreach (var fsChange in _applyRequest.SentChanges)
                {
                    if (!fsChange.Expired)
                    {
                        _changes.Remove(fsChange.Path);
                        if (responseResult.TryGetValue(fsChange.Path, out var fsChangeResult))
                        {
                            if (fsChangeResult.ResultCode != FsChangeResultCode.Ok)
                            {
                                var withSubdirectories = false;
                                // ignore sender errors: just resend
                                if (fsChangeResult.ResultCode != FsChangeResultCode.SenderError)
                                {
                                    // if rename failed -> send change with withSubdirectories
                                    if (fsChange.ChangeType == FsChangeType.Rename)
                                    {
                                        withSubdirectories = true;
                                    }
                                    else
                                    {
                                        hasErrors = true;
                                        _logger.Log(
                                            $"Change apply error {fsChange.ChangeType} {fsChange.Path}: {fsChangeResult.ErrorMessage ?? "-"}", LogLevel.Error);
                                    }
                                }
                                AddChange(FsChange.CreateChange(fsChange.Path), false, withSubdirectories);
                            }
                        }
                    }
                    else
                    {
                        // remove expired
                        if (_changes.TryGetValue(fsChange.Path, out var oldFsChange) && oldFsChange.Expired)
                        {
                            _changes.Remove(fsChange.Path);
                        }
                    }
                }
            }

            _sentReporter.Report(_applyRequest.SentChanges, _applyRequest.SentChangesSize, sw.Elapsed);
            _applyRequest.ClearChanges();
            UpdateHasWork();
            return(!hasErrors);
        }
예제 #5
0
파일: Sender.cs 프로젝트: lulzzz/DevSync
        private void Scan()
        {
            var            sw      = Stopwatch.StartNew();
            List <FsEntry> srcList = null;
            Dictionary <string, FsEntry> destList;

            /*
             * Start agent before scan source
             *
             * Old timeline: [Main thread]       Start ... Initialize ... Scan destination ... Finish
             *               [Secondary thread]  Scan source ................................. Finish
             *
             * New timeline: [Main thread]       Start ... Initialize ... Scan destination ... Finish
             *               [Secondary thread]            Scan source ....................... Finish
             *
             * A failed start could cause unnecessary scanning source in old timeline.
             * No need to scan source before start in most cases because it is about as fast as the scan destination.
             */
            _agentStarter.Start();

            using (var tokenSource = new CancellationTokenSource())
            {
                var cancellationToken = tokenSource.Token;

                // scan source
                var task = Task.Run(() =>
                {
                    try
                    {
                        var swScanSource  = Stopwatch.StartNew();
                        var scanDirectory =
                            new ScanDirectory(_logger, _excludeList, cancellationToken: cancellationToken);
                        srcList = scanDirectory.ScanPath(_srcPath).ToList();
                        cancellationToken.ThrowIfCancellationRequested();
                        _logger.Log($"Scanned source {srcList.Count} items in {swScanSource.ElapsedMilliseconds} ms");
                    }
                    catch (OperationCanceledException)
                    {
                        srcList = null;
                    }
                }, cancellationToken);

                try
                {
                    var swScanDestination = Stopwatch.StartNew();
                    // scan destination
                    var response = _agentStarter.SendCommand <ScanResponse>(new ScanRequest(_logger));
                    destList = response.FileList.ToDictionary(x => x.Path, y => y);
                    _logger.Log($"Scanned destination {destList.Count} items in {swScanDestination.ElapsedMilliseconds} ms");
                    task.Wait(cancellationToken);
                }
                catch (Exception)
                {
                    tokenSource.Cancel();
                    throw;
                }
            }

            // During scan, changes could come from file system events or from PathScanner, we should not overwrite them.
            var  itemsCount  = 0;
            long changesSize = 0;

            lock (_changes)
            {
                foreach (var srcEntry in srcList)
                {
                    if (!destList.TryGetValue(srcEntry.Path, out var destEntry))
                    {
                        destEntry = FsEntry.Empty;
                    }

                    // Skip changed srcEntry
                    if (!_changes.ContainsKey(srcEntry.Path))
                    {
                        // add to changes (no replace)
                        if (!srcEntry.Compare(destEntry))
                        {
                            itemsCount++;
                            if (!srcEntry.IsDirectory)
                            {
                                changesSize += srcEntry.Length;
                            }
                            AddChange(FsChange.CreateChange(srcEntry), false);
                        }
                    }

                    if (!destEntry.IsEmpty)
                    {
                        destList.Remove(destEntry.Path);
                    }
                }

                // add deletes
                foreach (var destEntry in destList.Values)
                {
                    // Skip changed destEntry
                    if (!_changes.ContainsKey(destEntry.Path))
                    {
                        itemsCount++;
                        AddChange(FsChange.CreateRemove(destEntry.Path), false);
                    }
                }
            }

            _needToScan = false;
            _logger.Log(
                $"Scanned in {sw.ElapsedMilliseconds} ms, {itemsCount} items, {PrettySize(changesSize)} to send");
            UpdateHasWork();
        }