Пример #1
0
Файл: dir.go Проект: dvln/util
// Exists checks if given dir exists, if you want to check for a *file*
// use the file.Exists() routine or if you want to check for both file and
// directory use the path.Exists() routine.
func Exists(dir string) (bool, error) {
	exists, err := path.Exists(dir)
	if err != nil {
		// error already wrapped by path.Exists()
		return exists, err
	}
	if exists {
		fileinfo, err := os.Stat(dir)
		if err != nil {
			return false, out.WrapErr(err, "Failed to stat directory, unable to verify existence", 4011)
		}
		if !fileinfo.IsDir() {
			exists = false
			err = out.NewErr("Item is not a directory hence directory existence check failed", 4012)
		}
	}
	return exists, err
}
Пример #2
0
Файл: file.go Проект: dvln/util
// Exists checks if given file exists, if you want to check for a directory
// use the dir.Exists() routine or if you want to check for both file and
// directory use the path.Exists() routine.
func Exists(file string) (bool, error) {
	exists, err := path.Exists(file)
	if err != nil {
		// error already wrapped by path.Exists()
		return exists, err
	}
	if exists {
		var fileinfo os.FileInfo
		fileinfo, err = os.Stat(file)
		if err != nil {
			return false, out.WrapErr(err, "Failed to stat file, unable to verify existence", 4013)
		}
		if fileinfo.IsDir() {
			exists = false
			err = out.NewErr("Item is a directory hence the file existence check failed", 4014)
		}
	}
	return exists, err
}