Ejemplo n.º 1
0
 /// <summary>
 /// Downloaded the given RemotePropery and adds it as a local file
 /// </summary>
 /// <param name="remoteProperty"></param>
 /// <param name="client"></param>
 /// <param name="conf"></param>
 /// <param name="logger"></param>
 /// <param name="indexManager"></param>
 private void FetchFileFromServer(CancellationToken cancleToken, IRemoteProperty remoteProperty, ISyncClient client, Configuration conf, ILogger logger, SyncIndexManager indexManager)
 {
     try
     {
         string localFilePath = conf.Local.LocalSyncDir + remoteProperty.DecodedRelativeRemotePath;
         if (!File.Exists(localFilePath))
         {
             //File does not exist local, will be downloaded and added
             _logger.Debug(remoteProperty.DecodedRelativeRemotePath + " does not exist locally. Will be downloaded and added to index");
             FileInfo temp = client.DownloadRemoteFileToTemp(cancleToken, remoteProperty.DecodedRelativeRemotePath);
             if (temp != null)
             {
                 FileInfo newFile = FileManager.CopyFile(temp.FullName, localFilePath, true, _logger);
                 if (newFile != null)
                 {
                     FileSyncElement newFileElement = new FileSyncElement(newFile, conf.Local.LocalSyncDir, logger);
                     indexManager.AddOrUpdate(CreateIndexElement(newFileElement, remoteProperty));
                 }
             }
         }
     } catch (Exception exc)
     {
         logger.Error("Unexpected error while fetching " + remoteProperty.RelativeRemotePath + " from server: ", exc);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Uploads the given ISync Element using the ISyncClient
        /// If success the Element will be added or an existing element will be updated with
        /// the new values
        /// </summary>
        /// <param name="element"></param>
        /// <param name="client"></param>
        /// <param name="indexManager"></param>
        private void Upload(CancellationToken cancleToken, ISyncElement element, ISyncClient client, SyncIndexManager indexManager)
        {
            IRemoteProperty uploaded = element.Upload(cancleToken, client);

            if (uploaded != null)
            {
                //Upload was successfull, add as tracked element to index
                indexManager.AddOrUpdate(CreateIndexElement(element, uploaded));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Patches the local file with the current File version provided by the ISyncClient
        /// </summary>
        /// <param name="file"></param>
        /// <param name="client"></param>
        /// <param name="indexManager"></param>
        private void PatchLocalFile(CancellationToken cancleToken, FileSyncElement file, ISyncClient client, SyncIndexManager indexManager,
                                    IEnumerable <IRemoteProperty> remoteProperties)
        {
            //File has been changed remotely
            FileInfo update = file.PatchLocalFile(cancleToken, client);

            if (update != null)
            {
                //Updte Index Manager
                indexManager.AddOrUpdate(CreateIndexElement(file, GetProperty(remoteProperties, file)));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handles the synchronisation of the given ISyncElement
        /// </summary>
        /// <param name="element"></param>
        /// <param name="client"></param>
        /// <param name="properties"></param>
        /// <param name="indexManager"></param>
        private void HandleISyncElement(CancellationToken cancleToken, ISyncElement element, ISyncClient client, IEnumerable <IRemoteProperty> properties,
                                        SyncIndexManager indexManager, ILogger logger, Configuration config)
        {
            if (cancleToken.IsCancellationRequested)
            {
                return;
            }
            //The general rules defining when to synchronize what are the same
            //for all ISyncClients and they follow the following design:
            //
            //1. Iterate all local folder and their files, check for updates on server:
            //  Both:
            //      1. when it does not exist in index and server -> upload
            //      2. when it exists only in index -> delete (has been deleted remotely)
            //      3. when exists on server but not index -> update index
            //  Files:
            //      1. When is exists in server and index -> check for updates
            //2. Add all files that are not in index to local copy
            //3. Remove all files that exist in index and server but not local from server -> deleted local

            (bool existInIndex, bool existsRemote)existance = CheckForElementExistance(element, properties, indexManager);

            if (!existance.existInIndex && !existance.existsRemote)
            {
                //Not in index and not in remote -> upload
                logger.Debug(element.Type + " " + element.RelativePath + " does not exist in local Index and remote, " +
                             "File will be added to both");
                Upload(cancleToken, element, client, indexManager);
                //Set the Method to ignore Updates -> Just handle children in case of Directory
                //Because directly checking for updates may cause exceptions because the server is
                //slow and does not return the correct properties on request yet
                HandleISyncElementChildrenOrUpdates(cancleToken, element, client, properties, indexManager, logger, config, true);
            }
            else if (existance.existInIndex && !existance.existsRemote)
            {
                //In index but not remote -> delete (has been deleted remotely)
                logger.Debug(element.Type + " " + element.RelativePath + " exists in index but has been removed remote. Deleting " + element.Type);
                DeleteLocalElement(element, indexManager);
            }
            else if (!existance.existInIndex && existance.existsRemote)
            {
                //Remote but not in index -> Add to index
                logger.Debug(element.Type + " " + element.RelativePath + " was missing in the index -> will be added.");
                //As it cannot be said if there was changes remote, no remote revision is added
                indexManager.AddOrUpdate(CreateIndexElement(element, null));
                HandleISyncElementChildrenOrUpdates(cancleToken, element, client, properties, indexManager, logger, config, false);
            }
            else if (existance.existInIndex && existance.existsRemote)
            {
                HandleISyncElementChildrenOrUpdates(cancleToken, element, client, properties, indexManager, logger, config, false);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Fetches the given Folder and its content from the server
        /// </summary>
        private void FetchFolderFromServer(CancellationToken cancleToken, string relativePath, ISyncClient client, Configuration conf, ILogger logger, SyncIndexManager indexManager)
        {
            if (cancleToken.IsCancellationRequested)
            {
                return;
            }

            //Check if folder exists locally -> else create
            string localDirectoryPath = conf.Local.LocalSyncDir + relativePath;

            try
            {
                if (!Directory.Exists(localDirectoryPath))
                {
                    _logger.Debug("Folder " + localDirectoryPath + " does not exist locally. Creating folder and adding to the index.");
                    Directory.CreateDirectory(localDirectoryPath);

                    indexManager.AddOrUpdate(CreateIndexElement(new DirectorySyncElement(new DirectoryInfo(localDirectoryPath), conf.Local.LocalSyncDir, logger), null));
                }
            } catch (Exception exc)
            {
                _logger.Error("A error occured while creating local folder", exc);
            }

            //Get Properties and Process them
            IEnumerable <IRemoteProperty> remoteProperties = client.GetProperties(GetRemotePath(conf, localDirectoryPath));

            //Something went wrong during the connection to the server
            if (remoteProperties == null)
            {
                _logger.Error("Something went wrong during connection to server.");
                return;
            }

            foreach (IRemoteProperty property in remoteProperties)
            {
                if (cancleToken.IsCancellationRequested)
                {
                    return;
                }
                if (property.ElementType.Equals(SyncElementType.Directory))
                {
                    FetchFolderFromServer(cancleToken, property.DecodedRelativeRemotePath, client, conf, logger, indexManager);
                }
                else
                {
                    FetchFileFromServer(cancleToken, property, client, conf, logger, indexManager);
                }
            }
        }