]> Cypherpunks repositories - gostls13.git/commitdiff
archive/tar: use placeholder name for global PAX records
authorJoe Tsai <joetsai@digital-static.net>
Thu, 16 Nov 2017 18:15:34 +0000 (10:15 -0800)
committerJoe Tsai <thebrokentoaster@gmail.com>
Wed, 29 Nov 2017 19:04:57 +0000 (19:04 +0000)
Several usages of tar (reasonably) just use the Header.FileInfo
to determine the type of the header. However, the os.FileMode type
is not expressive enough to represent "files" that are not files
at all, but some form of metadata.

Thus, Header{Typeflag: TypeXGlobalHeader}.FileInfo().Mode().IsRegular()
reports true, even though the expected result may have been false.

To reduce (not eliminate) the possibility of failure for such usages,
use the placeholder filename from the global PAX headers.
Thus, in the event the user did not handle special "meta" headers
specifically, they will just be written to disk as a regular file.

As an example use case, the "git archive --format=tgz" command produces
an archive where the first "file" is a global PAX header with the
name "global_pax_header". For users that do not explicitly check
the Header.Typeflag field to ignore such headers, they may end up
extracting a file named "global_pax_header". While it is a bogus file,
it at least does not stop the extraction process.

Updates #22748

Change-Id: I28448b528dcfacb4e92311824c33c71b482f49c9
Reviewed-on: https://go-review.googlesource.com/78355
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/archive/tar/common.go
src/archive/tar/reader.go
src/archive/tar/reader_test.go
src/archive/tar/writer.go

index 1d24faa6f617767afef530c18b74b6832d512d26..4a2c173bf3a7c669b56db744a2e1627ef4b3f402 100644 (file)
@@ -438,8 +438,9 @@ func (h Header) allowedFormats() (format Format, paxHdrs map[string]string, err
        case TypeXHeader, TypeGNULongName, TypeGNULongLink:
                return FormatUnknown, nil, headerError{"cannot manually encode TypeXHeader, TypeGNULongName, or TypeGNULongLink headers"}
        case TypeXGlobalHeader:
-               if !reflect.DeepEqual(h, Header{Typeflag: h.Typeflag, Xattrs: h.Xattrs, PAXRecords: h.PAXRecords, Format: h.Format}) {
-                       return FormatUnknown, nil, headerError{"only PAXRecords may be set for TypeXGlobalHeader"}
+               h2 := Header{Name: h.Name, Typeflag: h.Typeflag, Xattrs: h.Xattrs, PAXRecords: h.PAXRecords, Format: h.Format}
+               if !reflect.DeepEqual(h, h2) {
+                       return FormatUnknown, nil, headerError{"only PAXRecords should be set for TypeXGlobalHeader"}
                }
                whyOnlyPAX = "only PAX supports TypeXGlobalHeader"
                format.mayOnlyBe(FormatPAX)
index 4ec78a5eca0e65d51699fe030c11d34ef4ebc016..f4eeb557be9805a112bb56eeb1fc14904ce178cd 100644 (file)
@@ -95,6 +95,7 @@ loop:
                        if hdr.Typeflag == TypeXGlobalHeader {
                                mergePAX(hdr, paxHdrs)
                                return &Header{
+                                       Name:       hdr.Name,
                                        Typeflag:   hdr.Typeflag,
                                        Xattrs:     hdr.Xattrs,
                                        PAXRecords: hdr.PAXRecords,
index 2d8474fdcbfb94c250a2e270db57a6eb7ee05197..a6832d33b1b58575ba42dbfb602f2dab770ef992 100644 (file)
@@ -285,6 +285,7 @@ func TestReader(t *testing.T) {
                file: "testdata/pax-global-records.tar",
                headers: []*Header{{
                        Typeflag:   TypeXGlobalHeader,
+                       Name:       "global1",
                        PAXRecords: map[string]string{"path": "global1", "mtime": "1500000000.0"},
                        Format:     FormatPAX,
                }, {
@@ -300,6 +301,7 @@ func TestReader(t *testing.T) {
                        Format:     FormatPAX,
                }, {
                        Typeflag:   TypeXGlobalHeader,
+                       Name:       "GlobalHead.0.0",
                        PAXRecords: map[string]string{"path": ""},
                        Format:     FormatPAX,
                }, {
index 79b06b334f582a1d81a03345ece5e1d82a0dd76a..97d23f80388ee86c8be72f080e8d3d53e2f983b6 100644 (file)
@@ -179,7 +179,10 @@ func (tw *Writer) writePAXHeader(hdr *Header, paxHdrs map[string]string) error {
                var name string
                var flag byte
                if isGlobal {
-                       name = "GlobalHead.0.0"
+                       name = realName
+                       if name == "" {
+                               name = "GlobalHead.0.0"
+                       }
                        flag = TypeXGlobalHeader
                } else {
                        dir, file := path.Split(realName)