Esempio n. 1
0
        public DfsFileStream(DragonFs fs, DfsDirectoryEntry root, FileAccess access)
        {
            _Fs   = fs;
            _Root = root;
            if (root.FilePointer != 0)
            {
                if ((access == FileAccess.Write) || (access == FileAccess.ReadWrite))
                {
                    throw new NotSupportedException("Writing an existing file is not supported");
                }

                // Create MemoryStream from existing data
                _MemStream = new MemoryStream((int)(_Root.Flags & ~DfsDirectoryEntry.FLAG_MASK));

                int  remainingBytes = (int)Length;
                uint offset         = root.FilePointer;
                while (remainingBytes > 0)
                {
                    // Fetch a sector and write to the stream
                    DfsSector sector      = _Fs.FindSector(offset);
                    int       sectorBytes = (int)DfsSector.SECTOR_SIZE;
                    if (sectorBytes >= remainingBytes)
                    {
                        sectorBytes = remainingBytes;
                    }
                    _MemStream.Write(sector.Buffer, 0, sectorBytes);

                    remainingBytes -= sectorBytes;
                    offset         += DfsSector.SECTOR_SIZE;
                }
                _MemStream.Seek(0, SeekOrigin.Begin);
            }
            else
            {
                if (access == FileAccess.Read)
                {
                    throw new NotSupportedException("Cannot read file without content");
                }

                // Create MemoryStream for all file operations
                _MemStream = new MemoryStream();
            }

            switch (access)
            {
            case FileAccess.Read:
                _CanRead = true;
                break;

            case FileAccess.ReadWrite:
                _CanRead  = true;
                _CanWrite = true;
                break;

            case FileAccess.Write:
                _CanWrite = true;
                break;
            }
        }
Esempio n. 2
0
        private DfsSector NewSector()
        {
            DfsSector sector = new DfsSector(_NextOffset);

            _SectorList.Add(sector);
            _NextOffset += sector.Size;
            return(sector);
        }
Esempio n. 3
0
        public uint NewBlob(int size)
        {
            if (size <= 0)
            {
                throw new NotSupportedException("Cannot create blob with no data");
            }

            uint firstOffset = 0;

            while (size > 0)
            {
                DfsSector sector = NewSector();
                if (firstOffset == 0)
                {
                    firstOffset = sector.Offset;
                }
                size -= (int)DfsSector.SECTOR_SIZE;
            }
            return(firstOffset);
        }
Esempio n. 4
0
        public static DragonFs CreateFromStream(Stream source)
        {
            DragonFs  newFs  = new DragonFs();
            DfsSector sector = new DfsSector(0);

            source.Read(sector.Buffer, 0, (int)DfsSector.SECTOR_SIZE);
            DfsDirectoryEntry root = new DfsDirectoryEntry(sector);
            bool checkValid        = true;

            if ((root.Flags != ROOT_FLAGS) || (root.NextEntry != ROOT_NEXTENTRY))
            {
                checkValid = false;
            }
            else
            {
                // Check DFS version string
                if (!root.Path.Equals(DragonFs.ROOT_PATH))
                {
                    checkValid = false;
                }
            }

            if (checkValid == false)
            {
                throw new ArgumentException("source", "The stream does not contain a valid DFS 2.0 image");
            }

            while (source.Position != source.Length)
            {
                sector = newFs.NewSector();
                source.Read(sector.Buffer, 0, (int)DfsSector.SECTOR_SIZE);
            }

            // The virtual root directory entry needs to point to the first sector
            // TODO: Can this be done better?
            newFs._RootDirectory.FilePointer = DfsSector.SECTOR_SIZE;

            return(newFs);
        }
Esempio n. 5
0
 public DfsDirectoryEntry(DfsSector sector)
 {
     _Sector = sector;
 }