Skip to content

Commit 5dae0ae

Browse files
committed
pathrs: support errors.Is(unix.EXDEV) checks for ErrPossible*
Our ErrPossible* errors are conceptually equivalent to EXDEV so we should support checking against them the same way (this also lets users check this way too). Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 987574e commit 5dae0ae

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

pathrs-lite/internal/errors.go renamed to pathrs-lite/internal/errors_linux.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// SPDX-License-Identifier: MPL-2.0
22

3+
//go:build linux
4+
35
// Copyright (C) 2024-2025 Aleksa Sarai <[email protected]>
46
// Copyright (C) 2024-2025 SUSE LLC
57
//
@@ -12,15 +14,24 @@ package internal
1214

1315
import (
1416
"errors"
17+
18+
"golang.org/x/sys/unix"
1519
)
1620

21+
type xdevErrorish struct {
22+
description string
23+
}
24+
25+
func (err xdevErrorish) Error() string { return err.description }
26+
func (err xdevErrorish) Is(target error) bool { return target == unix.EXDEV }
27+
1728
var (
1829
// ErrPossibleAttack indicates that some attack was detected.
19-
ErrPossibleAttack = errors.New("possible attack detected")
30+
ErrPossibleAttack error = xdevErrorish{"possible attack detected"}
2031

2132
// ErrPossibleBreakout indicates that during an operation we ended up in a
2233
// state that could be a breakout but we detected it.
23-
ErrPossibleBreakout = errors.New("possible breakout detected")
34+
ErrPossibleBreakout error = xdevErrorish{"possible breakout detected"}
2435

2536
// ErrInvalidDirectory indicates an unlinked directory.
2637
ErrInvalidDirectory = errors.New("wandered into deleted directory")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: MPL-2.0
2+
3+
//go:build linux
4+
5+
// Copyright (C) 2024-2025 Aleksa Sarai <[email protected]>
6+
// Copyright (C) 2024-2025 SUSE LLC
7+
//
8+
// This Source Code Form is subject to the terms of the Mozilla Public
9+
// License, v. 2.0. If a copy of the MPL was not distributed with this
10+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
11+
12+
package internal
13+
14+
import (
15+
"fmt"
16+
"testing"
17+
18+
"github.com/stretchr/testify/assert"
19+
"golang.org/x/sys/unix"
20+
)
21+
22+
func TestErrorXdev(t *testing.T) {
23+
for _, test := range []struct {
24+
name string
25+
err error
26+
}{
27+
{"ErrPossibleAttack", ErrPossibleAttack},
28+
{"ErrPossibleBreakout", ErrPossibleBreakout},
29+
} {
30+
t.Run(test.name, func(t *testing.T) {
31+
assert.ErrorIs(t, test.err, test.err, "errors.Is(err, err) should succeed") //nolint:useless-assert,testifylint // we need to check this
32+
assert.ErrorIs(t, test.err, unix.EXDEV, "errors.Is(err, EXDEV) should succeed") //nolint:useless-assert,testifylint // we need to check this
33+
})
34+
35+
t.Run(test.name+"-Wrapped", func(t *testing.T) {
36+
err := fmt.Errorf("wrapped error: %w", test.err)
37+
assert.ErrorIs(t, err, test.err, "errors.Is(err, err) should succeed") //nolint:useless-assert,testifylint // we need to check this
38+
assert.ErrorIs(t, err, unix.EXDEV, "errors.Is(err, EXDEV) should succeed") //nolint:useless-assert,testifylint // we need to check this
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)