// +build !windows,!plan9

package shell

import (
	
	
	
	

	
)

// Returns a "run directory" for storing ephemeral files, which is guaranteed
// to be only accessible to the current user.
//
// The path of the run directory is either $XDG_RUNTIME_DIR/elvish or
// $tmpdir/elvish-$uid (where $tmpdir is the system temporary directory). The
// former is used if the XDG_RUNTIME_DIR environment variable exists and the
// latter directory does not exist.
func () (string, error) {
	 := getRunDirCandidates()
	for ,  := range  {
		if checkExclusiveAccess() {
			return , nil
		}
	}
	 := [0]
	 := os.MkdirAll(, 0700)
	if  != nil {
		return "", fmt.Errorf("mkdir: %v", )
	}
	if !checkExclusiveAccess() {
		return "", fmt.Errorf("cannot create %v as a secure run directory", )
	}
	return , nil
}

// Returns one or more candidates for the run directory, in descending order of
// preference.
func () []string {
	 := filepath.Join(os.TempDir(), fmt.Sprintf("elvish-%d", os.Getuid()))
	if os.Getenv(env.XDG_RUNTIME_DIR) != "" {
		 := filepath.Join(os.Getenv(env.XDG_RUNTIME_DIR), "elvish")
		return []string{, }
	}
	return []string{}
}

func ( string) bool {
	,  := os.Stat()
	if  != nil {
		return false
	}
	 := .Sys().(*syscall.Stat_t)
	return .IsDir() && int(.Uid) == os.Getuid() && .Mode&077 == 0
}