Skip to content

Commit

Permalink
Tar: Add processing for the LongLink header type
Browse files Browse the repository at this point in the history
Fixes #846
  • Loading branch information
DannyBoyk committed Jun 3, 2024
1 parent 6fc4b04 commit 24aedfb
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/SharpCompress/Common/Tar/Headers/TarHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,57 +101,73 @@ private void WriteLongFilenameHeader(Stream output)

internal bool Read(BinaryReader reader)
{
string? longName = null;
string? longLinkName = null;

read_next_block:
var buffer = ReadBlock(reader);

if (buffer.Length == 0)
{
return false;
}

// for symlinks, additionally read the linkname
if (ReadEntryType(buffer) == EntryType.SymLink)
{
LinkName = ArchiveEncoding.Decode(buffer, 157, 100).TrimNulls();
}
var entryType = ReadEntryType(buffer);

if (ReadEntryType(buffer) == EntryType.LongName)
// LongName and LongLink headers can follow each other and need
// to apply to the header that follows them.
if (entryType == EntryType.LongName)
{
Name = ReadLongName(reader, buffer);
buffer = ReadBlock(reader);
longName = ReadLongName(reader, buffer);
goto read_next_block;
}
else
else if (entryType == EntryType.LongLink)
{
Name = ArchiveEncoding.Decode(buffer, 0, 100).TrimNulls();
longLinkName = ReadLongName(reader, buffer);
goto read_next_block;
}

EntryType = ReadEntryType(buffer);
Name = longName ?? ArchiveEncoding.Decode(buffer, 0, 100).TrimNulls();
EntryType = entryType;
Size = ReadSize(buffer);

// for symlinks, additionally read the linkname
if (entryType == EntryType.SymLink
|| entryType == EntryType.HardLink)
{
LinkName = longLinkName ?? ArchiveEncoding.Decode(buffer, 157, 100).TrimNulls();
}

Mode = ReadAsciiInt64Base8(buffer, 100, 7);

if (EntryType == EntryType.Directory)
{
Mode |= 0b1_000_000_000;
}

UserId = ReadAsciiInt64Base8oldGnu(buffer, 108, 7);
GroupId = ReadAsciiInt64Base8oldGnu(buffer, 116, 7);

var unixTimeStamp = ReadAsciiInt64Base8(buffer, 136, 11);
LastModifiedTime = EPOCH.AddSeconds(unixTimeStamp).ToLocalTime();

LastModifiedTime = EPOCH.AddSeconds(unixTimeStamp).ToLocalTime();
Magic = ArchiveEncoding.Decode(buffer, 257, 6).TrimNulls();

if (!string.IsNullOrEmpty(Magic) && "ustar".Equals(Magic))
{
var namePrefix = ArchiveEncoding.Decode(buffer, 345, 157);
namePrefix = namePrefix.TrimNulls();
var namePrefix = ArchiveEncoding.Decode(buffer, 345, 157).TrimNulls();

if (!string.IsNullOrEmpty(namePrefix))
{
Name = namePrefix + "/" + Name;
}
}

if (EntryType != EntryType.LongName && Name.Length == 0)
{
return false;
}

return true;
}

Expand Down

0 comments on commit 24aedfb

Please sign in to comment.