62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
//go:build windows
|
|
// +build windows
|
|
|
|
package file
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
type FileInfo struct {
|
|
FileType string `json:"fileType"` // 文件类型
|
|
FileMode string `json:"fileMode"` // 文件的权限
|
|
LinkCount int64 `json:"linkCount"` // 硬链接数目
|
|
Owner string `json:"owner"` // 所属用户
|
|
Group string `json:"group"` // 所属组
|
|
Size int64 `json:"size"` // 文件的大小
|
|
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
|
|
FileName string `json:"fileName"` // 文件的名称
|
|
}
|
|
|
|
func GetFileInfo(dir, suffix string) ([]FileInfo, error) {
|
|
var files []FileInfo
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if path == dir {
|
|
return nil // 跳过当前目录
|
|
}
|
|
|
|
fileType := "file"
|
|
if info.IsDir() {
|
|
fileType = "directory"
|
|
} else if info.Mode()&os.ModeSymlink != 0 {
|
|
fileType = "symlink"
|
|
}
|
|
|
|
// check if match suffix
|
|
if (suffix != "" && filepath.Ext(path) == suffix) || suffix == "" {
|
|
fileInfo := FileInfo{
|
|
FileType: fileType,
|
|
FileMode: info.Mode().String(),
|
|
LinkCount: 0,
|
|
Owner: "-",
|
|
Group: "-",
|
|
Size: info.Size(),
|
|
ModifiedTime: info.ModTime().Unix(),
|
|
FileName: info.Name(),
|
|
}
|
|
files = append(files, fileInfo)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return files, nil
|
|
}
|