// DirExists checks if directory exists if given path is not empty.
//
// This function is rather specific as it is mostly used for checking
// overrides validity (like overriding temporary directory, where
// empty string means "do not override").
340035
func DirExists(path string) bool {
	if path != "" {
		fi, err := os.Stat(path)
		if err != nil || !fi.IsDir() {
			return false
		}
	}
	return true
}
584269
func (d Dir) SearchPaths() []string {
	if d.env == "" {
		panic("xdgdir.Dir.SearchPaths() on zero Dir")
	}
	var paths []string
	if p := d.Path(); p != "" {
		paths = append(paths, p)
	}
	if d.dirsEnv == "" {
		return paths
	}
	e := getenv(d.dirsEnv)
	if e == "" {
		paths = append(paths, d.dirsFallback...)
		return paths
	}
	epaths := filepath.SplitList(e)
	n := 0
	for _, p := range epaths {
		if isValidPath(p) {
			epaths[n] = p
			n++
		}
	}
	paths = append(paths, epaths[:n]...)
	return paths
}
945028
func hasDirPrefix(dirPrefix, fullpath string) bool {
	if !strings.HasPrefix(fullpath, dirPrefix) {
		return false
	}
	if len(fullpath) == len(dirPrefix) {
		return true
	}
	if fullpath[len(dirPrefix)] == filepath.Separator {
		return true
	}
	return false
}
509050
func (a Args) Directory() string {
	if info, err := os.Stat(a.Last); err == nil && info.IsDir() {
		return fixPathForm(a.Last, a.Last)
	}
	dir := filepath.Dir(a.Last)
	if info, err := os.Stat(dir); err != nil || !info.IsDir() {
		return "./"
	}
	return fixPathForm(a.Last, dir)
}
655945
func MustIsDir(filePath string) bool {
	fileInfo, err := os.Stat(filePath)
	if err != nil {
		log.Fatal(err)
	}
	return fileInfo.IsDir()
}
733859
func IsDir(path string) bool {
	f, err := os.Open(path)
	if err != nil {
		return false
	}
	defer func() {
		errutil.Trace(f.Close())
	}()

	fi, err := f.Stat()
	if err != nil {
		return false
	}

	return fi.Mode().IsDir()
}
1179392
func findExe(cmd string, paths []string) (string, bool) {
	for _, p := range paths {
		cp := filepath.Join(p, cmd)
		if c, err := exec.LookPath(cp); err == nil {
			return c, true
		}
	}
	return cmd, false
}
1053142
func (ns *NameStrategy) filterDirs(path string) []string {
	allDirs := strings.Split(path, GoSeperator)
	dirs := make([]string, 0, len(allDirs))
	for _, p := range allDirs {
		if ns.IgnoreWords == nil || !ns.IgnoreWords[p] {
			dirs = append(dirs, importPathNameSanitizer.Replace(p))
		}
	}
	return dirs
}
158775
func (d *dir) mkdirs(p string) (*dir, error) {
	p = normalize(p)

	n := d.find(p)

	if !n.isdir() {
		// Found something there
		return nil, errIsNotDir
	}

	if n.path() == p {
		return n.(*dir), nil
	}

	dd := n.(*dir)

	relative := strings.Trim(strings.TrimPrefix(p, n.path()), "/")

	if relative == "" {
		return dd, nil
	}

	components := strings.Split(relative, "/")
	for _, component := range components {
		d, err := dd.mkdir(component)

		if err != nil {
			// This should actually never happen, since there are no children.
			return nil, err
		}
		dd = d
	}

	return dd, nil
}
262565
func IsDir(fpath string) bool {
	d := cleanPath(fpath)
	if fi, err := os.Stat(d); err == nil {
		return fi.IsDir()
	}
	return false
}
693374
func EnsurePath(path string, dir bool) error {
	if !dir {
		path = filepath.Dir(path)
	}
	return os.MkdirAll(path, 0755)
}
1147184
func assertNotExists(p string) []error {
	var errs []error
	e, err := fileExists(p)
	if err != nil {
		errs = append(errs, fmt.Errorf("error checking %q exists: %v", p, err))
	}
	if e {
		errs = append(errs, fmt.Errorf("file %q exists unexpectedly", p))
	}
	return errs
}
298665
func DirMissingOrEmpty(path string) (bool, error) {
	dirExists, err := DirExists(path)
	if err != nil {
		return false, err
	}
	if !dirExists {
		return true, nil
	}

	dirEmpty, err := DirEmpty(path)
	if err != nil {
		return false, err
	}
	if dirEmpty {
		return true, nil
	}
	return false, nil
}
695315
func check(ctx context.Context, workingDir string, subdirs []string) bool {
	// `--quiet` means "exit with 1 if there are changes"
	args := []string{"diff", "--quiet"}
	args = append(args, "--")
	if len(subdirs) > 0 {
		args = append(args, subdirs...)
	}
	return execGitCmd(ctx, args, gitCmdConfig{dir: workingDir}) != nil
}
135044
func checkPathIsDir(ctx context.Context, cli keybase1.SimpleFSInterface, path keybase1.Path) (bool, string, error) {
	var isDir bool
	var pathString string
	var err error

	pathType, _ := path.PathType()
	switch pathType {
	case keybase1.PathType_KBFS, keybase1.PathType_KBFS_ARCHIVED:
		if pathType == keybase1.PathType_KBFS {
			pathString = path.Kbfs()
		} else {
			pathString = path.KbfsArchived().Path
		}
		// See if the dest is a path or file
		destEnt, err := cli.SimpleFSStat(ctx, keybase1.SimpleFSStatArg{Path: path})
		if err != nil {
			return false, "", err
		}

		if destEnt.DirentType == keybase1.DirentType_DIR {
			isDir = true
		}
	case keybase1.PathType_LOCAL:
		pathString = path.Local()
		// An error is OK, could be a target filename
		// that does not exist yet
		fileInfo, err := os.Stat(pathString)
		if err == nil {
			if fileInfo.IsDir() {
				isDir = true
			}
		}
	}
	return isDir, pathString, err
}
232014
func (s *PathSpec) Validate() error {
	if s.Local() {
		info, err := os.Stat(s.Path)
		if err != nil {
			return fmt.Errorf("invalid path %s: %v", s.Path, err)
		}
		if !info.IsDir() {
			return fmt.Errorf("path %s must point to a directory", s.Path)
		}
	}
	return nil
}
757807
func PathMatchesExtensions(path string, extensions []string) bool {
	if len(extensions) == 0 {
		return true
	}
	ext := strings.ToLower(filepath.Ext(path))
	for _, allowed := range extensions {
		if ext == allowed {
			return true
		}
	}
	return false
}