From 1e7c089b8fbe39675b5dc6413b3193dd441f1328 Mon Sep 17 00:00:00 2001 From: mstmdev Date: Wed, 20 Apr 2022 00:24:20 +0800 Subject: [PATCH] Add the -checksum flag to calculate and print the checksum for source file --- README-CN.md | 10 ++++++++++ README.md | 10 ++++++++++ checksum/checksum.go | 20 ++++++++++++++++++++ checksum/checksum_test.go | 19 +++++++++++++++++++ cmd/gofs/flag.go | 3 +++ cmd/gofs/main.go | 7 +++++++ conf/config.go | 3 +++ conf/example/gofs-remote-client.json | 3 ++- conf/example/gofs-remote-server.yaml | 3 ++- 9 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 checksum/checksum.go create mode 100644 checksum/checksum_test.go diff --git a/README-CN.md b/README-CN.md index 56d76af5..f5e7b3b1 100644 --- a/README-CN.md +++ b/README-CN.md @@ -298,6 +298,16 @@ $ gofs -source=./source -dest=./dest -log_file -log_level=0 -log_dir="./logs/" - $ gofs -conf=./gofs.yaml ``` +### 校验和 + +你可以使用`checksum`命令行参数来计算并打印文件的校验和 + +`chunk_size`和`checkpoint_count`命令行参数在这里同在[本地磁盘](#本地磁盘)中一样有效 + +```bash +$ gofs -source=./gofs -checksum +``` + ## 更多信息 ### 帮助信息 diff --git a/README.md b/README.md index 51412695..ad4000fd 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,16 @@ or the response of [Config API](#config-api). $ gofs -conf=./gofs.yaml ``` +### Checksum + +You can use the `checksum` flag to calculate the file checksum and print the result. + +The `chunk_size` and `checkpoint_count` flags are effective here the same as in the [Local Disk](#local-disk). + +```bash +$ gofs -source=./gofs -checksum +``` + ## For More Information ### Help Info diff --git a/checksum/checksum.go b/checksum/checksum.go new file mode 100644 index 00000000..d4f9353a --- /dev/null +++ b/checksum/checksum.go @@ -0,0 +1,20 @@ +package checksum + +import ( + "github.com/no-src/gofs/util/hashutil" + "github.com/no-src/gofs/util/jsonutil" + "github.com/no-src/log" +) + +// PrintChecksum calculate and print the checksum for file +func PrintChecksum(path string, chunkSize int64, checkpointCount int) error { + hvs, err := hashutil.CheckpointsMD5FromFileName(path, chunkSize, checkpointCount) + if err != nil { + log.Error(err, "calculate file checksum error") + return err + } + + hvsJson, _ := jsonutil.MarshalIndent(hvs) + log.Log(string(hvsJson)) + return err +} diff --git a/checksum/checksum_test.go b/checksum/checksum_test.go new file mode 100644 index 00000000..6b181a56 --- /dev/null +++ b/checksum/checksum_test.go @@ -0,0 +1,19 @@ +package checksum + +import "testing" + +func TestPrintChecksum(t *testing.T) { + path := "./checksum_test.go" + err := PrintChecksum(path, 1024*1024, 10) + if err != nil { + t.Errorf("test PrintChecksum error => %v", err) + } +} + +func TestPrintChecksumError(t *testing.T) { + path := "./" + err := PrintChecksum(path, 1024*1024, 10) + if err == nil { + t.Errorf("test PrintChecksum expect get error but get nil") + } +} diff --git a/cmd/gofs/flag.go b/cmd/gofs/flag.go index 4b8ad970..5349107b 100644 --- a/cmd/gofs/flag.go +++ b/cmd/gofs/flag.go @@ -84,6 +84,9 @@ func parseFlags() { flag.IntVar(&config.RandomPasswordLen, "rand_pwd_len", 10, "the length of the random user's password") flag.StringVar(&config.RandomDefaultPerm, "rand_perm", "r", "the default permission of every random user, like 'rwx'") + // checksum + flag.BoolVar(&config.Checksum, "checksum", false, "calculate and print the checksum for source file") + flag.Parse() } diff --git a/cmd/gofs/main.go b/cmd/gofs/main.go index 96181130..a7c4d355 100644 --- a/cmd/gofs/main.go +++ b/cmd/gofs/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/no-src/gofs/about" "github.com/no-src/gofs/auth" + "github.com/no-src/gofs/checksum" "github.com/no-src/gofs/conf" "github.com/no-src/gofs/daemon" "github.com/no-src/gofs/fs" @@ -139,6 +140,12 @@ func executeOnce() (exit bool) { return true } + // calculate checksum + if config.Checksum { + checksum.PrintChecksum(config.Source.Path(), config.ChunkSize, config.CheckpointCount) + return true + } + return false } diff --git a/conf/config.go b/conf/config.go index 017fa46c..ac09e806 100644 --- a/conf/config.go +++ b/conf/config.go @@ -64,4 +64,7 @@ type Config struct { RandomUserNameLen int `json:"rand_user_len" yaml:"rand_user_len"` RandomPasswordLen int `json:"rand_pwd_len" yaml:"rand_pwd_len"` RandomDefaultPerm string `json:"rand_perm" yaml:"rand_perm"` + + // checksum + Checksum bool `json:"checksum" yaml:"checksum"` } diff --git a/conf/example/gofs-remote-client.json b/conf/example/gofs-remote-client.json index 04025dc2..2091eb94 100644 --- a/conf/example/gofs-remote-client.json +++ b/conf/example/gofs-remote-client.json @@ -38,5 +38,6 @@ "rand_user_count": 0, "rand_user_len": 6, "rand_pwd_len": 10, - "rand_perm": "r" + "rand_perm": "r", + "checksum": false } \ No newline at end of file diff --git a/conf/example/gofs-remote-server.yaml b/conf/example/gofs-remote-server.yaml index a8838015..d8ff2295 100644 --- a/conf/example/gofs-remote-server.yaml +++ b/conf/example/gofs-remote-server.yaml @@ -37,4 +37,5 @@ users: gofs|password|rwx rand_user_count: 0 rand_user_len: 6 rand_pwd_len: 10 -rand_perm: r \ No newline at end of file +rand_perm: r +checksum: false \ No newline at end of file