// GetATime return time of last access func GetATime(path string) (time.Time, error) { path = PATH.Clean(path) atime, _, _, err := GetTimes(path) return atime, err }
// GetCTime return time of creation func GetCTime(path string) (time.Time, error) { path = PATH.Clean(path) _, _, ctime, err := GetTimes(path) return ctime, err }
// GetMTime return time of modification func GetMTime(path string) (time.Time, error) { path = PATH.Clean(path) _, mtime, _, err := GetTimes(path) return mtime, err }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 "" }
// 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 }
// 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 }
// 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 }
// 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 }
// 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 }
// 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)) }
// 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 }
// GetPerm return file permissions func GetPerm(path string) os.FileMode { path = PATH.Clean(path) return os.FileMode(getMode(path) & 0777) }