This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
272 lines (231 loc) · 5.7 KB
/
encode.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package filestream
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
)
// StreamOptions are configuration options for a stream.
type StreamOptions struct {
// Compression is the compression algorithm to use in transit.
// This package supports "gzip" and "lz4".
// Defaults to no compresion.
Compression string
// CompressionLevel is the level of compresion to use.
// Uses a sane default if omitted.
CompressionLevel int
}
// FileOptions are the set of options which can be applied to a file stream.
type FileOptions struct {
// Permissions are the unix permission code of the file.
// If the permissions component is 000, this will be converted to sane defaults.
// Optional.
Permissions os.FileMode
// User is the username of the owner.
// Optional.
User string
// Group is the groupname of the owning group.
// Optional.
Group string
}
// Writer is an encoder for a filestream.
type Writer struct {
curFile uint64
writing bool
w bufio.Writer
closer io.Closer
closed bool
}
// NewWriter creates a new file stream writer.
func NewWriter(dst io.Writer, opts StreamOptions) (*Writer, error) {
// obtain compressor
var z io.WriteCloser
if opts.Compression != "" {
zr, err := compress(opts.Compression, opts.CompressionLevel, dst)
if err != nil {
return nil, err
}
z = zr
}
// set up writer
w := new(Writer)
w.w = *bufio.NewWriter(dst)
if opts.Compression != "" {
w.closer = z
}
// write header
err := json.NewEncoder(&w.w).Encode(streamHeader{
Version: 0,
Compression: opts.Compression,
})
if err != nil {
return nil, fmt.Errorf("failed to write stream header: %s", err)
}
err = w.w.WriteByte('\x00')
if err != nil {
return nil, fmt.Errorf("failed to write stream header: %s", err)
}
// set destination to compressor
if opts.Compression != "" {
err = w.w.Flush()
if err != nil {
return nil, fmt.Errorf("failed to write stream header: %s", err)
}
w.w.Reset(z)
}
return w, nil
}
// File creates a new file stream at the given path.
// The file must be closed in order to be committed to the stream.
// Attempting to call File or Directory before closing a file may result in an error.
func (w *Writer) File(path string, opts FileOptions) (io.WriteCloser, error) {
if w.writing {
return nil, errors.New("attempted to open a file stream before finishing the previous")
}
w.writing = true
w.curFile++
return &fileWriter{
stream: w,
hdr: fileHeader{
Path: path,
Mode: opts.Permissions,
User: opts.User,
Group: opts.Group,
},
fileNo: w.curFile,
}, nil
}
// Directory creates a directory in the stream with the given path.
func (w *Writer) Directory(path string, opts FileOptions) error {
opts.Permissions |= os.ModeDir
f, err := w.File(path, opts)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return nil
}
// ErrWriteInterrupted indicates that a close operation interrupted a file stream and may have resulted in a corrupted stream.
var ErrWriteInterrupted = errors.New("write interrupted")
// Close ends the stream.
// If a file stream is incomplete, generates a corrupted stream and returns ErrWriteInterrupted.
func (w *Writer) Close() error {
// mark as closed
w.closed = true
// do not terminate incomplete writes
if w.writing {
return ErrWriteInterrupted
}
// write terminating header
err := json.NewEncoder(&w.w).Encode(fileHeader{
Path: "\x00",
})
if err != nil {
return fmt.Errorf("failed to terminate stream: %s", err)
}
err = w.w.WriteByte('\x00')
if err != nil {
return fmt.Errorf("failed to terminate stream: %s", err)
}
// flush stream to compressor
err = w.w.Flush()
if err != nil {
return fmt.Errorf("failed to terminate stream: %s", err)
}
// flush compressor
if w.closer != nil {
err = w.closer.Close()
if err != nil {
return fmt.Errorf("failed to terminate stream: %s", err)
}
}
return nil
}
func (w *Writer) write(file uint64, dat []byte) (int, error) {
// check that stream is open
if w.closed {
return 0, errors.New("filestream closed")
}
// check that file is correct
if file != w.curFile || !w.writing {
return 0, errors.New("writing to file that has already been closed")
}
// write length of chunk
_, err := w.w.WriteString(strconv.Itoa(len(dat)))
if err != nil {
return 0, err
}
err = w.w.WriteByte('\x00')
if err != nil {
return 0, err
}
// write data
n, err := w.w.Write(dat)
if err != nil {
return n, err
}
return len(dat), nil
}
func (w *Writer) startFile(hdr fileHeader) error {
if w.closed {
return errors.New("filestream closed")
}
if strings.Contains(hdr.Path, "\x00") {
return errors.New("illegal null character in file path")
}
err := json.NewEncoder(&w.w).Encode(hdr)
if err != nil {
return fmt.Errorf("failed to start file stream: %s", err)
}
err = w.w.WriteByte('\x00')
if err != nil {
return fmt.Errorf("failed to start file stream: %s", err)
}
return nil
}
// fileWriter is a stream for writing a file.
type fileWriter struct {
stream *Writer
fileNo uint64
started bool
hdr fileHeader
}
// Write writes the data to the file stream.
func (fw *fileWriter) Write(data []byte) (int, error) {
if !fw.started {
fw.started = true
err := fw.stream.startFile(fw.hdr)
if err != nil {
return 0, err
}
}
if len(data) == 0 {
return 0, nil
}
return fw.stream.write(fw.fileNo, data)
}
// Close closes a file stream.
func (fw *fileWriter) Close() error {
// for 0 length files, start the stream
if !fw.started {
_, err := fw.Write(nil)
if err != nil {
return err
}
}
// write terminating 0 length chunk
_, err := fw.stream.write(fw.fileNo, nil)
if err != nil {
return err
}
// mark as no longer writing
fw.stream.writing = false
return nil
}