add FileExists and DirExists for checking what is at a path
FileExists reports whether a path holds a regular file and DirExists whether it holds a directory, both answering false for anything they cannot look at. Comes with doc comments and table-driven tests. (closes #15) Model: opus-5
This commit is contained in:
31
exists.go
Normal file
31
exists.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package util
|
||||
|
||||
import "os"
|
||||
|
||||
// FileExists reports whether there is a regular file at path. A directory, a
|
||||
// device node or a socket gives false, and so does anything the process is not
|
||||
// allowed to look at, since from the caller's point of view there is no usable
|
||||
// file there either way.
|
||||
//
|
||||
// Symbolic links are followed, so a link pointing at a regular file gives true
|
||||
// and a link pointing at nothing gives false.
|
||||
func FileExists(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return info.Mode().IsRegular()
|
||||
}
|
||||
|
||||
// DirExists reports whether there is a directory at path. As with FileExists,
|
||||
// anything that cannot be looked at gives false, and symbolic links are
|
||||
// followed.
|
||||
func DirExists(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return info.IsDir()
|
||||
}
|
||||
Reference in New Issue
Block a user