예제 #1
0
        public uint WriteFile(Inode inode, string data) //перезаписывает файл целиком //возвращает успешность
        {
            if (!inode.isCanWrite(currUserId, currGroupId) || inode.Type != 1 || inode.ReadOnly)
            {
                return(0);
            }
            List <uint> fileAddr = ReadFile(inode.FirstDataBlockNum);

            if ((data.Length - fileAddr.Count * 2044) / 2044 > Sb.Dfree) //нет свободных блоков данных
            {
                return(0);
            }
            //пометить не исопльзуемые блоки данных как пустые
            for (int i = data.Length / 2045; i < fileAddr.Count - 1; i++)
            {
                BlockBitmap[(uint)(fileAddr.Count - 1)] = false;
                Sb.Dfree++;
            }
            //выделение новых блоков данных
            for (int i = fileAddr.Count - 1; i < data.Length / 2045; i++)
            {
                uint nextAddr = BlockBitmap.SeekFree();
                BlockBitmap[nextAddr] = true;
                Sb.Dfree--;
                fileAddr.Add(nextAddr);
            }
            for (int i = 0; i < fileAddr.Count; i++)
            {
                byte[] b = new byte[2048];
                Array.Copy(BitConverter.GetBytes(i == fileAddr.Count - 1 ? 0 : fileAddr[i + 1]), 0, b, 0, 4);
                string subStr = data.Substring(i * 2044, data.Length - 2044 * i > 2044 ? 2044 : data.Length - 2044 * i);
                Array.Copy(Encoding.GetEncoding(1251).GetBytes(subStr), 0, b, 4, subStr.Length);
                WriteBytes(b, (int)(Sb.Ssize * Sb.DataBlocksAddr() + Sb.Ssize * fileAddr[i]));
            }
            inode.ModifyDate = DateConverter.ToInt32(DateTime.Now);
            WriteChanges(inode);
            WriteChanges();
            return(1);
        }
예제 #2
0
        public bool RemoveDirectory(Inode parentInode, string name)
        {
            Directory parentDir = ReadDirectory(parentInode);

            if (!parentDir.Сontent.ContainsKey(name))
            {
                return(false);
            }
            Inode     inode = ReadInode(parentDir.Сontent[name]);
            Directory dir   = ReadDirectory(inode);

            foreach (KeyValuePair <string, uint> entry in dir.Сontent)
            {
                if (entry.Key == "." || entry.Key == "..")
                {
                    continue;
                }
                Inode i = ReadInode(entry.Value);
                if (i.Type == 2)
                {
                    RemoveDirectory(inode, entry.Key);
                }
                else
                {
                    RemoveFile(inode, entry.Key);
                }
            }

            DeleteDataBlock(inode.FirstDataBlockNum);
            DeleteInode(inode);
            //здесь должны быть мемы с >63, но их нет
            parentDir.Сontent.Remove(name);
            parentInode.ModifyDate = DateConverter.ToInt32(DateTime.Now);
            WriteChanges(parentInode);
            WriteChanges(parentDir, parentInode.InodeId);
            WriteChanges(dir, inode.FirstDataBlockNum);
            return(true);
        }
예제 #3
0
        public uint CreateFile(Inode parentDirInode, string name)//возвращает адрес инода
        {
            Directory parentDir = ReadDirectory(parentDirInode);

            if (!parentDirInode.isCanWrite(currUserId, currGroupId) || parentDirInode.Type != 2 || name.Length == 0 ||
                parentDir.Сontent.ContainsKey(name) || Sb.Dfree == 0 || Sb.Ifree == 0)
            {
                return(0);
            }
            uint  newInodeNum = InodeBitmap.SeekFree();
            Inode newInode    = new Inode(1, newInodeNum, currUserId, currGroupId, BlockBitmap.SeekFree(), 644);

            InodeBitmap[newInodeNum]            = true;
            BlockBitmap[BlockBitmap.SeekFree()] = true;
            Sb.Ifree--;
            Sb.Dfree--;
            WriteBytes(new byte[Sb.Ssize], (int)(Sb.Ssize * Sb.DataBlocksAddr() + Sb.Ssize * newInode.FirstDataBlockNum));
            parentDir.Сontent.Add(name, newInode.InodeId);
            parentDirInode.ModifyDate = DateConverter.ToInt32(DateTime.Now);
            WriteChanges(parentDirInode);
            WriteChanges(parentDir, parentDirInode.FirstDataBlockNum);
            WriteChanges(newInode);
            return(newInode.InodeId);
        }