From 250a2c6dee921c046c6d837baddfca96c593f4df Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Mon, 14 Aug 2023 20:28:36 -0400 Subject: [PATCH] Ignore global header entries when creating an FS Signed-off-by: Joe Lanford --- fs.go | 3 +++ fs_test.go | 16 ++++++++++++++++ test-with-global-header.tar | Bin 0 -> 10240 bytes 3 files changed, 19 insertions(+) create mode 100644 test-with-global-header.tar diff --git a/fs.go b/fs.go index cb66f4b..1acaaeb 100644 --- a/fs.go +++ b/fs.go @@ -53,6 +53,9 @@ func New(r io.Reader) (fs.FS, error) { if err != nil { return nil, err } + if h.Typeflag == tar.TypeXGlobalHeader { + continue + } name := path.Clean(h.Name) if name == "." { diff --git a/fs_test.go b/fs_test.go index e15fd5c..e32a0e8 100644 --- a/fs_test.go +++ b/fs_test.go @@ -413,3 +413,19 @@ func TestSparse(t *testing.T) { assert.Equal([]byte("file2"), file2Actual, "fs.ReadFile(tfs, \"file2\")") } } + +func TestIgnoreGlobalHeader(t *testing.T) { + require := require.New(t) + + // This file was created by initializing a git repository, + // committing a few files, and running: `git archive HEAD` + f, err := os.Open("test-with-global-header.tar") + require.NoError(err) + defer f.Close() + + tfs, err := New(f) + require.NoError(err) + + err = fstest.TestFS(tfs, "bar", "dir1", "dir1/file11") + require.NoError(err) +} diff --git a/test-with-global-header.tar b/test-with-global-header.tar new file mode 100644 index 0000000000000000000000000000000000000000..7543a9c73ab4e9afbd1b4ff3775d537d9e62ffb4 GIT binary patch literal 10240 zcmeH|QEtN^42F5`DRzSb0n+pYs08)PJ1+<6P&6F5LP# xO2ix>00JNY0w4eaAOHd&00JNY0w4eaAOHd&00JNY0w4eaAOHd&00I*UoB(2-YUTg{ literal 0 HcmV?d00001