Exemple #1
0
        private BobFsNode NewNode(string name, ENodeType type)
        {
            if (!IsDirectory)
            {
                throw new InvalidOperationException("Current node is not a directory.");
            }

            // Get free inum
            // TODO: Can be optimized
            // TODO: Implement caching
            int freeInum = -1;

            _bobFs.Source.ReadAll(BobFs.BlockSize * 2, _tmpBuffer, 0, BobFs.BlockSize);
            BitArray inodeBitmap = new BitArray(_tmpBuffer);

            for (int index = 0; index < BobFs.BlockSize * 8 && freeInum == -1; index++)
            {
                if (!inodeBitmap[index])
                {
                    freeInum = index;
                }
            }
            if (freeInum == -1)
            {
                throw new Exception("No free Inode.");
            }
            inodeBitmap[freeInum] = true;
            inodeBitmap.CopyTo(_tmpBuffer, 0);
            _bobFs.Source.WriteAll(BobFs.BlockSize * 2, _tmpBuffer, 0, BobFs.BlockSize);

            // Add to directory
            DirEntry newDirEntry = new DirEntry();

            newDirEntry.Inum = (uint)freeInum;
            newDirEntry.Name = name;
            int bytesWritten = newDirEntry.WriteTo(this, (int)Size);

            if (bytesWritten < 0)
            {
                throw new Exception("Current directory is full.");
            }

            // Create inode at inum
            BobFsNode newInode = new BobFsNode(_bobFs, (uint)freeInum);

            newInode.Type     = type;
            newInode.NumLinks = 1;
            newInode.Size     = 0;
            newInode.Commit();

            return(newInode);
        }
Exemple #2
0
        public void LinkNode(string name, BobFsNode file)
        {
            if (!IsDirectory)
            {
                throw new InvalidOperationException("Current node is not a directory.");
            }

            // Add to directory
            DirEntry newDirEntry = new DirEntry();

            newDirEntry.Inum = file.Inum;
            newDirEntry.Name = name;
            int bytesWritten = newDirEntry.WriteTo(this, (int)Size);

            if (bytesWritten < 0)
            {
                throw new Exception("Current directory is full.");
            }

            // Update nlinks in node
            file.Node.NumLinks++;
            file.Commit();
        }