Skip to content

Commit 42a18a3

Browse files
🐛 filesystem Unlink files before attempting to remove them in Remove (#664)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description <!-- Please add any detail or context that would be useful to a reviewer. --> Unlink files before attempting to remove them in Remove. This is because for things like socket files the remove will fail if they are linked. ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [ ] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [x] Additional tests are not required for this change (e.g. documentation update). --------- Co-authored-by: Adrien CABARBAYE <[email protected]>
1 parent 860c035 commit 42a18a3

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

changes/20250801093542.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:bug: `filsystem` Unlink files before attempting to remove them in Remove

changes/20250801105019.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `commonerrors` Add IgnoreCorrespondTo function

utils/commonerrors/errors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ func Ignore(target error, ignore ...error) error {
255255
return target
256256
}
257257

258+
// IgnoreCorrespondTo will return nil if the target error matches one of the descriptions of errors to ignore
259+
func IgnoreCorrespondTo(target error, ignore ...string) error {
260+
if CorrespondTo(target, ignore...) {
261+
return nil
262+
}
263+
return target
264+
}
265+
258266
// IsEmpty states whether an error is empty or not.
259267
// An error is considered empty if it is `nil` or has no description.
260268
func IsEmpty(err error) bool {

utils/commonerrors/errors_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func TestCorrespondTo(t *testing.T) {
4949
assert.True(t, CorrespondTo(fmt.Errorf("%v %v", faker.Sentence(), strings.ToUpper(ErrUndefined.Error())), strings.ToLower(ErrUndefined.Error())))
5050
}
5151

52+
func TestIgnoreCorrespondTo(t *testing.T) {
53+
assert.NoError(t, IgnoreCorrespondTo(errors.New("test"), "test"))
54+
assert.NoError(t, IgnoreCorrespondTo(errors.New("test 123"), "test"))
55+
assert.Error(t, IgnoreCorrespondTo(errors.New("test 123"), "abc", "def", faker.Word()))
56+
assert.NoError(t, IgnoreCorrespondTo(ErrCondition, "condition"))
57+
}
58+
5259
func TestContextErrorConversion(t *testing.T) {
5360
defer goleak.VerifyNone(t)
5461
task := func(ctx context.Context) {

utils/filesystem/extendedosfs.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ type ExtendedOsFs struct {
2222
}
2323

2424
func (c *ExtendedOsFs) Remove(name string) (err error) {
25-
err = commonerrors.Ignore(ConvertFileSystemError(c.OsFs.Remove(name)), commonerrors.ErrNotFound)
26-
if err != nil {
27-
return
28-
}
2925
// The following is to ensure sockets are correctly removed
3026
// https://stackoverflow.com/questions/16681944/how-to-reliably-unlink-a-unix-domain-socket-in-go-programming-language
3127
err = commonerrors.Ignore(ConvertFileSystemError(syscall.Unlink(name)), commonerrors.ErrNotFound)
28+
err = commonerrors.IgnoreCorrespondTo(err, "is a directory")
29+
if err != nil {
30+
return
31+
}
32+
33+
err = commonerrors.Ignore(ConvertFileSystemError(c.OsFs.Remove(name)), commonerrors.ErrNotFound)
3234
return
3335
}
3436

0 commit comments

Comments
 (0)