private void AddFileSystemButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var parent = GetSelectedSource();

                var dialog = new System.Windows.Forms.FolderBrowserDialog();
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var directory = new DirectoryInfo(dialog.SelectedPath);
                    var source    = new FileSystemSource()
                    {
                        Name = directory.Name, Path = dialog.SelectedPath, Parent = parent
                    };
                    trackController.LoadDirectory(directory);

                    sourceController.Save(source);

                    if (parent != null)
                    {
                        parent.AddChild(source);
                    }
                    else
                    {
                        boundSources.Add(source);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("SourceView.AddFileSystemButton_Click", ex);
            }
        }
Esempio n. 2
0
        public void GetFileLength()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            ntfs.OpenFile(@"AFILE.TXT", FileMode.Create).Dispose();
            Assert.Equal(0, ntfs.GetFileLength("AFILE.TXT"));

            using (var stream = ntfs.OpenFile(@"AFILE.TXT", FileMode.Open))
            {
                stream.Write(new byte[14325], 0, 14325);
            }
            Assert.Equal(14325, ntfs.GetFileLength("AFILE.TXT"));

            using (var attrStream = ntfs.OpenFile(@"AFILE.TXT:altstream", FileMode.Create))
            {
                attrStream.Write(new byte[122], 0, 122);
            }
            Assert.Equal(122, ntfs.GetFileLength("AFILE.TXT:altstream"));


            // Test NTFS options for hardlink behaviour
            ntfs.CreateDirectory("Dir");
            ntfs.CreateHardLink("AFILE.TXT", @"Dir\OtherLink.txt");

            using (var stream = ntfs.OpenFile("AFILE.TXT", FileMode.Open, FileAccess.ReadWrite))
            {
                stream.SetLength(50);
            }
            Assert.Equal(50, ntfs.GetFileLength("AFILE.TXT"));
            Assert.Equal(14325, ntfs.GetFileLength(@"Dir\OtherLink.txt"));

            ntfs.NtfsOptions.FileLengthFromDirectoryEntries = false;

            Assert.Equal(50, ntfs.GetFileLength(@"Dir\OtherLink.txt"));
        }
Esempio n. 3
0
        public void GetObjects()
        {
            var fileSystem = new MockFileSystem();

            fileSystem.RegisterDirectory(@"C:\dir");
            fileSystem.RegisterDirectory(@"C:\dir\dir1");
            fileSystem.RegisterFile(@"C:\dir\file1");
            fileSystem.RegisterFile(@"C:\file2");

            var serviceContainer = new ManualServiceContainer();

            serviceContainer.Register <IFileSystem>(fileSystem);

            var executionContext = new ExecutionContext(serviceContainer);

            var roots = new IFileSystemRoot[] {
                new DirectoryRoot(@"C:\dir", fileSystem),
                new FileRoot(@"C:\file2", fileSystem)
            };

            var source         = new FileSystemSource(roots);
            var sourceInstance = source.CreateInstance(executionContext);

            var backupContext = new BackupContext(null, executionContext);
            var result        = sourceInstance.GetObjects(backupContext).ToArray();

            var expected = new IObject[] {
                new DirectoryObject(@"C:\dir", fileSystem),
                new FileObject(@"C:\dir\file1", fileSystem),
                new DirectoryObject(@"C:\dir\dir1", fileSystem),
                new FileObject(@"C:\file2", fileSystem)
            };

            CollectionAssert.AreEqual(expected, result);
        }
Esempio n. 4
0
        public void FindConnectionStrings()
        {
            var basePath   = TestHelper.GetBasePath("ConnStrings");
            var baseSource = new FileSystemSource(basePath);
            var files      = baseSource.GetFiles();

            var            module        = CreateDatabaseHeuristicsModule();
            ICommonResults commonResults = module.Execute(files);

            Assert.IsNotNull(commonResults);
            Assert.IsNotNull(commonResults.Results);
            Assert.IsTrue(commonResults.Results.Any());
            Assert.AreEqual(commonResults.Results.Count(), 4);

            var msList = commonResults.Results.Where(r => r.Report.Contains("MSSQL")).ToList();

            Assert.IsTrue(msList.Any());
            Assert.AreEqual(msList.Count, 2);
            Assert.IsTrue(msList.All(r => r.File.GetFileName().ToLower().Contains("_ms")));

            var oraList = commonResults.Results.Where(r => r.Report.Contains("Oracle")).ToList();

            Assert.IsTrue(oraList.Any());
            Assert.AreEqual(oraList.Count, 2);
            Assert.IsTrue(oraList.All(r => r.File.GetFileName().ToLower().Contains("_ora")));
        }
Esempio n. 5
0
        public void ShortNames()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            // Check we can find a short name in the same directory
            using (Stream s = ntfs.OpenFile("ALongFileName.txt", FileMode.CreateNew)) {}
            ntfs.SetShortName("ALongFileName.txt", "ALONG~01.TXT");
            Assert.Equal("ALONG~01.TXT", ntfs.GetShortName("ALongFileName.txt"));
            Assert.True(ntfs.FileExists("ALONG~01.TXT"));

            // Check path handling
            ntfs.CreateDirectory("DIR");
            using (Stream s = ntfs.OpenFile(@"DIR\ALongFileName2.txt", FileMode.CreateNew)) { }
            ntfs.SetShortName(@"DIR\ALongFileName2.txt", "ALONG~02.TXT");
            Assert.Equal("ALONG~02.TXT", ntfs.GetShortName(@"DIR\ALongFileName2.txt"));
            Assert.True(ntfs.FileExists(@"DIR\ALONG~02.TXT"));

            // Check we can open a file by the short name
            using (Stream s = ntfs.OpenFile("ALONG~01.TXT", FileMode.Open)) { }

            // Delete the long name, and make sure the file is gone
            ntfs.DeleteFile("ALONG~01.TXT");
            Assert.False(ntfs.FileExists("ALONG~01.TXT"));

            // Delete the short name, and make sure the file is gone
            ntfs.DeleteFile(@"DIR\ALONG~02.TXT");
            Assert.False(ntfs.FileExists(@"DIR\ALongFileName2.txt"));
        }
Esempio n. 6
0
        public void OpenRawStream()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

#pragma warning disable 618
            Assert.Null(ntfs.OpenRawStream(@"$Extend\$ObjId", AttributeType.Data, null, FileAccess.Read));
#pragma warning restore 618
        }
        public static IProcessingModule CreateCodeBaseProcessingModule(string basePath)
        {
            var baseSource = new FileSystemSource(basePath);
            var parser     = new CsParser();
            var algorithm  = new MinHashAlgorithm(20, 8, 4, 15, 5);

            return(new CodeBaseProcessingModule(baseSource, parser, algorithm));
        }
Esempio n. 8
0
        public void Sparse()
        {
            int fileSize = 1 * 1024 * 1024;

            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            byte[] data = new byte[fileSize];
            for (int i = 0; i < fileSize; i++)
            {
                data[i] = (byte)i;
            }

            using (SparseStream s = ntfs.OpenFile("file.bin", FileMode.CreateNew))
            {
                s.Write(data, 0, fileSize);

                ntfs.SetAttributes("file.bin", ntfs.GetAttributes("file.bin") | FileAttributes.SparseFile);

                s.Position = 64 * 1024;
                s.Clear(128 * 1024);
                s.Position = fileSize - 64 * 1024;
                s.Clear(128 * 1024);
            }

            using (SparseStream s = ntfs.OpenFile("file.bin", FileMode.Open))
            {
                Assert.Equal(fileSize + 64 * 1024, s.Length);

                List <StreamExtent> extents = new List <StreamExtent>(s.Extents);

                Assert.Equal(2, extents.Count);
                Assert.Equal(0, extents[0].Start);
                Assert.Equal(64 * 1024, extents[0].Length);
                Assert.Equal((64 + 128) * 1024, extents[1].Start);
                Assert.Equal(fileSize - (64 * 1024) - ((64 + 128) * 1024), extents[1].Length);


                s.Position = 72 * 1024;
                s.WriteByte(99);

                byte[] readBuffer = new byte[fileSize];
                s.Position = 0;
                s.Read(readBuffer, 0, fileSize);

                for (int i = 64 * 1024; i < (128 + 64) * 1024; ++i)
                {
                    data[i] = 0;
                }
                for (int i = fileSize - (64 * 1024); i < fileSize; ++i)
                {
                    data[i] = 0;
                }
                data[72 * 1024] = 99;

                Assert.Equal(data, readBuffer);
            }
        }
Esempio n. 9
0
        public void Fragmented()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            ntfs.CreateDirectory(@"DIR");

            byte[] buffer = new byte[4096];

            for (int i = 0; i < 2500; ++i)
            {
                using (var stream = ntfs.OpenFile(@"DIR\file" + i + ".bin", FileMode.Create, FileAccess.ReadWrite))
                {
                    stream.Write(buffer, 0, buffer.Length);
                }

                using (var stream = ntfs.OpenFile(@"DIR\" + i + ".bin", FileMode.Create, FileAccess.ReadWrite))
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }

            for (int i = 0; i < 2500; ++i)
            {
                ntfs.DeleteFile(@"DIR\file" + i + ".bin");
            }

            // Create fragmented file (lots of small writes)
            using (var stream = ntfs.OpenFile(@"DIR\fragmented.bin", FileMode.Create, FileAccess.ReadWrite))
            {
                for (int i = 0; i < 2500; ++i)
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }

            // Try a large write
            byte[] largeWriteBuffer = new byte[200 * 1024];
            for (int i = 0; i < largeWriteBuffer.Length / 4096; ++i)
            {
                largeWriteBuffer[i * 4096] = (byte)i;
            }
            using (var stream = ntfs.OpenFile(@"DIR\fragmented.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                stream.Position = stream.Length - largeWriteBuffer.Length;
                stream.Write(largeWriteBuffer, 0, largeWriteBuffer.Length);
            }

            // And a large read
            byte[] largeReadBuffer = new byte[largeWriteBuffer.Length];
            using (var stream = ntfs.OpenFile(@"DIR\fragmented.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                stream.Position = stream.Length - largeReadBuffer.Length;
                stream.Read(largeReadBuffer, 0, largeReadBuffer.Length);
            }

            Assert.Equal(largeWriteBuffer, largeReadBuffer);
        }
Esempio n. 10
0
        private static IProcessingModule CreateCodeBaseModule()
        {
            var basePath   = @"C:\codebase";
            var baseSource = new FileSystemSource(basePath);
            var parser     = new CsParser();
            var algorithm  = new MinHashAlgorithm(20, 4, 4, 10, 5);
            var module     = new CodeBaseProcessingModule(baseSource, parser, algorithm);

            return(module);
        }
Esempio n. 11
0
        public void GetAlternateDataStreams()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            ntfs.OpenFile("AFILE.TXT", FileMode.Create).Dispose();
            Assert.Equal(0, ntfs.GetAlternateDataStreams("AFILE.TXT").Length);

            ntfs.OpenFile("AFILE.TXT:ALTSTREAM", FileMode.Create).Dispose();
            Assert.Equal(1, ntfs.GetAlternateDataStreams("AFILE.TXT").Length);
            Assert.Equal("ALTSTREAM", ntfs.GetAlternateDataStreams("AFILE.TXT")[0]);
        }
Esempio n. 12
0
        public bool TryGetSource(string uri, out ISource source)
        {
            source = default;
            if (!Path.IsPathRooted(uri) || !File.Exists(uri))
            {
                return(false);
            }

            source = new FileSystemSource(uri);
            return(true);
        }
Esempio n. 13
0
            private void WatcherOnCreated(object sender, FileSystemEventArgs e)
            {
                var src = new FileSystemSource(e.FullPath);

                if (!_files.TryAdd(e.FullPath, src))
                {
                    return;
                }

                CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, src));
            }
        public void DeleteAlternateDataStreams()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            ntfs.OpenFile("AFILE.TXT", FileMode.Create).Close();
            ntfs.OpenFile("AFILE.TXT:ALTSTREAM", FileMode.Create).Close();
            Assert.AreEqual(1, ntfs.GetAlternateDataStreams("AFILE.TXT").Length);

            ntfs.DeleteFile("AFILE.TXT:ALTSTREAM");
            Assert.AreEqual(1, ntfs.GetFileSystemEntries("").Length);
            Assert.AreEqual(0, ntfs.GetAlternateDataStreams("AFILE.TXT").Length);
        }
Esempio n. 15
0
        public void ReparsePoints_NonEmpty()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            ntfs.CreateDirectory("dir");
            ntfs.SetReparsePoint("dir", new ReparsePoint(123, new byte[] { 4, 5, 6 }));

            ReparsePoint rp = ntfs.GetReparsePoint("dir");

            Assert.Equal(123, rp.Tag);
            Assert.NotNull(rp.Content);
            Assert.Equal(3, rp.Content.Length);
        }
Esempio n. 16
0
        private static void Execute(IProcessingModule module, string path)
        {
            var source  = new FileSystemSource(path);
            var results = module.Execute(source.GetFiles());

            foreach (var result in results.Results)
            {
                if (false == string.IsNullOrWhiteSpace(result.Report))
                {
                    Console.WriteLine(result.Report);
                }
            }
        }
        public void ReparsePoints_Empty()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            ntfs.CreateDirectory("dir");
            ntfs.SetReparsePoint("dir", new ReparsePoint(12345, new byte[0]));

            ReparsePoint rp = ntfs.GetReparsePoint("dir");

            Assert.AreEqual(12345, rp.Tag);
            Assert.IsNotNull(rp.Content);
            Assert.AreEqual(0, rp.Content.Length);
        }
Esempio n. 18
0
            private void WatcherOnRenamed(object sender, RenamedEventArgs e)
            {
                if (_files.TryRemove(e.OldFullPath, out var oldSrc))
                {
                    CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldSrc));
                }

                var newSrc = new FileSystemSource(e.FullPath);

                if (_files.TryAdd(e.FullPath, newSrc))
                {
                    CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newSrc));
                }
            }
Esempio n. 19
0
        public void DeleteShortNameDir()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            ntfs.CreateDirectory(@"\TestLongName1\TestLongName2");
            ntfs.SetShortName(@"\TestLongName1\TestLongName2", "TESTLO~1");

            Assert.True(ntfs.DirectoryExists(@"\TestLongName1\TESTLO~1"));
            Assert.True(ntfs.DirectoryExists(@"\TestLongName1\TestLongName2"));

            ntfs.DeleteDirectory(@"\TestLongName1", true);

            Assert.False(ntfs.DirectoryExists(@"\TestLongName1"));
        }
Esempio n. 20
0
        public void HasHardLink()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            using (Stream s = ntfs.OpenFile("ALongFileName.txt", FileMode.CreateNew)) { }
            Assert.False(ntfs.HasHardLinks("ALongFileName.txt"));

            ntfs.CreateHardLink("ALongFileName.txt", "AHardLink.TXT");
            Assert.True(ntfs.HasHardLinks("ALongFileName.txt"));

            using (Stream s = ntfs.OpenFile("ALongFileName2.txt", FileMode.CreateNew)) { }

            // If we enumerate short names, then the initial long name results in two 'hardlinks'
            ntfs.NtfsOptions.HideDosFileNames = false;
            Assert.True(ntfs.HasHardLinks("ALongFileName2.txt"));
        }
Esempio n. 21
0
        public void HardLinkCount()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            using (Stream s = ntfs.OpenFile("ALongFileName.txt", FileMode.CreateNew)) { }
            Assert.Equal(1, ntfs.GetHardLinkCount("ALongFileName.txt"));

            ntfs.CreateHardLink("ALongFileName.txt", "AHardLink.TXT");
            Assert.Equal(2, ntfs.GetHardLinkCount("ALongFileName.txt"));

            ntfs.CreateDirectory("DIR");
            ntfs.CreateHardLink(@"ALongFileName.txt", @"DIR\SHORTLNK.TXT");
            Assert.Equal(3, ntfs.GetHardLinkCount("ALongFileName.txt"));

            // If we enumerate short names, then the initial long name results in two 'hardlinks'
            ntfs.NtfsOptions.HideDosFileNames = false;
            Assert.Equal(4, ntfs.GetHardLinkCount("ALongFileName.txt"));
        }
        public void correct_migration_number()
        {
            var dir = CreateTempDir();
            try
            {

                var content = "--sql header comment";
                CreateFile(dir, "20150520.sql", content);
                var source = new FileSystemSource(dir);
                var migrations = source.LoadMigrations().ToList();
                Assert.AreEqual(1, migrations.Count());
                Assert.AreEqual("20150520",migrations.First().Number);
            }
            finally
            {

                Directory.Delete(dir, true);
            }
        }
        public void ExecuteThread(StartingModulesParameters parameters)
        {
            // todo: строить список модулей исходя из включенных галочек
            var modules = new List <IProcessingModule>();

            modules.Add(ModuleFactory.CreateCodeBaseProcessingModule(parameters.ReestrPath));
            if (parameters.IsHeuristicEnabled)
            {
                modules.Add(ModuleFactory.CreateDatabaseHeuristicsModule());
            }

            var progressModule = ModuleFactory.CreateContainer(modules);

            progressModule.OnProgress += ProgressModuleDisplayHandler;
            var fileSourceList = new FileSystemSource(parameters.Path).GetFiles().ToList();
            var results        = progressModule.Execute(fileSourceList);

            ShowReport(results, fileSourceList);
        }
        public void AclInheritance()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            RawSecurityDescriptor sd = new RawSecurityDescriptor("O:BAG:BAD:(A;OICINP;GA;;;BA)");
            ntfs.CreateDirectory("dir");
            ntfs.SetSecurity("dir", sd);

            ntfs.CreateDirectory(@"dir\subdir");
            RawSecurityDescriptor inheritedSd = ntfs.GetSecurity(@"dir\subdir");

            Assert.NotNull(inheritedSd);
            Assert.AreEqual("O:BAG:BAD:(A;ID;GA;;;BA)", inheritedSd.GetSddlForm(AccessControlSections.All));

            using (ntfs.OpenFile(@"dir\subdir\file", FileMode.Create, FileAccess.ReadWrite)) { }
            inheritedSd = ntfs.GetSecurity(@"dir\subdir\file");
            Assert.NotNull(inheritedSd);
            Assert.AreEqual("O:BAG:BAD:", inheritedSd.GetSddlForm(AccessControlSections.All));
        }
Esempio n. 25
0
        public void AclInheritance()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            RawSecurityDescriptor sd = new RawSecurityDescriptor("O:BAG:BAD:(A;OICINP;GA;;;BA)");

            ntfs.CreateDirectory("dir");
            ntfs.SetSecurity("dir", sd);

            ntfs.CreateDirectory(@"dir\subdir");
            RawSecurityDescriptor inheritedSd = ntfs.GetSecurity(@"dir\subdir");

            Assert.NotNull(inheritedSd);
            Assert.Equal("O:BAG:BAD:(A;ID;GA;;;BA)", inheritedSd.GetSddlForm(AccessControlSections.All));

            using (ntfs.OpenFile(@"dir\subdir\file", FileMode.Create, FileAccess.ReadWrite)) { }
            inheritedSd = ntfs.GetSecurity(@"dir\subdir\file");
            Assert.NotNull(inheritedSd);
            Assert.Equal("O:BAG:BAD:", inheritedSd.GetSddlForm(AccessControlSections.All));
        }
Esempio n. 26
0
        public void MoveLongName()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            using (Stream s = ntfs.OpenFile("ALongFileName.txt", FileMode.CreateNew)) { }

            Assert.True(ntfs.FileExists("ALONGF~1.TXT"));

            ntfs.MoveFile("ALongFileName.txt", "ADifferentLongFileName.txt");

            Assert.False(ntfs.FileExists("ALONGF~1.TXT"));
            Assert.True(ntfs.FileExists("ADIFFE~1.TXT"));

            ntfs.CreateDirectory("ALongDirectoryName");
            Assert.True(ntfs.DirectoryExists("ALONGD~1"));

            ntfs.MoveDirectory("ALongDirectoryName", "ADifferentLongDirectoryName");
            Assert.False(ntfs.DirectoryExists("ALONGD~1"));
            Assert.True(ntfs.DirectoryExists("ADIFFE~1"));
        }
        public void ClusterInfo()
        {
            // 'Big' files have clusters
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();
            using (Stream s = ntfs.OpenFile(@"file", FileMode.Create, FileAccess.ReadWrite))
            {
                s.Write(new byte[(int)ntfs.ClusterSize], 0, (int)ntfs.ClusterSize);
            }

            var ranges = ntfs.PathToClusters("file");
            Assert.AreEqual(1, ranges.Length);
            Assert.AreEqual(1, ranges[0].Count);

            // Short files have no clusters (stored in MFT)
            using (Stream s = ntfs.OpenFile(@"file2", FileMode.Create, FileAccess.ReadWrite))
            {
                s.WriteByte(1);
            }
            ranges = ntfs.PathToClusters("file2");
            Assert.AreEqual(0, ranges.Length);
        }
Esempio n. 28
0
        public void ManyAttributes()
        {
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            using (Stream s = ntfs.OpenFile(@"file", FileMode.Create, FileAccess.ReadWrite))
            {
                s.WriteByte(32);
            }

            for (int i = 0; i < 50; ++i)
            {
                ntfs.CreateHardLink("file", "hl" + i);
            }

            using (Stream s = ntfs.OpenFile("hl35", FileMode.Open, FileAccess.ReadWrite))
            {
                Assert.Equal(32, s.ReadByte());
                s.Position = 0;
                s.WriteByte(12);
            }

            using (Stream s = ntfs.OpenFile("hl5", FileMode.Open, FileAccess.ReadWrite))
            {
                Assert.Equal(12, s.ReadByte());
            }

            for (int i = 0; i < 50; ++i)
            {
                ntfs.DeleteFile("hl" + i);
            }

            Assert.Equal(1, ntfs.GetFiles(@"\").Length);

            ntfs.DeleteFile("file");

            Assert.Equal(0, ntfs.GetFiles(@"\").Length);
        }
Esempio n. 29
0
        public void ClusterInfo()
        {
            // 'Big' files have clusters
            NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem();

            using (Stream s = ntfs.OpenFile(@"file", FileMode.Create, FileAccess.ReadWrite))
            {
                s.Write(new byte[(int)ntfs.ClusterSize], 0, (int)ntfs.ClusterSize);
            }

            var ranges = ntfs.PathToClusters("file");

            Assert.Equal(1, ranges.Length);
            Assert.Equal(1, ranges[0].Count);


            // Short files have no clusters (stored in MFT)
            using (Stream s = ntfs.OpenFile(@"file2", FileMode.Create, FileAccess.ReadWrite))
            {
                s.WriteByte(1);
            }
            ranges = ntfs.PathToClusters("file2");
            Assert.Equal(0, ranges.Length);
        }
        public void load_files()
        {
            var dir = CreateTempDir();
            try
            {

                var content = "--sql header comment";
                CreateFile(dir, "20150520.sql", content);
                CreateFile(dir, "20150521.sql", content);
                CreateFile(dir, "20150522.sql", content);
                var source = new FileSystemSource(dir);
                var migrations = source.LoadMigrations().ToList();
                Assert.AreEqual(3, migrations.Count());
                foreach (var migration in migrations)
                {
                    Assert.AreEqual(content, migration.Sql);
                }
            }
            finally
            {

                Directory.Delete(dir,true);
            }
        }
Esempio n. 31
0
 private InputBuilder Change(FileSystemSource source, WatcherChangeTypes type, string path)
 {
     _changes.Add(new FileSystemChange(source, type, path));
     return this;
 }
        public void DeleteShortNameDir()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            ntfs.CreateDirectory(@"\TestLongName1\TestLongName2");
            ntfs.SetShortName(@"\TestLongName1\TestLongName2", "TESTLO~1");

            Assert.IsTrue(ntfs.DirectoryExists(@"\TestLongName1\TESTLO~1"));
            Assert.IsTrue(ntfs.DirectoryExists(@"\TestLongName1\TestLongName2"));

            ntfs.DeleteDirectory(@"\TestLongName1", true);

            Assert.IsFalse(ntfs.DirectoryExists(@"\TestLongName1"));
        }
Esempio n. 33
0
 public FileSystemChange(FileSystemSource source, WatcherChangeTypes change, string fileOrDirectoryPath)
 {
     _source = source;
     _change = change;
     _fileOrDirectoryPath = fileOrDirectoryPath;
 }
        public void Fragmented()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            ntfs.CreateDirectory(@"DIR");

            byte[] buffer = new byte[4096];

            for(int i = 0; i < 2500; ++i)
            {
                using(var stream = ntfs.OpenFile(@"DIR\file" + i + ".bin", FileMode.Create, FileAccess.ReadWrite))
                {
                    stream.Write(buffer, 0,buffer.Length);
                }

                using(var stream = ntfs.OpenFile(@"DIR\" + i + ".bin", FileMode.Create, FileAccess.ReadWrite))
                {
                    stream.Write(buffer, 0,buffer.Length);
                }
            }

            for (int i = 0; i < 2500; ++i)
            {
                ntfs.DeleteFile(@"DIR\file" + i + ".bin");
            }

            // Create fragmented file (lots of small writes)
            using (var stream = ntfs.OpenFile(@"DIR\fragmented.bin", FileMode.Create, FileAccess.ReadWrite))
            {
                for (int i = 0; i < 2500; ++i)
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }

            // Try a large write
            byte[] largeWriteBuffer = new byte[200 * 1024];
            for (int i = 0; i < largeWriteBuffer.Length / 4096; ++i)
            {
                largeWriteBuffer[i * 4096] = (byte)i;
            }
            using (var stream = ntfs.OpenFile(@"DIR\fragmented.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                stream.Position = stream.Length - largeWriteBuffer.Length;
                stream.Write(largeWriteBuffer, 0, largeWriteBuffer.Length);
            }

            // And a large read
            byte[] largeReadBuffer = new byte[largeWriteBuffer.Length];
            using (var stream = ntfs.OpenFile(@"DIR\fragmented.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                stream.Position = stream.Length - largeReadBuffer.Length;
                stream.Read(largeReadBuffer, 0, largeReadBuffer.Length);
            }

            Assert.AreEqual(largeWriteBuffer, largeReadBuffer);
        }
        public void DeleteAlternateDataStreams()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            ntfs.OpenFile("AFILE.TXT", FileMode.Create).Close();
            ntfs.OpenFile("AFILE.TXT:ALTSTREAM", FileMode.Create).Close();
            Assert.AreEqual(1, ntfs.GetAlternateDataStreams("AFILE.TXT").Length);

            ntfs.DeleteFile("AFILE.TXT:ALTSTREAM");
            Assert.AreEqual(1, ntfs.GetFileSystemEntries("").Length);
            Assert.AreEqual(0, ntfs.GetAlternateDataStreams("AFILE.TXT").Length);
        }
        public void ShortNames()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            // Check we can find a short name in the same directory
            using (Stream s = ntfs.OpenFile("ALongFileName.txt", FileMode.CreateNew)) {}
            ntfs.SetShortName("ALongFileName.txt", "ALONG~01.TXT");
            Assert.AreEqual("ALONG~01.TXT", ntfs.GetShortName("ALongFileName.txt"));
            Assert.IsTrue(ntfs.FileExists("ALONG~01.TXT"));

            // Check path handling
            ntfs.CreateDirectory("DIR");
            using (Stream s = ntfs.OpenFile(@"DIR\ALongFileName2.txt", FileMode.CreateNew)) { }
            ntfs.SetShortName(@"DIR\ALongFileName2.txt", "ALONG~02.TXT");
            Assert.AreEqual("ALONG~02.TXT", ntfs.GetShortName(@"DIR\ALongFileName2.txt"));
            Assert.IsTrue(ntfs.FileExists(@"DIR\ALONG~02.TXT"));

            // Check we can open a file by the short name
            using (Stream s = ntfs.OpenFile("ALONG~01.TXT", FileMode.Open)) { }

            // Delete the long name, and make sure the file is gone
            ntfs.DeleteFile("ALONG~01.TXT");
            Assert.IsFalse(ntfs.FileExists("ALONG~01.TXT"));

            // Delete the short name, and make sure the file is gone
            ntfs.DeleteFile(@"DIR\ALONG~02.TXT");
            Assert.IsFalse(ntfs.FileExists(@"DIR\ALongFileName2.txt"));
        }
        public void Sparse()
        {
            int fileSize = 1 * 1024 * 1024;

            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            byte[] data = new byte[fileSize];
            for (int i = 0; i < fileSize; i++)
            {
                data[i] = (byte)i;
            }

            using (SparseStream s = ntfs.OpenFile("file.bin", FileMode.CreateNew))
            {
                s.Write(data, 0, fileSize);

                ntfs.SetAttributes("file.bin", ntfs.GetAttributes("file.bin") | FileAttributes.SparseFile);

                s.Position = 64 * 1024;
                s.Clear(128 * 1024);
                s.Position = fileSize - 64 * 1024;
                s.Clear(128 * 1024);
            }

            using (SparseStream s = ntfs.OpenFile("file.bin", FileMode.Open))
            {
                Assert.AreEqual(fileSize + 64 * 1024, s.Length);

                List<StreamExtent> extents = new List<StreamExtent>(s.Extents);

                Assert.AreEqual(2, extents.Count);
                Assert.AreEqual(0, extents[0].Start);
                Assert.AreEqual(64 * 1024, extents[0].Length);
                Assert.AreEqual((64 + 128) * 1024, extents[1].Start);
                Assert.AreEqual(fileSize - (64 * 1024) - ((64 + 128) * 1024), extents[1].Length);

                s.Position = 72 * 1024;
                s.WriteByte(99);

                byte[] readBuffer = new byte[fileSize];
                s.Position = 0;
                s.Read(readBuffer, 0, fileSize);

                for (int i = 64 * 1024; i < (128 + 64) * 1024; ++i)
                {
                    data[i] = 0;
                }
                for (int i = fileSize - (64 * 1024); i < fileSize; ++i)
                {
                    data[i] = 0;
                }
                data[72 * 1024] = 99;

                Assert.AreEqual(data, readBuffer);
            }
        }
        public void GetFileLength()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            ntfs.OpenFile(@"AFILE.TXT", FileMode.Create).Close();
            Assert.AreEqual(0, ntfs.GetFileLength("AFILE.TXT"));

            using (var stream = ntfs.OpenFile(@"AFILE.TXT", FileMode.Open))
            {
                stream.Write(new byte[14325], 0, 14325);
            }
            Assert.AreEqual(14325, ntfs.GetFileLength("AFILE.TXT"));

            using (var attrStream = ntfs.OpenFile(@"AFILE.TXT:altstream", FileMode.Create))
            {
                attrStream.Write(new byte[122], 0, 122);
            }
            Assert.AreEqual(122, ntfs.GetFileLength("AFILE.TXT:altstream"));

            // Test NTFS options for hardlink behaviour
            ntfs.CreateDirectory("Dir");
            ntfs.CreateHardLink("AFILE.TXT", @"Dir\OtherLink.txt");

            using (var stream = ntfs.OpenFile("AFILE.TXT", FileMode.Open, FileAccess.ReadWrite))
            {
                stream.SetLength(50);
            }
            Assert.AreEqual(50, ntfs.GetFileLength("AFILE.TXT"));
            Assert.AreEqual(14325, ntfs.GetFileLength(@"Dir\OtherLink.txt"));

            ntfs.NtfsOptions.FileLengthFromDirectoryEntries = false;

            Assert.AreEqual(50, ntfs.GetFileLength(@"Dir\OtherLink.txt"));
        }
        public void ReparsePoints_NonEmpty()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            ntfs.CreateDirectory("dir");
            ntfs.SetReparsePoint("dir", new ReparsePoint(123, new byte[] { 4, 5, 6 }));

            ReparsePoint rp = ntfs.GetReparsePoint("dir");

            Assert.AreEqual(123, rp.Tag);
            Assert.IsNotNull(rp.Content);
            Assert.AreEqual(3, rp.Content.Length);
        }
        public void OpenRawStream()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            #pragma warning disable 618
            Assert.Null(ntfs.OpenRawStream(@"$Extend\$ObjId", AttributeType.Data, null, FileAccess.Read));
            #pragma warning restore 618
        }
        public void MoveLongName()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            using (Stream s = ntfs.OpenFile("ALongFileName.txt", FileMode.CreateNew)) { }

            Assert.IsTrue(ntfs.FileExists("ALONGF~1.TXT"));

            ntfs.MoveFile("ALongFileName.txt", "ADifferentLongFileName.txt");

            Assert.IsFalse(ntfs.FileExists("ALONGF~1.TXT"));
            Assert.IsTrue(ntfs.FileExists("ADIFFE~1.TXT"));

            ntfs.CreateDirectory("ALongDirectoryName");
            Assert.IsTrue(ntfs.DirectoryExists("ALONGD~1"));

            ntfs.MoveDirectory("ALongDirectoryName", "ADifferentLongDirectoryName");
            Assert.IsFalse(ntfs.DirectoryExists("ALONGD~1"));
            Assert.IsTrue(ntfs.DirectoryExists("ADIFFE~1"));
        }
        public void ManyAttributes()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();
            using (Stream s = ntfs.OpenFile(@"file", FileMode.Create, FileAccess.ReadWrite))
            {
                s.WriteByte(32);
            }

            for (int i = 0; i < 50; ++i)
            {
                ntfs.CreateHardLink("file", "hl" + i);
            }

            using (Stream s = ntfs.OpenFile("hl35", FileMode.Open, FileAccess.ReadWrite))
            {
                Assert.AreEqual(32, s.ReadByte());
                s.Position = 0;
                s.WriteByte(12);
            }

            using (Stream s = ntfs.OpenFile("hl5", FileMode.Open, FileAccess.ReadWrite))
            {
                Assert.AreEqual(12, s.ReadByte());
            }

            for (int i = 0; i < 50; ++i)
            {
                ntfs.DeleteFile("hl" + i);
            }

            Assert.AreEqual(1, ntfs.GetFiles(@"\").Length);

            ntfs.DeleteFile("file");

            Assert.AreEqual(0, ntfs.GetFiles(@"\").Length);
        }
        public void HasHardLink()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            using (Stream s = ntfs.OpenFile("ALongFileName.txt", FileMode.CreateNew)) { }
            Assert.IsFalse(ntfs.HasHardLinks("ALongFileName.txt"));

            ntfs.CreateHardLink("ALongFileName.txt", "AHardLink.TXT");
            Assert.IsTrue(ntfs.HasHardLinks("ALongFileName.txt"));

            using (Stream s = ntfs.OpenFile("ALongFileName2.txt", FileMode.CreateNew)) { }

            // If we enumerate short names, then the initial long name results in two 'hardlinks'
            ntfs.NtfsOptions.HideDosFileNames = false;
            Assert.IsTrue(ntfs.HasHardLinks("ALongFileName2.txt"));
        }
        public void HardLinkCount()
        {
            NtfsFileSystem ntfs = new FileSystemSource().NtfsFileSystem();

            using (Stream s = ntfs.OpenFile("ALongFileName.txt", FileMode.CreateNew)) { }
            Assert.AreEqual(1, ntfs.GetHardLinkCount("ALongFileName.txt"));

            ntfs.CreateHardLink("ALongFileName.txt", "AHardLink.TXT");
            Assert.AreEqual(2, ntfs.GetHardLinkCount("ALongFileName.txt"));

            ntfs.CreateDirectory("DIR");
            ntfs.CreateHardLink(@"ALongFileName.txt", @"DIR\SHORTLNK.TXT");
            Assert.AreEqual(3, ntfs.GetHardLinkCount("ALongFileName.txt"));

            // If we enumerate short names, then the initial long name results in two 'hardlinks'
            ntfs.NtfsOptions.HideDosFileNames = false;
            Assert.AreEqual(4, ntfs.GetHardLinkCount("ALongFileName.txt"));
        }