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")); }
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")); }
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 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); } }
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); }
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]); }
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); }
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")); }
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")); }
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 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)); }
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 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); }
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); }