private static void PrintFileSystem(VirtualItem itemToPrint, string offset) { var directory = itemToPrint as VirtualDirectory; Console.Write(offset + itemToPrint.Name); if (directory != null) { Console.WriteLine("/"); IEnumerable<VirtualItem> items = directory.ReadAllItems(); string newSpacing = " | " + offset; foreach (VirtualItem item in items) { PrintFileSystem(item, newSpacing); } } else { Console.WriteLine(); } }
/// <summary> /// serializes a directory item entry. /// </summary> /// <param name="item">the item to be serialized.</param> /// <param name="dataWriter">the stream the item should be serialized to.</param> private static void SerializeItem(VirtualItem item, DataWriter dataWriter) { dataWriter.WriteUInt32(item.FileNumber); dataWriter.WriteByte((byte) (item is VirtualDirectory ? ItemType.Directory : ItemType.File)); dataWriter.WriteString(item.Name); }
internal void RenameItem(VirtualItem virtualItem, string newName) { var dataWriter = new DataWriter(); foreach (VirtualItem item in ReadAllItems()) { if (item.FileNumber == virtualItem.FileNumber) { item.Name = newName; } SerializeItem(item, dataWriter); } FileStream stream = GetFileStream(); byte[] data = dataWriter.GetData(); if (stream.Length < data.Length + 8) { ContainerFile.ChangeFileSize(FileNumber, (uint) (data.Length + 8)); } stream = GetFileStream(); stream.Write(BitConverter.GetBytes((ulong) data.Length)); stream.Write(data); }
/// <summary> /// removes an item entry from this directory. /// </summary> /// <param name="itemToRemove">the item to remove.</param> internal void RemoveItem(VirtualItem itemToRemove) { IEnumerable<VirtualItem> items = ReadAllItems(); var dataWriter = new DataWriter(); foreach (VirtualItem remainingItem in items.Where(item => item.FileNumber != itemToRemove.FileNumber)) { SerializeItem(remainingItem, dataWriter); } FileStream stream = GetFileStream(); byte[] data = dataWriter.GetData(); stream.Write(BitConverter.GetBytes((ulong) data.Length)); stream.Write(data); }
/// <summary> /// deletes an item entry from this directory. /// </summary> /// <param name="itemToDelete">the item to delete.</param> internal void DeleteItem(VirtualItem itemToDelete) { RemoveItem(itemToDelete); ContainerFile.DeleteFile(itemToDelete.FileNumber); }
/// <summary> /// adds an item to this directory. /// </summary> /// <param name="itemToAdd">the item to add.</param> internal void AddItem(VirtualItem itemToAdd) { FileStream fileStream = GetFileStream(); long currentSize = BitConverter.ToInt64(fileStream.Read(8), 0); var dataWriter = new DataWriter(); SerializeItem(itemToAdd, dataWriter); byte[] data = dataWriter.GetData(); long newSize = currentSize + data.Length; if (fileStream.Length < newSize + 8) { ContainerFile.ChangeFileSize(FileNumber, (uint) (newSize + 8)); } fileStream = GetFileStream(); fileStream.Write(BitConverter.GetBytes(newSize)); fileStream.Seek(currentSize + 8, SeekOrigin.Begin); fileStream.Write(data); }