forked from pactus-project/pactus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.go
256 lines (205 loc) · 5.67 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
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
package util
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func IsAbsPath(path string) bool {
return filepath.IsAbs(path)
}
func MakeAbs(path string) string {
if IsAbsPath(path) {
return path
}
wd, err := os.Getwd()
if err != nil {
panic(err)
}
return filepath.Clean(filepath.Join(wd, path))
}
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(path string) error {
// create the directory
if err := os.MkdirAll(path, 0o750); err != nil {
return fmt.Errorf("could not create directory %s", path)
}
return nil
}
func PathExists(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return err == nil
}
func TempDirPath() string {
path, err := os.MkdirTemp("", "pactus*")
if err != nil {
panic(err)
}
return path
}
func TempFilePath() string {
return filepath.Join(TempDirPath(), "file")
}
// IsDirEmpty checks if a directory is empty.
func IsDirEmpty(path string) bool {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer func() {
_ = file.Close()
}()
// read in ONLY one file
_, err = file.Readdir(1)
// and if the file is EOF... well, the dir is empty.
return errors.Is(err, io.EOF)
}
// IsDirNotExistsOrEmpty checks if the path exists and, if so, whether the directory is empty.
func IsDirNotExistsOrEmpty(path string) bool {
if !PathExists(path) {
return true
}
return IsDirEmpty(path)
}
func IsValidDirPath(path string) bool {
fi, err := os.Stat(path)
if err == nil {
if fi.IsDir() {
if err := os.WriteFile(path+"/test", []byte{}, 0o600); err != nil {
return false
}
_ = os.Remove(path + "/test")
return true
}
return false
}
if err := Mkdir(path); err != nil {
return false
}
_ = os.Remove(path)
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(data []byte) (int, error) {
lenp := len(data)
if w.pos+lenp > cap(w.b) {
return 0, io.ErrShortWrite
}
w.pos += copy(w.b[w.pos:], data)
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) {
count, err := fr.iobuf.Read(p)
if err != nil {
return 0, err
}
fr.pos += count
return count, nil
}
// NewFixedReader returns a new io.Reader that will error once more bytes than
// the specified max have been read.
func NewFixedReader(max int, data []byte) *FixedReader {
buf := make([]byte, max)
if data != nil {
copy(buf, data)
}
iobuf := bytes.NewBuffer(buf)
fr := FixedReader{buf, 0, iobuf}
return &fr
}
// MoveDirectory moves a directory from srcDir to dstDir, including all its contents.
// If dstDir already exists and is not empty, it returns an error.
// If the parent directory of dstDir does not exist, it will be created.
func MoveDirectory(srcDir, dstDir string) error {
if PathExists(dstDir) {
return fmt.Errorf("destination directory %s already exists", dstDir)
}
// Get the parent directory of the destination directory
parentDir := filepath.Dir(dstDir)
if err := Mkdir(parentDir); err != nil {
return fmt.Errorf("failed to create parent directories for %s: %w", dstDir, err)
}
if err := os.Rename(srcDir, dstDir); err != nil {
return fmt.Errorf("failed to move directory from %s to %s: %w", srcDir, dstDir, err)
}
return nil
}
// SanitizeArchivePath mitigates the "Zip Slip" vulnerability by sanitizing archive file paths.
// It ensures that the file path is contained within the specified base directory to prevent directory
// traversal attacks. For more details on the vulnerability, see https://snyk.io/research/zip-slip-vulnerability.
func SanitizeArchivePath(baseDir, archivePath string) (fullPath string, err error) {
fullPath = filepath.Join(baseDir, archivePath)
if strings.HasPrefix(fullPath, filepath.Clean(baseDir)) {
return fullPath, nil
}
return "", fmt.Errorf("%s: %s", "content filepath is tainted", archivePath)
}
// ListFilesInDir return list of files in directory.
func ListFilesInDir(dir string) ([]string, error) {
files := make([]string, 0)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}