Skip to content

Commit 66a89c3

Browse files
authored
[filesystem] Make file hashing context aware (#211)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description - Added `FileHashWithContext` so that the operation takes into account a context and can be cancelled ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 28a5eb9 commit 66a89c3

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

changes/20230222130436.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `[filesystem]` Make file hashing operation context aware

utils/filesystem/files.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,12 +958,16 @@ func (fs *VFS) moveFile(ctx context.Context, src string, dest string) (err error
958958
return
959959
}
960960

961-
func (fs *VFS) FileHash(hashAlgo string, path string) (hash string, err error) {
961+
func (fs *VFS) FileHash(hashAlgo string, path string) (string, error) {
962+
return fs.FileHashWithContext(context.Background(), hashAlgo, path)
963+
}
964+
965+
func (fs *VFS) FileHashWithContext(ctx context.Context, hashAlgo string, path string) (hash string, err error) {
962966
hasher, err := NewFileHash(hashAlgo)
963967
if err != nil {
964968
return
965969
}
966-
hash, err = hasher.CalculateFile(fs, path)
970+
hash, err = hasher.CalculateFileWithContext(ctx, fs, path)
967971
return
968972
}
969973

utils/filesystem/interfaces.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ type FS interface {
294294
UnzipWithContextAndLimits(ctx context.Context, source string, destination string, limits ILimits) (fileList []string, err error)
295295
// FileHash calculates file hash
296296
FileHash(hashAlgo string, path string) (string, error)
297+
// FileHashWithContext calculates file hash
298+
FileHashWithContext(ctx context.Context, hashAlgo string, path string) (string, error)
297299
// IsZip states whether a file is a zip file or not. If the file does not exist, it will state whether the filename has a zip extension or not.
298300
IsZip(filepath string) bool
299301
// IsZipWithContext states whether a file is a zip file or not. Since the process can take some time (i.e type detection with sniffers such as http.DetectContentType), it is controlled by a context.

utils/mocks/mock_filesystem.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)