2022-08-28 19:21:57 +08:00
|
|
|
// Package sha1sum provides the sha1sum command.
|
2016-08-06 00:12:27 +08:00
|
|
|
package sha1sum
|
|
|
|
|
|
|
|
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: "sha1sum remote:path",
|
|
|
|
Short: `Produces an sha1sum file for all the objects in the path.`,
|
|
|
|
Long: `
|
|
|
|
Produces an sha1sum file for all the objects in the path. This
|
|
|
|
is in the same format as the standard sha1sum tool produces.
|
2020-12-18 20:45:58 +08:00
|
|
|
|
|
|
|
By default, the hash is requested from the remote. If SHA-1 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 SHA-1 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 sha1sum remote:path`" + ` is equivalent
|
|
|
|
to running ` + "`rclone hashsum SHA1 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).
|
|
|
|
|
|
|
|
This command can also hash data received on STDIN, if not passing
|
|
|
|
a remote:path.
|
2016-08-06 00:12:27 +08:00
|
|
|
`,
|
2022-11-27 06:40:49 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.27",
|
2023-07-11 01:34:10 +08:00
|
|
|
"groups": "Filter,Listing",
|
2022-11-27 06:40:49 +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.SHA1, 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.SHA1, nil, hashsum.DownloadFlag)
|
|
|
|
}
|
2020-12-18 20:45:58 +08:00
|
|
|
if hashsum.HashsumOutfile == "" {
|
|
|
|
return operations.HashLister(context.Background(), hash.SHA1, 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.SHA1, 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
|
|
|
},
|
|
|
|
}
|