Skip to content

Commit

Permalink
feat: Include file path in TarException: Entry closed at [...] (Docke…
Browse files Browse the repository at this point in the history
…r build)
  • Loading branch information
HofmeisterAn committed Dec 22, 2022
1 parent ccd9455 commit de03900
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Testcontainers/Images/DockerfileArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,30 @@ public string Tar()
{
tarArchive.RootPath = dockerfileDirectoryPath;

foreach (var file in GetFiles(dockerfileDirectoryPath))
foreach (var absoluteFilePath in GetFiles(dockerfileDirectoryPath))
{
// SharpZipLib drops the root path: https://github.com/icsharpcode/SharpZipLib/pull/582.
var relativePath = file.Substring(dockerfileDirectoryPath.TrimEnd(Path.AltDirectorySeparatorChar).Length + 1);
var relativeFilePath = absoluteFilePath.Substring(dockerfileDirectoryPath.TrimEnd(Path.AltDirectorySeparatorChar).Length + 1);

if (dockerIgnoreFile.Denies(relativePath))
if (dockerIgnoreFile.Denies(relativeFilePath))
{
continue;
}

var tarEntry = TarEntry.CreateEntryFromFile(file);
tarEntry.Name = relativePath;
// SharpZipLib's WriteEntry(TarEntry, bool) writes the entry, but without bytes if the file is used by another process.
// This results in a TarException on TarArchive.Dispose(): Entry closed at '0' before the 'x' bytes specified in the header were written.
try
{
var fileStream = File.Open(absoluteFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
fileStream.Dispose();
}
catch (IOException e)
{
throw new IOException("Cannot create Docker image tar archive.", e);
}

var tarEntry = TarEntry.CreateEntryFromFile(absoluteFilePath);
tarEntry.Name = relativeFilePath;
tarArchive.WriteEntry(tarEntry, false);
}
}
Expand Down

0 comments on commit de03900

Please # to comment.