Skip to content

fix(tar): enable unix paths in tar RootPath #582

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

Merged
merged 15 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ public string RootPath
{
throw new ObjectDisposedException("TarArchive");
}
// Convert to forward slashes for matching. Trim trailing / for correct final path
rootPath = value.Replace('\\', '/').TrimEnd('/');
rootPath = value.ClearTarPath().TrimEnd('/');
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,10 @@ public void GetFileTarHeader(TarHeader header, string file)
}
*/

name = name.Replace(Path.DirectorySeparatorChar, '/');

// No absolute pathnames
// Windows (and Posix?) paths can start with UNC style "\\NetworkDrive\",
// so we loop on starting /'s.
while (name.StartsWith("/", StringComparison.Ordinal))
{
name = name.Substring(1);
}
name = name.ClearTarPath();

header.LinkName = String.Empty;
header.Name = name;
Expand Down
17 changes: 17 additions & 0 deletions src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.IO;

namespace ICSharpCode.SharpZipLib.Tar
{
internal static class TarStringExtension
{
public static string ClearTarPath(this string s)
{
var pathRoot = Path.GetPathRoot(s);
if (!string.IsNullOrEmpty(pathRoot))
{
s = s.Substring(pathRoot.Length);
}
return s.Replace(Path.DirectorySeparatorChar, '/');
}
}
}
82 changes: 77 additions & 5 deletions test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ public void ParseHeaderWithEncoding(int length, string encodingName)
reparseHeader.ParseBuffer(headerbytes, enc);
Assert.AreEqual(name, reparseHeader.Name);
// top 100 bytes are name field in tar header
for (int i = 0;i < encodedName.Length;i++)
for (int i = 0; i < encodedName.Length; i++)
{
Assert.AreEqual(encodedName[i], headerbytes[i]);
}
Expand All @@ -878,17 +878,17 @@ public void StreamWithJapaneseName(int length, string encodingName)
var entryName = new string((char)0x3042, length);
var data = new byte[32];
var encoding = Encoding.GetEncoding(encodingName);
using(var memoryStream = new MemoryStream())
using (var memoryStream = new MemoryStream())
{
using(var tarOutput = new TarOutputStream(memoryStream, encoding))
using (var tarOutput = new TarOutputStream(memoryStream, encoding))
{
var entry = TarEntry.CreateTarEntry(entryName);
entry.Size = 32;
tarOutput.PutNextEntry(entry);
tarOutput.Write(data, 0, data.Length);
}
using(var memInput = new MemoryStream(memoryStream.ToArray()))
using(var inputStream = new TarInputStream(memInput, encoding))
using (var memInput = new MemoryStream(memoryStream.ToArray()))
using (var inputStream = new TarInputStream(memInput, encoding))
{
var buf = new byte[64];
var entry = inputStream.GetNextEntry();
Expand All @@ -899,5 +899,77 @@ public void StreamWithJapaneseName(int length, string encodingName)
File.WriteAllBytes(Path.Combine(Path.GetTempPath(), $"jpnametest_{length}_{encodingName}.tar"), memoryStream.ToArray());
}
}
/// <summary>
/// This test could be considered integration test. it creates a tar archive with the root directory specified
/// Then extracts it and compares the two folders. This used to fail on unix due to issues with root folder handling
/// in the tar archive.
/// </summary>
[Test]
[Category("Tar")]
public void rootPathIsRespected()
{
// create dummy folder structure
var tempDirectory = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_test_folder");
CreateAndClearDirectory(tempDirectory);
using (var dummyfile = File.Create(Path.Combine(tempDirectory, "dummyfile")))
{
using (var randomStream = new ChaosStream())
{
randomStream.CopyTo(dummyfile);
}
}

var tarFileName = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_test_folder_archive.tar");

using (var tarFile = File.Open(tarFileName, FileMode.Create))
{
using (var tarOutputStream = TarArchive.CreateOutputTarArchive(tarFile))
{
tarOutputStream.RootPath = tempDirectory;
var entry = TarEntry.CreateEntryFromFile(tempDirectory);
tarOutputStream.WriteEntry(entry, true);
}
}

var extractDirectory = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_extract_folder");
CreateAndClearDirectory(extractDirectory);
using (var file = File.OpenRead(tarFileName))
{
using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8))
{
archive.ExtractContents(extractDirectory);
}
}

var expectationDirectory = new DirectoryInfo(tempDirectory);
foreach (var checkFile in expectationDirectory.GetFiles("", SearchOption.AllDirectories))
{
var relativePath = checkFile.FullName.Substring(expectationDirectory.FullName.Length + 1);
FileAssert.Exists(Path.Combine(extractDirectory, relativePath));
FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory, relativePath));
}
}

private void CreateAndClearDirectory(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
Directory.CreateDirectory(path);
}

public class ChaosStream : MemoryStream
{
private readonly int length = new Random().Next() % 500000 + 200;

// Create constructors as needed to match desired MemoryStream construction

public override int Read(byte[] buffer, int offset, int count)
{
int readCount = Math.Max(0, Math.Min(length - offset, count));
return base.Read(buffer, offset, readCount);
}
}
}
}