private void When_increasing_file_size_using_SetLength_it_must_fail() { // Arrange const string path = @"C:\file.txt"; IFileSystem fileSystem = new FakeFileSystemBuilder() .IncludingVolume("C:", new FakeVolumeInfoBuilder() .OfCapacity(8192) .WithFreeSpace(512)) .IncludingBinaryFile(path, BufferFactory.Create(32)) .Build(); using (IFileStream stream = fileSystem.File.OpenWrite(path)) { byte[] buffer = BufferFactory.Create(64); stream.Write(buffer, 0, buffer.Length); // Act // ReSharper disable once AccessToDisposedClosure Action action = () => stream.SetLength(1280); // Assert action.Should().ThrowExactly <IOException>().WithMessage("There is not enough space on the disk."); AssertFreeSpaceOnDrive(fileSystem, "C:", 448); } AssertFileSize(fileSystem, path, 64); }
private void When_reducing_length_it_must_succeed() { // Arrange const string path = @"C:\file.txt"; IFileSystem fileSystem = new FakeFileSystemBuilder() .IncludingTextFile(path, "ABCDEF") .Build(); using (IFileStream stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.ReadWrite)) { stream.Position = 5; // Act stream.SetLength(3); // Assert stream.Length.Should().Be(3); stream.Position.Should().Be(3); } string contents = fileSystem.File.ReadAllText(path); contents.Should().Be("ABC"); }
private FileEntry PrepareDestinationFile([NotNull] FileResolveResult destinationResolveResult, [NotNull] FileEntry sourceFile, [NotNull] FileCopyArguments arguments) { DateTime utcNow = Container.SystemClock.UtcNow(); FileEntry destinationFile; bool isNewlyCreated; if (destinationResolveResult.ExistingFileOrNull != null) { destinationFile = destinationResolveResult.ExistingFileOrNull; isNewlyCreated = false; AssertCanOverwriteFile(arguments.Overwrite, arguments.DestinationPath); AssertIsNotHiddenOrReadOnly(destinationFile, arguments.DestinationPath); AssertIsNotExternallyEncrypted(destinationFile, arguments.DestinationPath, arguments.IsCopyAfterMoveFailed); AssertSufficientDiskSpace(sourceFile.Size - destinationFile.Size, destinationResolveResult.ContainingDirectory.Root); AddChangeForExistingDestinationFile(destinationFile); } else { AssertSufficientDiskSpace(sourceFile.Size, destinationResolveResult.ContainingDirectory.Root); destinationFile = destinationResolveResult.ContainingDirectory.CreateFile(destinationResolveResult.FileName); destinationFile.CreationTimeUtc = utcNow; isNewlyCreated = true; } using (IFileStream createStream = destinationFile.Open(FileMode.Truncate, FileAccess.Write, arguments.DestinationPath, isNewlyCreated, false, false)) { createStream.SetLength(sourceFile.Size); } destinationFile.SetAttributes(sourceFile.Attributes); destinationFile.LastAccessTimeUtc = utcNow; destinationFile.LastWriteTimeUtc = utcNow; return(destinationFile); }
private void When_setting_length_in_closed_stream_it_must_fail() { // Arrange const string path = @"C:\file.txt"; IFileSystem fileSystem = new FakeFileSystemBuilder() .IncludingEmptyFile(path) .Build(); using (IFileStream stream = fileSystem.File.Open(path, FileMode.Open, FileAccess.Read)) { stream.Dispose(); // Act // ReSharper disable once AccessToDisposedClosure Action action = () => stream.SetLength(2); // Assert action.ShouldThrow <ObjectDisposedException>().WithMessage("Cannot access a closed file."); } }
private void When_increasing_file_size_using_SetLength_it_must_succeed() { // Arrange const string path = @"C:\file.txt"; IFileSystem fileSystem = new FakeFileSystemBuilder() .IncludingVolume("C:", new FakeVolumeInfoBuilder() .OfCapacity(8192) .WithFreeSpace(4096)) .IncludingBinaryFile(path, BufferFactory.Create(1024)) .Build(); using (IFileStream stream = fileSystem.File.OpenWrite(path)) { // Act stream.SetLength(1280); // Assert IDriveInfo driveInfo = fileSystem.ConstructDriveInfo("C:"); driveInfo.AvailableFreeSpace.Should().Be(2816); } }
private FileEntry ResolveDestinationFile([NotNull] AbsolutePath destinationPath, bool overwrite, [NotNull] FileEntry sourceFile) { var destinationResolver = new FileResolver(Root) { ErrorFileFoundAsDirectory = ErrorFactory.System.TargetIsNotFile }; FileResolveResult resolveResult = destinationResolver.TryResolveFile(destinationPath); DateTime utcNow = Root.SystemClock.UtcNow(); FileEntry destinationFile; if (resolveResult.ExistingFileOrNull != null) { AssertCanOverwriteFile(overwrite, destinationPath); AssertIsNotHiddenOrReadOnly(resolveResult.ExistingFileOrNull, destinationPath); destinationFile = resolveResult.ContainingDirectory.Files[resolveResult.FileName]; } else { destinationFile = resolveResult.ContainingDirectory.CreateFile(resolveResult.FileName); destinationFile.CreationTimeUtc = utcNow; } using (IFileStream createStream = destinationFile.Open(FileMode.Truncate, FileAccess.Write, destinationPath)) { createStream.SetLength(sourceFile.Size); } destinationFile.Attributes = sourceFile.Attributes; destinationFile.LastAccessTimeUtc = utcNow; destinationFile.LastWriteTimeUtc = sourceFile.LastWriteTimeUtc; return(destinationFile); }