Skip to content

Support Tar with large entry size #96209

Closed

Description

@ivanjx

Description

Currently i cannot use TarReader.GetNextEntryAsync whenever it sees a tar entry file with more than 8GB of size (my case is 9GB). In the entry header itself, instead of writing size of the file in octal, the size header is constructed like this:
[0x80, 0, 0, 0, <size in bytes big endian>]

Reproduction Steps

using TarReader tar = new TarReader(tarStream, true);
List<TarEntry> entries = new List<TarEntry>();

while (true)
{
    TarEntry? entry = await tar.GetNextEntryAsync();

    if (entry == null)
    {
        break;
    }

    entries.Add(entry);
}
...

Expected behavior

Not error

Actual behavior

error:

Unable to parse number.
   at System.Formats.Tar.TarHelpers.ThrowInvalidNumber()
   at System.Formats.Tar.TarHelpers.ParseOctal[T](ReadOnlySpan`1 buffer)
   at System.Formats.Tar.TarHeader.TryReadCommonAttributes(Span`1 buffer, TarEntryFormat initialFormat)
   at System.Formats.Tar.TarHeader.TryReadAttributes(TarEntryFormat initialFormat, Span`1 buffer)
   at System.Formats.Tar.TarHeader.<TryGetNextHeaderAsync>d__48.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Formats.Tar.TarReader.<TryGetNextEntryHeaderAsync>d__15.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Formats.Tar.TarReader.<GetNextEntryInternalAsync>d__13.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at xxxxx.<GetEntriesAsync>d__0.MoveNext() in [redacted]

Regression?

No response

Known Workarounds

this is currently my own tar parser implementation:

public async Task<TarEntry[]> GetEntriesAsync(Stream tarStream, CancellationToken cancellationToken)
    {
        byte[] entryHeaderBuff = new byte[512];
        List<TarEntry> entries = new List<TarEntry>();

        while (true)
        {
            cancellationToken.ThrowIfCancellationRequested();
            entryHeaderBuff[0] = 0; // Reset flag.
            int read = await tarStream.ReadAsync(
                entryHeaderBuff,
                cancellationToken);

            if (read < entryHeaderBuff.Length ||
                entryHeaderBuff[0] == 0)
            {
                // No more entries.
                break;
            }

            string fileName = Encoding.ASCII.GetString(entryHeaderBuff, 0, 100).Trim('\0');
            long fileSize;

            if (entryHeaderBuff[124] == 0x80)
            {
                byte[] sizeBuff = new byte[8];
                entryHeaderBuff
                    .AsSpan()
                    .Slice(124 + 4, 8)
                    .CopyTo(sizeBuff);
                Array.Reverse(sizeBuff);
                fileSize = BitConverter.ToInt64(sizeBuff);
            }
            else
            {
                string sizeStr = Encoding.UTF8.GetString(entryHeaderBuff, 124, 12).Trim('\0');
                fileSize = Convert.ToInt64(sizeStr, 8);
            }

            entries.Add(new TarEntry(fileName, fileSize));

            // Next.
            tarStream.Seek((fileSize + 511) / 512 * 512, SeekOrigin.Current);
        }

        return entries.ToArray();
    }

Configuration

No response

Other information

No response

Activity

  1. ghost added
    needs-area-label
    on Dec 20, 2023
  2. ghost added
    untriaged
    on Dec 20, 2023
  3. added and removed
    needs-area-label
    on Dec 20, 2023
  4. ivanjx commented on Dec 21, 2023

    @ivanjx
    Author

    closing in favor of #93763

  5. ghost removed
    untriaged
    on Dec 21, 2023
  6. locked and limited conversation to collaborators on Jan 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions