Add a download flag to hashsum and related commands to force rclone to download and hash files locally

This commit modifies the operations.hashSum function by adding an alternate code path. This code path is triggered by passing downloadFlag = True. When activated, rclone will download files from the remote and hash them locally. downloadFlag = False preserves the existing behavior of using the remote to retrieve the hash.

This commit modifies HashLister to support the new hashSum method as well as consolidating the roles of HashLister, HashListerBase64, Md5sum, and Sha1sum.  The printing of hashes from the function defined in HashLister has been revised to work with --progress.  There are light changes to operations.syncFprintf and cmd.startProgress for this.

The unit test operations_test.TestHashSums is modified to support this change and test the download functionality.

The command functions hashsum, md5sum, sha1sum, and dbhashsum are modified to support this change.  A download flag has been added and an output-file flag has been added.  The output-file flag writes hashes to a file instead of stdout to avoid the need to redirect stdout.
This commit is contained in:
lostheli
2020-12-18 07:45:58 -05:00
committed by Nick Craig-Wood
parent ed7af3f370
commit c8cfa43ccc
10 changed files with 319 additions and 75 deletions

View File

@@ -12,6 +12,7 @@ import (
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/accounting"
"github.com/rclone/rclone/fs/log"
"github.com/rclone/rclone/fs/operations"
"github.com/rclone/rclone/lib/terminal"
)
@@ -28,6 +29,8 @@ const (
func startProgress() func() {
stopStats := make(chan struct{})
oldLogPrint := fs.LogPrint
oldSyncPrint := operations.SyncPrintf
if !log.Redirected() {
// Intercept the log calls if not logging to file or syslog
fs.LogPrint = func(level fs.LogLevel, text string) {
@@ -35,6 +38,12 @@ func startProgress() func() {
}
}
// Intercept output from functions such as HashLister to stdout
operations.SyncPrintf = func(format string, a ...interface{}) {
printProgress(fmt.Sprintf(format, a...))
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
@@ -52,6 +61,7 @@ func startProgress() func() {
ticker.Stop()
printProgress("")
fs.LogPrint = oldLogPrint
operations.SyncPrintf = oldSyncPrint
fmt.Println("")
return
}