Ejemplo n.º 1
0
        public FSEntry Find(string path)
        {
            // good:  /foo/bar, /foo/bar/
            // bad:  foo, foo/bar, //foo/bar, /foo//bar, /foo/../foo/bar
            VirtualNode current = virtualFileSystem.RootNode;

            string[] elements = path.Split(PATH_SEPARATOR);

            foreach (string element in elements.Skip(1))
            {
                VirtualNode child = current.GetChild(element);
                if (child != null)
                {
                    current = child;
                }
                else
                {
                    return(null);
                }
            }

            FSEntry result = null;

            if (current.IsDirectory)
            {
                result = new SimpleDirectory(current);
            }
            else
            {
                result = new SimpleFile(current);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public FSEntry Find(string path)
        {
            // good:  /foo/bar, /foo/bar/
            // bad:  foo, foo/bar, //foo/bar, /foo//bar, /foo/../foo/bar

            // Make sure path starts correctly
            if (path.Length <= 0 || path[0] != PATH_SEPARATOR)
            {
                //Console.WriteLine("invalid path");
                return(null);
            }
            if (path.Length == 1)
            {
                return(new SimpleDirectory(virtualFileSystem.RootNode));
            }
            try
            {
                string[]    elements    = path.Split(PATH_SEPARATOR);
                VirtualNode currentNode = virtualFileSystem.RootNode;

                for (int i = 1; i < elements.Length; i++)
                {
                    if (currentNode.IsFile)
                    {
                        //Console.WriteLine("File, not directory");
                        return(null);
                    }
                    else
                    {
                        if (elements[i].Length != 0)
                        {
                            // Check if valid child
                            currentNode = currentNode.GetChild(elements[i]);
                            if (currentNode == null)
                            {
                                //Console.WriteLine("can't find child...");
                                return(null);
                            }
                        }
                        else
                        {
                            // Allow trailing path separator
                            if (i < elements.Length - 1)
                            {
                                //Console.WriteLine("empty path element!");
                                return(null);
                            }
                        }
                    }
                }

                return(currentNode.IsDirectory ? (FSEntry) new SimpleDirectory(currentNode) : new SimpleFile(currentNode));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught exception in simplefs.find(): " + ex.Message);
            }

            return(null);
        }
Ejemplo n.º 3
0
        public FSEntry Find(string path)
        {
            // Follow the path down and return a ne directory or file
            // wrapping the node
            // Path must be an absolute path, including the root /
            // Simplifying assumption... not allowing ".." or "." in our paths
            // good:  /foo/bar, /foo/bar/
            // bad:  foo, foo/bar, //foo/bar, /foo//bar, /foo/../foo/bar
            if (String.IsNullOrEmpty(path) || path[0] != '/')
            {
                throw new Exception("Hey! Invalid path!");
            }

            // If path ends in a trailing slash, then...
            bool mustBeDirectory = false;

            if (path.EndsWith(PATH_SEPARATOR.ToString()))
            {
                // Remove the trailing slash and remember that we expect a directory
                path            = path.Substring(0, path.Length - 1);
                mustBeDirectory = true;
            }

            // Walk through path and find the result
            string[]    elements = path.Split(PATH_SEPARATOR);
            VirtualNode current  = virtualFileSystem.RootNode;

            foreach (string element in elements.Skip(1))    // Skips the first blank
            {
                // We need to dig deeper, but we do not have a directory, so return null
                if (!current.IsDirectory)
                {
                    return(null);
                }

                try
                {
                    // Get the named child from the directory
                    VirtualNode child = current.GetChild(element);
                    current = child;
                }
                catch (Exception)
                {
                    // If we can't find the child, return null
                    return(null);
                }
            }

            if (mustBeDirectory && !current.IsDirectory)
            {
                return(null);
            }

            return(current.IsDirectory ? new SimpleDirectory(current) as FSEntry : new SimpleFile(current));
        }