| |
| |
| |
|
|
| package tar |
|
|
| import "strings" |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| type Format int |
|
|
| |
| const ( |
| |
| _ Format = (1 << iota) / 4 |
|
|
| |
| FormatUnknown |
|
|
| |
| formatV7 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| FormatUSTAR |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| FormatPAX |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| FormatGNU |
|
|
| |
| |
| |
| formatSTAR |
|
|
| formatMax |
| ) |
|
|
| func (f Format) has(f2 Format) bool { return f&f2 != 0 } |
| func (f *Format) mayBe(f2 Format) { *f |= f2 } |
| func (f *Format) mayOnlyBe(f2 Format) { *f &= f2 } |
| func (f *Format) mustNotBe(f2 Format) { *f &^= f2 } |
|
|
| var formatNames = map[Format]string{ |
| formatV7: "V7", FormatUSTAR: "USTAR", FormatPAX: "PAX", FormatGNU: "GNU", formatSTAR: "STAR", |
| } |
|
|
| func (f Format) String() string { |
| var ss []string |
| for f2 := Format(1); f2 < formatMax; f2 <<= 1 { |
| if f.has(f2) { |
| ss = append(ss, formatNames[f2]) |
| } |
| } |
| switch len(ss) { |
| case 0: |
| return "<unknown>" |
| case 1: |
| return ss[0] |
| default: |
| return "(" + strings.Join(ss, " | ") + ")" |
| } |
| } |
|
|
| |
| const ( |
| magicGNU, versionGNU = "ustar ", " \x00" |
| magicUSTAR, versionUSTAR = "ustar\x00", "00" |
| trailerSTAR = "tar\x00" |
| ) |
|
|
| |
| const ( |
| blockSize = 512 |
| nameSize = 100 |
| prefixSize = 155 |
|
|
| |
| |
| maxSpecialFileSize = 1 << 20 |
|
|
| |
| |
| |
| |
| maxSparseFileEntries = 1 << 20 |
| ) |
|
|
| |
| |
| func blockPadding(offset int64) (n int64) { |
| return -offset & (blockSize - 1) |
| } |
|
|
| var zeroBlock block |
|
|
| type block [blockSize]byte |
|
|
| |
| func (b *block) toV7() *headerV7 { return (*headerV7)(b) } |
| func (b *block) toGNU() *headerGNU { return (*headerGNU)(b) } |
| func (b *block) toSTAR() *headerSTAR { return (*headerSTAR)(b) } |
| func (b *block) toUSTAR() *headerUSTAR { return (*headerUSTAR)(b) } |
| func (b *block) toSparse() sparseArray { return sparseArray(b[:]) } |
|
|
| |
| |
| |
| func (b *block) getFormat() Format { |
| |
| var p parser |
| value := p.parseOctal(b.toV7().chksum()) |
| chksum1, chksum2 := b.computeChecksum() |
| if p.err != nil || (value != chksum1 && value != chksum2) { |
| return FormatUnknown |
| } |
|
|
| |
| magic := string(b.toUSTAR().magic()) |
| version := string(b.toUSTAR().version()) |
| trailer := string(b.toSTAR().trailer()) |
| switch { |
| case magic == magicUSTAR && trailer == trailerSTAR: |
| return formatSTAR |
| case magic == magicUSTAR: |
| return FormatUSTAR | FormatPAX |
| case magic == magicGNU && version == versionGNU: |
| return FormatGNU |
| default: |
| return formatV7 |
| } |
| } |
|
|
| |
| |
| func (b *block) setFormat(format Format) { |
| |
| switch { |
| case format.has(formatV7): |
| |
| case format.has(FormatGNU): |
| copy(b.toGNU().magic(), magicGNU) |
| copy(b.toGNU().version(), versionGNU) |
| case format.has(formatSTAR): |
| copy(b.toSTAR().magic(), magicUSTAR) |
| copy(b.toSTAR().version(), versionUSTAR) |
| copy(b.toSTAR().trailer(), trailerSTAR) |
| case format.has(FormatUSTAR | FormatPAX): |
| copy(b.toUSTAR().magic(), magicUSTAR) |
| copy(b.toUSTAR().version(), versionUSTAR) |
| default: |
| panic("invalid format") |
| } |
|
|
| |
| |
| var f formatter |
| field := b.toV7().chksum() |
| chksum, _ := b.computeChecksum() |
| f.formatOctal(field[:7], chksum) |
| field[7] = ' ' |
| } |
|
|
| |
| |
| |
| |
| func (b *block) computeChecksum() (unsigned, signed int64) { |
| for i, c := range b { |
| if 148 <= i && i < 156 { |
| c = ' ' |
| } |
| unsigned += int64(c) |
| signed += int64(int8(c)) |
| } |
| return unsigned, signed |
| } |
|
|
| |
| func (b *block) reset() { |
| *b = block{} |
| } |
|
|
| type headerV7 [blockSize]byte |
|
|
| func (h *headerV7) name() []byte { return h[000:][:100] } |
| func (h *headerV7) mode() []byte { return h[100:][:8] } |
| func (h *headerV7) uid() []byte { return h[108:][:8] } |
| func (h *headerV7) gid() []byte { return h[116:][:8] } |
| func (h *headerV7) size() []byte { return h[124:][:12] } |
| func (h *headerV7) modTime() []byte { return h[136:][:12] } |
| func (h *headerV7) chksum() []byte { return h[148:][:8] } |
| func (h *headerV7) typeFlag() []byte { return h[156:][:1] } |
| func (h *headerV7) linkName() []byte { return h[157:][:100] } |
|
|
| type headerGNU [blockSize]byte |
|
|
| func (h *headerGNU) v7() *headerV7 { return (*headerV7)(h) } |
| func (h *headerGNU) magic() []byte { return h[257:][:6] } |
| func (h *headerGNU) version() []byte { return h[263:][:2] } |
| func (h *headerGNU) userName() []byte { return h[265:][:32] } |
| func (h *headerGNU) groupName() []byte { return h[297:][:32] } |
| func (h *headerGNU) devMajor() []byte { return h[329:][:8] } |
| func (h *headerGNU) devMinor() []byte { return h[337:][:8] } |
| func (h *headerGNU) accessTime() []byte { return h[345:][:12] } |
| func (h *headerGNU) changeTime() []byte { return h[357:][:12] } |
| func (h *headerGNU) sparse() sparseArray { return sparseArray(h[386:][:24*4+1]) } |
| func (h *headerGNU) realSize() []byte { return h[483:][:12] } |
|
|
| type headerSTAR [blockSize]byte |
|
|
| func (h *headerSTAR) v7() *headerV7 { return (*headerV7)(h) } |
| func (h *headerSTAR) magic() []byte { return h[257:][:6] } |
| func (h *headerSTAR) version() []byte { return h[263:][:2] } |
| func (h *headerSTAR) userName() []byte { return h[265:][:32] } |
| func (h *headerSTAR) groupName() []byte { return h[297:][:32] } |
| func (h *headerSTAR) devMajor() []byte { return h[329:][:8] } |
| func (h *headerSTAR) devMinor() []byte { return h[337:][:8] } |
| func (h *headerSTAR) prefix() []byte { return h[345:][:131] } |
| func (h *headerSTAR) accessTime() []byte { return h[476:][:12] } |
| func (h *headerSTAR) changeTime() []byte { return h[488:][:12] } |
| func (h *headerSTAR) trailer() []byte { return h[508:][:4] } |
|
|
| type headerUSTAR [blockSize]byte |
|
|
| func (h *headerUSTAR) v7() *headerV7 { return (*headerV7)(h) } |
| func (h *headerUSTAR) magic() []byte { return h[257:][:6] } |
| func (h *headerUSTAR) version() []byte { return h[263:][:2] } |
| func (h *headerUSTAR) userName() []byte { return h[265:][:32] } |
| func (h *headerUSTAR) groupName() []byte { return h[297:][:32] } |
| func (h *headerUSTAR) devMajor() []byte { return h[329:][:8] } |
| func (h *headerUSTAR) devMinor() []byte { return h[337:][:8] } |
| func (h *headerUSTAR) prefix() []byte { return h[345:][:155] } |
|
|
| type sparseArray []byte |
|
|
| func (s sparseArray) entry(i int) sparseElem { return sparseElem(s[i*24:]) } |
| func (s sparseArray) isExtended() []byte { return s[24*s.maxEntries():][:1] } |
| func (s sparseArray) maxEntries() int { return len(s) / 24 } |
|
|
| type sparseElem []byte |
|
|
| func (s sparseElem) offset() []byte { return s[00:][:12] } |
| func (s sparseElem) length() []byte { return s[12:][:12] } |
|
|