cryptCheck checks the integrity of an encrypted remote
(ctx context.Context, fdst, fsrc fs.Fs)
| 65 | |
| 66 | // cryptCheck checks the integrity of an encrypted remote |
| 67 | func cryptCheck(ctx context.Context, fdst, fsrc fs.Fs) error { |
| 68 | // Check to see fcrypt is a crypt |
| 69 | fcrypt, ok := fdst.(*crypt.Fs) |
| 70 | if !ok { |
| 71 | return fmt.Errorf("%s:%s is not a crypt remote", fdst.Name(), fdst.Root()) |
| 72 | } |
| 73 | // Find a hash to use |
| 74 | funderlying := fcrypt.UnWrap() |
| 75 | hashType := funderlying.Hashes().GetOne() |
| 76 | if hashType == hash.None { |
| 77 | return fmt.Errorf("%s:%s does not support any hashes", funderlying.Name(), funderlying.Root()) |
| 78 | } |
| 79 | fs.Infof(nil, "Using %v for hash comparisons", hashType) |
| 80 | |
| 81 | opt, close, err := check.GetCheckOpt(fsrc, fcrypt) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | defer close() |
| 86 | |
| 87 | // checkIdentical checks to see if dst and src are identical |
| 88 | // |
| 89 | // it returns true if differences were found |
| 90 | // it also returns whether it couldn't be hashed |
| 91 | opt.Check = func(ctx context.Context, dst, src fs.Object) (differ bool, noHash bool, err error) { |
| 92 | cryptDst := dst.(*crypt.Object) |
| 93 | underlyingDst := cryptDst.UnWrap() |
| 94 | underlyingHash, err := underlyingDst.Hash(ctx, hashType) |
| 95 | if err != nil { |
| 96 | return true, false, fmt.Errorf("error reading hash from underlying %v: %w", underlyingDst, err) |
| 97 | } |
| 98 | if underlyingHash == "" { |
| 99 | return false, true, nil |
| 100 | } |
| 101 | cryptHash, err := fcrypt.ComputeHash(ctx, cryptDst, src, hashType) |
| 102 | if err != nil { |
| 103 | return true, false, fmt.Errorf("error computing hash: %w", err) |
| 104 | } |
| 105 | if cryptHash == "" { |
| 106 | return false, true, nil |
| 107 | } |
| 108 | if cryptHash != underlyingHash { |
| 109 | err = fmt.Errorf("hashes differ (%s:%s) %q vs (%s:%s) %q", fdst.Name(), fdst.Root(), cryptHash, fsrc.Name(), fsrc.Root(), underlyingHash) |
| 110 | fs.Errorf(src, "%s", err.Error()) |
| 111 | return true, false, nil |
| 112 | } |
| 113 | return false, false, nil |
| 114 | } |
| 115 | |
| 116 | return operations.CheckFn(ctx, opt) |
| 117 | } |
no test coverage detected
searching dependent graphs…