MCPcopy
hub / github.com/SurgeDM/Surge / computeFileHashMD5WithTimeout

Function computeFileHashMD5WithTimeout

internal/engine/state/state.go:141–178  ·  view source on GitHub ↗
(path string, timeout time.Duration)

Source from the content-addressed store, hash-verified

139}
140
141func computeFileHashMD5WithTimeout(path string, timeout time.Duration) (string, bool, error) {
142 if timeout <= 0 {
143 timeout = DefaultInlineHashTimeout
144 }
145 if timeout < time.Microsecond {
146 return "", true, nil
147 }
148
149 f, err := os.Open(path)
150 if err != nil {
151 return "", false, err
152 }
153 defer func() { _ = f.Close() }()
154
155 h := md5.New()
156 buf := make([]byte, types.MB)
157 deadline := time.Now().Add(timeout)
158
159 for {
160 if time.Now().After(deadline) {
161 return "", true, nil
162 }
163 n, readErr := f.Read(buf)
164 if n > 0 {
165 if _, err := h.Write(buf[:n]); err != nil {
166 return "", false, fmt.Errorf("failed to update md5 hash: %w", err)
167 }
168 }
169 if readErr == io.EOF {
170 break
171 }
172 if readErr != nil {
173 return "", false, fmt.Errorf("failed to read file for md5 hash: %w", readErr)
174 }
175 }
176
177 return hashPrefixMD5 + hex.EncodeToString(h.Sum(nil)), false, nil
178}
179
180func compareAgainstStoredFileHash(path string, storedHash string) (bool, error) {
181 algo, expected := parseStoredHash(storedHash)

Calls 3

AddMethod · 0.65
CloseMethod · 0.45
ReadMethod · 0.45