Exemplo n.º 1
0
        public void CreateModifiedInodes()
        {
            if (ModifiedInodes != null)
            {
                return;
            }

            ModifiedInodes = new List <BbInode>();
            foreach (var node in MainFsInodes)
            {
                var newNode = new BbInode();
                ModifiedInodes.Add(newNode.Copy(node));
            }

            ModifiedAllocTable = new List <short>();
            foreach (var alloc in MainFsAllocTable)
            {
                ModifiedAllocTable.Add(alloc);
            }

            var newSeqNo = MainFs.SeqNo + 1;

            MainFs          = new BbFat16();
            MainFs.Magic    = MAGIC_BBFS;
            MainFs.SeqNo    = newSeqNo;
            MainFs.Link     = 0;
            MainFs.CheckSum = 0;

            FsHeaders.Add(MainFs);
            FsInodes.Add(ModifiedInodes);
            FsAllocationTables.Add(ModifiedAllocTable);

            MainFsIndex = FsHeaders.Count - 1;
        }
Exemplo n.º 2
0
        public bool FileWrite(string fileName, byte[] fileData, ref BbInode newNode)
        {
            newNode            = new BbInode();
            newNode.NameString = fileName;
            newNode.Type       = 1;
            newNode.Size       = (uint)fileData.Length;

            // remove any existing file with this name
            FileDelete(newNode.NameString);

            // make sure we have a modified inodes collection ready...
            CreateModifiedInodes();

            int numBlocks = (fileData.Length + (BLOCK_SZ - 1)) / BLOCK_SZ;
            var blockList = TryAllocateBlocks(numBlocks);

            if (blockList == null)
            {
                return(false); // couldn't find enough unused blocks :(
            }
            // start writing the file data!
            for (int i = 0; i < numBlocks; i++)
            {
                int dataOffset = i * BLOCK_SZ;
                int numWrite   = BLOCK_SZ;
                if (dataOffset + numWrite > fileData.Length)
                {
                    numWrite = fileData.Length % BLOCK_SZ;
                }

                SeekToBlock(blockList[i]);
                io.Stream.Write(fileData, dataOffset, numWrite);
            }
            newNode.BlockIdx = blockList[0];

            // add new inode to inode list...
            // make seperate inode to provided one, to make sure nothing can change after adding it to inode list

            var realNewNode = new BbInode();

            realNewNode.Copy(newNode);
            ModifiedInodes.Add(realNewNode);

            var chain = GetBlockChain(newNode.BlockIdx);

            // success!
            return(true);
        }