public uint CreateDirectory(Inode parentInode, string name)//возвращает адрес инода { Directory parentDir = ReadDirectory(parentInode); if (!parentInode.isCanWrite(currUserId, currGroupId) || parentInode.Type != 2 || name.Length == 0 || parentDir.Сontent.ContainsKey(name) || Sb.Dfree == 0 || Sb.Ifree == 0 || parentInode.ReadOnly) { return(0); } uint newInodeNum = InodeBitmap.SeekFree(); Inode newInode = new Inode(2, newInodeNum, currUserId, currGroupId, BlockBitmap.SeekFree()); InodeBitmap[newInodeNum] = true; BlockBitmap[BlockBitmap.SeekFree()] = true; Sb.Ifree--; Sb.Dfree--; Directory newDir = new Directory(parentInode.InodeId, newInodeNum); parentDir.Сontent.Add(name, newInode.InodeId); parentInode.ModifyDate = DateConverter.ToInt32(DateTime.Now); WriteChanges(parentInode); WriteChanges(parentDir, parentInode.InodeId); WriteChanges(newInode); WriteChanges(newDir, newInode.FirstDataBlockNum); return(newInode.InodeId); }
public bool ChangeMode(uint inodeNum, int rwx) { Inode inode = ReadInode(inodeNum); if (inode.Type == 0 || !inode.isCanWrite(currUserId, currGroupId)) { return(false); } inode.Chmod(rwx); WriteChanges(inode); return(true); }
private uint CopyFile(Inode srcInode, Inode dstParentDirInode, string name) { Directory dstParent = ReadDirectory(dstParentDirInode); if (!dstParentDirInode.isCanWrite(currUserId, currGroupId) || dstParent.Сontent.ContainsKey(name)) { return(0); } uint newInodeNum = CreateFile(dstParentDirInode, name); WriteFile(ReadInode(newInodeNum), OpenFile(srcInode.InodeId)); return(newInodeNum); }
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); }