Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ZipArchiveFileSystem and project warnings #88

Merged
merged 4 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/Zio.Tests/FileSystems/TestZipArchiveFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,105 @@ public void TestOpenStreamsMultithreaded()
thread1.Join();
thread2.Join();
}


[Theory]
[InlineData("TestData/Linux.zip")]
[InlineData("TestData/Windows.zip")]
public void TestCaseInSensitiveZip(string path)
{
using var stream = File.OpenRead(path);
using var archive = new ZipArchive(stream, ZipArchiveMode.Read);
var fs = new ZipArchiveFileSystem(archive);

Assert.True(fs.DirectoryExists("/Folder"));
Assert.True(fs.DirectoryExists("/folder"));

Assert.False(fs.FileExists("/Folder"));
Assert.False(fs.FileExists("/folder"));

Assert.True(fs.FileExists("/Folder/File.txt"));
Assert.True(fs.FileExists("/folder/file.txt"));

Assert.False(fs.DirectoryExists("/Folder/file.txt"));
Assert.False(fs.DirectoryExists("/folder/File.txt"));
}

[Theory]
[InlineData("TestData/Linux.zip")]
[InlineData("TestData/Windows.zip")]
public void TestCaseSensitiveZip(string path)
{
using var stream = File.OpenRead(path);
using var archive = new ZipArchive(stream, ZipArchiveMode.Read);
var fs = new ZipArchiveFileSystem(archive, true);

Assert.True(fs.DirectoryExists("/Folder"));
Assert.False(fs.DirectoryExists("/folder"));

Assert.False(fs.FileExists("/Folder"));
Assert.False(fs.FileExists("/folder"));

Assert.True(fs.FileExists("/Folder/File.txt"));
Assert.False(fs.FileExists("/folder/file.txt"));

Assert.False(fs.DirectoryExists("/Folder/file.txt"));
Assert.False(fs.DirectoryExists("/folder/File.txt"));
}

[Fact]
public void TestSaveStream()
{
var stream = new MemoryStream();

using var fs = new ZipArchiveFileSystem(stream);

fs.WriteAllText("/a/b.txt", "abc");
fs.Save();

stream.Seek(0, SeekOrigin.Begin);

using (var fs2 = new ZipArchiveFileSystem(stream, ZipArchiveMode.Read, leaveOpen: true))
{
Assert.Equal("abc", fs2.ReadAllText("/a/b.txt"));
}

Assert.Equal("abc", fs.ReadAllText("/a/b.txt"));
fs.WriteAllText("/a/b.txt", "def");
fs.Save();

stream.Seek(0, SeekOrigin.Begin);

using (var fs2 = new ZipArchiveFileSystem(stream, ZipArchiveMode.Read, leaveOpen: true))
{
Assert.Equal("def", fs2.ReadAllText("/a/b.txt"));
}
}

[Fact]
public void TestSaveFile()
{
var path = Path.Combine(SystemPath, Guid.NewGuid().ToString("N") + ".zip");

try
{
using var fs = new ZipArchiveFileSystem(path);

Assert.Equal(0, new FileInfo(path).Length);

fs.WriteAllText("/a/b.txt", "abc");
fs.Save();

// We cannot check the content because the file is still open
Assert.NotEqual(0, new FileInfo(path).Length);

// Ensure we can save multiple times
fs.WriteAllText("/a/b.txt", "def");
fs.Save();
}
finally
{
File.Delete(path);
}
}
}
Binary file added src/Zio.Tests/TestData/Linux.zip
Binary file not shown.
Binary file added src/Zio.Tests/TestData/Windows.zip
Binary file not shown.
6 changes: 6 additions & 0 deletions src/Zio.Tests/Zio.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
<ItemGroup>
<ProjectReference Include="..\Zio\Zio.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestData\**\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions src/Zio/FileEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void WriteAllText(string content, Encoding encoding)
/// <remarks>
/// Given a string and a file path, this method opens the specified file, appends the string to the end of the file,
/// and then closes the file. The file handle is guaranteed to be closed by this method, even if exceptions are raised.
/// The method creates the file if it doesnt exist, but it doesn't create new directories. Therefore, the value of the
/// The method creates the file if it doesn't exist, but it doesn't create new directories. Therefore, the value of the
/// path parameter must contain existing directories.
/// </remarks>
public void AppendAllText(string content)
Expand All @@ -301,7 +301,7 @@ public void AppendAllText(string content)
/// <remarks>
/// Given a string and a file path, this method opens the specified file, appends the string to the end of the file,
/// and then closes the file. The file handle is guaranteed to be closed by this method, even if exceptions are raised.
/// The method creates the file if it doesnt exist, but it doesn't create new directories. Therefore, the value of the
/// The method creates the file if it doesn't exist, but it doesn't create new directories. Therefore, the value of the
/// path parameter must contain existing directories.
/// </remarks>
public void AppendAllText(string content, Encoding encoding)
Expand Down
4 changes: 2 additions & 2 deletions src/Zio/FileSystemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ public static void WriteAllText(this IFileSystem fs, UPath path, string content,
/// <remarks>
/// Given a string and a file path, this method opens the specified file, appends the string to the end of the file,
/// and then closes the file. The file handle is guaranteed to be closed by this method, even if exceptions are raised.
/// The method creates the file if it doesn’t exist, but it doesn't create new directories. Therefore, the value of the
/// The method creates the file if it doesn't exist, but it doesn't create new directories. Therefore, the value of the
/// path parameter must contain existing directories.
/// </remarks>
public static void AppendAllText(this IFileSystem fs, UPath path, string content)
Expand All @@ -531,7 +531,7 @@ public static void AppendAllText(this IFileSystem fs, UPath path, string content
/// <remarks>
/// Given a string and a file path, this method opens the specified file, appends the string to the end of the file,
/// and then closes the file. The file handle is guaranteed to be closed by this method, even if exceptions are raised.
/// The method creates the file if it doesn’t exist, but it doesn't create new directories. Therefore, the value of the
/// The method creates the file if it doesn't exist, but it doesn't create new directories. Therefore, the value of the
/// path parameter must contain existing directories.
/// </remarks>
public static void AppendAllText(this IFileSystem fs, UPath path, string content, Encoding encoding)
Expand Down
10 changes: 5 additions & 5 deletions src/Zio/FileSystems/FileSystemWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected void UnregisterEvents(IFileSystemWatcher watcher)
return pathFromEvent;
}

private void OnChanged(object sender, FileChangedEventArgs args)
private void OnChanged(object? sender, FileChangedEventArgs args)
{
var newPath = TryConvertPath(args.FullPath);
if (!newPath.HasValue)
Expand All @@ -235,7 +235,7 @@ private void OnChanged(object sender, FileChangedEventArgs args)
RaiseChanged(newArgs);
}

private void OnCreated(object sender, FileChangedEventArgs args)
private void OnCreated(object? sender, FileChangedEventArgs args)
{
var newPath = TryConvertPath(args.FullPath);
if (!newPath.HasValue)
Expand All @@ -247,7 +247,7 @@ private void OnCreated(object sender, FileChangedEventArgs args)
RaiseCreated(newArgs);
}

private void OnDeleted(object sender, FileChangedEventArgs args)
private void OnDeleted(object? sender, FileChangedEventArgs args)
{
var newPath = TryConvertPath(args.FullPath);
if (!newPath.HasValue)
Expand All @@ -259,12 +259,12 @@ private void OnDeleted(object sender, FileChangedEventArgs args)
RaiseDeleted(newArgs);
}

private void OnError(object sender, FileSystemErrorEventArgs args)
private void OnError(object? sender, FileSystemErrorEventArgs args)
{
RaiseError(args);
}

private void OnRenamed(object sender, FileRenamedEventArgs args)
private void OnRenamed(object? sender, FileRenamedEventArgs args)
{
var newPath = TryConvertPath(args.FullPath);
if (!newPath.HasValue)
Expand Down
10 changes: 5 additions & 5 deletions src/Zio/FileSystems/MemoryFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected override void DeleteDirectoryImpl(UPath path, bool isRecursive)
}
finally
{
if (deleteRootDirectory)
if (deleteRootDirectory && result.Node != null)
{
result.Node.DetachFromParent();
result.Node.Dispose();
Expand Down Expand Up @@ -995,7 +995,7 @@ private void MoveFileOrDirectory(UPath srcPath, UPath destPath, bool expectDirec
var parentSrcPath = srcPath.GetDirectory();
var parentDestPath = destPath.GetDirectory();

void AssertNoDestination(FileSystemNode node)
void AssertNoDestination(FileSystemNode? node)
{
if (expectDirectory)
{
Expand Down Expand Up @@ -1127,7 +1127,7 @@ private static void ValidateFile([NotNull] FileSystemNode? node, UPath srcPath)
}
}

private FileSystemNode TryFindNodeSafe(UPath path)
private FileSystemNode? TryFindNodeSafe(UPath path)
{
EnterFileSystemShared();
try
Expand Down Expand Up @@ -1180,7 +1180,7 @@ private void CreateDirectoryNode(UPath path)

private readonly struct NodeResult
{
public NodeResult(DirectoryNode? directory, FileSystemNode node, string? name, FindNodeFlags flags)
public NodeResult(DirectoryNode? directory, FileSystemNode? node, string? name, FindNodeFlags flags)
{
Directory = directory;
Node = node;
Expand All @@ -1190,7 +1190,7 @@ public NodeResult(DirectoryNode? directory, FileSystemNode node, string? name, F

public readonly DirectoryNode? Directory;

public readonly FileSystemNode Node;
public readonly FileSystemNode? Node;

public readonly string? Name;

Expand Down
Loading
Loading