mirror of
https://github.com/rclone/rclone.git
synced 2024-12-23 07:29:35 +01:00
c8cfa43ccc
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.
124 lines
2.7 KiB
Go
124 lines
2.7 KiB
Go
// Show the dynamic progress bar
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
const (
|
|
// interval between progress prints
|
|
defaultProgressInterval = 500 * time.Millisecond
|
|
// time format for logging
|
|
logTimeFormat = "2006-01-02 15:04:05"
|
|
)
|
|
|
|
// startProgress starts the progress bar printing
|
|
//
|
|
// It returns a func which should be called to stop the stats.
|
|
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) {
|
|
printProgress(fmt.Sprintf("%s %-6s: %s", time.Now().Format(logTimeFormat), level, text))
|
|
|
|
}
|
|
}
|
|
|
|
// 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() {
|
|
defer wg.Done()
|
|
progressInterval := defaultProgressInterval
|
|
if ShowStats() && *statsInterval > 0 {
|
|
progressInterval = *statsInterval
|
|
}
|
|
ticker := time.NewTicker(progressInterval)
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
printProgress("")
|
|
case <-stopStats:
|
|
ticker.Stop()
|
|
printProgress("")
|
|
fs.LogPrint = oldLogPrint
|
|
operations.SyncPrintf = oldSyncPrint
|
|
fmt.Println("")
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
return func() {
|
|
close(stopStats)
|
|
wg.Wait()
|
|
}
|
|
}
|
|
|
|
// state for the progress printing
|
|
var (
|
|
nlines = 0 // number of lines in the previous stats block
|
|
progressMu sync.Mutex
|
|
)
|
|
|
|
// printProgress prints the progress with an optional log
|
|
func printProgress(logMessage string) {
|
|
progressMu.Lock()
|
|
defer progressMu.Unlock()
|
|
|
|
var buf bytes.Buffer
|
|
w, _ := terminal.GetSize()
|
|
stats := strings.TrimSpace(accounting.GlobalStats().String())
|
|
logMessage = strings.TrimSpace(logMessage)
|
|
|
|
out := func(s string) {
|
|
buf.WriteString(s)
|
|
}
|
|
|
|
if logMessage != "" {
|
|
out("\n")
|
|
out(terminal.MoveUp)
|
|
}
|
|
// Move to the start of the block we wrote erasing all the previous lines
|
|
for i := 0; i < nlines-1; i++ {
|
|
out(terminal.EraseLine)
|
|
out(terminal.MoveUp)
|
|
}
|
|
out(terminal.EraseLine)
|
|
out(terminal.MoveToStartOfLine)
|
|
if logMessage != "" {
|
|
out(terminal.EraseLine)
|
|
out(logMessage + "\n")
|
|
}
|
|
fixedLines := strings.Split(stats, "\n")
|
|
nlines = len(fixedLines)
|
|
for i, line := range fixedLines {
|
|
if len(line) > w {
|
|
line = line[:w]
|
|
}
|
|
out(line)
|
|
if i != nlines-1 {
|
|
out("\n")
|
|
}
|
|
}
|
|
terminal.Write(buf.Bytes())
|
|
}
|