未验证 提交 68cf2474 编写于 作者: S Stephen Toub 提交者: GitHub

Clean up TarHeader.GlobalHeadFormatPrefix (#77580)

上级 55d35231
......@@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace System.Formats.Tar
{
......@@ -26,7 +25,7 @@ internal PaxGlobalExtendedAttributesTarEntry(TarHeader header, TarReader readerO
/// <param name="globalExtendedAttributes">An enumeration of string key-value pairs that represents the metadata to include as Global Extended Attributes.</param>
/// <exception cref="ArgumentNullException"><paramref name="globalExtendedAttributes"/> is <see langword="null"/>.</exception>
public PaxGlobalExtendedAttributesTarEntry(IEnumerable<KeyValuePair<string, string>> globalExtendedAttributes)
: base(TarEntryType.GlobalExtendedAttributes, TarHeader.GlobalHeadFormatPrefix, TarEntryFormat.Pax, isGea: true)
: base(TarEntryType.GlobalExtendedAttributes, nameof(PaxGlobalExtendedAttributesTarEntry), TarEntryFormat.Pax, isGea: true) // Name == name of type for lack of a better temporary name until the entry is written
{
ArgumentNullException.ThrowIfNull(globalExtendedAttributes);
_header.InitializeExtendedAttributesWithExisting(globalExtendedAttributes);
......
......@@ -893,32 +893,22 @@ private string GenerateExtendedAttributeName()
}
// Gets the special name for the 'name' field in a global extended attribute entry.
// Format: "%d/GlobalHead.%p/%n"
// Format: "%d/GlobalHead.%p.%n"
// - %d: The path of the $TMPDIR variable, if found. Otherwise, the value is '/tmp'.
// - %p: The current process ID.
// - %n: The sequence number of the global extended header record of the archive, starting at 1. In our case, since we only generate one, the value is always 1.
// - %n: The sequence number of the global extended header record of the archive, starting at 1.
// If the path of $TMPDIR makes the final string too long to fit in the 'name' field,
// then the TMPDIR='/tmp' is used.
private static string GenerateGlobalExtendedAttributeName(int globalExtendedAttributesEntryNumber)
{
Debug.Assert(globalExtendedAttributesEntryNumber >= 1);
string tmpDir = Path.GetTempPath();
if (Path.EndsInDirectorySeparator(tmpDir))
{
tmpDir = Path.TrimEndingDirectorySeparator(tmpDir);
}
int processId = Environment.ProcessId;
string result = string.Format(GlobalHeadFormatPrefix, tmpDir, processId);
string suffix = $".{globalExtendedAttributesEntryNumber}"; // GEA sequence number
if (result.Length + suffix.Length >= FieldLengths.Name)
{
result = string.Format(GlobalHeadFormatPrefix, "/tmp", processId);
}
result += suffix;
ReadOnlySpan<char> tmp = Path.TrimEndingDirectorySeparator(Path.GetTempPath());
return result;
string result = $"{tmp}/GlobalHead.{Environment.ProcessId}.{globalExtendedAttributesEntryNumber}";
return result.Length >= FieldLengths.Name ?
string.Concat("/tmp", result.AsSpan(tmp.Length)) :
result;
}
private static int GetUtf8TextLength(ReadOnlySpan<char> text)
......
......@@ -39,11 +39,6 @@ internal sealed partial class TarHeader
private const string PaxEaDevMajor = "devmajor";
private const string PaxEaDevMinor = "devminor";
// Global Extended Attribute entries have a special format in the Name field:
// "{tmpFolder}/GlobalHead.{processId}.{GEAEntryNumber}"
// Excludes ".{GEAEntryNumber}" because the number gets added on write.
internal const string GlobalHeadFormatPrefix = "{0}/GlobalHead.{1}";
internal Stream? _dataStream;
// Position in the stream where the data ends in this header.
......
......@@ -378,7 +378,9 @@ public async Task Add_Empty_GlobalExtendedAttributes_Async()
await using (TarWriter writer = new TarWriter(archive, leaveOpen: true))
{
PaxGlobalExtendedAttributesTarEntry gea = new PaxGlobalExtendedAttributesTarEntry(new Dictionary<string, string>());
Assert.Equal("PaxGlobalExtendedAttributesTarEntry", gea.Name);
await writer.WriteEntryAsync(gea);
Assert.Matches(@".*/GlobalHead\.\d+\.\d+", gea.Name);
}
archive.Seek(0, SeekOrigin.Begin);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册