Esempio n. 1
0
        // TODO: implement statistic reporting

        /// <summary>
        /// Remove metadata files
        /// </summary>
        public void ClearMetadata()
        {
            if (!IsEnabled)
            {
                try
                {
                    // cannot delete metadata while worker thread is active - could be dangerous

                    File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                             Consts.Application.ProfileFolder, String.Format(Consts.Application.MetadataFileFormat, LocalGUID.ToString())));

                    File.Delete(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                             Consts.Application.ProfileFolder, String.Format(Consts.Application.MetadataFileFormat, RemoteGUID.ToString())));
                }
                catch (Exception ex)
                {
                    Log.Write(String.Format("Error deleting metadata files for pair [{0}]<=>[{1}]", LocalPath, RemotePath), ex);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Tries to initialize folder pair, result is set via State
        /// </summary>
        private void Init()
        {
            try
            {
                State = SyncPairState.Initializing;
                // verify paths
                LocalPath  = Path.GetFullPath(LocalPath);
                RemotePath = Path.GetFullPath(RemotePath);
                String metafileFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                                     Consts.Application.ProfileFolder);
                String localMetadataFile  = String.Format(Consts.Application.MetadataFileFormat, LocalGUID.ToString());
                String remoteMetadataFile = String.Format(Consts.Application.MetadataFileFormat, RemoteGUID.ToString());

                // set sync framework environment
                FileSyncScopeFilter filterLocal  = new FileSyncScopeFilter();
                FileSyncScopeFilter filterRemote = new FileSyncScopeFilter();

                if (!Directory.Exists(metafileFolder))
                {
                    Directory.CreateDirectory(metafileFolder);
                }

                if (localProvider != null)
                {
                    localProvider.Dispose();
                    localProvider = null;
                }

                if (remoteProvider != null)
                {
                    remoteProvider.Dispose();
                    remoteProvider = null;
                }

                localProvider = new FileSyncProvider(LocalGUID, LocalPath, filterLocal,
                                                     FileSyncOptions.RecycleConflictLoserFiles | FileSyncOptions.RecycleDeletedFiles,
                                                     metafileFolder, localMetadataFile, LocalPath, null);

                remoteProvider = new FileSyncProvider(RemoteGUID, RemotePath, filterRemote,
                                                      FileSyncOptions.RecycleConflictLoserFiles | FileSyncOptions.RecycleDeletedFiles,
                                                      metafileFolder, remoteMetadataFile, RemotePath, null);

                localProvider.Configuration.ConflictResolutionPolicy  = ConflictResolutionPolicy.SourceWins;
                remoteProvider.Configuration.ConflictResolutionPolicy = ConflictResolutionPolicy.SourceWins;


                if (Strategy.AutoSync)
                {
                    if (localFSWatcher != null)
                    {
                        localFSWatcher.Dispose();
                        localFSWatcher = null;
                    }
                    if (remoteFSWatcher != null)
                    {
                        remoteFSWatcher.Dispose();
                        remoteFSWatcher = null;
                    }

                    localFSWatcher  = new FileSystemWatcher(LocalPath);
                    remoteFSWatcher = new FileSystemWatcher(RemotePath);

                    localFSWatcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime |
                                                  NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite |
                                                  NotifyFilters.Security | NotifyFilters.Size;
                    remoteFSWatcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime |
                                                   NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite |
                                                   NotifyFilters.Security | NotifyFilters.Size;

                    localFSWatcher.Filter  = Consts.FileSystem.WatcherFilter;
                    remoteFSWatcher.Filter = Consts.FileSystem.WatcherFilter;

                    localFSWatcher.IncludeSubdirectories  = true;
                    remoteFSWatcher.IncludeSubdirectories = true;

                    localFSWatcher.Changed  += FSEventHandler;
                    remoteFSWatcher.Changed += FSEventHandler;

                    localFSWatcher.Deleted  += FSEventHandler;
                    remoteFSWatcher.Deleted += FSEventHandler;

                    localFSWatcher.Created  += FSEventHandler;
                    remoteFSWatcher.Created += FSEventHandler;

                    localFSWatcher.Renamed  += FSRenamedHander;
                    remoteFSWatcher.Renamed += FSRenamedHander;

                    localFSWatcher.Error  += FSErrorHandler;
                    remoteFSWatcher.Error += FSErrorHandler;

                    localFSWatcher.EnableRaisingEvents  = true;
                    remoteFSWatcher.EnableRaisingEvents = true;

                    if (Parameters.DebugLog)
                    {
                        Log.Write(String.Format("Pair [{0}]<=>[{1}] - file system watchers set, buffers local={2}, remote={3}...", LocalPath, RemotePath,
                                                localFSWatcher.InternalBufferSize, remoteFSWatcher.InternalBufferSize));
                    }
                }
                State = SyncPairState.Ready;
            }
            catch (Exception ex)
            {
                Log.Write(String.Format("Problems during initialization of pair [{0}]<=>[{1}], initialization will continue.", LocalPath, RemotePath), ex);
                State = SyncPairState.NotInitialized;
            }
        }