| 1 |
// Package fileutil contains common file functions. |
| 2 |
package fileutil |
| 3 |
|
| 4 |
import ( |
| 5 |
"os" |
| 6 |
|
| 7 |
"golang.org/x/sys/unix" |
| 8 |
) |
| 9 |
|
| 10 |
// FileDoesExist returns true if the file exists. |
| 11 |
func FileDoesExist(path string) bool { |
| 12 |
_, err := os.Stat(path) |
| 13 |
return !os.IsNotExist(err) |
| 14 |
} |
| 15 |
|
| 16 |
// DirectoryDoesExist returns true if the file exists. |
| 17 |
func DirectoryDoesExist(path string) bool { |
| 18 |
fi, err := os.Stat(path) |
| 19 |
if err != nil { |
| 20 |
return false |
| 21 |
} |
| 22 |
|
| 23 |
return fi.Mode().IsDir() |
| 24 |
} |
| 25 |
|
| 26 |
const ( |
| 27 |
// AccessExists checks whether the file exists. |
| 28 |
AccessExists = unix.F_OK |
| 29 |
|
| 30 |
// AccessRead checks whether the user has read permissions on |
| 31 |
// the file. |
| 32 |
AccessRead = unix.R_OK |
| 33 |
|
| 34 |
// AccessWrite checks whether the user has write permissions |
| 35 |
// on the file. |
| 36 |
AccessWrite = unix.W_OK |
| 37 |
|
| 38 |
// AccessExec checks whether the user has executable |
| 39 |
// permissions on the file. |
| 40 |
AccessExec = unix.X_OK |
| 41 |
) |
| 42 |
|
| 43 |
// Access returns a boolean indicating whether the mode being checked |
| 44 |
// for is valid. |
| 45 |
func Access(path string, mode int) error { |
| 46 |
return unix.Access(path, uint32(mode)) |
| 47 |
} |