public bool RemoveFile(Inode parentDirInode, string name) { Directory parentDir = ReadDirectory(parentDirInode); if (!parentDir.Сontent.ContainsKey(name)) { return(false); } Inode inode = ReadInode(parentDir.Сontent[name]); if (inode.Type != 1 || inode.System) { return(false); } List <uint> addrFile = ReadFile(inode.FirstDataBlockNum); foreach (uint addr in addrFile) { DeleteDataBlock(addr); } DeleteInode(inode); parentDir.Сontent.Remove(name); parentDirInode.ModifyDate = DateConverter.ToInt32(DateTime.Now); WriteChanges(parentDirInode); WriteChanges(parentDir, parentDirInode.FirstDataBlockNum); return(true); }
private uint CopyDirectory(Inode srcInode, Inode dstParentDirInode, string name) { Directory srcDir = ReadDirectory(srcInode); Directory dstParentDir = ReadDirectory(dstParentDirInode); if (dstParentDir.Сontent.ContainsKey(name)) { return(0); } Inode i = ReadInode(CreateDirectory(dstParentDirInode, name)); foreach (KeyValuePair <string, uint> entry in srcDir.Сontent) { Inode curr = ReadInode(entry.Value); if (curr.Type == 1) { CopyFile(curr, i, entry.Key); } if (curr.Type == 2) { CopyDirectory(curr, i, entry.Key); } } return(0); }
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); }
private void DeleteInode(Inode inode) { inode.Type = 0; InodeBitmap[inode.InodeId] = false; Sb.Ifree++; WriteChanges(inode); }
public static void ChangeDir(string[] args) { if (args.Length != 1) { Console.WriteLine("Wrong syntax"); return; } Inode newInode = fs.ReadInode(getLastInode(args[0])); if (newInode.Type == 2 || newInode.isCanRead(fs.currUserId, fs.currGroupId)) { CurrDirectoryInode = newInode.InodeId; if (CurrDirectoryInode == 1) { Path = "/"; return; } switch (args[0]) { case ".": break; case "..": Inode i = fs.ReadInode(getLastInode(args[0])); Path = fs.ReadDirectory(i).Сontent.FirstOrDefault(x => x.Value == CurrDirectoryInode).Key; break; default: Path = args[0].Split('/').Last(); break; } } }
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 Copy(Inode srcInode, Inode dstParentDirInode, string name) { if (dstParentDirInode.Type != 2) { return(0); } switch (srcInode.Type) { case 1: return(CopyFile(srcInode, dstParentDirInode, name)); case 2: return(CopyDirectory(srcInode, dstParentDirInode, name)); default: return(0); } }
public string OpenFile(uint inodeNum) { string data = ""; Inode inode = ReadInode(inodeNum); if (!inode.isCanRead(currUserId, currGroupId)) { return("Cannot read file due to insufficient permissions"); } List <uint> fileAddr = ReadFile(inode.FirstDataBlockNum); foreach (uint addr in fileAddr) { data += Encoding.GetEncoding(1251).GetString(ReadBytes(2044, (int)(Sb.Ssize * Sb.DataBlocksAddr() + Sb.Ssize * addr + 4))).Split('\0')[0]; } return(data); }
public Directory ReadDirectory(Inode inode) { Directory dir = new Directory(); if (inode.Type != 2) { return(dir); } uint nextDataBlockNum = inode.FirstDataBlockNum; while (nextDataBlockNum != 0) { byte[] data = ReadBytes((int)Sb.Ssize, (int)(Sb.DataBlocksAddr() * Sb.Ssize + inode.FirstDataBlockNum * Sb.Ssize)); dir.Set(data); byte[] tempDataBlockNum = new byte[4]; Array.Copy(data, tempDataBlockNum, 4); nextDataBlockNum = BitConverter.ToUInt32(tempDataBlockNum, 0); } return(dir); }
public string[] getAllInfo(uint inodeNum) { string[] data = new string[5]; int i = 0; Inode inode = ReadInode(inodeNum); data[i++] += inode.GetFilePermissions(); data[i++] += inode.UserId; //todo выводить имя а не ид data[i++] += inode.GroupId; //todo if (inode.Type == 2) { data[i++] += "2048Б "; } if (inode.Type == 1) { data[i++] += (ReadFile(inode.FirstDataBlockNum).Count * 2048) + "Б"; } data[i++] += DateConverter.ToDateTime(inode.ModifyDate); return(data); }
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); }
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); }
public void WriteChanges(Inode inode) { WriteChanges(); WriteBytes(inode.ToBytes(), (int)(Sb.InodeAddr() * Sb.Ssize + inode.InodeId * Inode.Size)); }