Ejemplo n.º 1
0
// GetATime return time of last access
func GetATime(path string) (time.Time, error) {
	path = PATH.Clean(path)

	atime, _, _, err := GetTimes(path)

	return atime, err
}
Ejemplo n.º 2
0
// GetCTime return time of creation
func GetCTime(path string) (time.Time, error) {
	path = PATH.Clean(path)

	_, _, ctime, err := GetTimes(path)

	return ctime, err
}
Ejemplo n.º 3
0
// GetMTime return time of modification
func GetMTime(path string) (time.Time, error) {
	path = PATH.Clean(path)

	_, mtime, _, err := GetTimes(path)

	return mtime, err
}
Ejemplo n.º 4
0
// IsExist check if target is exist in fs or not
func IsExist(path string) bool {
	if path == "" {
		return false
	}

	path = PATH.Clean(path)

	return syscall.Access(path, syscall.F_OK) == nil
}
Ejemplo n.º 5
0
// IsNonEmpty check if file is empty or not
func IsNonEmpty(path string) bool {
	if path == "" {
		return false
	}

	path = PATH.Clean(path)

	return GetSize(path) != 0
}
Ejemplo n.º 6
0
// IsLink check if file is link or not
func IsLink(path string) bool {
	path = PATH.Clean(path)
	mode := getMode(path)

	if mode == 0 {
		return false
	}

	return mode&_IFMT == _IFLNK
}
Ejemplo n.º 7
0
// IsCharacterDevice check if target is character device or not
func IsCharacterDevice(path string) bool {
	path = PATH.Clean(path)
	mode := getMode(path)

	if mode == 0 {
		return false
	}

	return mode&_IFMT == _IFCHR
}
Ejemplo n.º 8
0
// IsBlockDevice check if target is block device or not
func IsBlockDevice(path string) bool {
	path = PATH.Clean(path)
	mode := getMode(path)

	if mode == 0 {
		return false
	}

	return mode&_IFMT == _IFBLK
}
Ejemplo n.º 9
0
// IsSocket check if target is socket or not
func IsSocket(path string) bool {
	path = PATH.Clean(path)
	mode := getMode(path)

	if mode == 0 {
		return false
	}

	return mode&_IFMT == _IFSOCK
}
Ejemplo n.º 10
0
// IsRegular check if target is regular file or not
func IsRegular(path string) bool {
	path = PATH.Clean(path)
	mode := getMode(path)

	if mode == 0 {
		return false
	}

	return mode&_IFMT == _IFREG
}
Ejemplo n.º 11
0
// ProperPath return first proper path from given slice
func ProperPath(props string, paths []string) string {
	for _, path := range paths {
		path = PATH.Clean(path)

		if CheckPerms(props, path) {
			return path
		}
	}

	return ""
}
Ejemplo n.º 12
0
// GetOwner return object owner pid and gid
func GetOwner(path string) (int, int, error) {
	if path == "" {
		return -1, -1, errors.New("Path is empty")
	}

	path = PATH.Clean(path)

	var stat = &syscall.Stat_t{}

	err := syscall.Stat(path, stat)

	if err != nil {
		return -1, -1, err
	}

	return int(stat.Uid), int(stat.Gid), nil
}
Ejemplo n.º 13
0
// GetSize return file size in bytes
func GetSize(path string) int64 {
	if path == "" {
		return 0
	}

	path = PATH.Clean(path)

	var stat = &syscall.Stat_t{}

	err := syscall.Stat(path, stat)

	if err != nil {
		return 0
	}

	return stat.Size
}
Ejemplo n.º 14
0
// GetTimestamps return time of access, modification and creation at once as linux timestamp
func GetTimestamps(path string) (int64, int64, int64, error) {
	if path == "" {
		return -1, -1, -1, errors.New("Path is empty")
	}

	path = PATH.Clean(path)

	var stat = &syscall.Stat_t{}

	err := syscall.Stat(path, stat)

	if err != nil {
		return -1, -1, -1, err
	}

	return int64(stat.Atimespec.Sec),
		int64(stat.Mtimespec.Sec),
		int64(stat.Ctimespec.Sec),
		nil
}
Ejemplo n.º 15
0
// GetTimes return time of access, modification and creation at once
func GetTimes(path string) (time.Time, time.Time, time.Time, error) {
	if path == "" {
		return time.Time{}, time.Time{}, time.Time{}, errors.New("Path is empty")
	}

	path = PATH.Clean(path)

	var stat = &syscall.Stat_t{}

	err := syscall.Stat(path, stat)

	if err != nil {
		return time.Time{}, time.Time{}, time.Time{}, err
	}

	return time.Unix(int64(stat.Atimespec.Sec), int64(stat.Atimespec.Nsec)),
		time.Unix(int64(stat.Mtimespec.Sec), int64(stat.Mtimespec.Nsec)),
		time.Unix(int64(stat.Ctimespec.Sec), int64(stat.Ctimespec.Nsec)),
		nil
}
Ejemplo n.º 16
0
// IsEmptyDir check if directory empty or not
func IsEmptyDir(path string) bool {
	if path == "" {
		return false
	}

	path = PATH.Clean(path)

	fd, err := syscall.Open(path, syscall.O_RDONLY, 0)

	if err != nil {
		return false
	}

	defer syscall.Close(fd)

	n, err := syscall.ReadDirent(fd, make([]byte, 4096))

	if n == 0x30 || err != nil {
		return true
	}

	return false
}
Ejemplo n.º 17
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))
}
Ejemplo n.º 18
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
}
Ejemplo n.º 19
0
// GetPerm return file permissions
func GetPerm(path string) os.FileMode {
	path = PATH.Clean(path)
	return os.FileMode(getMode(path) & 0777)
}