Created
January 22, 2016 19:55
-
-
Save mwmahlberg/24807f2a900860693668 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"io/ioutil" | |
) | |
// FileReadable returns true only if path is readable. | |
func FileReadable(path string) bool { | |
fi, err := os.Stat(path) | |
if err != nil { | |
// We can not call stats, so it is definetly not readable | |
return false | |
} | |
if fi.Mode().IsRegular() { | |
// Open in RDONLY mode... | |
f, err := os.Open(path) | |
if err != nil { | |
// ...didn't work | |
return false | |
} | |
f.Close() | |
return true | |
} else if fi.Mode().IsDir() { | |
if _, err := ioutil.ReadDir(path); err != nil { | |
// Reading content of directory did not work | |
return false | |
} | |
return true | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment