Пример #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
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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 ""
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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
}
Пример #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))
}
Пример #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
}
Пример #19
0
// GetPerm return file permissions
func GetPerm(path string) os.FileMode {
	path = PATH.Clean(path)
	return os.FileMode(getMode(path) & 0777)
}