hashsum: make generic tool for any hash to produce md5sum like output

This commit is contained in:
Nick Craig-Wood 2018-04-20 11:33:50 +01:00
parent 57a5b72d60
commit c51d97c752
3 changed files with 67 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import (
_ "github.com/ncw/rclone/cmd/delete"
_ "github.com/ncw/rclone/cmd/genautocomplete"
_ "github.com/ncw/rclone/cmd/gendocs"
_ "github.com/ncw/rclone/cmd/hashsum"
_ "github.com/ncw/rclone/cmd/info"
_ "github.com/ncw/rclone/cmd/link"
_ "github.com/ncw/rclone/cmd/listremotes"

61
cmd/hashsum/hashsum.go Normal file
View File

@ -0,0 +1,61 @@
package hashsum
import (
"errors"
"fmt"
"os"
"github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/fs/hash"
"github.com/ncw/rclone/fs/operations"
"github.com/spf13/cobra"
)
func init() {
cmd.Root.AddCommand(commandDefinition)
}
var commandDefinition = &cobra.Command{
Use: "hashsum <hash> remote:path",
Short: `Produces an hashsum file for all the objects in the path.`,
Long: `
Produces a hash file for all the objects in the path using the hash
named. The output is in the same format as the standard
md5sum/sha1sum tool.
Run without a hash to see the list of supported hashes, eg
$ rclone hashsum
Supported hashes are:
* MD5
* SHA-1
* DropboxHash
* QuickXorHash
Then
$ rclone hashsum MD5 remote:path
`,
RunE: func(command *cobra.Command, args []string) error {
cmd.CheckArgs(0, 2, command, args)
if len(args) == 0 {
fmt.Printf("Supported hashes are:\n")
for _, ht := range hash.Supported.Array() {
fmt.Printf(" * %v\n", ht)
}
return nil
} else if len(args) == 1 {
return errors.New("need hash type and remote")
}
var ht hash.Type
err := ht.Set(args[0])
if err != nil {
return err
}
fsrc := cmd.NewFsSrc(args[1:])
cmd.Run(false, false, command, func() error {
return operations.HashLister(ht, fsrc, os.Stdout)
})
return nil
},
}

View File

@ -840,7 +840,7 @@ func ListLong(f fs.Fs, w io.Writer) error {
//
// Lists in parallel which may get them out of order
func Md5sum(f fs.Fs, w io.Writer) error {
return hashLister(hash.MD5, f, w)
return HashLister(hash.MD5, f, w)
}
// Sha1sum list the Fs to the supplied writer
@ -849,7 +849,7 @@ func Md5sum(f fs.Fs, w io.Writer) error {
//
// Lists in parallel which may get them out of order
func Sha1sum(f fs.Fs, w io.Writer) error {
return hashLister(hash.SHA1, f, w)
return HashLister(hash.SHA1, f, w)
}
// DropboxHashSum list the Fs to the supplied writer
@ -858,7 +858,7 @@ func Sha1sum(f fs.Fs, w io.Writer) error {
//
// Lists in parallel which may get them out of order
func DropboxHashSum(f fs.Fs, w io.Writer) error {
return hashLister(hash.Dropbox, f, w)
return HashLister(hash.Dropbox, f, w)
}
// hashSum returns the human readable hash for ht passed in. This may
@ -876,7 +876,8 @@ func hashSum(ht hash.Type, o fs.Object) string {
return sum
}
func hashLister(ht hash.Type, f fs.Fs, w io.Writer) error {
// HashLister does a md5sum equivalent for the hash type passed in
func HashLister(ht hash.Type, f fs.Fs, w io.Writer) error {
return ListFn(f, func(o fs.Object) {
sum := hashSum(ht, o)
syncFprintf(w, "%*s %s\n", hash.Width[ht], sum, o.Remote())