Skip to content

Commit

Permalink
添加只读dryRun运行模式
Browse files Browse the repository at this point in the history
  • Loading branch information
tanganyu1114 committed Dec 29, 2020
1 parent 970f945 commit ee67ed5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
# 命令行文件去重工具

**编写这个工具的初衷在于给fastdfs小文件进行去重
并发的读写操作文件,目前默认goroutine数量为10,如需修改,只需要修改model包对应的channel数量**
并发的读写操作文件,目前默认goroutine最大数量为10,如需修改,只需要修改model包对应的channel数量**
* * *
* 命令行参数
```
> -c bool 是否启用级联去重操作,默认false
> -p string 去重操作的目录,默认路径/home/fastdfs/storage/data
> -dm string 删除的方式,目前支持2种参数:rm 直接删除重复文件; ln 删除重复文件,并且以第一个文件为源目标,创建其他文件的硬链接
> -n int 并发执行的线程数,默认4,最低1,最高10
> -c bool 是否启用级联去重操作(默认 false)
> -p string 去重操作的目录 (默认路径 /home/fastdfs/storage/data)
> -dm string 删除的方式,目前支持3种参数(默认 ln):
-dm="dry" 只模拟查看重复文件和执行效果,不真实执行删除操作
-dm="rm" 直接删除重复文件;
-dm="ln" 删除重复文件,并且以第一个文件为源目标,创建其他文件的硬链接
> -n int 并发执行的线程数,最低1,最高10(默认 4)
```
* 示例
```
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Init() {
flag.StringVar(&basePath, "p", "/home/fastdfs/storage/data", "Plz enter an absolute path")
flag.BoolVar(&casCade, "c", false, "Cascade control the dir (default false)")
flag.IntVar(&parallel, "n", 4, "The number of parallel control")
flag.StringVar(&delMethod, "dm", "ln", "Plz Usage 'rm' or 'ln'; rm: remove the file ,ln: remove and link the file")
flag.StringVar(&delMethod, "dm", "ln", "Plz Usage 'rm','ln' or 'dry'; rm: remove the file, ln: remove and link the file, dry: dryRun mode")
flag.Parse()
}

Expand Down
Binary file added picture/result.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 3 additions & 7 deletions read/readdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ func TestReadDir(t *testing.T) {
}

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)
fmt.Printf("aaaaaaaaaaaaa\taaaaaa\taaaa\t\n")
fmt.Printf("bbbb\tbbbb\tbbbbbbbbbbbbbbbbbbb\t\n")
fmt.Printf("c\tcc\tccc\t\n")
}
37 changes: 31 additions & 6 deletions write/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import (
// 在这个包主要操作文件,删除文件创建ln

func Write(dm string) {
// 删除文件
RemoveFile()
if dm == "ln" {
CreateLink()
// 判断文件处理模式
switch dm {
case "dry":
dryRunFile()
case "rm":
removeFile()
case "ln":
removeFile()
linkFile()
}
// 等待record记录完成 退出协程 关闭通道
fmt.Printf("[INFO]: Wait to record the write infomation .")
Expand All @@ -32,7 +37,27 @@ func Write(dm string) {
close(model.ControlCH)
}

func RemoveFile() {
func dryRunFile() {
wg := sync.WaitGroup{}
for hash, files := range model.FileMap {
wg.Add(1)
model.ControlCH <- 1
go func(hash string, files []string) {
defer wg.Done()
defer func() { <-model.ControlCH }()
if len(files) > 1 {
for _, file := range files[1:] {
fmt.Printf("[INFO]: Duplicate File: %s\n", file)
rd := model.NewWrite(true, hash, file, model.FileSize[hash])
model.RecordCH <- rd
}
}
}(hash, files)
}
wg.Wait()
}

func removeFile() {
wg := sync.WaitGroup{}
for hash, files := range model.FileMap {
wg.Add(1)
Expand Down Expand Up @@ -60,7 +85,7 @@ func RemoveFile() {
wg.Wait()
}

func CreateLink() {
func linkFile() {
wg := sync.WaitGroup{}
for hash, files := range model.FileMap {
wg.Add(1)
Expand Down

0 comments on commit ee67ed5

Please sign in to comment.