OSDN Git Service

Update Go library to last weekly.
[pf3gnuchains/gcc-fork.git] / libgo / go / archive / tar / common.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package tar implements access to tar archives.
6 // It aims to cover most of the variations, including those produced
7 // by GNU and BSD tars.
8 //
9 // References:
10 //   http://www.freebsd.org/cgi/man.cgi?query=tar&sektion=5
11 //   http://www.gnu.org/software/tar/manual/html_node/Standard.html
12 package tar
13
14 const (
15         blockSize = 512
16
17         // Types
18         TypeReg           = '0'    // regular file.
19         TypeRegA          = '\x00' // regular file.
20         TypeLink          = '1'    // hard link.
21         TypeSymlink       = '2'    // symbolic link.
22         TypeChar          = '3'    // character device node.
23         TypeBlock         = '4'    // block device node.
24         TypeDir           = '5'    // directory.
25         TypeFifo          = '6'    // fifo node.
26         TypeCont          = '7'    // reserved.
27         TypeXHeader       = 'x'    // extended header.
28         TypeXGlobalHeader = 'g'    // global extended header.
29 )
30
31 // A Header represents a single header in a tar archive.
32 // Some fields may not be populated.
33 type Header struct {
34         Name     string // name of header file entry.
35         Mode     int64  // permission and mode bits.
36         Uid      int    // user id of owner.
37         Gid      int    // group id of owner.
38         Size     int64  // length in bytes.
39         Mtime    int64  // modified time; seconds since epoch.
40         Typeflag byte   // type of header entry.
41         Linkname string // target name of link.
42         Uname    string // user name of owner.
43         Gname    string // group name of owner.
44         Devmajor int64  // major number of character or block device.
45         Devminor int64  // minor number of character or block device.
46         Atime    int64  // access time; seconds since epoch.
47         Ctime    int64  // status change time; seconds since epoch.
48
49 }
50
51 var zeroBlock = make([]byte, blockSize)
52
53 // POSIX specifies a sum of the unsigned byte values, but the Sun tar uses signed byte values.
54 // We compute and return both.
55 func checksum(header []byte) (unsigned int64, signed int64) {
56         for i := 0; i < len(header); i++ {
57                 if i == 148 {
58                         // The chksum field (header[148:156]) is special: it should be treated as space bytes.
59                         unsigned += ' ' * 8
60                         signed += ' ' * 8
61                         i += 7
62                         continue
63                 }
64                 unsigned += int64(header[i])
65                 signed += int64(int8(header[i]))
66         }
67         return
68 }
69
70 type slicer []byte
71
72 func (sp *slicer) next(n int) (b []byte) {
73         s := *sp
74         b, *sp = s[0:n], s[n:]
75         return
76 }