lsf: add option to print hashes

This commit is contained in:
Nick Craig-Wood
2018-01-06 17:53:37 +00:00
parent 7c71ee1a5b
commit c74c3b37da
5 changed files with 95 additions and 10 deletions
+26
View File
@@ -11,6 +11,7 @@ import (
"github.com/ncw/rclone/dropbox/dbhash"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)
// HashType indicates a standard hashing algorithm
@@ -87,6 +88,31 @@ func (h HashType) String() string {
}
}
// Set a HashType from a flag
func (h *HashType) Set(s string) error {
switch s {
case "None":
*h = HashNone
case "MD5":
*h = HashMD5
case "SHA-1":
*h = HashSHA1
case "DropboxHash":
*h = HashDropbox
default:
return errors.Errorf("Unknown hash type %q", s)
}
return nil
}
// Type of the value
func (h HashType) Type() string {
return "string"
}
// Check it satisfies the interface
var _ pflag.Value = (*HashType)(nil)
// hashFromTypes will return hashers for all the requested types.
// The types must be a subset of SupportedHashes,
// and this function must support all types.
+31 -10
View File
@@ -1068,17 +1068,24 @@ func DropboxHashSum(f Fs, w io.Writer) error {
return hashLister(HashDropbox, f, w)
}
// hashSum returns the human readable hash for ht passed in. This may
// be UNSUPPORTED or ERROR.
func hashSum(ht HashType, o Object) string {
Stats.Checking(o.Remote())
sum, err := o.Hash(ht)
Stats.DoneChecking(o.Remote())
if err == ErrHashUnsupported {
sum = "UNSUPPORTED"
} else if err != nil {
Debugf(o, "Failed to read %v: %v", ht, err)
sum = "ERROR"
}
return sum
}
func hashLister(ht HashType, f Fs, w io.Writer) error {
return ListFn(f, func(o Object) {
Stats.Checking(o.Remote())
sum, err := o.Hash(ht)
Stats.DoneChecking(o.Remote())
if err == ErrHashUnsupported {
sum = "UNSUPPORTED"
} else if err != nil {
Debugf(o, "Failed to read %v: %v", ht, err)
sum = "ERROR"
}
sum := hashSum(ht, o)
syncFprintf(w, "%*s %s\n", HashWidth[ht], sum, o.Remote())
})
}
@@ -1792,6 +1799,7 @@ type ListFormat struct {
dirSlash bool
output []func() string
entry DirEntry
hash bool
}
// SetSeparator changes separator in struct
@@ -1816,7 +1824,9 @@ func (l *ListFormat) AddModTime() {
// AddSize adds file's size to output
func (l *ListFormat) AddSize() {
l.AppendOutput(func() string { return strconv.FormatInt(l.entry.Size(), 10) })
l.AppendOutput(func() string {
return strconv.FormatInt(l.entry.Size(), 10)
})
}
// AddPath adds path to file to output
@@ -1831,6 +1841,17 @@ func (l *ListFormat) AddPath() {
})
}
// AddHash adds the hash of the type given to the output
func (l *ListFormat) AddHash(ht HashType) {
l.AppendOutput(func() string {
o, ok := l.entry.(Object)
if !ok {
return ""
}
return hashSum(ht, o)
})
}
// AppendOutput adds string generated by specific function to printed output
func (l *ListFormat) AppendOutput(functionToAppend func() string) {
if len(l.output) > 0 {
+16
View File
@@ -999,4 +999,20 @@ func TestListFormat(t *testing.T) {
list.SetSeparator("__SEP__")
assert.Equal(t, "1__SEP__a__SEP__"+items[0].ModTime().Format("2006-01-02 15:04:05"), fs.ListFormatted(&items[0], &list))
assert.Equal(t, fmt.Sprintf("%d", items[1].Size())+"__SEP__subdir/__SEP__"+items[1].ModTime().Format("2006-01-02 15:04:05"), fs.ListFormatted(&items[1], &list))
for _, test := range []struct {
ht fs.HashType
want string
}{
{fs.HashMD5, "0cc175b9c0f1b6a831c399e269772661"},
{fs.HashSHA1, "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"},
{fs.HashDropbox, "bf5d3affb73efd2ec6c36ad3112dd933efed63c4e1cbffcfa88e2759c144f2d8"},
} {
list.SetOutput(nil)
list.AddHash(test.ht)
got := fs.ListFormatted(&items[0], &list)
if got != "UNSUPPORTED" {
assert.Equal(t, test.want, got)
}
}
}