Ejemplo n.º 1
0
        //protected virtual object ItemExistsDynamicParameters(string path);

        // SetItem method of the item provider interface is not implemented, using SetContent
        // from content provider interface instead, same as the built-in FileSystem provider.
        //protected virtual void SetItem(string path, object value);
        //protected virtual object SetItemDynamicParameters(string path, object value);

        #region ContainerCmdletProvider

        //protected virtual bool ConvertPath(string path, string filter, ref string updatedPath, ref string updatedFilter);
        //protected virtual void CopyItem(string path, string copyPath, bool recurse);
        //protected virtual object CopyItemDynamicParameters(string path, string destination, bool recurse);
        protected override void GetChildItems(string path, bool recurse)
        {
            if (PathIsDrive(path))
            {
                foreach (var deviceName in JAFS.GetDeviceNames())
                {
                    WriteItemObject(deviceName, path, true);
                }
            }
            else
            {
                Collection <JFSObject> pathObjects = JAFS.GetPathObjects(JAFSPath(path));
                JFSObject job = pathObjects.Count > 0 ? pathObjects[pathObjects.Count - 1] : null;
                if (job is JFSDevice)
                {
                    JFSDevice dev = job as JFSDevice;
                    foreach (var mntName in dev.GetMountPointNames())
                    {
                        WriteItemObject(mntName, path, true);
                    }
                }
                else if (job is JFSMountPoint)
                {
                    JFSMountPoint mnt = job as JFSMountPoint;
                    foreach (var folName in mnt.GetFoldersNames())
                    {
                        WriteItemObject(folName, path, true);
                    }
                    foreach (var filName in mnt.GetFileNames())
                    {
                        WriteItemObject(filName, path, false);
                    }
                }
                else if (job is JFSFolder)
                {
                    JFSFolder fol = job as JFSFolder;
                    foreach (var subName in fol.GetFoldersNames())
                    {
                        WriteItemObject(subName, path, true);
                    }
                    foreach (var filName in fol.GetFileNames())
                    {
                        WriteItemObject(filName, path, false);
                    }
                }
                else if (job is JFSBasicFile)
                {
                    JFSBasicFile fil = job as JFSBasicFile;
                    WriteItemObject(fil.Name, path, false);
                }
                else
                {
                    throw new ArgumentException("Invalid path");
                }
            }
        }
Ejemplo n.º 2
0
        //protected virtual object GetChildItemsDynamicParameters(string path, bool recurse);
        //protected virtual void GetChildNames(string path, ReturnContainers returnContainers);
        //protected virtual object GetChildNamesDynamicParameters(string path);
        protected override bool HasChildItems(string path)
        {
            // This is part of Container provider and tells if the container contains any children.
            // Must be implemented for RemoveItem to work, to decide how to handle possible recursion.
            // See also IsItemContainer implemented for Navigation provider.
            if (PathIsDrive(path))
            {
                return(true);
            }
            Collection <JFSObject> pathObjects = JAFS.GetPathObjects(JAFSPath(path));
            JFSObject job = pathObjects.Count > 0 ? pathObjects[pathObjects.Count - 1] : null;

            if (job != null)
            {
                if (job is JFSDevice)
                {
                    JFSDevice dev = job as JFSDevice;
                    return(dev.NumberOfApiMountPoints > 0); // NB: Includes deleted and special mount points!
                }
                else if (job is JFSMountPoint)
                {
                    JFSMountPoint mnt = job as JFSMountPoint;
                    return(mnt.NumberOfFilesAndFolders > 0);
                }
                else if (job is JFSFolder)
                {
                    JFSFolder fol = job as JFSFolder;
                    return(fol.NumberOfFilesAndFolders > 0);
                }
                else if (job is JFSBasicFile)
                {
                    return(false);
                }
                else
                {
                    throw new ArgumentException("Unexpected item type");
                }
            }
            else
            {
                return(false); //?
            }
        }
Ejemplo n.º 3
0
        protected override void RemoveItem(string path, bool recurse)
        {
            RemoveItemDynamicParameters parameters = DynamicParameters as RemoveItemDynamicParameters;

            if (PathIsDrive(path))
            {
                throw new ArgumentException("Remove not supported for this item");
            }
            Collection <JFSObject> pathObjects = JAFS.GetPathObjects(JAFSPath(path));
            JFSObject job = pathObjects.Count > 0 ? pathObjects[pathObjects.Count - 1] : null;

            if (job is JFSDevice)
            {
                JFSDevice dev = job as JFSDevice;
                if (parameters.Permanent)
                {
                    if (ShouldProcess(path, "RemoveItem"))
                    {
                        JAFS.DeleteDevicePermanently(dev);
                    }
                }
                else
                {
                    throw new ArgumentException("Devices can only be removed permanently");
                }
            }
            else if (job is JFSMountPoint)
            {
                JFSMountPoint mnt = job as JFSMountPoint;
                if (parameters.Permanent)
                {
                    JFSDevice dev = pathObjects.Count > 1 ? pathObjects[pathObjects.Count - 2] as JFSDevice : null; // Parent is the device
                    if (dev == null)
                    {
                        throw new ArgumentException("Failed to find parent item needed for permanent remove");
                    }
                    if (ShouldProcess(path, "RemoveItem"))
                    {
                        dev.DeleteMountPointPermanently(mnt);
                    }
                }
                else
                {
                    if (ShouldProcess(path, "RemoveItem"))
                    {
                        mnt.Delete();
                    }
                }
            }
            else if (job is JFSFolder)
            {
                JFSFolder fol = job as JFSFolder;
                if (parameters.Permanent)
                {
                    JFSObject parent = pathObjects.Count > 1 ? pathObjects[pathObjects.Count - 2] : null;
                    if (parent is JFSMountPoint)
                    {
                        JFSMountPoint mnt = parent as JFSMountPoint;
                        if (mnt == null)
                        {
                            throw new ArgumentException("Failed to find parent item needed for permanent remove");
                        }
                        if (ShouldProcess(path, "RemoveItem"))
                        {
                            mnt.DeleteFolderPermanently(fol);
                        }
                    }
                    else if (parent is JFSFolder)
                    {
                        JFSFolder pf = parent as JFSFolder;
                        if (pf == null)
                        {
                            throw new ArgumentException("Failed to find parent item needed for permanent remove");
                        }
                        if (ShouldProcess(path, "RemoveItem"))
                        {
                            pf.DeleteFolderPermanently(fol);
                        }
                    }
                }
                else
                {
                    if (ShouldProcess(path, "RemoveItem"))
                    {
                        fol.Delete();
                    }
                }
            }
            else if (job is JFSFile)
            {
                JFSFile fil = job as JFSFile;
                if (parameters.Permanent)
                {
                    JFSObject parent = pathObjects.Count > 1 ? pathObjects[pathObjects.Count - 2] : null;
                    if (parent is JFSMountPoint)
                    {
                        JFSMountPoint mnt = parent as JFSMountPoint;
                        if (mnt == null)
                        {
                            throw new ArgumentException("Failed to find parent item needed for permanent remove");
                        }
                        if (ShouldProcess(path, "RemoveItem"))
                        {
                            mnt.DeleteFilePermanently(fil);
                        }
                    }
                    else if (parent is JFSFolder)
                    {
                        JFSFolder pf = parent as JFSFolder;
                        if (pf == null)
                        {
                            throw new ArgumentException("Failed to find parent item needed for permanent remove");
                        }
                        if (ShouldProcess(path, "RemoveItem"))
                        {
                            pf.DeleteFilePermanently(fil);
                        }
                    }
                }
                else
                {
                    if (ShouldProcess(path, "RemoveItem"))
                    {
                        fil.Delete();
                    }
                }
            }
            else
            {
                throw new ArgumentException("Remove not supported for this item");
            }
        }
Ejemplo n.º 4
0
        protected override void NewItem(string path, string itemTypeName, object newItemValue)
        {
            NewItemDynamicParameters parameters = DynamicParameters as NewItemDynamicParameters;
            // The itemTypeName is the type of path of the container:
            //  - If a drive is specified, then a device can be created under it.
            //  - If a device is specified a mount point can be created under it.
            //  - If mount point or folder a folder can be created under it (or file, but we currently does not support creating for instance empty file using the NewItem operation).
            string itemName = newItemValue as string;

            if (String.IsNullOrEmpty(itemName))
            {
                throw new ArgumentException("Value argument must be name of the item to be created");
            }
            if (String.Equals(itemTypeName, "device", StringComparison.OrdinalIgnoreCase))
            {
                if (PathIsDrive(path))
                {
                    if (ShouldProcess(itemName, "New device"))
                    {
                        WriteItemObject(JAFS.NewDevice(itemName, parameters.DeviceType), path + PSPathSeparator + itemName, true);
                    }
                }
                else
                {
                    throw new ArgumentException("A device can only be created at root level");
                }
            }
            else if (String.Equals(itemTypeName, "mountpoint", StringComparison.OrdinalIgnoreCase))
            {
                if (!PathIsDrive(path))
                {
                    Collection <JFSObject> pathObjects = JAFS.GetPathObjects(JAFSPath(path));
                    JFSDevice dev = pathObjects.Count > 0 ? pathObjects[pathObjects.Count - 1] as JFSDevice : null;
                    if (dev != null)
                    {
                        if (ShouldProcess(itemName, "New mount point"))
                        {
                            WriteItemObject(dev.NewMountpoint(itemName), path + PSPathSeparator + itemName, true);
                        }
                    }
                    else
                    {
                        throw new ArgumentException("A mountpoint can only be created from a device and the specified path does not represent one");
                    }
                }
                else
                {
                    throw new ArgumentException("A mountpoint can only be created from a device and the specified path does not represent one");
                }
            }
            else if (String.Equals(itemTypeName, "folder", StringComparison.OrdinalIgnoreCase))
            {
                if (!PathIsDrive(path))
                {
                    Collection <JFSObject> pathObjects = JAFS.GetPathObjects(JAFSPath(path));
                    JFSObject job = pathObjects.Count > 0 ? pathObjects[pathObjects.Count - 1] : null;
                    if (job is JFSMountPoint)
                    {
                        JFSMountPoint mnt = job as JFSMountPoint;
                        if (mnt != null)
                        {
                            if (ShouldProcess(itemName, "New folder"))
                            {
                                WriteItemObject(mnt.NewFolder(itemName), path + PSPathSeparator + itemName, true);
                            }
                        }
                    }
                    else if (job is JFSFolder)
                    {
                        JFSFolder pf = job as JFSFolder;
                        if (pf != null)
                        {
                            if (ShouldProcess(itemName, "New folder"))
                            {
                                WriteItemObject(pf.NewFolder(itemName), path + PSPathSeparator + itemName, true);
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("A folder can only be created from a mount point or folder and the specified path does not represent one");
                    }
                }
                else
                {
                    throw new ArgumentException("A folder can only be created from a mount point or folder and the specified path does not represent one");
                }
            }
            else if (String.Equals(itemTypeName, "file", StringComparison.OrdinalIgnoreCase))
            {
                if (!PathIsDrive(path))
                {
                    Collection <JFSObject> pathObjects = JAFS.GetPathObjects(JAFSPath(path));
                    JFSObject job = pathObjects.Count > 0 ? pathObjects[pathObjects.Count - 1] : null;
                    if (job is JFSMountPoint)
                    {
                        JFSMountPoint mnt = job as JFSMountPoint;
                        if (mnt != null)
                        {
                            if (ShouldProcess(itemName, "New file"))
                            {
                                WriteItemObject(mnt.NewFile(itemName, new byte[0]), path + PSPathSeparator + itemName, false);
                            }
                        }
                    }
                    else if (job is JFSFolder)
                    {
                        JFSFolder pf = job as JFSFolder;
                        if (pf != null)
                        {
                            if (ShouldProcess(itemName, "New file"))
                            {
                                WriteItemObject(pf.NewFile(itemName, new byte[0]), path + PSPathSeparator + itemName, false);
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("A file can only be created from a mount point or folder and the specified path does not represent one");
                    }
                }
                else
                {
                    throw new ArgumentException("A file can only be created from a mount point or folder and the specified path does not represent one");
                }
            }
            else
            {
                WriteError(new ErrorRecord(new ArgumentException("Type must be one of the following: device, mountpoint or folder"),
                                           "CannotCreateSpecifiedObject", ErrorCategory.InvalidArgument, path));
                throw new ArgumentException("This provider can only create items of types \"device\", \"mountpoint\" and \"folder\"");
            }
        }