示例#1
0
        public void Remove(Urn urn)
        {
            if (!DoesCacheExist())
            {
                return;
            }

            using FileStream cache    = CacheDatabase.Open(FileMode.Open);
            using BinaryReader reader = new(cache);
            using BinaryWriter writer = new(cache);
            // Skip file header
            reader.BaseStream.Seek(FILE_MAGIC.Length, SeekOrigin.Begin);

            // Loop through items in cache until target URN is found
            byte[] targetUrnBytes = Encoding.Default.GetBytes(urn.ToString());
            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                // Read entry
                long entryPos = writer.BaseStream.Position;
                if (reader.ReadByte() != byte.MaxValue)
                {
                    continue;
                }

                CacheEntry entry = CacheEntry.ReadFromStream(reader);

                // Check for equality
                if (entry.urnBytes.SequenceEqual(targetUrnBytes))
                {
                    // Delete cached file, if it exists
                    var downloadItem = entry.GetDownloadItem();
                    if (downloadItem.Exists)
                    {
                        downloadItem.RecursiveDelete();
                    }

                    // Zero out entry, acts like a NOP slide
                    writer.BaseStream.Seek(entryPos, SeekOrigin.Begin);
                    for (int i = 0; i < entry.Length; i++)
                    {
                        writer.Write(byte.MinValue);
                    }

                    break;
                }
            }
        }