Пример #1
0
        public bool OpenFile(string path)
        {
            bool   ret;
            string fileName;
            string filePath;

            // TODO: make separator a member of the filesystem?
            char          separator = '/';
            CoreDirectory dir       = new CoreDirectory(_coreFileSystem);

            filePath = path;

            for (;;)
            {
                //
                // find the last / in the path.  we will split the string based
                // on this.  we will then have '/path/to/file/dir' and 'file_name'
                //
                string[] parts = filePath.Split(separator);

                if (parts.Length <= 1)
                {
                    ret = false;
                    break;
                }
                fileName = parts[parts.Length - 1];

                //
                // open the directory that contains our file
                //
                ret = dir.OpenDirectory(filePath);

                if (!ret)
                {
                    break;
                }

                //
                // enumerate through the directory contents to find our file
                //
                while (dir.EnumerateDirectory(ref _dirEntry))
                {
                    //
                    // we found the file we're looking for, seek to the file
                    // location in the filesystem and then we should be ready
                    // to start reading from the file
                    //
                    unsafe
                    {
                        string dirEntryFileName = "";
                        fixed(byte *fileNameFixed = _dirEntry.fileName)
                        {
                            dirEntryFileName = Marshal.PtrToStringAnsi(new IntPtr(fileNameFixed));
                        }

                        if (dirEntryFileName == fileName)
                        {
                            ret = _coreFileSystem.SeekToBlock(_dirEntry.copies, false);

                            if (!ret)
                            {
                                break;
                            }

                            //
                            // we're ready to read from the file
                            //
                            _endOfFile = false;
                            break;
                        }
                    }
                }

                break;
            }

            return(ret);
        }
Пример #2
0
        public bool FindInCurrentDirectory(string item, ref CoreDirectoryEntry dirEntry)
        {
            bool ret;
            bool found = false;
            CoreDirectoryEntry newEntry;
            CoreDirectoryEntry currentEntry;

            //
            // can't do much of anything if we don't have a current directory
            //
            if (_dirTree.Count <= 0)
            {
                return(false);
            }

            //
            // if the desired directory is '..', then we want to go up a dir.
            // get the parent dir and adjust the current path
            //

            // TODO string comp
            if (item == "..")
            {
                currentEntry = _dirTree[0];
                _dirTree.RemoveAt(0);
                newEntry = _dirTree[0];

                ret = _coreFileSystem.SeekToBlock(newEntry.copies, false);

                if (!ret)
                {
                    _dirTree.Insert(0, currentEntry);
                    return(false);
                }

                ret = _coreFileSystem.ReadDirectoryHeader(ref _coreDirectoryHeader);

                if (!ret)
                {
                    _dirTree.Insert(0, currentEntry);
                    return(false);
                }

                // remove the rightmost path element from our current path
                unsafe
                {
                    IntPtr stringPointer        = new IntPtr((int)currentEntry.fileName);
                    string currentEntryFileName = Marshal.PtrToStringAnsi(stringPointer);
                    _path = _path.Substring(0, _path.Length - currentEntryFileName.Length - 1);
                }

                _endOfDir = false;

                return(true);
            }

            //
            // move the filesystem fp to the beginning of the dir and reset
            // the end of directory indicator so we can enumerate
            //

            currentEntry = _dirTree[0];
            ret          = _coreFileSystem.SeekToBlock(currentEntry.copies, false);

            if (!ret)
            {
                return(false);
            }

            ret = _coreFileSystem.ReadDirectoryHeader(ref _coreDirectoryHeader);

            if (!ret)
            {
                return(false);
            }

            _endOfDir = false;

            while (EnumerateDirectory(ref dirEntry))
            {
                string dirEntryFileName = "";
                unsafe
                {
                    fixed(byte *fileNameFixed = dirEntry.fileName)
                    {
                        dirEntryFileName = Marshal.PtrToStringAnsi(new IntPtr(fileNameFixed));
                    }
                }

                if (dirEntryFileName == item)
                {
                    //
                    // we found what we wanted.
                    // move to the beginning of the directory and read the header
                    //

                    ret = _coreFileSystem.SeekToBlock(dirEntry.copies, false);

                    if (!ret)
                    {
                        break;
                    }

                    ret = _coreFileSystem.ReadDirectoryHeader(ref _coreDirectoryHeader);

                    if (!ret)
                    {
                        break;
                    }

                    // update the directory tree
                    newEntry = dirEntry;

                    _dirTree.Insert(0, newEntry);

                    // update our current path
                    _path = _path + item + "/";

                    found = true;
                    break;
                }
            }

            if (found)
            {
                _endOfDir = false;
            }

            return(found);
        }