-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
72 lines (60 loc) · 1.33 KB
/
writer.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
package inode
import (
"errors"
"os"
"github.com/mdouchement/fichenn/tarball"
)
// ErrUnsupported is used when an action is not supported.
var ErrUnsupported = errors.New("unsupported action")
// A Writer can write a folder from a tarball or a file.
type Writer struct {
write func(p []byte) (int, error)
sync func() error
close func() error
stat func() (os.FileInfo, error)
}
// NewWriter returns a new Writer.
func NewWriter(inode string, extract bool) (*Writer, error) {
if extract {
tw, err := tarball.NewWriter(inode)
if err != nil {
return nil, err
}
return &Writer{
write: tw.Write,
sync: func() error {
return nil
},
close: tw.Close,
stat: func() (os.FileInfo, error) {
return nil, ErrUnsupported
},
}, nil
}
f, err := os.Create(inode)
if err != nil {
return nil, err
}
return &Writer{
write: f.Write,
sync: f.Sync,
close: f.Close,
stat: f.Stat,
}, nil
}
// Write implements io.Writer.
func (w *Writer) Write(p []byte) (int, error) {
return w.write(p)
}
// Close implements io.Closer.
func (w *Writer) Close() error {
return w.close()
}
// Sync commits the current contents of the file to stable storage.
func (w *Writer) Sync() error {
return w.sync()
}
// Stat returns the FileInfo structure describing file.
func (w *Writer) Stat() (os.FileInfo, error) {
return w.stat()
}