forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
34 lines (31 loc) · 719 Bytes
/
fs.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
package utils
import (
"io"
"os"
)
// IsDir is a helper function to quickly check if a given path is a valid directory
func IsDir(dirPath string) bool {
fi, err := os.Stat(dirPath)
if err == nil {
return fi.IsDir()
}
return false
}
// ReadAll is similarl to ioutil.ReadAll, except it doesn't use ever-increasing
// internal buffer, instead asking for the exact buffer size.
//
// This is useful when you want to limit the sze of Read/Writes (websockets)
func ReadAll(r io.Reader, bufsize int) (out []byte, err error) {
buff := make([]byte, bufsize)
n := 0
for err == nil {
n, err = r.Read(buff)
if n > 0 {
out = append(out, buff[:n]...)
}
}
if err == io.EOF {
err = nil
}
return out, err
}