Ejemplo n.º 1
0
        public void FindSourceFiles()
        {
            var parFiles = Directory.EnumerateFiles(TestDir)
                           .Select(x => new FileInfo(x))
                           .Where(x => x.Extension == ".par2")
                           .Select(x => RecoveryFile.FromUri(new Uri(x.FullName)))
                           .GroupBy(x => x.Name)
                           .FirstOrDefault();

            var sourceFiles = parFiles.SelectMany(x => x.GetSourceList()).Distinct(x => x.ID).ToList();

            sourceFiles.ForEach(x =>
            {
                var location = new Uri(new Uri(TestDir + "\\", UriKind.Absolute), x.Location);
                if (File.Exists(location.AbsolutePath))
                {
                    x.Location = location;
                }
            });

            var testPath = Path.Combine(TestDir, @"test\VK5v0qUjGhM0SvLLqYOfRjXY043XG04JVYRKjmKYj.part001.rar.tmp");

            if (sourceFiles.First().Equals(File.Open(testPath, FileMode.Open, FileAccess.Read)))
            {
                sourceFiles.First().Location = new Uri(testPath, UriKind.Absolute);
            }

            Assert.AreNotEqual(0, sourceFiles.Count());
        }
Ejemplo n.º 2
0
 private void DisposeIfNotMemberVariable(RecoveryFile recoveryFile)
 {
     if (_recoveryFile == null)
     {
         recoveryFile.Dispose();
     }
 }
Ejemplo n.º 3
0
        public void LoadRecoverySetWithNonExistingRelativePaths()
        {
            Environment.CurrentDirectory = TestDir + @"..";
            var parFiles = Directory.EnumerateFiles(TestDir)
                           .Select(x => new FileInfo(x))
                           .Where(x => x.Extension == ".par2")
                           .Select(x => RecoveryFile.FromUri(new Uri(x.Name, UriKind.Relative)))
                           .GroupBy(x => x.Name)
                           .FirstOrDefault();

            Assert.AreEqual(false, parFiles.First().Location.IsAbsoluteUri);
        }
Ejemplo n.º 4
0
        public void LoadRecoverySetWithAbsolutePaths()
        {
            var parFiles = Directory.EnumerateFiles(TestDir)
                           .Select(x => new FileInfo(x))
                           .Where(x => x.Extension == ".par2")
                           .Select(x => RecoveryFile.FromUri(new Uri(x.FullName)))
                           .GroupBy(x => x.Name)
                           .FirstOrDefault();

            Assert.AreEqual(true, parFiles.First().Location.IsAbsoluteUri);
            Assert.AreEqual(TestDir, new FileInfo(parFiles.First().Location.AbsolutePath).DirectoryName);
        }
Ejemplo n.º 5
0
        public void CorrectlyRecoverSequentiallyUpdatedPage()
        {
            long index;

            using (var fileSystemPageManager = new FileSystemPageManager(4096))
                using (var storage = new Storage(fileSystemPageManager))
                {
                    storage.OpenOrCreate(StoragePath);
                    var page = fileSystemPageManager.CreatePage();
                    index = page.Index;
                }

            string fileName;

            using (var fileSystemPageManager = new FileSystemPageManager(4096))
                using (var storage = new Storage(fileSystemPageManager))
                    using (var recoveryFile = new RecoveryFile(fileSystemPageManager, fileSystemPageManager.PageSize))
                    {
                        storage.OpenOrCreate(StoragePath);
                        var page = fileSystemPageManager.FetchPage(index);
                        index = page.Index;
                        fileSystemPageManager.Dispose();

                        Assert.AreEqual(index, page.Index);
                        Assert.AreNotEqual(0xff, page.Content[10]);

                        // Update twice with different bytes to make sure the latest one wins
                        page.Content[9]  = 0xaa;
                        page.Content[10] = 0xaa;
                        recoveryFile.WriteUpdatePageRecord(page);
                        page.Content[10] = 0xff;
                        recoveryFile.WriteUpdatePageRecord(page);

                        recoveryFile.WriteFinalMarker();
                        fileName = recoveryFile.FileName;
                    }

            Assert.True(File.Exists(fileName));
            Assert.GreaterOrEqual(new FileInfo(fileName).Length, 1);
            Assert.AreNotEqual(index, 0);

            using (var manager = new FileSystemPageManager(4096))
                using (var storage = new Storage(manager))
                {
                    storage.OpenOrCreate(StoragePath);
                    var page = manager.FetchPage(index);
                    Assert.AreEqual(index, page.Index);
                    Assert.AreEqual(0xaa, page.Content[9]);
                    Assert.AreEqual(0xff, page.Content[10]);
                }
        }
Ejemplo n.º 6
0
        public void CreateRecoveryFileFromAbsoluteUri()
        {
            Uri uri = new Uri(new Uri(TestDir), TestFile);

            var r = RecoveryFile.FromUri(uri);

            Assert.AreEqual(true, r.Location.IsAbsoluteUri);
            Assert.AreEqual(
                uri,
                r.Location);
            Assert.AreEqual(
                new Range <long> {
                Minimum = 1, Maximum = 2
            },
                r.Exponents);
        }
Ejemplo n.º 7
0
        public void CreateRecoveryFileFromRelativeUri()
        {
            Uri uri = new Uri(TestFile, UriKind.Relative);

            var r = RecoveryFile.FromUri(uri);

            Assert.AreEqual(false, r.Location.IsAbsoluteUri);
            Assert.AreEqual(
                uri,
                r.Location);
            Assert.AreEqual(
                new Range <long> {
                Minimum = 1, Maximum = 2
            },
                r.Exponents);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the FileSystemPageManager.
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="forcedWrites"></param>
        /// <param name="writeBufferSize">The size of buffer (in pages) using to async write changes to disk. Async writing is not applied when forcedWrites is true.</param>
        /// <param name="useRecoveryFile"></param>
        internal FileSystemPageManager(int pageSize, bool forcedWrites, int writeBufferSize, bool useRecoveryFile)
        {
            if (pageSize < 4096)
            {
                throw new ArgumentOutOfRangeException(nameof(pageSize), "Too small page size");
            }

            _pageSize        = pageSize;
            ForcedWrites     = forcedWrites;
            _writeBufferSize = forcedWrites || writeBufferSize < 1 ? 1 : writeBufferSize;

            _pagemap = new PageMap(this);
            if (useRecoveryFile)
            {
                _recoveryFile = new RecoveryFile(this, pageSize);
            }
        }
Ejemplo n.º 9
0
        public void CorrectlyApplyDeleteRecord()
        {
            long deleteIndex;

            using (var fileSystemPageManager = new FileSystemPageManager(4096))
                using (var storage = new Storage(fileSystemPageManager))
                {
                    storage.OpenOrCreate(StoragePath);
                    deleteIndex = fileSystemPageManager.CreatePage().Index;
                }

            using (var fileSystemPageManager = new FileSystemPageManager(4096))
                using (var storage = new Storage(fileSystemPageManager))
                {
                    storage.OpenOrCreate(StoragePath);
                    Assert.True(fileSystemPageManager.PageExists(deleteIndex));
                }

            string fileName;

            using (var fileSystemPageManager = new FileSystemPageManager(4096))
                using (var storage = new Storage(fileSystemPageManager))
                    using (var recoveryFile = new RecoveryFile(fileSystemPageManager, fileSystemPageManager.PageSize))
                    {
                        storage.OpenOrCreate(StoragePath);
                        fileSystemPageManager.Dispose();

                        recoveryFile.WriteDeletePageRecord(deleteIndex);

                        recoveryFile.WriteFinalMarker();
                        fileName = recoveryFile.FileName;
                    }

            Assert.True(File.Exists(fileName));
            Assert.GreaterOrEqual(new FileInfo(fileName).Length, 1);

            using (var manager = new FileSystemPageManager(4096))
                using (var storage = new Storage(manager))
                {
                    storage.OpenOrCreate(StoragePath);
                    Assert.False(manager.PageExists(deleteIndex));
                    Assert.AreEqual(deleteIndex, manager.CreatePage().Index);
                }
        }
Ejemplo n.º 10
0
        public void CorrectlyRecoverResurrectedPage()
        {
            long index;
            long resurrectIndex;

            using (var fileSystemPageManager = new FileSystemPageManager(4096))
                using (var storage = new Storage(fileSystemPageManager))
                {
                    storage.OpenOrCreate(StoragePath);
                    var page = fileSystemPageManager.CreatePage();
                    index = page.Index;
                }

            using (var fileSystemPageManager = new FileSystemPageManager(4096))
                using (var storage = new Storage(fileSystemPageManager))
                {
                    storage.OpenOrCreate(StoragePath);
                    var page = fileSystemPageManager.FetchPage(index);
                    Assert.AreEqual(index, page.Index);
                    Assert.AreNotEqual(0xff, page.Content[10]);
                }

            string fileName;

            using (var fileSystemPageManager = new FileSystemPageManager(4096))
                using (var storage = new Storage(fileSystemPageManager))
                    using (var recoveryFile = new RecoveryFile(fileSystemPageManager, fileSystemPageManager.PageSize))
                    {
                        storage.OpenOrCreate(StoragePath);
                        var page = fileSystemPageManager.FetchPage(index);
                        index = page.Index;

                        page.Content[10] = 0xaf;

                        fileSystemPageManager.RemovePage(index);
                        var resurrectedPage = fileSystemPageManager.CreatePage();
                        resurrectIndex = resurrectedPage.Index;

                        //check we're not going wrong way
                        Assert.AreEqual(index, resurrectIndex);

                        resurrectedPage.Content[10] = 0xff;
                        Assert.AreNotEqual(0xff, page.Content[10]);

                        fileSystemPageManager.Dispose();

                        recoveryFile.WriteUpdatePageRecord(page);
                        recoveryFile.WriteDeletePageRecord(index);
                        recoveryFile.WriteUpdatePageRecord(resurrectedPage);

                        recoveryFile.WriteFinalMarker();

                        fileName = recoveryFile.FileName;
                    }

            Assert.True(File.Exists(fileName));
            Assert.GreaterOrEqual(new FileInfo(fileName).Length, 1);

            using (var manager = new FileSystemPageManager(4096))
                using (var storage = new Storage(manager))
                {
                    storage.OpenOrCreate(StoragePath);
                    var page = manager.FetchPage(index);
                    Assert.AreEqual(resurrectIndex, page.Index);
                    Assert.AreEqual(0xff, page.Content[10]);
                }
        }