2022-08-28 19:21:57 +08:00
|
|
|
// Package md5sum provides the md5sum command.
|
2016-08-06 00:12:27 +08:00
|
|
|
package md5sum
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2016-08-06 00:12:27 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2020-12-18 20:45:58 +08:00
|
|
|
"github.com/rclone/rclone/cmd/hashsum"
|
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2016-08-06 00:12:27 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 23:58:11 +08:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2020-12-18 20:45:58 +08:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2021-11-03 07:34:20 +08:00
|
|
|
hashsum.AddHashsumFlags(cmdFlags)
|
2016-08-06 00:12:27 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 23:58:11 +08:00
|
|
|
var commandDefinition = &cobra.Command{
|
2016-08-06 00:12:27 +08:00
|
|
|
Use: "md5sum remote:path",
|
|
|
|
Short: `Produces an md5sum file for all the objects in the path.`,
|
|
|
|
Long: `
|
|
|
|
Produces an md5sum file for all the objects in the path. This
|
|
|
|
is in the same format as the standard md5sum tool produces.
|
2020-12-18 20:45:58 +08:00
|
|
|
|
|
|
|
By default, the hash is requested from the remote. If MD5 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 MD5 for any remote.
|
2021-11-03 07:34:20 +08:00
|
|
|
|
2022-06-19 21:51:37 +08:00
|
|
|
For other algorithms, see the [hashsum](/commands/rclone_hashsum/)
|
|
|
|
command. Running ` + "`rclone md5sum remote:path`" + ` is equivalent
|
|
|
|
to running ` + "`rclone hashsum MD5 remote:path`" + `.
|
|
|
|
|
2021-11-03 07:34:20 +08:00
|
|
|
This command can also hash data received on standard input (stdin),
|
|
|
|
by not passing a remote:path, or by passing a hyphen as remote:path
|
2022-08-14 10:56:32 +08:00
|
|
|
when there is data to read (if not, the hyphen will be treated literally,
|
2021-11-03 07:34:20 +08:00
|
|
|
as a relative path).
|
2016-08-06 00:12:27 +08:00
|
|
|
`,
|
2021-11-03 07:34:20 +08:00
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
|
|
|
cmd.CheckArgs(0, 1, command, args)
|
|
|
|
if found, err := hashsum.CreateFromStdinArg(hash.MD5, args, 0); found {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-06 00:12:27 +08:00
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2016-12-05 00:52:24 +08:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2021-07-07 23:34:16 +08:00
|
|
|
if hashsum.ChecksumFile != "" {
|
|
|
|
fsum, sumFile := cmd.NewFsFile(hashsum.ChecksumFile)
|
|
|
|
return operations.CheckSum(context.Background(), fsrc, fsum, sumFile, hash.MD5, nil, hashsum.DownloadFlag)
|
|
|
|
}
|
2020-12-18 20:45:58 +08:00
|
|
|
if hashsum.HashsumOutfile == "" {
|
|
|
|
return operations.HashLister(context.Background(), hash.MD5, hashsum.OutputBase64, hashsum.DownloadFlag, fsrc, nil)
|
|
|
|
}
|
|
|
|
output, close, err := hashsum.GetHashsumOutput(hashsum.HashsumOutfile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer close()
|
|
|
|
return operations.HashLister(context.Background(), hash.MD5, hashsum.OutputBase64, hashsum.DownloadFlag, fsrc, output)
|
2016-08-06 00:12:27 +08:00
|
|
|
})
|
2021-11-03 07:34:20 +08:00
|
|
|
return nil
|
2016-08-06 00:12:27 +08:00
|
|
|
},
|
|
|
|
}
|