Skip to content

Commit

Permalink
Add the -checksum flag to calculate and print the checksum for source…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
mstmdev committed Apr 19, 2022
1 parent 55024f4 commit 1e7c089
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

## 更多信息

### 帮助信息
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions checksum/checksum.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions checksum/checksum_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
3 changes: 3 additions & 0 deletions cmd/gofs/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
7 changes: 7 additions & 0 deletions cmd/gofs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
3 changes: 2 additions & 1 deletion conf/example/gofs-remote-client.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
"rand_user_count": 0,
"rand_user_len": 6,
"rand_pwd_len": 10,
"rand_perm": "r"
"rand_perm": "r",
"checksum": false
}
3 changes: 2 additions & 1 deletion conf/example/gofs-remote-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ users: gofs|password|rwx
rand_user_count: 0
rand_user_len: 6
rand_pwd_len: 10
rand_perm: r
rand_perm: r
checksum: false

0 comments on commit 1e7c089

Please sign in to comment.