diff --git a/changes/20250801093542.bugfix b/changes/20250801093542.bugfix new file mode 100644 index 0000000000..814844bdfc --- /dev/null +++ b/changes/20250801093542.bugfix @@ -0,0 +1 @@ +:bug: `filsystem` Unlink files before attempting to remove them in Remove diff --git a/changes/20250801105019.feature b/changes/20250801105019.feature new file mode 100644 index 0000000000..22c5a3e63e --- /dev/null +++ b/changes/20250801105019.feature @@ -0,0 +1 @@ +:sparkles: `commonerrors` Add IgnoreCorrespondTo function diff --git a/utils/commonerrors/errors.go b/utils/commonerrors/errors.go index 7eabf99247..34d23f3402 100644 --- a/utils/commonerrors/errors.go +++ b/utils/commonerrors/errors.go @@ -255,6 +255,14 @@ func Ignore(target error, ignore ...error) error { return target } +// IgnoreCorrespondTo will return nil if the target error matches one of the descriptions of errors to ignore +func IgnoreCorrespondTo(target error, ignore ...string) error { + if CorrespondTo(target, ignore...) { + return nil + } + return target +} + // IsEmpty states whether an error is empty or not. // An error is considered empty if it is `nil` or has no description. func IsEmpty(err error) bool { diff --git a/utils/commonerrors/errors_test.go b/utils/commonerrors/errors_test.go index b6a495e1b3..dc5227cf6b 100644 --- a/utils/commonerrors/errors_test.go +++ b/utils/commonerrors/errors_test.go @@ -49,6 +49,13 @@ func TestCorrespondTo(t *testing.T) { assert.True(t, CorrespondTo(fmt.Errorf("%v %v", faker.Sentence(), strings.ToUpper(ErrUndefined.Error())), strings.ToLower(ErrUndefined.Error()))) } +func TestIgnoreCorrespondTo(t *testing.T) { + assert.NoError(t, IgnoreCorrespondTo(errors.New("test"), "test")) + assert.NoError(t, IgnoreCorrespondTo(errors.New("test 123"), "test")) + assert.Error(t, IgnoreCorrespondTo(errors.New("test 123"), "abc", "def", faker.Word())) + assert.NoError(t, IgnoreCorrespondTo(ErrCondition, "condition")) +} + func TestContextErrorConversion(t *testing.T) { defer goleak.VerifyNone(t) task := func(ctx context.Context) { diff --git a/utils/filesystem/extendedosfs.go b/utils/filesystem/extendedosfs.go index ba01768311..b79b727095 100644 --- a/utils/filesystem/extendedosfs.go +++ b/utils/filesystem/extendedosfs.go @@ -22,13 +22,15 @@ type ExtendedOsFs struct { } func (c *ExtendedOsFs) Remove(name string) (err error) { - err = commonerrors.Ignore(ConvertFileSystemError(c.OsFs.Remove(name)), commonerrors.ErrNotFound) - if err != nil { - return - } // The following is to ensure sockets are correctly removed // https://stackoverflow.com/questions/16681944/how-to-reliably-unlink-a-unix-domain-socket-in-go-programming-language err = commonerrors.Ignore(ConvertFileSystemError(syscall.Unlink(name)), commonerrors.ErrNotFound) + err = commonerrors.IgnoreCorrespondTo(err, "is a directory") + if err != nil { + return + } + + err = commonerrors.Ignore(ConvertFileSystemError(c.OsFs.Remove(name)), commonerrors.ErrNotFound) return }