Esempio n. 1
0
// IsExecutable check if file is executable or not
func IsExecutable(path string) bool {
	if path == "" {
		return false
	}

	path = PATH.Clean(path)

	var stat = &syscall.Stat_t{}

	err := syscall.Stat(path, stat)

	if err != nil {
		return false
	}

	user, err := system.CurrentUser()

	if err != nil {
		return false
	}

	return isExecutableStat(stat, user.UID, getGIDList(user))
}
Esempio n. 2
0
// CheckPerms check many props at once.
//
// F - is file
// D - is directory
// X - is executable
// L - is link
// W - is writable
// R - is readable
// S - not empty (only for files)
//
func CheckPerms(props, path string) bool {
	if len(props) == 0 || path == "" {
		return false
	}

	path = PATH.Clean(path)
	props = strings.ToUpper(props)

	var stat = &syscall.Stat_t{}

	err := syscall.Stat(path, stat)

	if err != nil {
		return false
	}

	var user *system.User

	for _, k := range props {
		switch k {

		case 'F':
			if stat.Mode&_IFMT != _IFREG {
				return false
			}

		case 'D':
			if stat.Mode&_IFMT != _IFDIR {
				return false
			}

		case 'L':
			if stat.Mode&_IFMT != _IFLNK {
				return false
			}

		case 'X':
			if user == nil {
				user, err = system.CurrentUser()

				if err != nil {
					return false
				}
			}

			if !isExecutableStat(stat, user.UID, getGIDList(user)) {
				return false
			}

		case 'W':
			if user == nil {
				user, err = system.CurrentUser()

				if err != nil {
					return false
				}
			}

			if !isWritableStat(stat, user.UID, getGIDList(user)) {
				return false
			}

		case 'R':
			if user == nil {
				user, err = system.CurrentUser()

				if err != nil {
					return false
				}
			}

			if !isReadableStat(stat, user.UID, getGIDList(user)) {
				return false
			}

		case 'S':
			if stat.Size == 0 {
				return false
			}
		}
	}

	return true
}