Пример #1
0
        public HttpResponseMessage Patch(string name, string rename)
        {
            name   = RavenFileNameHelper.RavenPath(name);
            rename = RavenFileNameHelper.RavenPath(rename);

            try
            {
                ConcurrencyAwareExecutor.Execute(() =>
                                                 Storage.Batch(accessor =>
                {
                    AssertFileIsNotBeingSynced(name, accessor, true);

                    var metadata = accessor.GetFile(name, 0, 0).Metadata;
                    if (metadata.Keys.Contains(SynchronizationConstants.RavenDeleteMarker))
                    {
                        throw new FileNotFoundException();
                    }

                    var existingHeader = accessor.ReadFile(rename);
                    if (existingHeader != null && !existingHeader.Metadata.ContainsKey(SynchronizationConstants.RavenDeleteMarker))
                    {
                        throw new HttpResponseException(
                            Request.CreateResponse(HttpStatusCode.Forbidden,
                                                   new InvalidOperationException("Cannot rename because file " + rename + " already exists")));
                    }

                    Historian.UpdateLastModified(metadata);

                    var operation = new RenameFileOperation
                    {
                        FileSystem             = FileSystem.Name,
                        Name                   = name,
                        Rename                 = rename,
                        MetadataAfterOperation = metadata
                    };

                    accessor.SetConfig(RavenFileNameHelper.RenameOperationConfigNameForFile(name), JsonExtensions.ToJObject(operation));
                    accessor.PulseTransaction();     // commit rename operation config

                    StorageOperationsTask.RenameFile(operation);
                }), ConcurrencyResponseException);
            }
            catch (FileNotFoundException)
            {
                log.Debug("Cannot rename a file '{0}' to '{1}' because a file was not found", name, rename);
                return(GetEmptyMessage(HttpStatusCode.NotFound));
            }

            log.Debug("File '{0}' was renamed to '{1}'", name, rename);

            StartSynchronizeDestinationsInBackground();

            return(GetMessageWithString("", HttpStatusCode.NoContent));
        }
Пример #2
0
        public HttpResponseMessage Rename(string fileSystemName, string fileName, string rename)
        {
            bool isConflictResolved = false;

            var canonicalFilename = FileHeader.Canonize(fileName);
            var canonicalRename   = FileHeader.Canonize(rename);

            var sourceServerInfo = ReadInnerHeaders.Value <ServerInfo>(SyncingMultipartConstants.SourceServerInfo);
            var sourceFileETag   = Guid.Parse(GetHeader(Constants.MetadataEtagField).Trim('\"'));
            var sourceMetadata   = GetFilteredMetadataFromHeaders(ReadInnerHeaders);

            Log.Debug("Starting to rename a file '{0}' to '{1}' with ETag {2} from {3} because of synchronization", fileName,
                      rename, sourceFileETag, sourceServerInfo);

            var report = new SynchronizationReport(canonicalFilename, sourceFileETag, SynchronizationType.Rename);

            try
            {
                Storage.Batch(accessor =>
                {
                    AssertFileIsNotBeingSynced(canonicalFilename, accessor);
                    FileLockManager.LockByCreatingSyncConfiguration(canonicalFilename, sourceServerInfo, accessor);
                });

                SynchronizationTask.IncomingSynchronizationStarted(canonicalFilename, sourceServerInfo, sourceFileETag, SynchronizationType.Rename);

                PublishSynchronizationNotification(fileSystemName, canonicalFilename, sourceServerInfo, report.Type, SynchronizationAction.Start);

                Storage.Batch(accessor => StartupProceed(canonicalFilename, accessor));

                var localMetadata = GetLocalMetadata(canonicalFilename);

                AssertConflictDetection(canonicalFilename, localMetadata, sourceMetadata, sourceServerInfo, out isConflictResolved);

                if (isConflictResolved)
                {
                    ConflictArtifactManager.Delete(canonicalFilename);
                }

                StorageOperationsTask.RenameFile(new RenameFileOperation
                {
                    FileSystem             = FileSystem.Name,
                    Name                   = canonicalFilename,
                    Rename                 = canonicalRename,
                    MetadataAfterOperation = sourceMetadata.WithETag(sourceFileETag).DropRenameMarkers()
                });
            }
            catch (Exception ex)
            {
                if (ShouldAddExceptionToReport(ex))
                {
                    report.Exception = ex;
                    Log.WarnException(string.Format("Error was occurred during renaming synchronization of file '{0}' from {1}", fileName, sourceServerInfo), ex);
                }
            }
            finally
            {
                FinishSynchronization(canonicalFilename, report, sourceServerInfo, sourceFileETag);
            }

            PublishSynchronizationNotification(fileSystemName, canonicalFilename, sourceServerInfo, report.Type, SynchronizationAction.Finish);

            if (isConflictResolved)
            {
                Publisher.Publish(new ConflictNotification
                {
                    FileName = fileName,
                    Status   = ConflictStatus.Resolved
                });
            }

            if (report.Exception == null)
            {
                Log.Debug("File '{0}' was renamed to '{1}' during synchronization from {2}", fileName, rename, sourceServerInfo);
            }

            return(GetMessageWithObject(report));
        }
        public HttpResponseMessage Rename(string fileName, string rename)
        {
            var sourceServerInfo = InnerHeaders.Value <ServerInfo>(SyncingMultipartConstants.SourceServerInfo);
            var sourceFileETag   = Guid.Parse(InnerHeaders.GetValues("ETag").First().Trim('\"'));
            var sourceMetadata   = GetFilteredMetadataFromHeaders(InnerHeaders);

            Log.Debug("Starting to rename a file '{0}' to '{1}' with ETag {2} from {3} because of synchronization", fileName,
                      rename, sourceFileETag, sourceServerInfo);

            var report = new SynchronizationReport(fileName, sourceFileETag, SynchronizationType.Rename);

            try
            {
                Storage.Batch(accessor =>
                {
                    AssertFileIsNotBeingSynced(fileName, accessor);
                    FileLockManager.LockByCreatingSyncConfiguration(fileName, sourceServerInfo, accessor);
                });

                PublishSynchronizationNotification(fileName, sourceServerInfo, report.Type, SynchronizationAction.Start);

                Storage.Batch(accessor => StartupProceed(fileName, accessor));

                var localMetadata = GetLocalMetadata(fileName);

                bool isConflictResolved;

                AssertConflictDetection(fileName, localMetadata, sourceMetadata, sourceServerInfo, out isConflictResolved);

                if (isConflictResolved)
                {
                    ConflictArtifactManager.Delete(fileName);
                    Publisher.Publish(new ConflictResolvedNotification {
                        FileName = fileName
                    });
                }

                StorageOperationsTask.RenameFile(new RenameFileOperation
                {
                    Name   = fileName,
                    Rename = rename,
                    MetadataAfterOperation = sourceMetadata.WithETag(sourceFileETag).DropRenameMarkers()
                });
            }
            catch (Exception ex)
            {
                report.Exception = ex;
                Log.WarnException(string.Format("Error was occurred during renaming synchronization of file '{0}' from {1}", fileName, sourceServerInfo), ex);
            }
            finally
            {
                FinishSynchronization(fileName, report, sourceServerInfo, sourceFileETag);

                PublishSynchronizationNotification(fileName, sourceServerInfo, report.Type, SynchronizationAction.Finish);
            }

            if (report.Exception == null)
            {
                Log.Debug("File '{0}' was renamed to '{1}' during synchronization from {2}", fileName, rename, sourceServerInfo);
            }

            return(this.GetMessageWithObject(report, HttpStatusCode.OK));
        }