476 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Provider;
using System.Management.Automation.Runspaces;
namespace Intel.Management.PSModule
{
public class DriveProvider : NavigationCmdletProvider,
IPropertyCmdletProvider,
IContentCmdletProvider
{
public DriveProvider()
: base()
{
}
public PSDriveInfo GetDriveInfo()
{
return this.PSDriveInfo;
}
public object GetDynamicParameters()
{
return DynamicParameters;
}
/// <summary>
/// Sets or Changes the value of an item
/// parameter.
/// </summary>
/// <param name="path">Specifies the path to the item</param>
/// <param name="values">Comma separated string of values</param>
protected override void SetItem(string path, object values)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
item.SetItem(values, this);
}
protected override void ClearItem(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
item.ClearItem(this);
}
/// <summary>
/// Create a new Item
/// parameter.
/// </summary>
/// <param name="path">Specifies the path to the item</param>
/// <param name="itemTypeName">The Type of the new Item</param>
/// <param name="newItemValue">The value of the new Item</param>
protected override void NewItem(string path, string itemTypeName, object newItemValue)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = null;
string searchPath = path;
item = info.GetItemFromPath(searchPath);
while (item == null)
{
searchPath = GetParentPath(searchPath, info.Root);
if (searchPath == null) break;
item = info.GetItemFromPath(searchPath);
}
//get remaining path
if (item != null)
{
string itemName = path.Substring(searchPath.Length);
if (itemName.StartsWith("\\") || itemName.StartsWith("/"))
itemName = itemName.Substring(1);
item = item.NewItem(itemName, itemTypeName, newItemValue, this);
}
if (item != null)
WriteItemObject(item.GetReturnObject(), path, false);
}
protected override object NewItemDynamicParameters(string path, string itemTypeName, object newItemValue)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = null;
string searchPath = path;
item = info.GetItemFromPath(searchPath);
while (item == null)
{
searchPath = GetParentPath(searchPath, info.Root);
if (searchPath == null) break;
item = info.GetItemFromPath(searchPath);
}
if (item != null)
return item.NewItemDynamicParameters(itemTypeName, newItemValue);
return base.NewItemDynamicParameters(path, itemTypeName, newItemValue);
}
/// <summary>
/// Removes an Item
/// </summary>
protected override void RemoveItem(string path, bool recurse)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
item.RemoveItem( this);
}
protected override void InvokeDefaultAction(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
item.Invoke(this);
//this.NewDriveDynamicParameters(
//this.
}
protected override object InvokeDefaultActionDynamicParameters(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
return item.InvokeDefaultActionDynamicParameters();
throw new InvalidOperationException(path);
}
/// <summary>
/// Retrieves an item using the specified path.
/// </summary>
/// <param name="path">The path to the item to return.</param>
protected override void GetItem(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path,this);
WriteItemObject(item.GetReturnObject(), path, item is DriveContainer);
}
/// <summary>
/// Test to see if the specified item exists.
/// </summary>
/// <param name="path">The path to the item to verify.</param>
/// <returns>True if the item is found.</returns>
protected override bool ItemExists(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
return item != null;
}
/// <summary>
/// Returns Children of a container
/// </summary>
protected override void GetChildItems(string path, bool recurse)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
//IsContainer already passed
DriveContainer parent = (DriveContainer)info.GetItemFromPath(path);
ChildWriter writer =new ChildWriter(parent,path,recurse,this);
writer.Flush();
}
/// <summary>
/// Return the names of all child items.
/// </summary>
/// <param name="path">The root path.</param>
/// <param name="returnContainers">Not used.</param>
protected override void GetChildNames(string path,
ReturnContainers returnContainers)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveContainer parent = (DriveContainer)info.GetItemFromPath(path);
ChildWriter writer = new ChildWriter(parent, null, false, this);
writer.Flush();
foreach (DriveItem item in parent.GetChildItems())
{
WriteItemObject(item.Name, path +"\\" +item.Name, item is DriveContainer);
}
}
/// <summary>
/// Determines if the specified path has child items.
/// </summary>
/// <param name="path">The path to examine.</param>
/// <returns>
/// True if the specified path has child items.
/// </returns>
protected override bool HasChildItems(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
return item is DriveContainer;
}
/// <summary>
/// Determine if the path specified is that of a container.
/// </summary>
/// <param name="path">The path to check.</param>
/// <returns>True if the path specifies a container.</returns>
protected override bool IsItemContainer(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
return item is DriveContainer;
}
/// <summary>
/// Throws an argument exception stating that the specified path does
/// not represent either a table or a row
/// </summary>
/// <param name="path">path which is invalid</param>
private void ThrowTerminatingInvalidPathException(string path)
{
StringBuilder message = new StringBuilder("Invalid AmtSystem path:");
message.Append(path);
throw new ArgumentException(message.ToString());
}
/// <summary>
/// Get the name of the leaf element in the specified path
/// </summary>
///
/// <param name="path">
/// The full or partial provider specific path
/// </param>
///
/// <returns>
/// The leaf element in the path
/// </returns>
protected override string GetChildName(string path)
{
return base.GetChildName(path);
}
protected override void RenameItem(string path, string newName)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
item.RenameItem(newName);
}
/// <summary>
/// Removes the child segment of the path and returns the remaining
/// parent portion
/// </summary>
///
/// <param name="path">
/// A full or partial provider specific path. The path may be to an
/// item that may or may not exist.
/// </param>
///
/// <param name="root">
/// The fully qualified path to the root of a drive. This parameter
/// may be null or empty if a mounted drive is not in use for this
/// operation. If this parameter is not null or empty the result
/// of the method should not be a path to a container that is a
/// parent or in a different tree than the root.
/// </param>
///
/// <returns></returns>
protected override string GetParentPath(string path, string root)
{
return base.GetParentPath(path, root); // return parentpath
}
protected override object SetItemDynamicParameters(string path, object value)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
return item.SetItemDynamicParameters();
}
public void ClearProperty(string path, Collection<string> propertyToClear)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
item.ClearProperty(propertyToClear, this);
}
public object ClearPropertyDynamicParameters(string path, Collection<string> propertyToClear)
{
return null;
}
public void GetProperty(string path, Collection<string> providerSpecificPickList)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
WritePropertyObject(
item.GetProperty(providerSpecificPickList,this), path);
}
public void SetProperty(string path, PSObject propertyValue)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
item.SetProperty(propertyValue, this);
WritePropertyObject(propertyValue, path);
}
public object GetPropertyDynamicParameters(string path, Collection<string> providerSpecificPickList)
{
return null;
}
public object SetPropertyDynamicParameters(string path, PSObject propertyValue)
{
return null;
}
public object ClearContentDynamicParameters(string path)
{
return null;
}
public void ClearContent(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
item.ClearContent(this);
}
public object GetContentWriterDynamicParameters(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
return item.GetContentWriterDynamicParameters();
}
public IContentWriter GetContentWriter(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
return item.GetContentWriter( this);
}
public object GetContentReaderDynamicParameters(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
return item.GetContentReaderDynamicParameters();
}
public IContentReader GetContentReader(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
DriveItem item = info.GetItemFromPath(path);
return item.GetContentReader( this);
}
/*
private void WriteErrors(Exception exp)
{
//WriteErrors(exp,
// ErrorCategory.
// if (exp is HECIClass.UnauthorizedException)
{
// WriteError(new ErrorRecord(
// exp,
// exp.GetType().Name,
// ErrorCategory.PermissionDenied,
/// PSDriveInfo));
}
// else
{
WriteError(new ErrorRecord(
exp,
exp.GetType().Name,
ErrorCategory.NotSpecified,
PSDriveInfo));
}
}*/
/// <summary>
/// Checks if a given path is actually a drive name.
/// </summary>
/// <param name="path">The path to check.</param>
/// <returns>
/// True if the path given represents a drive, false otherwise.
/// </returns>
private bool PathIsDrive(string path)
{
// check for root path (with or without separators)
if (string.IsNullOrEmpty(path) ||
string.Compare(path, PSDriveInfo.Name, true) == 0 ||
string.Compare(path, PSDriveInfo.Name + "\\", true) == 0 ||
string.Compare(path, PSDriveInfo.Name + "/", true) == 0 ||
string.Compare(path, "/", true) == 0 ||
string.Compare(path, "\\", true) == 0)
{
return true;
}
else
{
return false;
}
} // PathIsDrive
/// <summary>
/// Test to see if the specified path is syntactically valid.
/// </summary>
/// <param name="path">The path to validate.</param>
/// <returns>True if the specified path is valid.</returns>
protected override bool IsValidPath(string path)
{
DriveInfo info = (DriveInfo)PSDriveInfo;
object item = info.GetItemFromPath(path);
return item != null;
}
}
}