From 17fa5a7c9f41bed796b5e236b517d0bf03a3f31d Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Mon, 14 Aug 2017 16:14:08 -0700 Subject: [PATCH] archive/tar: roundtrip reading device numbers Both GNU and BSD tar do not care if the devmajor and devminor values are set on entries (like regular files) that aren't character or block devices. While this is non-sensible, it is more consistent with the Writer to actually read these fields always. In a vast majority of the cases these will still be zero. In the rare situation where someone actually cares about these, at least information was not silently lost. Change-Id: I6e4ba01cd897a1b13c28b1837e102a4fdeb420ba Reviewed-on: https://go-review.googlesource.com/55572 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/archive/tar/reader.go | 6 ++---- src/archive/tar/reader_test.go | 11 +++++++++++ src/archive/tar/testdata/ustar-file-devs.tar | Bin 0 -> 1536 bytes 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 src/archive/tar/testdata/ustar-file-devs.tar diff --git a/src/archive/tar/reader.go b/src/archive/tar/reader.go index 21c0330420..98f6ea86fa 100644 --- a/src/archive/tar/reader.go +++ b/src/archive/tar/reader.go @@ -468,10 +468,8 @@ func (tr *Reader) readHeader() (*Header, *block, error) { ustar := tr.blk.USTAR() hdr.Uname = p.parseString(ustar.UserName()) hdr.Gname = p.parseString(ustar.GroupName()) - if hdr.Typeflag == TypeChar || hdr.Typeflag == TypeBlock { - hdr.Devmajor = p.parseNumeric(ustar.DevMajor()) - hdr.Devminor = p.parseNumeric(ustar.DevMinor()) - } + hdr.Devmajor = p.parseNumeric(ustar.DevMajor()) + hdr.Devminor = p.parseNumeric(ustar.DevMinor()) var prefix string switch format { diff --git a/src/archive/tar/reader_test.go b/src/archive/tar/reader_test.go index 3592a14842..79d271717c 100644 --- a/src/archive/tar/reader_test.go +++ b/src/archive/tar/reader_test.go @@ -384,6 +384,17 @@ func TestReader(t *testing.T) { Uid: 010000000, ModTime: time.Unix(0, 0), }}, + }, { + // USTAR archive with a regular entry with non-zero device numbers. + file: "testdata/ustar-file-devs.tar", + headers: []*Header{{ + Name: "file", + Mode: 0644, + Typeflag: '0', + ModTime: time.Unix(0, 0), + Devmajor: 1, + Devminor: 1, + }}, }} for _, v := range vectors { diff --git a/src/archive/tar/testdata/ustar-file-devs.tar b/src/archive/tar/testdata/ustar-file-devs.tar new file mode 100644 index 0000000000000000000000000000000000000000..146e25b79d8980a5eb0d370d0d9a0b7534c05135 GIT binary patch literal 1536 zcmYex%t>XSF)#oEGZPaA5Df(AG_n|kZD?R%Y|fxyKx21-jV~=ONh|_7iBh{jE;leR XgnG`9QsYMTkA}c#2#kgRtwR6+ua*#N literal 0 HcmV?d00001 -- 2.50.0