2018-04-20 10:33:50 +00:00
package hashsum
import (
2019-06-17 08:34:30 +00:00
"context"
2018-04-20 10:33:50 +00:00
"fmt"
"os"
2020-12-18 12:45:58 +00:00
"github.com/pkg/errors"
2019-07-28 17:47:38 +00:00
"github.com/rclone/rclone/cmd"
2020-12-18 12:45:58 +00:00
"github.com/rclone/rclone/fs"
2019-10-26 19:27:33 +00:00
"github.com/rclone/rclone/fs/config/flags"
2019-07-28 17:47:38 +00:00
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/fs/operations"
2018-04-20 10:33:50 +00:00
"github.com/spf13/cobra"
2020-12-18 12:45:58 +00:00
"github.com/spf13/pflag"
2018-04-20 10:33:50 +00:00
)
2020-12-18 12:45:58 +00:00
// Global hashsum flags for reuse in md5sum, sha1sum, and dbhashsum
2019-10-26 19:27:33 +00:00
var (
2020-12-18 12:45:58 +00:00
OutputBase64 = false
DownloadFlag = false
HashsumOutfile = ""
2019-10-26 19:27:33 +00:00
)
2018-04-20 10:33:50 +00:00
func init ( ) {
cmd . Root . AddCommand ( commandDefinition )
2019-10-26 19:27:33 +00:00
cmdFlags := commandDefinition . Flags ( )
2020-12-18 12:45:58 +00:00
AddHashFlags ( cmdFlags )
}
// AddHashFlags is a convenience function to add the command flags OutputBase64 and DownloadFlag to hashsum, md5sum, sha1sum, and dbhashsum
func AddHashFlags ( cmdFlags * pflag . FlagSet ) {
flags . BoolVarP ( cmdFlags , & OutputBase64 , "base64" , "" , OutputBase64 , "Output base64 encoded hashsum" )
flags . StringVarP ( cmdFlags , & HashsumOutfile , "output-file" , "" , HashsumOutfile , "Output hashsums to a file rather than the terminal" )
flags . BoolVarP ( cmdFlags , & DownloadFlag , "download" , "" , DownloadFlag , "Download the file and hash it locally; if this flag is not specified, the hash is requested from the remote" )
}
// GetHashsumOutput opens and closes the output file when using the output-file flag
func GetHashsumOutput ( filename string ) ( out * os . File , close func ( ) , err error ) {
out , err = os . Create ( filename )
if err != nil {
err = errors . Wrapf ( err , "Failed to open output file %v" , filename )
return nil , nil , err
}
close = func ( ) {
err := out . Close ( )
if err != nil {
fs . Errorf ( nil , "Failed to close output file %v: %v" , filename , err )
}
}
return out , close , nil
2018-04-20 10:33:50 +00:00
}
var commandDefinition = & cobra . Command {
Use : "hashsum <hash> remote:path" ,
2020-05-19 11:02:44 +00:00
Short : ` Produces a hashsum file for all the objects in the path. ` ,
2018-04-20 10:33:50 +00:00
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 .
2020-12-18 12:45:58 +00:00
By default , the hash is requested from the remote . If the hash is
not supported by the remote , no hash will be returned . With the
download flag , the file will be downloaded from the remote and
hashed locally enabling any hash for any remote .
Run without a hash to see the list of all supported hashes , e . g .
2018-04-20 10:33:50 +00:00
$ 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" )
2019-09-23 13:32:36 +00:00
for _ , ht := range hash . Supported ( ) . Array ( ) {
2018-04-20 10:33:50 +00:00
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 : ] )
2020-12-18 12:45:58 +00:00
2018-04-20 10:33:50 +00:00
cmd . Run ( false , false , command , func ( ) error {
2020-12-18 12:45:58 +00:00
if HashsumOutfile == "" {
return operations . HashLister ( context . Background ( ) , ht , OutputBase64 , DownloadFlag , fsrc , nil )
}
output , close , err := GetHashsumOutput ( HashsumOutfile )
if err != nil {
return err
2019-10-26 19:27:33 +00:00
}
2020-12-18 12:45:58 +00:00
defer close ( )
return operations . HashLister ( context . Background ( ) , ht , OutputBase64 , DownloadFlag , fsrc , output )
2018-04-20 10:33:50 +00:00
} )
return nil
} ,
}