mager: 合并11.3版本,包名和主线一样方便复制
This commit is contained in:
@@ -6,8 +6,12 @@ package file
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
type FileInfo struct {
|
||||
@@ -16,7 +20,7 @@ type FileInfo struct {
|
||||
LinkCount int64 `json:"linkCount"` // 硬链接数目
|
||||
Owner string `json:"owner"` // 所属用户
|
||||
Group string `json:"group"` // 所属组
|
||||
Size int64 `json:"size"` // 文件的大小
|
||||
Size string `json:"size"` // 文件的大小
|
||||
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
|
||||
FileName string `json:"fileName"` // 文件的名称
|
||||
}
|
||||
@@ -42,13 +46,26 @@ func GetFileInfo(dir, suffix string) ([]FileInfo, error) {
|
||||
|
||||
// check if match suffix
|
||||
if (suffix != "" && filepath.Ext(path) == suffix) || suffix == "" {
|
||||
stat, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
return fmt.Errorf("not a syscall.Stat_t")
|
||||
}
|
||||
userInfo, err := user.LookupId(fmt.Sprint(stat.Uid))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
groupInfo, err := user.LookupGroupId(fmt.Sprint(stat.Gid))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
humanReadableSize := humanize.Bytes(uint64(info.Size()))
|
||||
fileInfo := FileInfo{
|
||||
FileType: fileType,
|
||||
FileMode: info.Mode().String(),
|
||||
LinkCount: int64(info.Sys().(*syscall.Stat_t).Nlink),
|
||||
Owner: fmt.Sprintf("%d", info.Sys().(*syscall.Stat_t).Uid),
|
||||
Group: fmt.Sprintf("%d", info.Sys().(*syscall.Stat_t).Gid),
|
||||
Size: info.Size(),
|
||||
Owner: userInfo.Username,
|
||||
Group: groupInfo.Name,
|
||||
Size: strings.ToUpper(humanReadableSize),
|
||||
ModifiedTime: info.ModTime().Unix(),
|
||||
FileName: info.Name(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user