2017-02-13 00:30:18 +08:00
|
|
|
package cryptcheck
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2021-11-04 18:12:57 +08:00
|
|
|
"fmt"
|
2019-06-17 16:34:30 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/backend/crypt"
|
|
|
|
"github.com/rclone/rclone/cmd"
|
2020-03-09 18:54:41 +08:00
|
|
|
"github.com/rclone/rclone/cmd/check"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/hash"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2017-02-13 00:30:18 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 23:58:11 +08:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 23:55:04 +08:00
|
|
|
cmdFlag := commandDefinition.Flags()
|
2020-03-09 18:54:41 +08:00
|
|
|
check.AddFlags(cmdFlag)
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 23:58:11 +08:00
|
|
|
var commandDefinition = &cobra.Command{
|
2017-02-13 00:30:18 +08:00
|
|
|
Use: "cryptcheck remote:path cryptedremote:path",
|
2017-06-26 05:43:28 +08:00
|
|
|
Short: `Cryptcheck checks the integrity of a crypted remote.`,
|
2017-02-13 00:30:18 +08:00
|
|
|
Long: `
|
2022-06-19 21:51:37 +08:00
|
|
|
rclone cryptcheck checks a remote against a [crypted](/crypt/) remote.
|
|
|
|
This is the equivalent of running rclone [check](/commands/rclone_check/),
|
|
|
|
but able to check the checksums of the crypted remote.
|
2017-02-13 00:30:18 +08:00
|
|
|
|
|
|
|
For it to work the underlying remote of the cryptedremote must support
|
|
|
|
some kind of checksum.
|
|
|
|
|
|
|
|
It works by reading the nonce from each file on the cryptedremote: and
|
|
|
|
using that to encrypt each file on the remote:. It then checks the
|
|
|
|
checksum of the underlying file on the cryptedremote: against the
|
|
|
|
checksum of the file it has just encrypted.
|
|
|
|
|
|
|
|
Use it like this
|
|
|
|
|
|
|
|
rclone cryptcheck /path/to/files encryptedremote:path
|
|
|
|
|
|
|
|
You can use it like this also, but that will involve downloading all
|
|
|
|
the files in remote:path.
|
|
|
|
|
|
|
|
rclone cryptcheck remote:path encryptedremote:path
|
|
|
|
|
|
|
|
After it has run it will log the status of the encryptedremote:.
|
2020-03-09 18:54:41 +08:00
|
|
|
` + check.FlagsHelp,
|
2017-02-13 00:30:18 +08:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(2, 2, command, args)
|
|
|
|
fsrc, fdst := cmd.NewFsSrcDst(args)
|
|
|
|
cmd.Run(false, true, command, func() error {
|
2019-06-17 16:34:30 +08:00
|
|
|
return cryptCheck(context.Background(), fdst, fsrc)
|
2017-02-13 00:30:18 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// cryptCheck checks the integrity of a crypted remote
|
2019-06-17 16:34:30 +08:00
|
|
|
func cryptCheck(ctx context.Context, fdst, fsrc fs.Fs) error {
|
2017-02-13 00:30:18 +08:00
|
|
|
// Check to see fcrypt is a crypt
|
|
|
|
fcrypt, ok := fdst.(*crypt.Fs)
|
|
|
|
if !ok {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("%s:%s is not a crypt remote", fdst.Name(), fdst.Root())
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|
|
|
|
// Find a hash to use
|
|
|
|
funderlying := fcrypt.UnWrap()
|
|
|
|
hashType := funderlying.Hashes().GetOne()
|
2018-01-19 04:27:52 +08:00
|
|
|
if hashType == hash.None {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("%s:%s does not support any hashes", funderlying.Name(), funderlying.Root())
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|
|
|
|
fs.Infof(nil, "Using %v for hash comparisons", hashType)
|
|
|
|
|
2020-03-09 18:54:41 +08:00
|
|
|
opt, close, err := check.GetCheckOpt(fsrc, fcrypt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer close()
|
|
|
|
|
2017-02-13 00:30:18 +08:00
|
|
|
// checkIdentical checks to see if dst and src are identical
|
|
|
|
//
|
|
|
|
// it returns true if differences were found
|
|
|
|
// it also returns whether it couldn't be hashed
|
2020-03-09 18:54:41 +08:00
|
|
|
opt.Check = func(ctx context.Context, dst, src fs.Object) (differ bool, noHash bool, err error) {
|
2017-02-13 00:30:18 +08:00
|
|
|
cryptDst := dst.(*crypt.Object)
|
|
|
|
underlyingDst := cryptDst.UnWrap()
|
2019-06-17 16:34:30 +08:00
|
|
|
underlyingHash, err := underlyingDst.Hash(ctx, hashType)
|
2017-02-13 00:30:18 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return true, false, fmt.Errorf("error reading hash from underlying %v: %w", underlyingDst, err)
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|
|
|
|
if underlyingHash == "" {
|
2020-03-09 18:54:41 +08:00
|
|
|
return false, true, nil
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
cryptHash, err := fcrypt.ComputeHash(ctx, cryptDst, src, hashType)
|
2017-02-13 00:30:18 +08:00
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return true, false, fmt.Errorf("error computing hash: %w", err)
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|
|
|
|
if cryptHash == "" {
|
2020-03-09 18:54:41 +08:00
|
|
|
return false, true, nil
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|
|
|
|
if cryptHash != underlyingHash {
|
2021-11-04 18:12:57 +08:00
|
|
|
err = fmt.Errorf("hashes differ (%s:%s) %q vs (%s:%s) %q", fdst.Name(), fdst.Root(), cryptHash, fsrc.Name(), fsrc.Root(), underlyingHash)
|
2017-11-15 13:32:00 +08:00
|
|
|
fs.Errorf(src, err.Error())
|
2020-03-09 18:54:41 +08:00
|
|
|
return true, false, nil
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|
2020-03-09 18:54:41 +08:00
|
|
|
return false, false, nil
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|
|
|
|
|
2020-03-09 18:54:41 +08:00
|
|
|
return operations.CheckFn(ctx, opt)
|
2017-02-13 00:30:18 +08:00
|
|
|
}
|