Exemplo n.º 1
0
 private void OnActionStart(SyncAction action)
 {
     if (this.ActionStart != null)
     {
         this.ActionStart(this, action);
     }
 }
        public SyncActionType Choose(SyncAction action)
        {
            // With two way, the only way we can get a delete is if it's in
            // a continuation state. Otherwise it's an initial scan and
            // everything is real and new
            if (action.Local != null && action.Local.IsDeleted && action.Remote != null && action.Remote.IsDeleted)
            {
                return(SyncActionType.None);
            }
            else if (action.Local != null && action.Local.IsDeleted)
            {
                return(SyncActionType.DeleteRemote);
            }
            else if (action.Remote != null && action.Remote.IsDeleted)
            {
                return(SyncActionType.DeleteLocal);
            }

            // Simple case of upload
            if (action.Local != null && action.Remote == null)
            {
                return(SyncActionType.Upload);
            }

            // And download
            if (action.Local == null && action.Remote != null)
            {
                return(SyncActionType.Download);
            }

            // To get here, both local and remote exist
            if (action.Local.ServerRev == action.Remote.ServerRev)
            {
                return(SyncActionType.None);
            }

            // Conflict
            switch (this.context.ConflictStrategy)
            {
            case ConflictStrategy.None:
                return(SyncActionType.None);

            case ConflictStrategy.LocalWin:
                return(SyncActionType.Upload);

            case ConflictStrategy.RemoteWin:
                return(SyncActionType.Download);

            case ConflictStrategy.LatestWin:
                return(action.Local.LastModified > action.Remote.LastModified ?
                       SyncActionType.Upload :
                       SyncActionType.Download);

            case ConflictStrategy.KeepBoth:
                return(SyncActionType.KeepBoth);
            }

            throw new InvalidOperationException("Unknown");
        }
Exemplo n.º 3
0
        public void ActionDelete(SyncAction action)
        {
            string sql = string.Format(@"delete from action where Type={0} and Path={1};",
                                       DbController.ToParameter(action.Type.ToString()),
                                       DbController.ToParameter(action.CommonPath));

            this.dbController.ExecuteNonQuery(sql);
        }
Exemplo n.º 4
0
        public void ActionWrite(SyncAction action)
        {
            this.ActionDelete(action);
            string sql = string.Format(@"insert into action values ({0}, {1});",
                                       DbController.ToParameter(action.Type.ToString()),
                                       DbController.ToParameter(action.CommonPath));

            this.dbController.ExecuteNonQuery(sql);
        }
Exemplo n.º 5
0
        private void Add(FileItem file)
        {
            string key = this.Key(file);

            if (this.IsExclusionMatch(key))
            {
                return;
            }

            lock (mutex)
            {
                SyncAction action = null;

                if (this.ActionDictionary.ContainsKey(key))
                {
                    action = this.ActionDictionary[key];
                }
                else
                {
                    action = new SyncAction
                    {
                        CommonPath = this.Context.ToCommonPath(file),
                        LocalPath  = this.Context.ToLocalPath(file),
                        RemotePath = this.Context.ToRemotePath(file)
                    };

                    this.ActionDictionary[action.Key] = action;
                }

                if (file.Source == FileService.Local)
                {
                    if (action.Local != null)
                    {
                        throw new InvalidOperationException(
                                  string.Format("Cannot add local file twice: {0}", action.LocalPath));
                    }

                    action.Local = file;
                    this.LocalFileCount++;
                }
                else
                {
                    if (action.Remote != null)
                    {
                        throw new InvalidOperationException(
                                  string.Format("Cannot add remote file twice: {0}", action.RemotePath));
                    }

                    action.Remote = file;
                    this.RemoteFileCount++;
                }
            }
        }
Exemplo n.º 6
0
        private void OnActionComplete(SyncAction action)
        {
            this.ActionsCompleteCount++;

            if (action.Type != SyncActionType.None)
            {
                if (action.Type == SyncActionType.Download)
                {
                    this.DownloadedBytes += action.Size;
                }
                else if (action.Type == SyncActionType.Upload)
                {
                    this.UploadedBytes += action.Size;
                }

                if (this.ActionComplete != null)
                {
                    this.ActionComplete(this, action);
                }
            }
        }
Exemplo n.º 7
0
        private async Task DoActionAsync(SyncAction action)
        {
            log.Debug("DoAction(" + action.Key + ")");

            if (action.IsUnconstructed)
            {
                await action.Reconstruct(this.Context);
            }

            this.OnActionStart(action);

            switch (action.Type)
            {
            case SyncActionType.DeleteLocal:
                await this.Context.LocalFilesystem.DeleteAsync(action.LocalPath);

                this.Context.LocalStorage.IndexDelete(action.LocalPath);
                break;

            case SyncActionType.Download:
                if (action.Remote != null)
                {
                    await this.Context.CloudService.DownloadAsync(action.Remote);

                    var item = this.Context.LocalFilesystem.ToFileItem(action.LocalPath);
                    if (item != null)
                    {
                        item.ServerRev = action.Remote.ServerRev;
                        this.Context.LocalStorage.IndexWrite(item);
                    }
                }
                else
                {
                    log.WarnFormat("Remote file ({0}) no longer exists. Ignoring.", action.RemotePath);
                }

                break;

            case SyncActionType.DeleteRemote:
                await this.Context.CloudService.DeleteAsync(action.RemotePath);

                this.Context.LocalStorage.IndexDelete(action.LocalPath);
                break;

            case SyncActionType.Upload:
                if (action.Local != null)
                {
                    await this.Context.CloudService.UploadAsync(action.Local);

                    this.Context.LocalStorage.IndexWrite(action.Local);
                }
                else
                {
                    log.WarnFormat("Local file ({0}) no longer exists. Ignoring.", action.LocalPath);
                    this.Context.LocalStorage.IndexDelete(action.LocalPath);
                }

                break;

            default:
                throw new NotImplementedException();
            }

            this.Context.LocalStorage.ActionDelete(action);
            this.OnActionComplete(action);
        }