MCPcopy
hub / github.com/rclone/rclone / cryptCheck

Function cryptCheck

cmd/cryptcheck/cryptcheck.go:67–117  ·  view source on GitHub ↗

cryptCheck checks the integrity of an encrypted remote

(ctx context.Context, fdst, fsrc fs.Fs)

Source from the content-addressed store, hash-verified

65
66// cryptCheck checks the integrity of an encrypted remote
67func 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}

Callers 1

cryptcheck.goFile · 0.85

Calls 14

InfofFunction · 0.92
GetCheckOptFunction · 0.92
ErrorfFunction · 0.92
CheckFnFunction · 0.92
closeFunction · 0.85
GetOneMethod · 0.80
ComputeHashMethod · 0.80
NameMethod · 0.65
RootMethod · 0.65
UnWrapMethod · 0.65
HashesMethod · 0.65
HashMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…