Пример #1
0
    public virtual int write(string path, int flags, string buffer, int offset, int length)
    {
        string workPath = "/";
        FileNode aNode;
        string[] pathArray = path.Split('/');
        int counter = 0;
        string loadPath = "";
        FileNode newDir;
        FileNode newfile;

        while (counter <= pathArray.Length - 2)
        {
            if (workPath == "/")
                workPath = workPath + pathArray[counter];
            else
                workPath = workPath + '/' + pathArray[counter];
            counter++;
        }

        if (flags == writeFlags.DELETE)
        {
            //unlink a node
            counter = 0;
            aNode = this.walk(path);

            if (aNode.getType() == fileTypes.Mount)
            {
                loadPath = this.parsePath(aNode, path);
                return (aNode.getMount().write(loadPath, flags, buffer, offset, length));
            }
            if (aNode.getType() == fileTypes.Symlink)
            {
                string[] fileStruct = path.Split('/');
                string remPath = aNode.getName() + "/" + fileStruct[fileStruct.Length - 1];
                return (this.write(remPath, flags, buffer, offset, length));
            }
            if (aNode.getType() == fileTypes.Union)
            {
                string[] fileStruct = path.Split('/');
                counter = 0;

                while (counter < aNode.getDirList().Count)
                {
                    string remPath = aNode.getDirList()[counter] + "/" + fileStruct[fileStruct.Length - 1];
                    this.write(remPath, flags, buffer, offset, length);
                    counter++;
                }
                return 0;
            }
            FileNode prevNode = aNode.getPrev();
            FileNode nextNode = aNode.getNext();

            while (aNode.getParent().getDirList()[counter].getName() != aNode.getName() && counter < aNode.getParent().getDirList().Count)
            {
                counter++;
            }

            prevNode.setNext(nextNode);
            nextNode.setPrev(prevNode);
            return 0;
        }

        if (flags == writeFlags.CREATEDIR)
        {
            aNode = this.walk(workPath);

            if (aNode.getType() == 1 && buffer == "1")
            {
                newDir =  new FileNode(pathArray[pathArray.Length - 1], fileTypes.Directory);
                newDir.addParent(aNode);
                aNode.addChild(newDir);

                if (aNode.countChildren() > 0)
                {
                    newDir.setPrev(aNode.getDirList().ElementAt(aNode.countChildren()-1));
                }

                if (aNode.countChildren() >= 2)
                {
                    aNode.getDirList()[aNode.countChildren() - 2].setNext(newDir);
                }

                this.touched(aNode);
                return 0;
            }
            else if (aNode.getType() == fileTypes.Symlink)
            {
                return (this.write(aNode.getSym() + "/" + pathArray[pathArray.Length - 1], flags, buffer, offset, length));
            }

            else if (aNode.getType() == fileTypes.Mount)
            {
                loadPath = this.parsePath(aNode, path);
                return (aNode.getMount().write(loadPath, flags, buffer, offset, length));
            }
            else if (aNode.getType() == fileTypes.Union)
            {
                loadPath = this.parsePath(aNode, path);
                int count = 0;
                while (count < aNode.getDirList().Count)
                {
                    this.touched(aNode);
                    this.write(aNode.getDirList()[count] + loadPath, flags, buffer, offset, length);
                    count++;
                }
                return (0);
            }
            else
                return -1;

        }
        else if (flags == writeFlags.CREATEDATAFILE)
        {
            aNode = this.walk(workPath);

            if (aNode.getType() == 1)
            {
                var count = 0;
                if (aNode.getDirList().Count > 0)
                {
                    while (count < aNode.getData().Length - 1 && aNode.getDirList()[count].getName() != pathArray[pathArray.Length - 1])
                    {
                        count++;
                    }
                    aNode.getDirList();
                    //Console.WriteLine("write file debug:\n count"+count+"\ndir list length"+aNode.getDirList().Count + "\npatharray" + pathArray[pathArray.Length - 1]);
                    if (aNode.getDirList()[count].getName() == pathArray[pathArray.Length - 1] && aNode.getDirList()[count].getName() != "")
                    {
                        aNode.getDirList()[count].putData(buffer);
                        this.touched(aNode);
                        return 0;
                    }
                }

                FileNode newFile = new FileNode(pathArray[pathArray.Length - 1], fileTypes.Text);

                newFile.addParent(aNode);
                aNode.addChild(newFile);

                if (aNode.countChildren() > 0)
                {
                    newFile.setPrev(aNode.getDirList().ElementAt(aNode.countChildren() - 1));
                }

                if (aNode.countChildren() >= 2)
                {
                    aNode.getDirList()[aNode.countChildren() - 2].setNext(newFile);
                }

                newFile.putData(buffer);
                this.touched(aNode);
                return 0;
            }

            else if (aNode.getType() == fileTypes.Mount)
            {
                loadPath = this.parsePath(aNode, path);
                return aNode.getMount().write(loadPath, flags, buffer, offset, length);
            }
            else if (aNode.getType() == fileTypes.Union)
            {
                loadPath = this.parsePath(aNode, path);
                counter = 0;

                while (counter < aNode.getData().Length)
                {
                    this.touched(aNode);
                    this.write(aNode.getDirList()[counter] + loadPath, flags, buffer, offset, length);
                    counter++;
                }
                return 0;
            }
            else
                return -2;
        }

        else if (flags == 3)
        {
            aNode = this.walk(workPath);
            var bNode = this.walk(buffer);

            if (bNode.getType() == fileTypes.Directory || bNode.getType() == fileTypes.Mount)
            {
                if (aNode.getType() == fileTypes.Directory)
                {
                    newDir = aNode.getDirList()[aNode.getDirList().Count] = new FileNode(pathArray[pathArray.Length - 1], fileTypes.Symlink);
                    newDir.setSym(bNode);

                    newDir.addParent(aNode);
                    aNode.addChild(newDir);

                    if (aNode.countChildren() > 0)
                    {
                        newDir.setPrev(aNode.getDirList().ElementAt(aNode.countChildren() - 1));
                    }

                    if (aNode.countChildren() >= 2)
                    {
                        aNode.getDirList()[aNode.countChildren() - 2].setNext(newDir);
                    }

                    this.touched(aNode);
                    return 0;
                }

                else if (aNode.getType() == fileTypes.Mount)
                {
                    loadPath = this.parsePath(aNode, path);
                    this.touched(aNode);
                    return (aNode.getMount().write(loadPath, flags, buffer, offset, length));
                }

                else
                    return -3;
            }
            if (bNode.getType() == fileTypes.Mount)
            {
                if (aNode.getType() == fileTypes.Directory)
                {

                    newfile = aNode.getDirList()[aNode.getDirList().Count] = new FileNode(pathArray[pathArray.Length - 1], fileTypes.Symlink);
                    newfile.setSym(bNode);
                    newfile.addParent(aNode);
                    aNode.addChild(newfile);

                    if (aNode.countChildren() > 0)
                    {
                        newfile.setPrev(aNode.getDirList().ElementAt(aNode.countChildren() - 1));
                    }

                    if (aNode.countChildren() >= 2)
                    {
                        aNode.getDirList()[aNode.countChildren() - 2].setNext(newfile);
                    }

                    this.touched(aNode);
                    return 0;
                 }

                if (aNode.getType() == fileTypes.Mount)
                {
                    loadPath = this.parsePath(aNode, path);
                    this.touched(aNode);
                    return (aNode.getMount().write(loadPath, flags, buffer, offset, length));
                }
                else
                    return -3;
            }
            if (bNode.getType() == fileTypes.Union)
            {
                if (aNode.getType() == 1)
                {
                    newfile = new FileNode(pathArray[pathArray.Length - 1], 4);
                    aNode.getDirList()[aNode.getDirList().Count] = newfile;
                    /*
                    newfile.Name = pathArray[pathArray.length - 1];
                    newfile.Type = 4;
                    newfile.next = 0;
                    newfile.Data = buffer;
                    newfile.prev = aNode.Data[aNode.Data.length - 1];
                    newfile.Parent = aNode;

                    if (aNode.Data.length >= 2)
                    {
                        aNode.Data[aNode.Data.length - 2].next = newfile;
                    }*/
                    this.touched(aNode);
                    return 0;
                }

                if (aNode.getType() == fileTypes.Mount)
                {
                    loadPath = this.parsePath(aNode, path);
                    this.touched(aNode);
                    return aNode.getMount().write(loadPath, flags, buffer, offset, length);
                }
                else
                    return -4;
            }

            else
                return bNode.getType();
        }
        else if (flags == fileTypes.Symlink)
        {
            aNode = this.walk(workPath);

            if (aNode.getType() == fileTypes.Directory)
            {/*
                newfile = 0;
                newfile = aNode.Data[aNode.Data.length] = new fileNode();
                newfile.Name = pathArray[pathArray.length - 1];
                newfile.Type = 3;
                newfile.next = 0;
                newfile.Data = buffer;
                newfile.prev = aNode.Data[aNode.Data.length - 1];
                newfile.Parent = aNode;
                //newfile.watchers = [];
                //newfile.meta = [];
                //newfile.watchers = [];
                //newfile.meta = ["owner:local", "perm:755", "type:type/default"];
                if (aNode.Data.length >= 2)
                {
                    aNode.Data[aNode.Data.length - 2].next = newfile;
                }*/
                this.touched(aNode);
                 return 0;
            }

            else if (aNode.getType() == fileTypes.Mount)
            {
                loadPath = this.parsePath(aNode, path);
                this.touched(aNode);
                return (aNode.getMount().write(loadPath, flags, buffer, offset, length));
            }

            else if (aNode.getType() == fileTypes.Union)
            {
                loadPath = this.parsePath(aNode, path);

                counter = 0;
                /*
                while (counter < aNode.Data.length)
                {
                    this.touched(path);
                    this.journal.write(path, buffer);
                    this.write(aNode.Data[counter] + loadPath, flags, buffer, offset, length, this.sessionKey);
                    counter++;
                }*/

                return 0;
            }

            else
                return -2;
        }
        else if (flags == 5)
        {
            aNode = this.walk(workPath);

            if (aNode.getType() == fileTypes.Directory)
            {/*
                aNode.Data[aNode.Data.length] = new fileNode();
                newDir = 0;
                newDir = aNode.Data[aNode.Data.length - 1];
                newDir.Name = pathArray[pathArray.length - 1];
                newDir.Type = 7;
                newDir.next = 0;
                newDir.Data = buffer;
                newDir.prev = aNode.Data[aNode.Data.length - 1];
                newDir.Parent = aNode;
                //newDir.watchers = [];
                //newDir.meta = ["owner:local", "perm:755", "type:type/default"];*/
                this.touched(aNode);
                return 0;
            }

            if (aNode.getType() == fileTypes.Directory)
            {
                loadPath = this.parsePath(aNode, path);
                this.touched(aNode);

                return (aNode.getMount().write(loadPath, flags, buffer, offset, length));
            }

            else
                return -4;
        }
        else if (flags == fileTypes.Mount)
        {
            if (this.walk(path) != null)
            {
                aNode = this.walk(path);

                if (aNode.getType() == 5)
                {
                    return -1;
                }
                if (aNode.getType() == fileTypes.Union)
                {
                    //aNode.Data[aNode.Data.length] = buffer;
                    this.touched(aNode);
                    return 0;
                }
                else
                    aNode.putData(  buffer);
                return 0;
            }
            else
                return -5;
        }
        else
            return -10;
    }
Пример #2
0
    public fileSystem mount(int mode, string fsTypeName, string mountPoint, string[] args)
    {
        string workPath = "/";
        FileNode aNode;
        string[] pathArray = mountPoint.Split('/');
        int counter = 0;
        fileSystem toMount = null;
        /*
        if (fsTypeName == "localfs")
            toMount = new LocalFS();
        if (fsTypeName == "timefs")
            toMount = new ClockFS();
        if (fsTypeName == "winfs")
            toMount = new WinFS();
        if (fsTypeName == "sysfs")
            toMount = new SysFS(args[0]);
        if (fsTypeName == "homefs")
            toMount = new HomeFS();
        if (fsTypeName == "procfs")
            toMount = new ProcFS();
        if (fsTypeName == "xmppfs")
            toMount = new XMPPFS();
        */
        while (counter <= pathArray.Length - 2)
        {
            if (workPath == "/")
                workPath = workPath + pathArray[counter];
            else
                workPath = workPath + '/' + pathArray[counter];
            counter++;
        }

        aNode = this.walk(workPath);
        //parse list of available filesystems for the one we've been asked to mount
        //while (counter < vfsList.length && vfsList[counter].Name != fs_type)
        //{
        //    counter++;
        //}

        if (aNode.getType() == 1)//&& vfsList[counter].Name == fs_type)
        {
            var count = 0;
            if (aNode.getDirList().Count > 0)
            {
                while (count < aNode.getData().Length - 1 && aNode.getDirList()[count].getName() != pathArray[pathArray.Length - 1])
                {
                    count++;
                }
                aNode.getDirList();
            }

            FileNode newFile = new FileNode(pathArray[pathArray.Length - 1], fileTypes.Mount);

            newFile.addParent(aNode);
            aNode.addChild(newFile);

            if (aNode.countChildren() > 0)
            {
                newFile.setPrev(aNode.getDirList().ElementAt(aNode.countChildren() - 1));
            }

            if (aNode.countChildren() >= 2)
            {
                aNode.getDirList()[aNode.countChildren() - 2].setNext(newFile);
            }

            this.touched(aNode);
            newFile.setMount(toMount);
            return toMount.onmount(mountPoint);// serverAddress, fs_type, mountPoint, user, pass, argv));
           }
           else
            return null;
    }