private void RemoveFile(FileEntry parent, FileEntry fileEntry) { var block = fileEntry.StartingBlockNum; for (var i = 0; i < fileEntry.BlocksForFile; i++) { var he = GetHashEntry(block); he.Status = BlockStatus.PreviouslyAllocated; if (UnallocatedHashEntries.Contains(block)) { throw new Exception("qwe"); } UnallocatedHashEntries.Add(block); block = he.NextBlock; } fileEntry.BlocksForFile = 0; fileEntry.Name = string.Empty; parent.Files.Remove(fileEntry); }
public void ReplaceFile(FileEntry fileEntry, byte[] content) { if (fileEntry.IsDirectory) { throw new NotSupportedException("Directories doesn't have content!"); } var remaining = content.Length; var allocatedBlockCount = fileEntry.BlocksForFile; var newBlockCount = (content.Length + 0xFFF) / 0x1000; var blocks = AllocateBlocks(newBlockCount - allocatedBlockCount); var block = fileEntry.StartingBlockNum; if (allocatedBlockCount == 0) { block = blocks[0]; fileEntry.StartingBlockNum = blocks[0]; } var consecutive = true; for (var i = 0; i < newBlockCount; i++) { var pos = GetRealAddressOfBlock(block); var size = remaining > 0x1000 ? 0x1000 : remaining; remaining -= size; var buffer = new byte[0x1000]; Buffer.BlockCopy(content, i * 0x1000, buffer, 0, size); Binary.WriteBytes(pos, buffer, 0, 0x1000); var he = GetHashEntry(block); if (i < allocatedBlockCount - 1) { he.Status = BlockStatus.Allocated; if (he.NextBlock != block + 1) { consecutive = false; } block = he.NextBlock; } else if (i < newBlockCount - 1) { he.Status = BlockStatus.NewlyAllocated; block = blocks[i - allocatedBlockCount + 1]; he.NextBlock = block; if (he.NextBlock != he.Block + 1) { consecutive = false; } } else { he.Status = BlockStatus.NewlyAllocated; he.NextBlock = 0xFFFFFF; } } for (var i = newBlockCount; i < allocatedBlockCount; i++) { var he = GetHashEntry(block); if (UnallocatedHashEntries.Contains(block)) { throw new Exception("qwe"); } UnallocatedHashEntries.Add(block); he.Status = BlockStatus.PreviouslyAllocated; block = he.NextBlock; he.NextBlock = 0xFFFFFF; } fileEntry.FileSize = content.Length; fileEntry.BlocksForFile = newBlockCount; fileEntry.BlocksForFileCopy = newBlockCount; fileEntry.BlocksAreConsecutive = consecutive; }