1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
filepath_posix.go 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
// +build linux freebsd openbsd netbsd darwin
package file
import (
"os"
"os/user"
"strconv"
"syscall"
"github.com/joeshaw/multierror"
"github.com/pkg/errors"
)
func Stat(path string) (*Metadata, error) {
info, err := os.Lstat(path)
if err != nil {
return nil, errors.Wrap(err, "failed to stat path")
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return nil, errors.Errorf("unexpected fileinfo sys type %T for %v", info.Sys(), path)
}
fileInfo := &Metadata{
Inode: stat.Ino,
UID: stat.Uid,
GID: stat.Gid,
Mode: info.Mode(),
Size: info.Size(),
}
fileInfo.ATime, fileInfo.MTime, fileInfo.CTime = fileTimes(stat)
switch {
case info.IsDir():
fileInfo.Type = "dir"
case info.Mode().IsRegular():
fileInfo.Type = "file"
case info.Mode()&os.ModeSymlink > 0:
fileInfo.Type = "symlink"
}
// Lookup UID and GID
var errs multierror.Errors
owner, err := user.LookupId(strconv.Itoa(int(fileInfo.UID)))
if err != nil {
errs = append(errs, err)
} else {
fileInfo.Owner = owner.Username
}
group, err := user.LookupGroupId(strconv.Itoa(int(fileInfo.GID)))
if err != nil {
errs = append(errs, err)
} else {
fileInfo.Group = group.Name
}
return fileInfo, errs.Err()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.0.0

搜索帮助