You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.0 KiB
Go
118 lines
3.0 KiB
Go
package dfinder
|
|
|
|
import (
|
|
"b612.me/starlog"
|
|
"b612.me/staros"
|
|
"b612.me/wincmd/ntfs/mft"
|
|
"fmt"
|
|
"github.com/spf13/cobra"
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
var dfpath, dfreg, dfoutpath string
|
|
var dfonlyname, dfshow bool
|
|
|
|
func init() {
|
|
Cmd.Flags().StringVarP(&dfoutpath, "outpath", "o", "", "outpath")
|
|
Cmd.Flags().StringVarP(&dfpath, "path", "p", "./", "path you want to search")
|
|
Cmd.Flags().StringVarP(&dfreg, "regexp", "r", ".*", "search regexp")
|
|
Cmd.Flags().BoolVarP(&dfonlyname, "only-filter-name", "f", false, "only regexp for name not path")
|
|
Cmd.Flags().BoolVarP(&dfshow, "show", "s", true, "show on the stdout")
|
|
}
|
|
|
|
var Cmd = &cobra.Command{
|
|
Use: "dfinder",
|
|
Short: "查找nfts磁盘文件",
|
|
Long: "查找nfts磁盘文件",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if !staros.IsRoot() {
|
|
starlog.Criticalln("need administator permission to run this command")
|
|
os.Exit(1)
|
|
}
|
|
os.Exit(fileFinder(dfpath, dfreg, dfonlyname, dfoutpath, dfshow))
|
|
},
|
|
}
|
|
|
|
func fileFinder(path string, reg string, onlyFilterName bool, outpath string, show bool) int {
|
|
fullPath, err := filepath.Abs(path)
|
|
if err != nil {
|
|
starlog.Criticalln("filepath not a vaild path", path, err)
|
|
return 1
|
|
}
|
|
vol := filepath.VolumeName(fullPath) + `\`
|
|
fmt.Println(vol)
|
|
if staros.IsFile(fullPath) {
|
|
fullPath = filepath.Dir(fullPath)
|
|
}
|
|
fmt.Println("consider folder is", fullPath)
|
|
|
|
fileLists, err := mft.GetFileListsByMftFn(vol, func(name string, isFolder bool) bool {
|
|
if isFolder {
|
|
return true
|
|
}
|
|
if onlyFilterName {
|
|
if matched, _ := regexp.MatchString(reg, name); matched {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
return true
|
|
})
|
|
if err != nil {
|
|
starlog.Errorln("read mft failed", err)
|
|
return 3
|
|
}
|
|
getSize := func(size uint64) string {
|
|
floatSize := float64(size)
|
|
var sizeC = []string{"byte", "KB", "MB", "GB", "TB"}
|
|
if floatSize < 1024 {
|
|
return ""
|
|
}
|
|
for i := 0; i < 4; i++ {
|
|
if floatSize/math.Pow(1024, float64(i+1)) < 1024 {
|
|
return fmt.Sprintf("%.4f%s", floatSize/math.Pow(1024, float64(i+1)), sizeC[i+1])
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
var fp *os.File
|
|
if outpath != "" {
|
|
fp, err = os.Create(outpath)
|
|
if err != nil {
|
|
starlog.Criticalln(err)
|
|
} else {
|
|
defer fp.Close()
|
|
fp.WriteString(`名称,路径,大小,大小(格式化),修改时间,是否是文件夹` + "\n")
|
|
}
|
|
}
|
|
newReg := regexp.MustCompile(reg)
|
|
for _, v := range fileLists {
|
|
if !strings.Contains(v.Path, fullPath) {
|
|
continue
|
|
}
|
|
if onlyFilterName {
|
|
if show {
|
|
fmt.Printf("name:%-10s path:%-20s size:%-10s modTime:%v\n", v.Name, v.Path, getSize(v.Size), v.ModTime)
|
|
}
|
|
if fp != nil {
|
|
fp.WriteString(fmt.Sprintf("%s,%s,%v,%s,%v,%v\n", v.Name, v.Path, v.Size, getSize(v.Size), v.ModTime, v.IsDir))
|
|
}
|
|
} else {
|
|
if newReg.MatchString(v.Path) {
|
|
if show {
|
|
fmt.Printf("name:%-10s path:%-20s size:%-10s modTime:%v\n", v.Name, v.Path, getSize(v.Size), v.ModTime)
|
|
}
|
|
if fp != nil {
|
|
fp.WriteString(fmt.Sprintf("%s,%s,%v,%s,%v,%v\n", v.Name, v.Path, v.Size, getSize(v.Size), v.ModTime, v.IsDir))
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
return 0
|
|
}
|