/
io.go
196 lines (156 loc) · 3.7 KB
/
io.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
package util
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
func IsAbsPath(p string) bool {
return filepath.IsAbs(p)
}
func MakeAbs(p string) string {
if IsAbsPath(p) {
return p
}
wd, err := os.Getwd()
if err != nil {
panic(err)
}
return filepath.Clean(filepath.Join(wd, p))
}
func ReadFile(filename string) ([]byte, error) {
return os.ReadFile(filename)
}
func WriteFile(filename string, data []byte) error {
// create directory
if err := Mkdir(filepath.Dir(filename)); err != nil {
return err
}
if err := os.WriteFile(filename, data, 0o600); err != nil {
return fmt.Errorf("failed to write to %s: %w", filename, err)
}
return nil
}
func Mkdir(dir string) error {
// create the directory
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("could not create directory %s", dir)
}
return nil
}
func PathExists(p string) bool {
_, err := os.Stat(p)
if os.IsNotExist(err) {
return false
}
return err == nil
}
func TempDirPath() string {
p, err := os.MkdirTemp("", "pactus*")
if err != nil {
panic(err)
}
return p
}
func TempFilePath() string {
return filepath.Join(TempDirPath(), "file")
}
func IsDirEmpty(name string) bool {
f, err := os.Open(name)
if err != nil {
panic(err)
}
defer f.Close()
// read in ONLY one file
_, err = f.Readdir(1)
// and if the file is EOF... well, the dir is empty.
return errors.Is(err, io.EOF)
}
func IsDirNotExistsOrEmpty(name string) bool {
if !PathExists(name) {
return true
}
return IsDirEmpty(name)
}
func IsValidDirPath(fp string) bool {
fi, err := os.Stat(fp)
if err == nil {
if fi.IsDir() {
if err := os.WriteFile(fp+"/test", []byte{}, 0o600); err != nil {
return false
}
os.Remove(fp + "/test")
return true
}
return false
}
if err := Mkdir(fp); err != nil {
return false
}
os.Remove(fp)
return true
}
// TODO: move these to a test suite
// fixedWriter implements the io.Writer interface and intentionally allows
// testing of error paths by forcing short writes.
type FixedWriter struct {
b []byte
pos int
}
// Write writes the contents of p to w. When the contents of p would cause
// the writer to exceed the maximum allowed size of the fixed writer,
// io.ErrShortWrite is returned and the writer is left unchanged.
//
// This satisfies the io.Writer interface.
func (w *FixedWriter) Write(p []byte) (int, error) {
lenp := len(p)
if w.pos+lenp > cap(w.b) {
return 0, io.ErrShortWrite
}
w.pos += copy(w.b[w.pos:], p)
return lenp, nil
}
// Bytes returns the bytes already written to the fixed writer.
func (w *FixedWriter) Bytes() []byte {
return w.b
}
// newFixedWriter returns a new io.Writer that will error once more bytes than
// the specified max have been written.
func NewFixedWriter(max int) *FixedWriter {
b := make([]byte, max)
fw := FixedWriter{b, 0}
return &fw
}
// fixedReader implements the io.Reader interface and intentionally allows
// testing of error paths by forcing short reads.
type FixedReader struct {
buf []byte
pos int
iobuf *bytes.Buffer
}
// Read reads the next len(p) bytes from the fixed reader. When the number of
// bytes read would exceed the maximum number of allowed bytes to be read from
// the fixed writer, an error is returned.
//
// This satisfies the io.Reader interface.
func (fr *FixedReader) Read(p []byte) (int, error) {
n, err := fr.iobuf.Read(p)
if err != nil {
return 0, err
}
fr.pos += n
return n, nil
}
// newFixedReader returns a new io.Reader that will error once more bytes than
// the specified max have been read.
func NewFixedReader(max int, buf []byte) *FixedReader {
b := make([]byte, max)
if buf != nil {
copy(b, buf)
}
iobuf := bytes.NewBuffer(b)
fr := FixedReader{b, 0, iobuf}
return &fr
}