-
Notifications
You must be signed in to change notification settings - Fork 44
/
tarfs.go
162 lines (143 loc) · 3.38 KB
/
tarfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package desync
import (
gnutar "archive/tar"
"io"
"io/ioutil"
"os"
"path"
)
// TarWriter uses a GNU tar archive for tar/untar operations of a catar archive.
type TarWriter struct {
w *gnutar.Writer
format gnutar.Format
}
var _ FilesystemWriter = TarWriter{}
// NewTarFS initializes a new instance of a GNU tar archive that can be used
// for catar archive tar/untar operations.
func NewTarWriter(w io.Writer) TarWriter {
return TarWriter{gnutar.NewWriter(w), gnutar.FormatGNU}
}
func (fs TarWriter) CreateDir(n NodeDirectory) error {
hdr := &gnutar.Header{
Typeflag: gnutar.TypeDir,
Name: n.Name,
Uid: n.UID,
Gid: n.GID,
Mode: int64(n.Mode),
ModTime: n.MTime,
Xattrs: n.Xattrs,
Format: fs.format,
}
return fs.w.WriteHeader(hdr)
}
func (fs TarWriter) CreateFile(n NodeFile) error {
hdr := &gnutar.Header{
Typeflag: gnutar.TypeReg,
Name: n.Name,
Uid: n.UID,
Gid: n.GID,
Mode: int64(n.Mode),
ModTime: n.MTime,
Size: int64(n.Size),
Xattrs: n.Xattrs,
Format: fs.format,
}
if err := fs.w.WriteHeader(hdr); err != nil {
return err
}
_, err := io.Copy(fs.w, n.Data)
return err
}
func (fs TarWriter) CreateSymlink(n NodeSymlink) error {
hdr := &gnutar.Header{
Typeflag: gnutar.TypeSymlink,
Linkname: n.Target,
Name: n.Name,
Uid: n.UID,
Gid: n.GID,
Mode: int64(n.Mode),
ModTime: n.MTime,
Xattrs: n.Xattrs,
Format: fs.format,
}
return fs.w.WriteHeader(hdr)
}
// We're not using os.Filemode here but the low-level system modes where the mode bits
// are in the lower half. Can't use os.ModeCharDevice here.
const modeChar = 0x4000
func (fs TarWriter) CreateDevice(n NodeDevice) error {
var typ byte = gnutar.TypeBlock
if n.Mode&modeChar != 0 {
typ = gnutar.TypeChar
}
hdr := &gnutar.Header{
Typeflag: typ,
Name: n.Name,
Uid: n.UID,
Gid: n.GID,
Mode: int64(n.Mode),
ModTime: n.MTime,
Xattrs: n.Xattrs,
Devmajor: int64(n.Major),
Devminor: int64(n.Minor),
}
return fs.w.WriteHeader(hdr)
}
func (fs TarWriter) Close() error {
return fs.w.Close()
}
// TarReader uses a GNU tar archive as source for a tar operation (to produce
// a catar).
type TarReader struct {
r *gnutar.Reader
root *File
}
type TarReaderOptions struct {
AddRoot bool
}
var _ FilesystemReader = &TarReader{}
// NewTarFS initializes a new instance of a GNU tar archive that can be used
// for catar archive tar/untar operations.
func NewTarReader(r io.Reader, opts TarReaderOptions) *TarReader {
var root *File
if opts.AddRoot {
root = &File{
Name: ".",
Path: ".",
Mode: os.ModeDir | 0755,
}
}
return &TarReader{
r: gnutar.NewReader(r),
root: root,
}
}
// Next returns the next filesystem entry or io.EOF when done. The caller is responsible
// for closing the returned File object.
func (fs *TarReader) Next() (f *File, err error) {
if fs.root != nil {
f = fs.root
fs.root = nil
return f, nil
}
h, err := fs.r.Next()
if err != nil {
return nil, err
}
info := h.FileInfo()
f = &File{
Name: info.Name(),
Path: path.Clean(h.Name),
Mode: info.Mode(),
ModTime: info.ModTime(),
Size: uint64(info.Size()),
LinkTarget: h.Linkname,
Uid: h.Uid,
Gid: h.Gid,
Xattrs: h.Xattrs,
DevMajor: uint64(h.Devmajor),
DevMinor: uint64(h.Devminor),
Data: ioutil.NopCloser(fs.r),
}
return f, nil
}