forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
43 lines (39 loc) · 902 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
35
36
37
38
39
40
41
42
43
package utils
import (
"io"
"os"
)
// IsFile returns true if a given file path points to an existing file
func IsFile(fp string) bool {
fi, err := os.Stat(fp)
if err == nil {
return !fi.IsDir()
}
return false
}
// 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
}