cryptcheck.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Package cryptcheck provides the cryptcheck command.
  2. package cryptcheck
  3. import (
  4. "context"
  5. "fmt"
  6. "github.com/rclone/rclone/backend/crypt"
  7. "github.com/rclone/rclone/cmd"
  8. "github.com/rclone/rclone/cmd/check"
  9. "github.com/rclone/rclone/fs"
  10. "github.com/rclone/rclone/fs/hash"
  11. "github.com/rclone/rclone/fs/operations"
  12. "github.com/spf13/cobra"
  13. )
  14. func init() {
  15. cmd.Root.AddCommand(commandDefinition)
  16. cmdFlag := commandDefinition.Flags()
  17. check.AddFlags(cmdFlag)
  18. }
  19. var commandDefinition = &cobra.Command{
  20. Use: "cryptcheck remote:path cryptedremote:path",
  21. Short: `Cryptcheck checks the integrity of an encrypted remote.`,
  22. Long: `
  23. rclone cryptcheck checks a remote against a [crypted](/crypt/) remote.
  24. This is the equivalent of running rclone [check](/commands/rclone_check/),
  25. but able to check the checksums of the encrypted remote.
  26. For it to work the underlying remote of the cryptedremote must support
  27. some kind of checksum.
  28. It works by reading the nonce from each file on the cryptedremote: and
  29. using that to encrypt each file on the remote:. It then checks the
  30. checksum of the underlying file on the cryptedremote: against the
  31. checksum of the file it has just encrypted.
  32. Use it like this
  33. rclone cryptcheck /path/to/files encryptedremote:path
  34. You can use it like this also, but that will involve downloading all
  35. the files in remote:path.
  36. rclone cryptcheck remote:path encryptedremote:path
  37. After it has run it will log the status of the encryptedremote:.
  38. ` + check.FlagsHelp,
  39. Annotations: map[string]string{
  40. "versionIntroduced": "v1.36",
  41. "groups": "Filter,Listing,Check",
  42. },
  43. Run: func(command *cobra.Command, args []string) {
  44. cmd.CheckArgs(2, 2, command, args)
  45. fsrc, fdst := cmd.NewFsSrcDst(args)
  46. cmd.Run(false, true, command, func() error {
  47. return cryptCheck(context.Background(), fdst, fsrc)
  48. })
  49. },
  50. }
  51. // cryptCheck checks the integrity of an encrypted remote
  52. func cryptCheck(ctx context.Context, fdst, fsrc fs.Fs) error {
  53. // Check to see fcrypt is a crypt
  54. fcrypt, ok := fdst.(*crypt.Fs)
  55. if !ok {
  56. return fmt.Errorf("%s:%s is not a crypt remote", fdst.Name(), fdst.Root())
  57. }
  58. // Find a hash to use
  59. funderlying := fcrypt.UnWrap()
  60. hashType := funderlying.Hashes().GetOne()
  61. if hashType == hash.None {
  62. return fmt.Errorf("%s:%s does not support any hashes", funderlying.Name(), funderlying.Root())
  63. }
  64. fs.Infof(nil, "Using %v for hash comparisons", hashType)
  65. opt, close, err := check.GetCheckOpt(fsrc, fcrypt)
  66. if err != nil {
  67. return err
  68. }
  69. defer close()
  70. // checkIdentical checks to see if dst and src are identical
  71. //
  72. // it returns true if differences were found
  73. // it also returns whether it couldn't be hashed
  74. opt.Check = func(ctx context.Context, dst, src fs.Object) (differ bool, noHash bool, err error) {
  75. cryptDst := dst.(*crypt.Object)
  76. underlyingDst := cryptDst.UnWrap()
  77. underlyingHash, err := underlyingDst.Hash(ctx, hashType)
  78. if err != nil {
  79. return true, false, fmt.Errorf("error reading hash from underlying %v: %w", underlyingDst, err)
  80. }
  81. if underlyingHash == "" {
  82. return false, true, nil
  83. }
  84. cryptHash, err := fcrypt.ComputeHash(ctx, cryptDst, src, hashType)
  85. if err != nil {
  86. return true, false, fmt.Errorf("error computing hash: %w", err)
  87. }
  88. if cryptHash == "" {
  89. return false, true, nil
  90. }
  91. if cryptHash != underlyingHash {
  92. err = fmt.Errorf("hashes differ (%s:%s) %q vs (%s:%s) %q", fdst.Name(), fdst.Root(), cryptHash, fsrc.Name(), fsrc.Root(), underlyingHash)
  93. fs.Errorf(src, err.Error())
  94. return true, false, nil
  95. }
  96. return false, false, nil
  97. }
  98. return operations.CheckFn(ctx, opt)
  99. }