From e4bde0510465eecd4c8a8293418b1cbed1e0e623 Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Mon, 1 Jan 2018 16:16:43 -0800 Subject: archive/tar: automatically promote TypeRegA Change Reader to promote TypeRegA to TypeReg in headers, unless their name have a trailing slash which is already promoted to TypeDir. This will allow client code to handle just TypeReg instead both TypeReg and TypeRegA. Change Writer to promote TypeRegA to TypeReg or TypeDir in the headers depending on whether the name has a trailing slash. This normalization is motivated by the specification (in pax(1)): 0 represents a regular file. For backwards-compatibility, a typeflag value of binary zero ( '\0' ) should be recognized as meaning a regular file when extracting files from the archive. Archives written with this version of the archive file format create regular files with a typeflag value of the ISO/IEC 646:1991 standard IRV '0'. Fixes #22768. Change-Id: I149ec55824580d446cdde5a0d7a0457ad7b03466 Reviewed-on: https://go-review.googlesource.com/85656 Reviewed-by: Joe Tsai Run-TryBot: Joe Tsai TryBot-Result: Gobot Gobot --- src/archive/tar/writer.go | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/archive/tar/writer.go') diff --git a/src/archive/tar/writer.go b/src/archive/tar/writer.go index 97d23f8038..d6f69314e0 100644 --- a/src/archive/tar/writer.go +++ b/src/archive/tar/writer.go @@ -71,6 +71,16 @@ func (tw *Writer) WriteHeader(hdr *Header) error { } tw.hdr = *hdr // Shallow copy of Header + // Avoid usage of the legacy TypeRegA flag, and automatically promote + // it to use TypeReg or TypeDir. + if tw.hdr.Typeflag == TypeRegA { + if strings.HasSuffix(tw.hdr.Name, "/") { + tw.hdr.Typeflag = TypeDir + } else { + tw.hdr.Typeflag = TypeReg + } + } + // Round ModTime and ignore AccessTime and ChangeTime unless // the format is explicitly chosen. // This ensures nominal usage of WriteHeader (without specifying the format) -- cgit v1.2.1