Beispiel #1
0
        protected override bool IsItemContainer(string path)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return(true);
            }

            //if the provider is not mounted, then create it first
            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                return(false);
            }

            if (parts.Count == 1)
            {
                return(true);
            }

            //delegate it
            return(drive.IsItemContainer(PathResolver.GetSubpath(path)));
        }
Beispiel #2
0
        public void GetProperty(string path, System.Collections.ObjectModel.Collection <string> providerSpecificPickList)
        {
            if (PathResolver.IsLocalPath(path))
            {
                return;
            }

            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return;
            }

            //if the provider is not mounted, then create it first
            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                return;
            }

            //delegate it
            drive.GetProperty(PathResolver.GetSubpath(path), providerSpecificPickList);
        }
Beispiel #3
0
        protected override void RemoveItem(string path, bool recurse)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return;
            }

            //if the provider is not mounted, then return
            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                return;
            }

            if (parts.Count == 1)
            {
                //unmount it
                var mountedLabel = parts[0];
                UnmountDrive(parts[0]);
            }
            else
            {
                //delegate it
                drive.RemoveItem(PathResolver.GetSubpath(path), recurse);
            }
        }
Beispiel #4
0
        protected override void GetChildItems(string path, bool recurse)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0) //if listing root, then list all PathResolver.Drives
            {
                foreach (var pair in PathResolver.Drives)
                {
                    WriteItemObject(pair.Value, path, true);
                    if (recurse)
                    {
                        pair.Value.GetChildItems(PathResolver.GetSubpath(path), recurse);
                    }
                }

                return;
            }
            else
            {
                var drive = GetDrive(parts[0]);
                if (drive == null)
                {
                    return;
                }

                drive.GetChildItems(PathResolver.GetSubpath(path), recurse);
            }
        }
Beispiel #5
0
        public void SetProperty(string path, PSObject propertyValue)
        {
            if (PathResolver.IsLocalPath(path))
            {
                return;
            }

            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return;
            }

            //if the provider is not mounted, then create it first
            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                return;
            }

            //delegate it
            drive.SetProperty(PathResolver.GetSubpath(path), propertyValue);
        }
Beispiel #6
0
        public IContentReader GetContentReader(string path)
        {
            if (PathResolver.IsLocalPath(path))
            {
                return(null);
            }

            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return(null);
            }

            //if the provider is not mounted, then create it first
            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                return(null);
            }

            //delegate it
            return(drive.GetContentReader(PathResolver.GetSubpath(path)));
        }
        private void CancelCopy(string path, string copyId)
        {
            var parts     = PathResolver.SplitPath(path);
            var container = this.Client.GetContainerReference(parts[0]);
            var destBlob  = container.GetBlobReference(PathResolver.GetSubpath(path));

            destBlob.AbortCopy(copyId);
        }
        private void AsyncCopy(string path, string url)
        {
            var parts     = PathResolver.SplitPath(path);
            var container = this.Client.GetContainerReference(parts[0]);
            var destBlob  = container.GetBlobReference(PathResolver.GetSubpath(path));
            var copyId    = destBlob.StartCopy(new Uri(url));

            this.RootProvider.WriteItemObject(new { CopyId = copyId }, path, false);
        }
        internal void CreateDirectory(string path)
        {
            var parts = PathResolver.SplitPath(path);


            if (parts.Count > 0)
            {
                var container = CreateContainerIfNotExists(parts[0]);

                if (parts.Count > 1)
                {
                    var dirObj = container.GetPageBlobReference(PathResolver.GetSubpath(path) + PathResolver.DirSeparator);
                    dirObj.Create(0);
                }
            }
        }
Beispiel #10
0
        protected override bool HasChildItems(string path)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return(PathResolver.Drives.Count > 0);
            }

            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                return(false);
            }

            return(drive.HasChildItems(PathResolver.GetSubpath(path)));
        }
Beispiel #11
0
        protected override void NewItem(
            string path,
            string type,
            object newItemValue)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                return;
            }

            //if the provider is not mounted, then create it first
            var drive = GetDrive(parts[0]);

            if (drive == null)
            {
                drive = DriveFactory.CreateInstance(type, newItemValue, parts[0]);
                if (drive == null)
                {
                    WriteError(new ErrorRecord(
                                   new Exception("Unregcognized type"),
                                   "NewItem type",
                                   ErrorCategory.InvalidArgument,
                                   ""));
                    return;
                }
                else
                {
                    PathResolver.Drives.Add(parts[0], drive);
                }
            }
            else
            {
                if (parts.Count > 1)
                {
                    //delegate it if needed
                    drive.NewItem(PathResolver.GetSubpath(path), type, newItemValue);
                }
            }
        }
Beispiel #12
0
        protected void CopyItemInternal(string path, string destination, bool recurse, bool deleteOriginal = false, bool exactDest = false)
        {
            var sourceDrive = PathResolver.FindDrive(path);
            var targetDrive = PathResolver.FindDrive(destination);

            if (sourceDrive is AzureTableServiceDriveInfo && targetDrive is AzureTableServiceDriveInfo)
            {
                var sd = sourceDrive as AzureTableServiceDriveInfo;
                var td = targetDrive as AzureTableServiceDriveInfo;
                sd.CopyTo(PathResolver.GetSubpath(path), td, PathResolver.GetSubpath(destination), deleteOriginal);
                return;
            }

            if (sourceDrive.IsItemContainer(PathResolver.GetSubpath(path)))
            {
                if (!recurse)
                {
                    throw new Exception(path + " is a container");
                }

                if (targetDrive.IsItemContainer(PathResolver.GetSubpath(destination)) && !exactDest)
                {
                    destination = MakePath(destination, PathResolver.SplitPath(path).Last());
                }

                targetDrive.NewItem(PathResolver.GetSubpath(destination), "Directory", null);
                foreach (var c in sourceDrive.GetChildNamesList(PathResolver.GetSubpath(path), PathType.Item))
                {
                    CopySingleItem(MakePath(path, c), MakePath(destination, c), exactDest: true);
                }
                foreach (var c in sourceDrive.GetChildNamesList(PathResolver.GetSubpath(path), PathType.Container))
                {
                    CopyItemInternal(MakePath(path, c), MakePath(destination, c), recurse, deleteOriginal, exactDest: true);
                }
            }
            else
            {
                CopySingleItem(path, destination);
            }
        }
        private void ShowCopyStatus(string path)
        {
            var parts     = PathResolver.SplitPath(path);
            var container = this.Client.GetContainerReference(parts[0]);
            var destBlob  = container.GetBlobReference(PathResolver.GetSubpath(path));

            destBlob.FetchAttributes();
            var state = destBlob.CopyState;

            if (state != null)
            {
                this.RootProvider.WriteWarning(string.Format("Copy is {0}\r\nId: {1}\r\nBytes Copied: {2}/{3}",
                                                             state.Status.ToString(),
                                                             state.CopyId,
                                                             state.BytesCopied,
                                                             state.TotalBytes));
            }
            else
            {
                this.RootProvider.WriteWarning(string.Format("CopyStatus is null"));
            }
        }
Beispiel #14
0
        protected override void GetChildNames(string path, ReturnContainers returnContainers)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count == 0)
            {
                foreach (var pair in PathResolver.Drives)
                {
                    WriteItemObject(pair.Key, "/", true);
                }
            }
            else
            {
                var drive = GetDrive(parts[0]);
                if (drive == null)
                {
                    return;
                }

                drive.GetChildNames(PathResolver.GetSubpath(path), returnContainers);
            }
        }
Beispiel #15
0
        protected void CopySingleItem(string path, string destination, bool exactDest = false)
        {
            var    sourceDrive = PathResolver.FindDrive(path);
            var    targetDrive = PathResolver.FindDrive(destination);
            var    sourcePath = PathResolver.GetSubpath(path);
            string targetDir, targetFile;

            if (targetDrive.IsItemContainer(PathResolver.GetSubpath(destination)) && !exactDest)
            {
                targetDir  = PathResolver.GetSubpath(destination);
                targetFile = PathResolver.SplitPath(path).Last();
            }
            else
            {
                targetDir  = PathResolver.GetSubpathDirectory(destination);
                targetFile = PathResolver.SplitPath(destination).Last();
            }
            using (var sourceStream = sourceDrive.CopyFrom(sourcePath))
                using (var targetStream = targetDrive.CopyTo(targetDir, targetFile))
                {
                    sourceStream.Seek(0, SeekOrigin.Begin);
                    sourceStream.CopyTo(targetStream);
                }
        }
        private void ListPageRanges(string path)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count > 1)
            {
                var blob = this.Client.GetContainerReference(parts[0]).GetPageBlobReference(PathResolver.GetSubpath(path));
                if (!blob.Exists())
                {
                    this.RootProvider.WriteWarning("PageBlob " + path + " does not exist.");
                    return;
                }

                blob.FetchAttributes();
                var totalLength = blob.Properties.Length;

                var count  = 0L;
                var offset = 0L;
                var length = 4 * 1024 * 1024L; //4MB
                while (true)
                {
                    PageRange page  = null;
                    var       round = 0L;

                    length = (offset + length > totalLength) ? (totalLength - offset) : length;
                    foreach (var r in blob.GetPageRanges(offset, length))
                    {
                        page = r;
                        round++;
                        this.RootProvider.WriteWarning(string.Format("[{3}]\t[{0} - {1}] {2}", r.StartOffset, r.EndOffset, r.EndOffset - r.StartOffset + 1, count++));
                    }

                    if (offset + length >= totalLength)
                    {
                        //reach the end
                        break;
                    }

                    //1. move offset
                    offset += length;

                    //2. calculate next length
                    if (round < 200)
                    {
                        length *= 2;
                    }
                    else if (round > 500)
                    {
                        length /= 2;
                    }
                }
            }
            else
            {
                this.RootProvider.WriteWarning("Please specify the page blob path.");
            }
        }
        private void ShowEtag(string path)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count > 1)
            {
                var blob = this.Client.GetContainerReference(parts[0]).GetBlobReference(PathResolver.GetSubpath(path));
                blob.FetchAttributes();
                this.RootProvider.WriteItemObject(blob.Properties.ETag, path, false);
            }
            else
            {
                this.RootProvider.WriteWarning("Please specify the target blob.");
            }
        }
        public override void NewItem(
            string path,
            string type,
            object newItemValue)
        {
            if (string.Equals(type, "Directory", StringComparison.InvariantCultureIgnoreCase))
            {
                this.CreateDirectory(path);
            }
            else if (string.Equals(type, "PageBlob", StringComparison.InvariantCultureIgnoreCase))
            {
                if (newItemValue != null)
                {
                    var size = 0L;
                    if (long.TryParse(newItemValue.ToString(), out size))
                    {
                        this.CreateEmptyFile(path, size);
                    }
                    else
                    {
                        this.CreateEmptyFile(path, 0);
                    }
                }
            }
            else if (string.Equals(type, "RandomPages", StringComparison.InvariantCultureIgnoreCase))
            {
                //fill page blob with random data, each page data is 512Byte, and count is required
                //e.g. ni PageBlob -type RandomPages -value <count>
                if (newItemValue != null)
                {
                    var size = 0L;
                    if (long.TryParse(newItemValue.ToString(), out size))
                    {
                        this.FillDataInPageBlob(path, size);
                    }
                    else
                    {
                        this.RootProvider.WriteWarning("Value is required.");
                    }
                }
            }
            else if (string.Equals(type, "ListPages", StringComparison.InvariantCultureIgnoreCase))
            {
                //List page ranges in page blob
                //e.g. ni pageBlob -type ListPages
                this.ListPageRanges(path);
            }
            else if (string.Equals(type, "ContainerSAStoken", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 0)
                {
                    var containerName = parts[0];
                    var container     = this.Client.GetContainerReference(containerName);
                    var policyName    = string.Empty;
                    var policy        = CreateBlobPolicy(newItemValue as string, ref policyName);

                    if (policyName != string.Empty) //policy-based SAStoken
                    {
                        var token = container.GetSharedAccessSignature(policy, policyName);
                        this.RootProvider.WriteItemObject(token, path, false);
                    }
                    else
                    {
                        var token = container.GetSharedAccessSignature(policy);
                        this.RootProvider.WriteItemObject(token, path, false);
                    }
                }
            }
            else if (string.Equals(type, "BlobSAStoken", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 1)
                {
                    var containerName = parts[0];
                    var container     = this.Client.GetContainerReference(containerName);
                    var blob          = container.GetBlobReference(PathResolver.GetSubpath(path));
                    var policyName    = string.Empty;
                    var policy        = CreateBlobPolicy(newItemValue as string, ref policyName);

                    if (policyName != string.Empty) //policy-based SAStoken
                    {
                        var token = blob.GetSharedAccessSignature(policy, policyName);
                        this.RootProvider.WriteItemObject(blob.StorageUri.PrimaryUri.ToString() + token, path, false);
                    }
                    else
                    {
                        var token = blob.GetSharedAccessSignature(policy);
                        this.RootProvider.WriteItemObject(blob.StorageUri.PrimaryUri.ToString() + token, path, false);
                    }
                }
            }
            else if (string.Equals(type, "Policy", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 0)
                {
                    var containerName     = parts[0];
                    var container         = this.Client.GetContainerReference(containerName);
                    var policyName        = parts.Last();
                    var policyPlaceHolder = string.Empty;
                    var policy            = CreateBlobPolicy(newItemValue as string, ref policyPlaceHolder);

                    var permissions = container.GetPermissions();
                    if (permissions.SharedAccessPolicies.ContainsKey(policyName))
                    {
                        if (!this.RootProvider.ShouldContinue(string.Format("Should continue to update existing policy {0}?", policyName), "Policy existed"))
                        {
                            this.RootProvider.WriteWarning("Cancelled");
                            return;
                        }
                        else
                        {
                            permissions.SharedAccessPolicies[policyName] = policy;
                        }
                    }
                    else
                    {
                        permissions.SharedAccessPolicies.Add(policyName, policy);
                    }

                    this.RootProvider.WriteWarning(string.Format("Policy {0} updated or added.", policyName));
                }

                return;
            }
            else if (string.Equals(type, "BlockBlob", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count == 1)
                {
                    this.CreateContainerIfNotExists(parts[0]);
                }
                else
                {
                    this.CreateBlockBlob(path, newItemValue.ToString());
                }
            }
            else if (string.Equals(type, "AppendBlob", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count == 1)
                {
                    this.CreateContainerIfNotExists(parts[0]);
                }
                else
                {
                    this.CreateAppendBlob(path, newItemValue.ToString());
                }
            }
            else if (string.Equals(type, "Permission", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 0)
                {
                    switch (newItemValue.ToString().ToLowerInvariant())
                    {
                    case "publiccontainer":
                        this.SetContainerPermission(containerName: parts[0], toBePublic: true);
                        this.RootProvider.WriteWarning(string.Format("Done setting container {0} to be public", parts[0]));
                        break;

                    case "privatecontainer":
                        this.SetContainerPermission(containerName: parts[0], toBePublic: false);
                        this.RootProvider.WriteWarning(string.Format("Done setting container {0} to be private", parts[0]));
                        break;

                    default:
                        this.RootProvider.WriteWarning("Invalid value. Supported values: PublicContainer, PrivateContainer");
                        break;
                    }
                }
                else
                {
                    this.RootProvider.WriteWarning("Please do this operation in a container.");
                }
            }
            else if (string.Equals(type, "AsyncCopy", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 1)
                {
                    if (newItemValue != null && newItemValue.ToString().Length > 0)
                    {
                        this.AsyncCopy(path, newItemValue.ToString());
                        this.RootProvider.WriteWarning("Started Async copy");
                    }
                    else
                    {
                        this.RootProvider.WriteWarning("Must specify the source url in -value.");
                    }
                }
                else
                {
                    this.RootProvider.WriteWarning("Please do this operation in a container and specify the target blob name.");
                }
            }
            else if (string.Equals(type, "CopyStatus", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 1)
                {
                    this.ShowCopyStatus(path);
                }
                else
                {
                    this.RootProvider.WriteWarning("Please do this operation in a container and specify the target blob name.");
                }
            }
            else if (string.Equals(type, "CancelCopy", StringComparison.InvariantCultureIgnoreCase))
            {
                var parts = PathResolver.SplitPath(path);
                if (parts.Count > 1)
                {
                    if (newItemValue != null && newItemValue.ToString().Length > 0)
                    {
                        this.CancelCopy(path, newItemValue.ToString());
                    }
                    else
                    {
                        this.RootProvider.WriteWarning("Must specify the copy ID in -value.");
                    }
                }
                else
                {
                    this.RootProvider.WriteWarning("Please do this operation in a container and specify the target blob name.");
                }
            }
            else if (string.Equals(type, "Etag", StringComparison.InvariantCultureIgnoreCase))
            {
                this.ShowEtag(path);
            }
            else
            {
                this.RootProvider.WriteWarning("No operation type is specified by <-type>.");
                this.RootProvider.WriteWarning("Supported operation type: ");
                this.RootProvider.WriteWarning("\tDirectory:            Create directory <-path>");
                this.RootProvider.WriteWarning("\tPageBlob:             Create page blob <-path> with size <-value>");
                this.RootProvider.WriteWarning("\tRandomPages:          Fill page blob <-path> with size <-value> using random data");
                this.RootProvider.WriteWarning("\tListPages:            List page ranges in page blob <-path>");
                this.RootProvider.WriteWarning("\tBlockBlob:            Create block blob <-path> with contents <-value>");
                this.RootProvider.WriteWarning("\tAppendBlob:           Create append blob <-path> with contents <-value>");
                this.RootProvider.WriteWarning("\tContainerSAStoken:    Expected <-value>: start=<days>;expiry=<days>;policy=<policy>;p=rwdl");
                this.RootProvider.WriteWarning("\tBlobSAStoken:         Expected <-value>: start=<days>;expiry=<days>;policy=<policy>;p=rwdl");
                this.RootProvider.WriteWarning("\tPolicy:               Expected <-value>: start=<days>;expiry=<days>;policy=<policy>;p=rwdl");
                this.RootProvider.WriteWarning("\tPermission:           Supported <-value>: PublicContainer, PrivateContainer");
                this.RootProvider.WriteWarning("\tAsyncCopy:            AsyncCopy blob from url <-value> to <-path>");
                this.RootProvider.WriteWarning("\tCopyStatus:           Show copy status of blob <-path>");
                this.RootProvider.WriteWarning("\tCancelCopy:           Specify the destBlob in <-path> and the copy ID in <-value>.");
                this.RootProvider.WriteWarning("\tEtag:                 Show the Etag of the blob <-path> ");
            }
        }
        private void FillDataInPageBlob(string path, long count)
        {
            var parts = PathResolver.SplitPath(path);

            if (parts.Count > 1)
            {
                var blob = this.Client.GetContainerReference(parts[0]).GetPageBlobReference(PathResolver.GetSubpath(path));
                if (!blob.Exists())
                {
                    this.RootProvider.WriteWarning("PageBlob " + path + " does not exist.");
                    return;
                }

                blob.FetchAttributes();
                var total  = blob.Properties.Length / 512;
                var data   = new byte[512];
                var random = new Random();
                random.NextBytes(data);

                this.RootProvider.WriteWarning("Start writing pages...");
                var tasks = new Task[count];

                for (var i = 0; i < count; ++i)
                {
                    var p = (long)(random.NextDouble() * total);

                    var task = blob.WritePagesAsync(new MemoryStream(data), p * 512, null);
                    tasks[i] = task;
                }

                this.RootProvider.WriteWarning("Waiting writing pages...");
                Task.WaitAll(tasks);
                this.RootProvider.WriteWarning("Completed writing pages...");
            }
            else
            {
                this.RootProvider.WriteWarning("Please specify the page blob path.");
            }
        }