From da9e2e718445fe9f35924c3c19c74cdd2c3a230c Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 12 Sep 2015 03:54:43 -0700 Subject: [PATCH] Adding some file access utils. --- fileutil/fileutil.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 fileutil/fileutil.go diff --git a/fileutil/fileutil.go b/fileutil/fileutil.go new file mode 100644 index 0000000..b7575ed --- /dev/null +++ b/fileutil/fileutil.go @@ -0,0 +1,47 @@ +// Package fileutil contains common file functions. +package fileutil + +import ( + "os" + + "golang.org/x/sys/unix" +) + +// FileExistsP returns true if the file exists. +func FileExistsP(path string) bool { + _, err := os.Stat(path) + return !os.IsNotExist(err) +} + +// DirectoryExistsP returns true if the file exists. +func DirectoryExistsP(path string) bool { + fi, err := os.Stat(path) + if err != nil { + return false + } + + return fi.Mode().IsDir() +} + +const ( + // AccessExists checks whether the file exists. + AccessExists = unix.F_OK + + // AccessRead checks whether the user has read permissions on + // the file. + AccessRead = unix.R_OK + + // AccessWrite checks whether the user has write permissions + // on the file. + AccessWrite = unix.W_OK + + // AccessExec checks whether the user has executable + // permissions on the file. + AccessExec = unix.X_OK +) + +// Access returns a boolean indicating whether the mode being checked +// for is valid. +func Access(path string, mode int) error { + return unix.Access(path, uint32(mode)) +}