Skip to content

Tar: Not able to write a PaxTarEntry from a TarReader. #75215

Description

@jozkee

Consider this method that creates a new archive copying the entries from an existing one:

static void CopyTar(string archive, string destination)
{
    Directory.CreateDirectory(destination);
    using var reader = new TarReader(File.OpenRead(archive));
    using var writer = new TarWriter(File.Create(Path.Combine(destination, "copy_tar")));

    TarEntry? e;
    while ((e = reader.GetNextEntry()) != null)
    {
        writer.WriteEntry(e);
    }
}

When the archive is in pax format, it throws the following exception:

Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: path
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Formats.Tar.TarHeader.CollectExtendedAttributesFromStandardFieldsIfNeeded()
   at System.Formats.Tar.TarHeader.WriteAsPax(Stream archiveStream, Span`1 buffer)
   at System.Formats.Tar.TarWriter.WriteEntry(TarEntry entry)
   at Program.<<Main>$>g__Extract|0_0(String archive, String destination, TarEntryFormat format) in /Users/jozkee/dotnet-consoles/tar_utf8/Program.cs:line 17
   at Program.<Main>$(String[] args) in /Users/jozkee/dotnet-consoles/tar_utf8/Program.cs:line 5

This is because the TarWriter attempts to add "Path" to the Extended Attributes when the entry read with TarReader already contained one.

This is somewhat important since PAX is our default format, but there may be a workaround, that is, use the new PaxTarEntry(TarEntryType entryType, string entryName, IEnumerable<KeyValuePair<string, string>> extendedAttributes) ctor.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions