public override void ExecuteCmdlet() { if (AsJob.IsPresent) { DoBeginProcessing(); } string filePath = this.Source; FileInfo localFile = new FileInfo(filePath); if (!localFile.Exists) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.SourceFileNotFound, this.Source)); } // if FIPS policy is enabled, must use native MD5 if (fipsEnabled) { CloudStorageAccount.UseV1MD5 = false; } bool isDirectory; string[] path = NamingUtil.ValidatePath(this.Path, out isDirectory); var cloudFileToBeUploaded = BuildCloudFileInstanceFromPathAsync(localFile.Name, path, isDirectory).ConfigureAwait(false).GetAwaiter().GetResult(); if (ShouldProcess(cloudFileToBeUploaded.Name, "Set file content")) { // Step 2: Build the CloudFile object which pointed to the // destination cloud file. this.RunTask(async taskId => { var progressRecord = new ProgressRecord( this.OutputStream.GetProgressId(taskId), string.Format(CultureInfo.CurrentCulture, Resources.SendAzureFileActivity, localFile.Name, cloudFileToBeUploaded.GetFullPath(), cloudFileToBeUploaded.Share.Name), Resources.PrepareUploadingFile); await DataMovementTransferHelper.DoTransfer(() => this.TransferManager.UploadAsync( localFile.FullName, cloudFileToBeUploaded, new UploadOptions { PreserveSMBAttributes = context is null ? false : context.PreserveSMBAttribute.IsPresent }, this.GetTransferContext(progressRecord, localFile.Length), this.CmdletCancellationToken), progressRecord, this.OutputStream).ConfigureAwait(false); if (this.PassThru) { this.OutputStream.WriteObject(taskId, cloudFileToBeUploaded); } });
public override void ExecuteCmdlet() { if (String.IsNullOrEmpty(ShareName)) { return; } CloudFileShare fileShare = null; CloudFile file = null; if (null != this.File) { file = this.File; fileShare = this.File.Share; } else { string[] path = NamingUtil.ValidatePath(this.Path, true); fileShare = Channel.GetShareReference(this.ShareName); file = fileShare.GetRootDirectoryReference().GetFileReferenceByPath(path); } SharedAccessFilePolicy accessPolicy = new SharedAccessFilePolicy(); bool shouldSetExpiryTime = SasTokenHelper.ValidateShareAccessPolicy( Channel, fileShare.Name, accessPolicyIdentifier, !string.IsNullOrEmpty(this.Permission), this.StartTime.HasValue, this.ExpiryTime.HasValue); SetupAccessPolicy(accessPolicy, shouldSetExpiryTime); string sasToken = file.GetSharedAccessSignature(accessPolicy, null, accessPolicyIdentifier, Protocol, Util.SetupIPAddressOrRangeForSAS(IPAddressOrRange)); if (FullUri) { string fullUri = SasTokenHelper.GetFullUriWithSASToken(file.SnapshotQualifiedUri.AbsoluteUri.ToString(), sasToken); WriteObject(fullUri); } else { WriteObject(sasToken); } }
public override void ExecuteCmdlet() { // Step 1: Validate source file. FileInfo localFile = new FileInfo(this.GetUnresolvedProviderPathFromPSPath(this.Source)); if (!localFile.Exists) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.SourceFileNotFound, this.Source)); } bool isDirectory; string[] path = NamingUtil.ValidatePath(this.Path, out isDirectory); var cloudFileToBeUploaded = BuildCloudFileInstanceFromPathAsync(localFile.Name, path, isDirectory).ConfigureAwait(false).GetAwaiter().GetResult(); if (ShouldProcess(cloudFileToBeUploaded.Name, "Set file content")) { // Step 2: Build the CloudFile object which pointed to the // destination cloud file. this.RunTask(async taskId => { var progressRecord = new ProgressRecord( this.OutputStream.GetProgressId(taskId), string.Format(CultureInfo.CurrentCulture, Resources.SendAzureFileActivity, localFile.Name, cloudFileToBeUploaded.GetFullPath(), cloudFileToBeUploaded.Share.Name), Resources.PrepareUploadingFile); await DataMovementTransferHelper.DoTransfer(() => this.TransferManager.UploadAsync( localFile.FullName, cloudFileToBeUploaded, null, this.GetTransferContext(progressRecord, localFile.Length), this.CmdletCancellationToken), progressRecord, this.OutputStream); if (this.PassThru) { this.OutputStream.WriteObject(taskId, cloudFileToBeUploaded); } }); } }
public override void ExecuteCmdlet() { CloudFile file = null; if (null != this.File) { file = this.File; } else { string[] path = NamingUtil.ValidatePath(this.FilePath, true); file = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference().GetFileReferenceByPath(path); } long taskId = InternalTotalTaskCount; jobList.Enqueue(new Tuple <long, CloudFile>(taskId, file)); InternalTotalTaskCount++; }
private CloudFile GetDestFile() { var destChannal = this.GetDestinationChannel(); if (null != this.DestFile) { return(this.DestFile); } else { string destPath = this.DestFilePath; NamingUtil.ValidateShareName(this.DestShareName, false); CloudFileShare share = destChannal.GetShareReference(this.DestShareName); string[] path = NamingUtil.ValidatePath(destPath, true); return(share.GetRootDirectoryReference().GetFileReferenceByPath(path)); } }
public override void ExecuteCmdlet() { IStorageFileManagement localChannel = Channel; CloudFile file = null; if (null != this.File) { file = this.File; } else { string[] path = NamingUtil.ValidatePath(this.FilePath); file = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference().GetFileReferenceByPath(path); } Func <long, Task> taskGenerator = (taskId) => this.StopCopyFile(taskId, localChannel, file, CopyId); RunTask(taskGenerator); }
public override void ExecuteCmdlet() { string[] path = NamingUtil.ValidatePath(this.Path, true); CloudFile fileToBeRemoved; switch (this.ParameterSetName) { case Constants.FileParameterSetName: fileToBeRemoved = this.File; break; case Constants.ShareNameParameterSetName: var share = this.BuildFileShareObjectFromName(this.ShareName); fileToBeRemoved = share.GetRootDirectoryReference().GetFileReferenceByPath(path); break; case Constants.ShareParameterSetName: fileToBeRemoved = this.Share.GetRootDirectoryReference().GetFileReferenceByPath(path); break; case Constants.DirectoryParameterSetName: fileToBeRemoved = this.Directory.GetFileReferenceByPath(path); break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); } this.RunTask(async taskId => { if (this.ShouldProcess(fileToBeRemoved.GetFullPath())) { await this.Channel.DeleteFileAsync(fileToBeRemoved, this.AccessCondition, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken); } if (this.PassThru) { this.OutputStream.WriteObject(taskId, fileToBeRemoved); } }); }
private void StartCopyFromFile() { CloudFile sourceFile = null; string filePath = null; if (null != this.SrcFile) { sourceFile = this.SrcFile; filePath = this.SrcFile.GetFullPath(); } else { CloudFileDirectory dir = null; if (null != this.SrcShare) { dir = this.SrcShare.GetRootDirectoryReference(); } else { NamingUtil.ValidateShareName(this.SrcShareName, false); dir = this.BuildFileShareObjectFromName(this.SrcShareName).GetRootDirectoryReference(); } string[] path = NamingUtil.ValidatePath(this.SrcFilePath, true); sourceFile = dir.GetFileReferenceByPath(path); filePath = this.SrcFilePath; } CloudFile destFile = this.GetDestFile(); Func <long, Task> taskGenerator = (taskId) => StartAsyncCopy( taskId, destFile, () => this.ConfirmOverwrite(sourceFile.SnapshotQualifiedUri.ToString(), destFile.SnapshotQualifiedUri.ToString()), () => destFile.StartCopyAsync(sourceFile.GenerateCopySourceFile())); this.RunTask(taskGenerator); }
public override void ExecuteCmdlet() { string[] path = NamingUtil.ValidatePath(this.Path); CloudFileDirectory baseDirectory; switch (this.ParameterSetName) { case Constants.DirectoryParameterSetName: baseDirectory = this.Directory; break; case Constants.ShareNameParameterSetName: var share = this.BuildFileShareObjectFromName(this.ShareName); baseDirectory = share.GetRootDirectoryReference(); break; case Constants.ShareParameterSetName: baseDirectory = this.Share.GetRootDirectoryReference(); break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); } var directoryToBeRemoved = baseDirectory.GetDirectoryReferenceByPath(path); this.RunTask(async taskId => { if (this.ShouldProcess(directoryToBeRemoved.GetFullPath(), "Remove directory")) { await this.Channel.DeleteDirectoryAsync(directoryToBeRemoved, null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false); } if (this.PassThru) { this.OutputStream.WriteObject(taskId, directoryToBeRemoved); } }); }
public override void ExecuteCmdlet() { // Step 1: Validate source file. FileInfo localFile = new FileInfo(this.GetUnresolvedProviderPathFromPSPath(this.Source)); if (!localFile.Exists) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.SourceFileNotFound, this.Source)); } // Step 2: Build the CloudFile object which pointed to the // destination cloud file. this.RunTask(async taskId => { bool isDirectory; string[] path = NamingUtil.ValidatePath(this.Path, out isDirectory); var cloudFileToBeUploaded = await this.BuildCloudFileInstanceFromPathAsync(localFile.Name, path, isDirectory); var uploadJob = new FileUploadJob() { SourcePath = localFile.FullName, DestFile = cloudFileToBeUploaded, }; var progressRecord = new ProgressRecord( this.OutputStream.GetProgressId(taskId), string.Format(CultureInfo.CurrentCulture, Resources.SendAzureFileActivity, localFile.Name, cloudFileToBeUploaded.GetFullPath(), cloudFileToBeUploaded.Share.Name), Resources.PrepareUploadingFile); await this.RunTransferJob(uploadJob, progressRecord); if (this.PassThru) { this.OutputStream.WriteObject(taskId, cloudFileToBeUploaded); } }); }
public override void ExecuteCmdlet() { if (AsJob.IsPresent) { DoBeginProcessing(); } string filePath = this.Source; FileInfo localFile = new FileInfo(filePath); if (!localFile.Exists) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.SourceFileNotFound, this.Source)); } long fileSize = localFile.Length; // if FIPS policy is enabled, must use native MD5 for DMlib. if (fipsEnabled) { if (fileSize < sizeTB) { CloudStorageAccount.UseV1MD5 = false; } else // use Track2 SDK { WriteWarning("The uploaded file won't have Content MD5 hash, since caculate MD5 hash fail, most possiblly caused by FIPS is enabled on this machine."); } } bool isDirectory; string[] path = NamingUtil.ValidatePath(this.Path, out isDirectory); var cloudFileToBeUploaded = BuildCloudFileInstanceFromPathAsync(localFile.Name, path, isDirectory).ConfigureAwait(false).GetAwaiter().GetResult(); if (ShouldProcess(cloudFileToBeUploaded.Name, "Set file content")) { // Step 2: Build the CloudFile object which pointed to the // destination cloud file. this.RunTask(async taskId => { var progressRecord = new ProgressRecord( this.OutputStream.GetProgressId(taskId), string.Format(CultureInfo.CurrentCulture, Resources.SendAzureFileActivity, localFile.Name, cloudFileToBeUploaded.GetFullPath(), cloudFileToBeUploaded.Share.Name), Resources.PrepareUploadingFile); if (fileSize <= sizeTB) { await DataMovementTransferHelper.DoTransfer(() => this.TransferManager.UploadAsync( localFile.FullName, cloudFileToBeUploaded, new UploadOptions { PreserveSMBAttributes = context is null ? false : context.PreserveSMBAttribute.IsPresent }, this.GetTransferContext(progressRecord, localFile.Length), this.CmdletCancellationToken), progressRecord, this.OutputStream).ConfigureAwait(false); } else // use Track2 SDK { //Create File ShareFileClient fileClient = AzureStorageFile.GetTrack2FileClient(cloudFileToBeUploaded, Channel.StorageContext); // confirm overwrite if file exist if (!this.Force.IsPresent && fileClient.Exists(this.CmdletCancellationToken) && !await this.OutputStream.ConfirmAsync(string.Format(CultureInfo.CurrentCulture, Resources.OverwriteConfirmation, Util.ConvertToString(cloudFileToBeUploaded)))) { return; } await fileClient.CreateAsync(fileSize, cancellationToken: this.CmdletCancellationToken).ConfigureAwait(false); //Prepare progress Handler IProgress <long> progressHandler = new Progress <long>((finishedBytes) => { if (progressRecord != null) { // Size of the source file might be 0, when it is, directly treat the progress as 100 percent. progressRecord.PercentComplete = 0 == fileSize ? 100 : (int)(finishedBytes * 100 / fileSize); progressRecord.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.FileTransmitStatus, progressRecord.PercentComplete); this.OutputStream.WriteProgress(progressRecord); } }); long blockSize = 4 * 1024 * 1024; int maxWorkers = 4; List <Task> runningTasks = new List <Task>(); IncrementalHash hash = null; if (!fipsEnabled) { hash = IncrementalHash.CreateHash(HashAlgorithmName.MD5); } using (FileStream stream = File.OpenRead(localFile.FullName)) { byte[] buffer = null; long lastBlockSize = 0; for (long offset = 0; offset < fileSize; offset += blockSize) { long currentBlockSize = offset + blockSize < fileSize ? blockSize : fileSize - offset; // Only need to create new buffer when chunk size change if (currentBlockSize != lastBlockSize) { buffer = new byte[currentBlockSize]; lastBlockSize = currentBlockSize; } await stream.ReadAsync(buffer: buffer, offset: 0, count: (int)currentBlockSize); if (!fipsEnabled && hash != null) { hash.AppendData(buffer); } Task task = UploadFileRangAsync(fileClient, new HttpRange(offset, currentBlockSize), new MemoryStream(buffer), progressHandler); runningTasks.Add(task); // Check if any of upload range tasks are still busy if (runningTasks.Count >= maxWorkers) { await Task.WhenAny(runningTasks).ConfigureAwait(false); // Clear any completed blocks from the task list for (int i = 0; i < runningTasks.Count; i++) { Task runningTask = runningTasks[i]; if (!runningTask.IsCompleted) { continue; } await runningTask.ConfigureAwait(false); runningTasks.RemoveAt(i); i--; } } } // Wait for all upload range tasks finished await Task.WhenAll(runningTasks).ConfigureAwait(false); } // Need set file properties if ((!fipsEnabled && hash != null) || (context != null && context.PreserveSMBAttribute.IsPresent)) { ShareFileHttpHeaders header = null; if (!fipsEnabled && hash != null) { header = new ShareFileHttpHeaders(); header.ContentHash = hash.GetHashAndReset(); } FileSmbProperties smbProperties = null; if (context != null && context.PreserveSMBAttribute.IsPresent) { FileInfo sourceFileInfo = new FileInfo(localFile.FullName); smbProperties = new FileSmbProperties(); smbProperties.FileCreatedOn = sourceFileInfo.CreationTimeUtc; smbProperties.FileLastWrittenOn = sourceFileInfo.LastWriteTimeUtc; smbProperties.FileAttributes = Util.LocalAttributesToAzureFileNtfsAttributes(File.GetAttributes(localFile.FullName)); } // set file header and attributes to the file fileClient.SetHttpHeaders(httpHeaders: header, smbProperties: smbProperties); } if (this.PassThru) { // fetch latest file properties for output cloudFileToBeUploaded.FetchAttributes(); } } if (this.PassThru) { WriteCloudFileObject(taskId, this.Channel, cloudFileToBeUploaded); } });
public override void ExecuteCmdlet() { if (this.ShouldProcess(string.Format("Close File Handles for File or FileDirectory on Path: {0}", this.Path != null? this.Path : (this.FileHandle != null? this.FileHandle.Path: null)), "This operation will force the provided file handle(s) closed, which may cause data loss or corruption for active applications/users.", null)) { CloudFileDirectory baseDirectory = null; switch (this.ParameterSetName) { case DirectoryCloseAllParameterSetName: baseDirectory = this.Directory; break; case ShareNameCloseSingleParameterSetName: case ShareNameCloseAllParameterSetName: baseDirectory = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference(); break; case ShareCloseSingleParameterSetName: case ShareCloseAllParameterSetName: baseDirectory = this.Share.GetRootDirectoryReference(); break; case FileCloseAllParameterSetName: // Don't need to set baseDirectory when input is a CloudFile break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); } if (ParameterSetName == ShareNameCloseSingleParameterSetName || ParameterSetName == ShareCloseSingleParameterSetName) { this.Path = FileHandle.Path; } // When not input path/File, the list handle target must be a Dir bool foundAFolder = true; CloudFileDirectory targetDir = baseDirectory; CloudFile targetFile = null; if (this.File != null) { targetFile = this.File; foundAFolder = false; } else { if (!string.IsNullOrEmpty(this.Path)) { string[] subfolders = NamingUtil.ValidatePath(this.Path); targetDir = baseDirectory.GetDirectoryReferenceByPath(subfolders); // Don't need check the path target to File or FileDir since: // 1. check File/FileDir exist will fail on File/FileDir with DeletePending status // 2. The File handle request send with CloudFileDirectory and CloudFile are same with same path, so need to differ it. } } // Recursive only take effect on File Dir if (!foundAFolder && Recursive.IsPresent) { WriteVerbose("The target object of the 'Path' is an Azure File, the parameter '-Recursive' won't take effect."); } //Close handle CloseFileHandleResultSegment closeResult = null; FileContinuationToken continuationToken = null; int numHandlesClosed = 0; do { if (foundAFolder) { if (FileHandle != null) { // close single handle on fileDir if (this.FileHandle.HandleId == null) { throw new System.ArgumentException(string.Format("The HandleId of the FileHandle on path {0} should not be null.", this.FileHandle.Path), "FileHandle"); } closeResult = targetDir.CloseHandleSegmented(this.FileHandle.HandleId.ToString(), continuationToken, Recursive, null, this.RequestOptions, this.OperationContext); } else { // close all handle on fileDir closeResult = targetDir.CloseAllHandlesSegmented(continuationToken, Recursive, null, this.RequestOptions, this.OperationContext); } } else { if (FileHandle != null) { // close single handle on file if (this.FileHandle.HandleId == null) { throw new System.ArgumentException(string.Format("The HandleId of the FileHandle on path {0} should not be null.", this.FileHandle.Path), "FileHandle"); } closeResult = targetFile.CloseHandleSegmented(this.FileHandle.HandleId.ToString(), continuationToken, null, this.RequestOptions, this.OperationContext); } else { // close all handle on file closeResult = targetFile.CloseAllHandlesSegmented(continuationToken, null, this.RequestOptions, this.OperationContext); } } numHandlesClosed += closeResult.NumHandlesClosed; continuationToken = closeResult.ContinuationToken; } while (continuationToken != null && continuationToken.NextMarker != null); if (PassThru) { WriteObject(numHandlesClosed); } } }
public override void ExecuteCmdlet() { CloudFile fileToBeDownloaded; string[] path = NamingUtil.ValidatePath(this.Path, true); switch (this.ParameterSetName) { case LocalConstants.FileParameterSetName: fileToBeDownloaded = this.File; break; case LocalConstants.ShareNameParameterSetName: var share = this.BuildFileShareObjectFromName(this.ShareName); fileToBeDownloaded = share.GetRootDirectoryReference().GetFileReferenceByPath(path); break; case LocalConstants.ShareParameterSetName: fileToBeDownloaded = this.Share.GetRootDirectoryReference().GetFileReferenceByPath(path); break; case LocalConstants.DirectoryParameterSetName: fileToBeDownloaded = this.Directory.GetFileReferenceByPath(path); break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); } string resolvedDestination = this.GetUnresolvedProviderPathFromPSPath( string.IsNullOrWhiteSpace(this.Destination) ? "." : this.Destination); FileMode mode = this.Force ? FileMode.Create : FileMode.CreateNew; string targetFile; if (LocalDirectory.Exists(resolvedDestination)) { // If the destination pointed to an existing directory, we // would download the file into the folder with the same name // on cloud. targetFile = LocalPath.Combine(resolvedDestination, fileToBeDownloaded.GetBaseName()); } else { // Otherwise we treat the destination as a file no matter if // there's one existing or not. The overwrite behavior is configured // by FileMode. targetFile = resolvedDestination; } if (ShouldProcess(targetFile, "Download")) { this.RunTask(async taskId => { await fileToBeDownloaded.FetchAttributesAsync(null, this.RequestOptions, OperationContext, CmdletCancellationToken); var progressRecord = new ProgressRecord( this.OutputStream.GetProgressId(taskId), string.Format(CultureInfo.CurrentCulture, Resources.ReceiveAzureFileActivity, fileToBeDownloaded.GetFullPath(), targetFile), Resources.PrepareDownloadingFile); await DataMovementTransferHelper.DoTransfer(() => { return(this.TransferManager.DownloadAsync( fileToBeDownloaded, targetFile, new DownloadOptions { DisableContentMD5Validation = !this.CheckMd5 }, this.GetTransferContext(progressRecord, fileToBeDownloaded.Properties.Length), CmdletCancellationToken)); }, progressRecord, this.OutputStream); if (this.PassThru) { this.OutputStream.WriteObject(taskId, fileToBeDownloaded); } }); } }
public override void ExecuteCmdlet() { CloudFile fileToBeDownloaded; string[] path = NamingUtil.ValidatePath(this.Path, true); switch (this.ParameterSetName) { case LocalConstants.FileParameterSetName: fileToBeDownloaded = this.File; break; case LocalConstants.ShareNameParameterSetName: var share = this.BuildFileShareObjectFromName(this.ShareName); fileToBeDownloaded = share.GetRootDirectoryReference().GetFileReferenceByPath(path); break; case LocalConstants.ShareParameterSetName: fileToBeDownloaded = this.Share.GetRootDirectoryReference().GetFileReferenceByPath(path); break; case LocalConstants.DirectoryParameterSetName: fileToBeDownloaded = this.Directory.GetFileReferenceByPath(path); break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); } string resolvedDestination = this.GetUnresolvedProviderPathFromPSPath( string.IsNullOrWhiteSpace(this.Destination) ? "." : this.Destination); FileMode mode = this.Force ? FileMode.Create : FileMode.CreateNew; string targetFile; if (LocalDirectory.Exists(resolvedDestination)) { // If the destination pointed to an existing directory, we // would download the file into the folder with the same name // on cloud. targetFile = LocalPath.Combine(resolvedDestination, fileToBeDownloaded.GetBaseName()); } else { // Otherwise we treat the destination as a file no matter if // there's one existing or not. The overwrite behavior is configured // by FileMode. targetFile = resolvedDestination; } this.RunTask(async taskId => { var downloadJob = new TransferJob( new TransferLocation(fileToBeDownloaded), new TransferLocation(targetFile), TransferMethod.SyncCopy); var progressRecord = new ProgressRecord( this.OutputStream.GetProgressId(taskId), string.Format(CultureInfo.CurrentCulture, Resources.ReceiveAzureFileActivity, fileToBeDownloaded.GetFullPath(), targetFile), Resources.PrepareDownloadingFile); await this.RunTransferJob(downloadJob, progressRecord); if (this.PassThru) { this.OutputStream.WriteObject(taskId, fileToBeDownloaded); } }); }
public override void ExecuteCmdlet() { CloudFileDirectory baseDirectory; switch (this.ParameterSetName) { case Constants.DirectoryParameterSetName: baseDirectory = this.Directory; break; case Constants.ShareNameParameterSetName: baseDirectory = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference(); break; case Constants.ShareParameterSetName: baseDirectory = this.Share.GetRootDirectoryReference(); break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); } if (string.IsNullOrEmpty(this.Path)) { this.RunTask(async(taskId) => { await this.Channel.EnumerateFilesAndDirectoriesAsync( baseDirectory, item => this.OutputStream.WriteObject(taskId, item), this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false); }); } else { this.RunTask(async(taskId) => { bool foundAFolder = true; string[] subfolders = NamingUtil.ValidatePath(this.Path); CloudFileDirectory targetDir = baseDirectory.GetDirectoryReferenceByPath(subfolders); try { await this.Channel.FetchDirectoryAttributesAsync( targetDir, null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false); } catch (StorageException se) { if (null == se.RequestInformation || (int)HttpStatusCode.NotFound != se.RequestInformation.HttpStatusCode) { throw; } foundAFolder = false; } if (foundAFolder) { this.OutputStream.WriteObject(taskId, targetDir); return; } string[] filePath = NamingUtil.ValidatePath(this.Path, true); CloudFile targetFile = baseDirectory.GetFileReferenceByPath(filePath); await this.Channel.FetchFileAttributesAsync( targetFile, null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false); this.OutputStream.WriteObject(taskId, targetFile); }); } }
public override void ExecuteCmdlet() { CloudFileDirectory baseDirectory = null; switch (this.ParameterSetName) { case Constants.DirectoryParameterSetName: baseDirectory = this.Directory; break; case Constants.ShareNameParameterSetName: baseDirectory = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference(); break; case Constants.ShareParameterSetName: baseDirectory = this.Share.GetRootDirectoryReference(); break; case Constants.FileParameterSetName: // Don't need to set baseDirectory when input is a CloudFile break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName)); } // When not input path/File, the list handle target must be a Dir bool foundAFolder = true; CloudFileDirectory targetDir = baseDirectory; CloudFile targetFile = null; if (this.File != null) { targetFile = this.File; foundAFolder = false; } else { if (!string.IsNullOrEmpty(this.Path)) { string[] subfolders = NamingUtil.ValidatePath(this.Path); targetDir = baseDirectory.GetDirectoryReferenceByPath(subfolders); if (!targetDir.Exists()) { foundAFolder = false; } if (!foundAFolder) { //Get file string[] filePath = NamingUtil.ValidatePath(this.Path, true); targetFile = baseDirectory.GetFileReferenceByPath(filePath); this.Channel.FetchFileAttributesAsync( targetFile, null, this.RequestOptions, this.OperationContext, this.CmdletCancellationToken).ConfigureAwait(false); } } } // Recursive only take effect on File Dir if (!foundAFolder && Recursive.IsPresent) { WriteVerbose("The target object of the 'Path' is an Azure File, the parameter '-Recursive' won't take effect."); } //List handle FileHandleResultSegment listResult; FileContinuationToken continuationToken = null; List <PSFileHandle> handleReturn = new List <PSFileHandle>(); ulong first = MyInvocation.BoundParameters.ContainsKey("First") ? this.PagingParameters.First : ulong.MaxValue; ulong skip = MyInvocation.BoundParameters.ContainsKey("Skip") ? this.PagingParameters.Skip : 0; // Any items before this count should be return ulong lastCount = MyInvocation.BoundParameters.ContainsKey("First") ? skip + first : ulong.MaxValue; ulong currentCount = 0; do { if (foundAFolder) { // list handle on fileDir listResult = targetDir.ListHandlesSegmented(continuationToken, null, Recursive, null, this.RequestOptions, this.OperationContext); } else { // list handle on file listResult = targetFile.ListHandlesSegmented(continuationToken, null, null, this.RequestOptions, this.OperationContext); } List <FileHandle> handleList = new List <FileHandle>(listResult.Results); if (currentCount + (ulong)handleList.Count - 1 < skip) { // skip the whole chunk if they are all in skip currentCount += (ulong)handleList.Count; } else { foreach (FileHandle handle in handleList) { // not return "skip" count of items in the begin, and only return "first" count of items after that. if (currentCount >= skip && currentCount < lastCount) { handleReturn.Add(new PSFileHandle(handle)); } currentCount++; if (currentCount >= lastCount) { break; } } } continuationToken = listResult.ContinuationToken; } while (continuationToken != null && continuationToken.NextMarker != null && currentCount < lastCount); WriteObject(handleReturn, true); }