diff --git a/main.go b/main.go index d6fedbd..47f09f0 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "FileCleaner/record" "FileCleaner/write" "flag" - "fmt" "time" ) @@ -35,6 +34,7 @@ func main() { read.Read(basePath, casCade) // 写文件 write.Write(delMethod) - // 累积耗时 - fmt.Printf("本次去重总共耗时: %s\n", time.Since(startTime).String()) + // 输出统计结果信息 + record.Report(startTime) + } diff --git a/model/model.go b/model/model.go index 49d4cda..5985772 100644 --- a/model/model.go +++ b/model/model.go @@ -43,8 +43,9 @@ type writeRes struct { } type linkRes struct { - ErrNum int - ErrMap map[string][]string // 源文件 目标文件 + TotalNum int + ErrNum int + ErrMap map[string][]string // 源文件 目标文件 } type Record interface { @@ -135,11 +136,11 @@ type link struct { } func (l link) Record(res *Result) { + res.Link.TotalNum += 1 if !l.stat { res.Link.ErrNum += 1 res.Link.ErrMap[l.src] = append(res.Link.ErrMap[l.src], l.dist) } - } func NewLink(stat bool, src, dist string) *link { diff --git a/read/readdir_test.go b/read/readdir_test.go index 6d317b8..e70d182 100644 --- a/read/readdir_test.go +++ b/read/readdir_test.go @@ -1,6 +1,7 @@ package read import ( + "fmt" "testing" ) @@ -11,3 +12,13 @@ func TestReadDir(t *testing.T) { } t.Log("path:",path)*/ } + +func TestEchoFloat(t *testing.T) { + fmt.Printf("%.2f ", 0.15) + var aa int = 1024 + var bb int = 1010 + fmt.Println(float64(aa) / 1024 / 1024) + fmt.Println(float64(aa / bb)) + numPct := fmt.Sprintf("%.2f %%", float64(aa)/float64(bb)) + fmt.Println(numPct) +} diff --git a/record/record.go b/record/record.go index 3910e53..850b233 100644 --- a/record/record.go +++ b/record/record.go @@ -3,47 +3,77 @@ package record import ( "FileCleaner/model" "fmt" + "time" ) +// 操作结果集 +var result model.Result + // 后台记录记录文件信息 func Record() { - // 操作结果集 - var result model.Result -LOOP: for { select { // 记录读文件信息 case record := <-model.RecordCH: record.Record(&result) case <-model.SignalCH: - break LOOP + return } } +} + +func Report(t time.Time) { + totalSize := fmt.Sprintf("%.2f GB", float64(result.Read.TotalSize)/1024/1024/1024) + removeSize := fmt.Sprintf("%.2f GB", float64(result.Write.TotalSize)/1024/1024/1024) + saveSize := fmt.Sprintf("%.2f GB", float64(result.Read.TotalSize-result.Write.TotalSize)/1024/1024/1024) + numPct := fmt.Sprintf("%.2f %%", float64(result.Write.TotalNum)/float64(result.Read.TotalNum)) + sizePct := fmt.Sprintf("%.2f %%", float64(result.Write.TotalSize)/float64(result.Read.TotalSize)) // 输出结果 fmt.Printf(` -结果概览: +本次操作总计耗时: %s +结果概览: 总目录数量: %d 总文件数量: %d 重复文件数量: %d - 总文件大小: %s 去重后文件大小: %s 重复文件大小: %s 去重空间比: %d + 总文件大小: %s 重复文件大小: %s 剩余文件大小: %s - 读取文件目录成功: %d 失败: %d - 读取文件数量成功: %d 失败: %d - 删除文件数量成功: %d 失败: %d - 创建文件链接成功: %d 失败: %d + 文件去重比: %s 空间优化比: %s -`) -} + 读取文件目录 成功: %d 失败: %d + 读取文件数量 成功: %d 失败: %d + 删除文件数量 成功: %d 失败: %d + 创建文件链接 成功: %d 失败: %d -/* -输出结果信息 -总计: - 总文件大小: 去重后文件大小: 去重大小: 去重空间占比: - 读取文件目录: 成功: 失败: - 读取文件数量: 成功: 失败: - 删除文件数量: 成功: 失败: - 创建文件链接: 成功: 失败: -if 失败> 0 { -失败详情: - * * * +`, time.Since(t).String(), + result.Path.TotalNum, result.Read.TotalNum, result.Write.TotalNum, + totalSize, removeSize, saveSize, + numPct, sizePct, + result.Path.TotalNum-result.Path.ErrNum, result.Path.ErrNum, + result.Read.TotalNum-result.Read.ErrNum, result.Read.ErrNum, + result.Write.TotalNum-result.Write.ErrNum, result.Write.ErrNum, + result.Link.TotalNum-result.Link.ErrNum, result.Link.ErrNum) + + if result.Path.ErrNum != 0 { + fmt.Printf("读取目录失败列表:\n") + for _, path := range result.Path.Errs { + fmt.Println(path) + } + } + if result.Read.ErrNum != 0 { + fmt.Printf("读取文件失败列表:\n") + for _, path := range result.Read.Errs { + fmt.Println(path) + } + } + if result.Write.ErrNum != 0 { + fmt.Printf("删除文件失败列表:\n") + for _, path := range result.Write.Errs { + fmt.Println(path) + } + } + if result.Link.ErrNum != 0 { + fmt.Printf("创建文件硬链接失败列表:\n") + for src, dist := range result.Link.ErrMap { + fmt.Printf("源文件:%s --> 目标文件:%s\n", src, dist) + } + } } -*/ diff --git a/write/write.go b/write/write.go index 70775f8..5f02927 100644 --- a/write/write.go +++ b/write/write.go @@ -14,9 +14,8 @@ func Write(dm string) { RemoveFile() if dm == "ln" { CreateLink() - } else { - close(model.ControlCH) } + model.SignalCH <- true } func RemoveFile() {