Ejemplo n.º 1
0
        private List <IUpdateItem> ProcessDirectory(INode remoteDir, DirectoryInfo localDir,
                                                    bool recursive, bool removeExtraClientFiles, Func <INode, FileInfo, bool> versionEqualsComparer,
                                                    CancellationToken cancellationToken)
        {
            var results = new List <IUpdateItem>();

            var localContents = new List <FileSystemInfo>();

            if (localDir.Exists)
            {
                localContents.AddRange(localDir.GetFileSystemInfos("*", SearchOption.TopDirectoryOnly));
            }

            foreach (var remoteItem in GetSubNodes(remoteDir))
            {
                cancellationToken.ThrowIfCancellationRequested();

                switch (remoteItem.Type)
                {
                case NodeType.File:
                {
                    var localFile = localContents.OfType <FileInfo>().FirstOrDefault(x => string.Equals(x.Name, remoteItem.Name, StringComparison.OrdinalIgnoreCase));
                    if (localFile == null)
                    {
                        localFile = new FileInfo(Path.Combine(localDir.FullName, remoteItem.Name));
                    }
                    else
                    {
                        localContents.Remove(localFile);
                    }

                    var localIsUpToDate = localFile.Exists && versionEqualsComparer(remoteItem, localFile);

                    if (!localIsUpToDate)
                    {
                        results.Add(new MegaUpdateItem(remoteItem, this, localFile));
                    }

                    if (_latestModifiedDate < (remoteItem.ModificationDate ?? remoteItem.CreationDate))
                    {
                        _latestModifiedDate = remoteItem.ModificationDate ?? remoteItem.CreationDate;
                    }
                }
                break;

                case NodeType.Directory:
                    if (recursive)
                    {
                        var localItem = localContents.OfType <DirectoryInfo>().FirstOrDefault(x => string.Equals(x.Name, remoteItem.Name, StringComparison.OrdinalIgnoreCase));
                        if (localItem == null)
                        {
                            localItem = new DirectoryInfo(Path.Combine(localDir.FullName, remoteItem.Name));
                        }
                        else
                        {
                            localContents.Remove(localItem);
                        }

                        results.AddRange(ProcessDirectory(remoteItem, localItem, recursive, removeExtraClientFiles, versionEqualsComparer, cancellationToken));
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            // Remove all files that were not on the remote
            if (removeExtraClientFiles)
            {
                results.AddRange(UpdateSourceManager.FileInfosToDeleteItems(localContents));
            }

            return(results);
        }
Ejemplo n.º 2
0
        private async Task <List <IUpdateItem> > ProcessDirectory(ZipEntry remoteDir, DirectoryInfo localDir,
                                                                  bool recursive, bool removeNotExisting, Func <ZipEntry, FileInfo, bool> versionEqualsComparer,
                                                                  CancellationToken cancellationToken)
        {
            if (!remoteDir.IsDirectory)
            {
                throw new DirectoryNotFoundException();
            }

            var results = new List <IUpdateItem>();

            var localContents = new List <FileSystemInfo>();

            if (localDir.Exists)
            {
                localContents.AddRange(localDir.GetFileSystemInfos("*", SearchOption.TopDirectoryOnly));
            }

            foreach (var remoteItem in GetChildren(remoteDir))
            {
                cancellationToken.ThrowIfCancellationRequested();

                var remoteFilename = Path.GetFileName(remoteItem.FileName.TrimEnd('\\', '/'));

                if (remoteItem.IsDirectory)
                {
                    if (recursive)
                    {
                        var localItem = localContents.OfType <DirectoryInfo>().FirstOrDefault(x => string.Equals(x.Name, remoteFilename, StringComparison.OrdinalIgnoreCase));
                        if (localItem == null)
                        {
                            localItem = new DirectoryInfo(Path.Combine(localDir.FullName, remoteFilename));
                        }
                        else
                        {
                            localContents.Remove(localItem);
                        }

                        results.AddRange(await ProcessDirectory(remoteItem, localItem, recursive, removeNotExisting, versionEqualsComparer, cancellationToken));
                    }
                }
                else
                {
                    var localFile = localContents.OfType <FileInfo>().FirstOrDefault(x => string.Equals(x.Name, remoteFilename, StringComparison.OrdinalIgnoreCase));
                    if (localFile == null)
                    {
                        localFile = new FileInfo(Path.Combine(localDir.FullName, remoteFilename));
                    }
                    else
                    {
                        localContents.Remove(localFile);
                    }

                    var localIsUpToDate = localFile.Exists && versionEqualsComparer(remoteItem, localFile);
                    if (!localIsUpToDate)
                    {
                        results.Add(new ZipUpdateItem(remoteItem, localFile, this));
                    }

                    if (_latestModifiedDate < remoteItem.LastModified)
                    {
                        _latestModifiedDate = remoteItem.LastModified;
                    }
                }
            }

            cancellationToken.ThrowIfCancellationRequested();

            // Remove all files that were not on the remote
            if (removeNotExisting)
            {
                results.AddRange(UpdateSourceManager.FileInfosToDeleteItems(localContents));
            }

            return(results);
        }
Ejemplo n.º 3
0
        private async Task <List <IUpdateItem> > ProcessDirectory(FtpListItem remoteDir, DirectoryInfo localDir,
                                                                  bool recursive, bool removeNotExisting, Func <FtpListItem, FileInfo, bool> versionEqualsComparer,
                                                                  CancellationToken cancellationToken)
        {
            if (remoteDir.Type != FtpFileSystemObjectType.Directory)
            {
                throw new DirectoryNotFoundException();
            }

            var results = new List <IUpdateItem>();

            var localContents = new List <FileSystemInfo>();

            if (localDir.Exists)
            {
                localContents.AddRange(localDir.GetFileSystemInfos("*", SearchOption.TopDirectoryOnly));
            }

            foreach (var remoteItem in GetSubNodes(remoteDir))
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (remoteItem.Type == FtpFileSystemObjectType.Directory)
                {
                    if (recursive)
                    {
                        var localItem = localContents.OfType <DirectoryInfo>().FirstOrDefault(x => string.Equals(x.Name, remoteItem.Name, StringComparison.OrdinalIgnoreCase));
                        if (localItem == null)
                        {
                            localItem = new DirectoryInfo(Path.Combine(localDir.FullName, remoteItem.Name));
                        }
                        else
                        {
                            localContents.Remove(localItem);
                        }

                        results.AddRange(await ProcessDirectory(remoteItem, localItem, recursive, removeNotExisting, versionEqualsComparer, cancellationToken));
                    }
                }
                else if (remoteItem.Type == FtpFileSystemObjectType.File)
                {
                    var itemDate = GetDate(remoteItem);
                    if (itemDate > _latestModifiedDate)
                    {
                        _latestModifiedDate = itemDate;
                    }

                    var localFile = localContents.OfType <FileInfo>().FirstOrDefault(x => string.Equals(x.Name, remoteItem.Name, StringComparison.OrdinalIgnoreCase));
                    if (localFile == null)
                    {
                        localFile = new FileInfo(Path.Combine(localDir.FullName, remoteItem.Name));
                    }
                    else
                    {
                        localContents.Remove(localFile);
                    }

                    var localIsUpToDate = localFile.Exists && versionEqualsComparer(remoteItem, localFile);
                    results.Add(new FtpUpdateItem(remoteItem, this, localFile, localIsUpToDate));
                }
            }

            cancellationToken.ThrowIfCancellationRequested();

            // Remove all files that were not on the remote
            if (removeNotExisting)
            {
                results.AddRange(UpdateSourceManager.FileInfosToDeleteItems(localContents));
            }

            return(results);
        }
Ejemplo n.º 4
0
        private List <UpdateItem> ProcessDirectory(IRemoteItem remoteDir, DirectoryInfo localDir,
                                                   bool recursive, bool removeNotExisting, Func <IRemoteItem, FileInfo, bool> versionEqualsComparer,
                                                   CancellationToken cancellationToken)
        {
            if (!remoteDir.IsDirectory)
            {
                throw new DirectoryNotFoundException();
            }

            var results = new List <UpdateItem>();

            var localContents = new List <FileSystemInfo>();

            if (localDir.Exists)
            {
                localContents.AddRange(localDir.GetFileSystemInfos("*", SearchOption.TopDirectoryOnly));
            }

            var enabledName = localContents.OfType <FileInfo>().ToDictionary(x => x, GetEnabledName);

            foreach (var remoteItem in remoteDir.GetDirectoryContents(cancellationToken))
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (remoteItem.IsDirectory)
                {
                    if (recursive)
                    {
                        var localItem = localContents.OfType <DirectoryInfo>().FirstOrDefault(x => string.Equals(x.Name, remoteItem.Name, StringComparison.OrdinalIgnoreCase));
                        if (localItem == null)
                        {
                            localItem = new DirectoryInfo(Path.Combine(localDir.FullName, remoteItem.Name));
                        }
                        else
                        {
                            localContents.Remove(localItem);
                        }

                        results.AddRange(ProcessDirectory(remoteItem, localItem, recursive, removeNotExisting, versionEqualsComparer, cancellationToken));
                    }
                }
                else if (remoteItem.IsFile)
                {
                    var itemDate = remoteItem.ModifiedTime;
                    if (itemDate > _latestModifiedDate)
                    {
                        _latestModifiedDate = itemDate;
                    }

                    var localFile = localContents.OfType <FileInfo>().FirstOrDefault(x => string.Equals(enabledName[x], remoteItem.Name, StringComparison.OrdinalIgnoreCase));
                    if (localFile == null)
                    {
                        localFile = new FileInfo(Path.Combine(localDir.FullName, remoteItem.Name));
                    }
                    else
                    {
                        localContents.Remove(localFile);
                    }

                    var localIsUpToDate = localFile.Exists && versionEqualsComparer(remoteItem, localFile);
                    results.Add(new UpdateItem(localFile, remoteItem, localIsUpToDate));
                }
            }

            cancellationToken.ThrowIfCancellationRequested();

            // Remove all files that were not on the remote
            if (removeNotExisting)
            {
                results.AddRange(UpdateSourceManager.FileInfosToDeleteItems(localContents));
            }

            return(results);
        }