private void CreateDestinationDirectory(CancellationToken cancellationToken)
        {
            if (this.dest.Type == TransferLocationType.LocalDirectory)
            {
                var localFileDestLocation = this.dest as DirectoryLocation;
                if (!LongPathDirectory.Exists(localFileDestLocation.DirectoryPath))
                {
                    LongPathDirectory.CreateDirectory(localFileDestLocation.DirectoryPath);
                }
            }
            else if (this.dest.Type == TransferLocationType.AzureFileDirectory)
            {
                AzureFileDirectoryLocation fileDirLocation = this.dest as AzureFileDirectoryLocation;

                var fileDirectory = fileDirLocation.FileDirectory;

                if (string.Equals(fileDirectory.SnapshotQualifiedUri.AbsolutePath, fileDirectory.Share.SnapshotQualifiedUri.AbsolutePath))
                {
                    return;
                }

                try
                {
                    CreateCloudFileDestinationDirectory(fileDirectory, cancellationToken);
                }
                catch (StorageException storageException)
                {
                    throw new TransferException(TransferErrorCode.FailToVadlidateDestination,
                                                string.Format(CultureInfo.CurrentCulture,
                                                              Resources.FailedToValidateDestinationException,
                                                              storageException.ToErrorDetail()),
                                                storageException);
                }
            }
        }
コード例 #2
0
        protected static TransferLocation GetSourceTransferLocation(TransferLocation dirLocation, TransferEntry entry)
        {
            switch (dirLocation.Type)
            {
            case TransferLocationType.AzureBlobDirectory:
                AzureBlobDirectoryLocation azureBlobDirLocation = dirLocation as AzureBlobDirectoryLocation;
                AzureBlobEntry             azureBlobEntry       = entry as AzureBlobEntry;

                AzureBlobLocation azureBlobLocation = new AzureBlobLocation(azureBlobEntry.Blob);
                azureBlobLocation.BlobRequestOptions = azureBlobDirLocation.BlobRequestOptions;

                return(azureBlobLocation);

            case TransferLocationType.AzureFileDirectory:
                AzureFileDirectoryLocation azureFileDirLocation = dirLocation as AzureFileDirectoryLocation;
                AzureFileEntry             azureFileEntry       = entry as AzureFileEntry;

                AzureFileLocation azureFileLocation = new AzureFileLocation(azureFileEntry.File);
                azureFileLocation.FileRequestOptions = azureFileDirLocation.FileRequestOptions;

                return(azureFileLocation);

            case TransferLocationType.LocalDirectory:
                FileEntry fileEntry = entry as FileEntry;

                return(new FileLocation(fileEntry.FullPath, fileEntry.RelativePath));

            default:
                throw new ArgumentException("TransferLocationType");
            }
        }
        protected static TransferLocation GetSourceDirectoryTransferLocation(TransferLocation dirLocation, string relativePath)
        {
            if (dirLocation.Type == TransferLocationType.AzureFileDirectory)
            {
                AzureFileDirectoryLocation azureFileDirLocation = dirLocation as AzureFileDirectoryLocation;
                var destDirectory = azureFileDirLocation.FileDirectory.GetDirectoryReference(relativePath);

                AzureFileDirectoryLocation azureFileLocation = new AzureFileDirectoryLocation(destDirectory);
                azureFileLocation.FileRequestOptions = azureFileDirLocation.FileRequestOptions;

                return(azureFileLocation);
            }
            else if (dirLocation.Type == TransferLocationType.LocalDirectory)
            {
                DirectoryLocation localDirLocation = dirLocation as DirectoryLocation;
                var destDirectory = Path.Combine(localDirLocation.DirectoryPath, relativePath);

                DirectoryLocation localSubDirLocation = new DirectoryLocation(destDirectory);

                return(localSubDirLocation);
            }
            else
            {
                // For now, HierarchyDirectoryTransfer should only be used when source is Azure File Directory.
                throw new ArgumentException("TransferLocationType");
            }
        }
コード例 #4
0
 private static void UpdateCredentials(TransferLocation dirLocation, TransferLocation subLocation)
 {
     if (dirLocation.Type == TransferLocationType.AzureBlobDirectory)
     {
         AzureBlobDirectoryLocation blobDirectoryLocation = dirLocation as AzureBlobDirectoryLocation;
         (subLocation as AzureBlobLocation).UpdateCredentials(blobDirectoryLocation.BlobDirectory.ServiceClient.Credentials);
     }
     else if (dirLocation.Type == TransferLocationType.AzureFileDirectory)
     {
         AzureFileDirectoryLocation fileDirectoryLocation = dirLocation as AzureFileDirectoryLocation;
         (subLocation as AzureFileLocation).UpdateCredentials(fileDirectoryLocation.FileDirectory.ServiceClient.Credentials);
     }
 }
        protected TransferLocation GetDestinationSubDirTransferLocation(TransferLocation dirLocation, TransferEntry entry)
        {
            string destRelativePath = this.NameResolver.ResolveName(entry);

            switch (dirLocation.Type)
            {
            case TransferLocationType.AzureBlobDirectory:
            {
                AzureBlobDirectoryLocation blobDirLocation = dirLocation as AzureBlobDirectoryLocation;
                BlobType destBlobType = this.BlobType;

                // TODO: should handle blob type here.
                AzureBlobDirectoryLocation retLocation = new AzureBlobDirectoryLocation(blobDirLocation.BlobDirectory.GetDirectoryReference(destRelativePath));
                retLocation.BlobRequestOptions = blobDirLocation.BlobRequestOptions;
                return(retLocation);
            }

            case TransferLocationType.AzureFileDirectory:
            {
                AzureFileDirectoryLocation fileDirLocation = dirLocation as AzureFileDirectoryLocation;
                CloudFileDirectory         azureDirectory  = fileDirLocation.FileDirectory.GetDirectoryReference(destRelativePath);

                AzureFileDirectoryLocation retLocation = new AzureFileDirectoryLocation(azureDirectory);
                retLocation.FileRequestOptions = fileDirLocation.FileRequestOptions;
                return(retLocation);
            }

            case TransferLocationType.LocalDirectory:
            {
                DirectoryLocation localDirLocation = dirLocation as DirectoryLocation;
                string            path             = LongPath.Combine(localDirLocation.DirectoryPath, destRelativePath);

                return(new DirectoryLocation(path));
            }

            default:
                throw new ArgumentException("TransferLocationType");
            }
        }
コード例 #6
0
        protected TransferLocation GetDestTransferLocationForEmptyDir(TransferLocation dirLocation, TransferEntry entry)
        {
            string destRelativePath = this.nameResolver.ResolveName(entry);

            AzureBlobEntry sourceBlobEntry = entry as AzureBlobEntry;

            switch (dirLocation.Type)
            {
            case TransferLocationType.AzureBlobDirectory:
            {
                return(null);
            }

            case TransferLocationType.AzureFileDirectory:
            {
                AzureFileDirectoryLocation fileDirLocation = dirLocation as AzureFileDirectoryLocation;
                CloudFileDirectory         destDirLocation = fileDirLocation.FileDirectory;

                if (!string.IsNullOrEmpty(destRelativePath))
                {
                    destDirLocation = destDirLocation.GetDirectoryReference(destRelativePath);
                }

                AzureFileDirectoryLocation retLocation = new AzureFileDirectoryLocation(destDirLocation);
                retLocation.FileRequestOptions = fileDirLocation.FileRequestOptions;
                return(retLocation);
            }

            case TransferLocationType.LocalDirectory:
            {
                return(null);
            }

            default:
                throw new ArgumentException("TransferLocationType");
            }
        }
コード例 #7
0
        protected TransferLocation GetDestinationTransferLocation(TransferLocation dirLocation, TransferEntry entry)
        {
            string destRelativePath = this.nameResolver.ResolveName(entry);

            AzureBlobEntry sourceBlobEntry = entry as AzureBlobEntry;

            switch (dirLocation.Type)
            {
            case TransferLocationType.AzureBlobDirectory:
            {
                AzureBlobDirectoryLocation blobDirLocation = dirLocation as AzureBlobDirectoryLocation;
                BlobType destBlobType = this.BlobType;

                if (sourceBlobEntry != null)
                {
                    // if source is Azure blob storage, source and destination blob share the same blob type
                    destBlobType = sourceBlobEntry.Blob.BlobType;
                }

                CloudBlob blob = null;
                switch (destBlobType)
                {
                case Blob.BlobType.BlockBlob:
                case Blob.BlobType.Unspecified:
                    blob = blobDirLocation.BlobDirectory.GetBlockBlobReference(destRelativePath);
                    break;

                case Blob.BlobType.PageBlob:
                    blob = blobDirLocation.BlobDirectory.GetPageBlobReference(destRelativePath);
                    break;

                case Blob.BlobType.AppendBlob:
                    blob = blobDirLocation.BlobDirectory.GetAppendBlobReference(destRelativePath);
                    break;
                }

                AzureBlobLocation retLocation = new AzureBlobLocation(blob);
                retLocation.BlobRequestOptions = blobDirLocation.BlobRequestOptions;
                return(retLocation);
            }

            case TransferLocationType.AzureFileDirectory:
            {
                AzureFileDirectoryLocation fileDirLocation = dirLocation as AzureFileDirectoryLocation;
                CloudFile file = fileDirLocation.FileDirectory.GetFileReference(destRelativePath);

                AzureFileLocation retLocation = new AzureFileLocation(file);
                retLocation.FileRequestOptions = fileDirLocation.FileRequestOptions;
                return(retLocation);
            }

            case TransferLocationType.LocalDirectory:
            {
                DirectoryLocation localDirLocation = dirLocation as DirectoryLocation;
                string            path             = Path.Combine(localDirLocation.DirectoryPath, destRelativePath);

                return(new FileLocation(path, destRelativePath));
            }

            default:
                throw new ArgumentException("TransferLocationType");
            }
        }
コード例 #8
0
        private void GetSourceProperites(out CloudFileNtfsAttributes?fileAttributes,
                                         out string portableSDDL,
                                         out DateTimeOffset?creationTime,
                                         out DateTimeOffset?lastWriteTime,
                                         out IDictionary <string, string> metadata,
                                         CancellationToken cancellationToken)
        {
            fileAttributes = null;
            portableSDDL   = null;
            creationTime   = null;
            lastWriteTime  = null;
            metadata       = null;

            if (this.source.Type == TransferLocationType.LocalDirectory)
            {
                if ((this.baseDirectoryTransfer.PreserveSMBAttributes) ||
                    (PreserveSMBPermissions.None != this.baseDirectoryTransfer.PreserveSMBPermissions))
                {
                    var    sourceLocalDirLocation = this.source as DirectoryLocation;
                    string directoryPath          = sourceLocalDirLocation.DirectoryPath;

                    if (this.baseDirectoryTransfer.PreserveSMBAttributes)
                    {
                        FileAttributes?localFileAttributes = null;
#if DOTNET5_4
                        LongPathFile.GetFileProperties(directoryPath, out creationTime, out lastWriteTime, out localFileAttributes, true);
#else
                        LongPathFile.GetFileProperties(directoryPath, out creationTime, out lastWriteTime, out localFileAttributes);
#endif
                        fileAttributes = Utils.LocalAttributesToAzureFileNtfsAttributes(localFileAttributes.Value);
                    }

                    portableSDDL = FileSecurityOperations.GetFilePortableSDDL(directoryPath, this.baseDirectoryTransfer.PreserveSMBPermissions);
                }
            }
            else if (this.source.Type == TransferLocationType.AzureFileDirectory)
            {
                if (this.baseDirectoryTransfer.PreserveSMBAttributes ||
                    (PreserveSMBPermissions.None != this.baseDirectoryTransfer.PreserveSMBPermissions) ||
                    this.dest.Type == TransferLocationType.AzureFileDirectory)
                {
                    AzureFileDirectoryLocation sourceFileDirLocation = this.source as AzureFileDirectoryLocation;

                    var sourceFileDirectory = sourceFileDirLocation.FileDirectory;
                    sourceFileDirectory.FetchAttributesAsync(
                        null,
                        Utils.GenerateFileRequestOptions(sourceFileDirLocation.FileRequestOptions),
                        Utils.GenerateOperationContext(this.baseDirectoryTransfer.Context),
                        cancellationToken).GetAwaiter().GetResult();

                    if (this.baseDirectoryTransfer.PreserveSMBAttributes)
                    {
                        fileAttributes = sourceFileDirectory.Properties.NtfsAttributes;
                        creationTime   = sourceFileDirectory.Properties.CreationTime;
                        lastWriteTime  = sourceFileDirectory.Properties.LastWriteTime;
                    }

                    metadata = sourceFileDirectory.Metadata;

                    if (PreserveSMBPermissions.None != this.baseDirectoryTransfer.PreserveSMBPermissions)
                    {
                        if (!string.IsNullOrEmpty(sourceFileDirectory.FilePermission))
                        {
                            portableSDDL = sourceFileDirectory.FilePermission;
                        }
                        else if (!string.IsNullOrEmpty(sourceFileDirectory.Properties.FilePermissionKey))
                        {
                            this.baseDirectoryTransfer.SDDLCache.TryGetValue(sourceFileDirectory.Properties.FilePermissionKey, out portableSDDL);

                            if (null == portableSDDL)
                            {
                                portableSDDL = sourceFileDirectory.Share.GetFilePermissionAsync(
                                    sourceFileDirectory.Properties.FilePermissionKey,
                                    Utils.GenerateFileRequestOptions(sourceFileDirLocation.FileRequestOptions),
                                    Utils.GenerateOperationContext(this.baseDirectoryTransfer.Context),
                                    cancellationToken).GetAwaiter().GetResult();
                                this.baseDirectoryTransfer.SDDLCache.TryAddValue(sourceFileDirectory.Properties.FilePermissionKey,
                                                                                 portableSDDL);
                            }
                        }
                        else
                        {
                            portableSDDL = null;
                        }
                    }
                }
            }
        }
コード例 #9
0
        private void CreateDestinationDirectory(CancellationToken cancellationToken)
        {
            if (this.dest.Type == TransferLocationType.AzureBlobDirectory)
            {
                // No physical destination directory needed.
                return;
            }

            if ((this.dest.Type == TransferLocationType.AzureFileDirectory) &&
                (null == (this.dest as AzureFileDirectoryLocation).FileDirectory.Parent))
            {
                //Root Azure File directory, no need to create.
                return;
            }

            CloudFileNtfsAttributes?fileAttributes = null;
            string         portableSDDL            = null;
            DateTimeOffset?creationTime            = null;
            DateTimeOffset?lastWriteTime           = null;
            IDictionary <string, string> metadata  = null;

            this.GetSourceProperites(out fileAttributes, out portableSDDL, out creationTime, out lastWriteTime, out metadata, cancellationToken);

            if (this.dest.Type == TransferLocationType.LocalDirectory)
            {
                var localFileDestLocation = this.dest as DirectoryLocation;
                if (!LongPathDirectory.Exists(localFileDestLocation.DirectoryPath))
                {
                    LongPathDirectory.CreateDirectory(localFileDestLocation.DirectoryPath);
                }

                if (fileAttributes.HasValue)
                {
                    LongPathFile.SetFileTime(localFileDestLocation.DirectoryPath, creationTime.Value, lastWriteTime.Value, true);
                    LongPathFile.SetAttributes(localFileDestLocation.DirectoryPath, Utils.AzureFileNtfsAttributesToLocalAttributes(fileAttributes.Value));
                }

                if (!string.IsNullOrEmpty(portableSDDL))
                {
                    FileSecurityOperations.SetFileSecurity(localFileDestLocation.DirectoryPath, portableSDDL, this.baseDirectoryTransfer.PreserveSMBPermissions);
                }
            }
            else if (this.dest.Type == TransferLocationType.AzureFileDirectory)
            {
                AzureFileDirectoryLocation fileDirLocation = this.dest as AzureFileDirectoryLocation;

                var fileDirectory = fileDirLocation.FileDirectory;

                if (string.Equals(fileDirectory.SnapshotQualifiedUri.AbsolutePath, fileDirectory.Share.SnapshotQualifiedUri.AbsolutePath))
                {
                    return;
                }

                try
                {
                    CreateCloudFileDestinationDirectory(fileDirectory,
                                                        fileAttributes,
                                                        creationTime,
                                                        lastWriteTime,
                                                        metadata,
                                                        portableSDDL,
                                                        cancellationToken);
                }
                catch (StorageException storageException)
                {
                    if ((null != storageException.InnerException) &&
                        (storageException.InnerException is OperationCanceledException))
                    {
                        throw storageException.InnerException;
                    }
                    else
                    {
                        throw new TransferException(TransferErrorCode.FailToVadlidateDestination,
                                                    string.Format(CultureInfo.CurrentCulture,
                                                                  Resources.FailedToValidateDestinationException,
                                                                  storageException.ToErrorDetail()),
                                                    storageException);
                    }
                }
            }
        }
        private void CreateDestinationDirectory(CancellationToken cancellationToken)
        {
            if (this.dest.Type == TransferLocationType.AzureBlobDirectory)
            {
                // No physical destination directory needed.
                return;
            }

            if ((this.dest.Type == TransferLocationType.AzureFileDirectory) &&
                (null == (this.dest as AzureFileDirectoryLocation).FileDirectory.Parent))
            {
                //Root Azure File directory, no need to create.
                return;
            }

            CloudFileNtfsAttributes?     fileAttributes = null;
            DateTimeOffset?              creationTime   = null;
            DateTimeOffset?              lastWriteTime  = null;
            IDictionary <string, string> metadata       = null;

            if (this.source.Type == TransferLocationType.LocalDirectory)
            {
                if (this.baseDirectoryTransfer.PreserveSMBAttributes)
                {
                    var sourceLocalDirLocation = this.source as DirectoryLocation;

                    FileAttributes?localFileAttributes = null;

                    string longDirectoryPath = sourceLocalDirLocation.DirectoryPath;

                    if (Interop.CrossPlatformHelpers.IsWindows)
                    {
                        longDirectoryPath = LongPath.ToUncPath(longDirectoryPath);
                    }

#if DOTNET5_4
                    LongPathFile.GetFileProperties(longDirectoryPath, out creationTime, out lastWriteTime, out localFileAttributes, true);
#else
                    LongPathFile.GetFileProperties(longDirectoryPath, out creationTime, out lastWriteTime, out localFileAttributes);
#endif
                    fileAttributes = Utils.LocalAttributesToAzureFileNtfsAttributes(localFileAttributes.Value);
                }
            }
            else if (this.source.Type == TransferLocationType.AzureFileDirectory)
            {
                if (this.baseDirectoryTransfer.PreserveSMBAttributes || this.dest.Type == TransferLocationType.AzureFileDirectory)
                {
                    AzureFileDirectoryLocation sourceFileDirLocation = this.source as AzureFileDirectoryLocation;

                    var sourceFileDirectory = sourceFileDirLocation.FileDirectory;
                    sourceFileDirectory.FetchAttributesAsync(
                        null,
                        Utils.GenerateFileRequestOptions(sourceFileDirLocation.FileRequestOptions),
                        Utils.GenerateOperationContext(this.baseDirectoryTransfer.Context),
                        cancellationToken).GetAwaiter().GetResult();

                    if (this.baseDirectoryTransfer.PreserveSMBAttributes)
                    {
                        fileAttributes = sourceFileDirectory.Properties.NtfsAttributes;
                        creationTime   = sourceFileDirectory.Properties.CreationTime;
                        lastWriteTime  = sourceFileDirectory.Properties.LastWriteTime;
                    }

                    metadata = sourceFileDirectory.Metadata;
                }
            }

            if (this.dest.Type == TransferLocationType.LocalDirectory)
            {
                var localFileDestLocation = this.dest as DirectoryLocation;
                if (!LongPathDirectory.Exists(localFileDestLocation.DirectoryPath))
                {
                    LongPathDirectory.CreateDirectory(localFileDestLocation.DirectoryPath);
                }

                if (fileAttributes.HasValue)
                {
                    LongPathFile.SetFileTime(localFileDestLocation.DirectoryPath, creationTime.Value, lastWriteTime.Value, true);
                    LongPathFile.SetAttributes(localFileDestLocation.DirectoryPath, Utils.AzureFileNtfsAttributesToLocalAttributes(fileAttributes.Value));
                }
            }
            else if (this.dest.Type == TransferLocationType.AzureFileDirectory)
            {
                AzureFileDirectoryLocation fileDirLocation = this.dest as AzureFileDirectoryLocation;

                var fileDirectory = fileDirLocation.FileDirectory;

                if (string.Equals(fileDirectory.SnapshotQualifiedUri.AbsolutePath, fileDirectory.Share.SnapshotQualifiedUri.AbsolutePath))
                {
                    return;
                }

                try
                {
                    CreateCloudFileDestinationDirectory(fileDirectory,
                                                        fileAttributes,
                                                        creationTime,
                                                        lastWriteTime,
                                                        metadata,
                                                        cancellationToken);
                }
                catch (StorageException storageException)
                {
                    if ((null != storageException.InnerException) &&
                        (storageException.InnerException is OperationCanceledException))
                    {
                        throw storageException.InnerException;
                    }
                    else
                    {
                        throw new TransferException(TransferErrorCode.FailToVadlidateDestination,
                                                    string.Format(CultureInfo.CurrentCulture,
                                                                  Resources.FailedToValidateDestinationException,
                                                                  storageException.ToErrorDetail()),
                                                    storageException);
                    }
                }
            }
        }