示例#1
0
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// 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
文件: fs.go 项目: essentialkaos/ek
// GetPerm return file permissions
func GetPerm(path string) os.FileMode {
	path = PATH.Clean(path)
	return os.FileMode(getMode(path) & 0777)
}