82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package go4kids
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"syscall"
|
|
)
|
|
|
|
// IsFile is a function that returns true if the path exists and is a regular file,
|
|
// i.e. not a directory, symlink, block device, etc.
|
|
func IsFile(path string) bool {
|
|
if f, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
} else {
|
|
return f.Mode().IsRegular()
|
|
}
|
|
}
|
|
|
|
// IsDir is a function that returns true if the path exists and is a directory.
|
|
func IsDir(path string) bool {
|
|
if f, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
} else {
|
|
return f.IsDir()
|
|
}
|
|
}
|
|
|
|
// PathExists is a function that returns true if the path exists, whether it
|
|
// is a file, directory, symlink, or anything else.
|
|
func PathExists(path string) bool {
|
|
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// IsSymlink is a function that returns true if the path exists and is a symlink.
|
|
func IsSymlink(path string) bool {
|
|
if f, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
} else {
|
|
return f.Mode()&os.ModeSymlink != 0
|
|
}
|
|
}
|
|
|
|
// IsWritable is a function that returns true if the path exists and is writable.
|
|
func IsWritable(path string) bool {
|
|
if f, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
} else {
|
|
return f.Mode().Perm()&0222 != 0
|
|
}
|
|
}
|
|
|
|
// IsReadable is a function that returns true if the path exists and is readable.
|
|
func IsReadable(path string) bool {
|
|
if f, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
} else {
|
|
return f.Mode().Perm()&0444 != 0
|
|
}
|
|
}
|
|
|
|
// IsExecutable is a function that returns true if the path exists and is executable.
|
|
func IsExecutable(path string) bool {
|
|
if f, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
} else {
|
|
return f.Mode().Perm()&0111 != 0
|
|
}
|
|
}
|
|
|
|
// IsOwner is a function that returns true if the path exists and is owned by the
|
|
// current user.
|
|
func IsOwner(path string) bool {
|
|
if f, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
} else {
|
|
return f.Sys().(*syscall.Stat_t).Uid == uint32(os.Getuid())
|
|
}
|
|
}
|