Exemplo n.º 1
0
        /// <summary>
        /// Iterates trough all Index Elements and checks wheter the still exist
        /// If not they have been deleted locally and will be removed on the server
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="client"></param>
        /// <param name="logger"></param>
        /// <param name="indexManager"></param>
        private void DetectLocalDeletedElements(CancellationToken cancleToken, Configuration dto, ISyncClient client, ILogger logger, SyncIndexManager indexManager)
        {
            IEnumerable <SyncIndexElementDTO> indexElements  = indexManager.GetOrCreateIndexFile();
            List <SyncIndexElementDTO>        removeElements = new List <SyncIndexElementDTO>();

            foreach (SyncIndexElementDTO element in indexElements)
            {
                if (cancleToken.IsCancellationRequested)
                {
                    return;
                }

                bool   exists       = true;
                string absolutePath = dto.Local.LocalSyncDir + element.ReleativeFilePath;
                try
                {
                    if (element.ElementType.Equals(SyncElementType.File))
                    {
                        exists = File.Exists(absolutePath);
                    }
                    else if (element.ElementType.Equals(SyncElementType.Directory))
                    {
                        exists = Directory.Exists(absolutePath);
                    }
                } catch (Exception exc) { _logger.Error("Error occured while checking " + element.ElementType + "existance: ", exc); }
                if (!exists)
                {
                    _logger.Debug(element.ElementType + " " + element.ReleativeFilePath + " does not exist anymore. Will be deleted from server and index.");
                    removeElements.Add(element);
                }
            }

            //Get List of Directories that are contained in the removeElements list
            List <SyncIndexElementDTO> directories = removeElements.Where(x => x.ElementType == SyncElementType.Directory).ToList();

            foreach (SyncIndexElementDTO element in directories)
            {
                //remove all Elements that are contained in removeElements list and
                //are part of this Directory
                removeElements.RemoveAll(x => x.ReleativeFilePath.Length > element.ReleativeFilePath.Length &&
                                         x.ReleativeFilePath.Substring(0, element.ReleativeFilePath.Length) == element.ReleativeFilePath);
            }

            //Remove all elements that can be removed
            foreach (SyncIndexElementDTO element in removeElements)
            {
                if (client.Remove(cancleToken, element.ReleativeFilePath))
                {
                    //If its a Directory delete all Elements that are contained in this Directory
                    //From the index!
                    if (element.ElementType == SyncElementType.Directory)
                    {
                        indexManager.RemoveAll(x => x.ReleativeFilePath.Length >= element.ReleativeFilePath.Length &&
                                               x.ReleativeFilePath.Substring(0, element.ReleativeFilePath.Length) == element.ReleativeFilePath);
                    }
                    else
                    {
                        indexManager.Remove(element);
                    }
                }
            }
        }