예제 #1
0
        public async Task <SyncIngestResult> IngestAsync(SyncConfig syncConfig)
        {
            var result = new SyncIngestResult(syncConfig);

            await IngestCoreAsync(syncConfig, syncConfig.Source, syncConfig.Target, result);

            return(result);
        }
예제 #2
0
        private async Task IngestCoreAsync(SyncConfig syncConfig, string src, string target, SyncIngestResult result)
        {
            _cancellationToken.ThrowIfCancellationRequested();

            Directory.CreateDirectory(target);
            foreach (var dir in Directory.EnumerateDirectories(src, "*", SearchOption.TopDirectoryOnly))
            {
                _cancellationToken.ThrowIfCancellationRequested();
                result.SourceDirCount++;
                await IngestCoreAsync(syncConfig, dir, Path.Combine(target, new DirectoryInfo(dir).Name), result);
            }

            foreach (var file in Directory.EnumerateFiles(src, "*", SearchOption.TopDirectoryOnly))
            {
                _cancellationToken.ThrowIfCancellationRequested();
                result.SourceFileCount++;

                var fileName = Path.GetFileName(file);
                if (_options.ExcludePatterns.Value != null && _options.ExcludePatterns.Value.Any())
                {
                    var matched = false;
                    foreach (var excludePattern in _options.ExcludePatterns.Value)
                    {
                        if (excludePattern.IsMatch(fileName))
                        {
                            matched = true;
                            break;
                        }
                    }

                    if (matched)
                    {
                        continue;
                    }
                }

                if (_options.IncludePatterns.Value != null && _options.IncludePatterns.Value.Any())
                {
                    var matched = false;
                    foreach (var includePattern in _options.IncludePatterns.Value)
                    {
                        if (includePattern.IsMatch(fileName))
                        {
                            matched = true;
                            break;
                        }
                    }

                    if (!matched)
                    {
                        continue;
                    }
                }

                var fileInfo = new FileInfo(file);
                if (fileInfo.LastWriteTime < _options.NewerModifyDateTime)
                {
                    continue;
                }

                var targetFilePath = Path.Combine(target, fileName);
                var syncInfo       = new SyncFileInfo(syncConfig, file, targetFilePath);
                if (!File.Exists(targetFilePath))
                {
                    _syncQueue.Enqueue(syncInfo);
                }
                else
                {
                    if (_options.Force)
                    {
                        syncInfo.Overwrite = true;
                        _syncQueue.Enqueue(syncInfo);
                    }
                    else if (_options.Strict)
                    {
                        var exactlySame =
                            await FileCompareUtil.ExactlySameAsync(file, targetFilePath, _cancellationToken);

                        if (!exactlySame)
                        {
                            syncInfo.Overwrite = true;
                            _syncQueue.Enqueue(syncInfo);
                        }
                    }
                }
            }
        }