Skip to content

Commit 12d70b7

Browse files
committed
Refactor xattr error handling in exec_binfmt to use a single error handler function
Signed-off-by: Tiger Kaovilai <[email protected]>
1 parent 2efa7e7 commit 12d70b7

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

solver/llbsolver/ops/exec_binfmt.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,15 @@ func (m *staticEmulatorMount) Mount() ([]mount.Mount, func() error, error) {
6565
if err := copy.Copy(context.TODO(), filepath.Dir(m.path), filepath.Base(m.path), tmpdir, qemuMountName, func(ci *copy.CopyInfo) {
6666
m := 0555
6767
ci.Mode = &m
68-
ci.XAttrErrorHandler = func(dst, src, xattrKey string, err error) error {
69-
// Ignore ENOTSUP (operation not supported) errors when copying xattrs
70-
// This is needed for systems with SELinux enabled where security.selinux
71-
// xattrs cannot be modified
72-
if errors.Is(err, syscall.ENOTSUP) {
73-
return nil
74-
}
75-
return err
68+
}, copy.WithChown(uid, gid), copy.WithXAttrErrorHandler(func(dst, src, xattrKey string, err error) error {
69+
// Ignore ENOTSUP (operation not supported) errors when copying xattrs
70+
// This is needed for systems with SELinux enabled where security.selinux
71+
// xattrs cannot be modified
72+
if errors.Is(err, syscall.ENOTSUP) {
73+
return nil
7674
}
77-
}, copy.WithChown(uid, gid)); err != nil {
75+
return err
76+
})); err != nil {
7877
return nil, nil, err
7978
}
8079

solver/llbsolver/ops/exec_binfmt_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,40 @@ import (
1111
// TestXAttrErrorHandler tests the XAttrErrorHandler logic used in exec_binfmt.go
1212
func TestXAttrErrorHandler(t *testing.T) {
1313
tests := []struct {
14-
name string
15-
inputErr error
14+
name string
15+
inputErr error
1616
shouldIgnore bool
17-
description string
17+
description string
1818
}{
1919
{
20-
name: "ENOTSUP error should be ignored",
21-
inputErr: syscall.ENOTSUP,
20+
name: "ENOTSUP error should be ignored",
21+
inputErr: syscall.ENOTSUP,
2222
shouldIgnore: true,
23-
description: "ENOTSUP errors occur on SELinux systems and should be ignored",
23+
description: "ENOTSUP errors occur on SELinux systems and should be ignored",
2424
},
2525
{
26-
name: "errors.Is should work with wrapped ENOTSUP",
27-
inputErr: errors.Wrap(syscall.ENOTSUP, "failed to set xattr"),
26+
name: "errors.Is should work with wrapped ENOTSUP",
27+
inputErr: errors.Wrap(syscall.ENOTSUP, "failed to set xattr"),
2828
shouldIgnore: true,
29-
description: "Wrapped ENOTSUP errors should also be ignored",
29+
description: "Wrapped ENOTSUP errors should also be ignored",
3030
},
3131
{
32-
name: "EPERM error should not be ignored",
33-
inputErr: syscall.EPERM,
32+
name: "EPERM error should not be ignored",
33+
inputErr: syscall.EPERM,
3434
shouldIgnore: false,
35-
description: "Other permission errors should be propagated",
35+
description: "Other permission errors should be propagated",
3636
},
3737
{
38-
name: "EIO error should not be ignored",
39-
inputErr: syscall.EIO,
38+
name: "EIO error should not be ignored",
39+
inputErr: syscall.EIO,
4040
shouldIgnore: false,
41-
description: "I/O errors should be propagated",
41+
description: "I/O errors should be propagated",
4242
},
4343
{
44-
name: "nil error should return nil",
45-
inputErr: nil,
44+
name: "nil error should return nil",
45+
inputErr: nil,
4646
shouldIgnore: true,
47-
description: "nil errors should return nil",
47+
description: "nil errors should return nil",
4848
},
4949
}
5050

@@ -59,7 +59,7 @@ func TestXAttrErrorHandler(t *testing.T) {
5959
}
6060

6161
result := handler("dst", "src", "security.selinux", tt.inputErr)
62-
62+
6363
if tt.shouldIgnore {
6464
require.NoError(t, result, tt.description)
6565
} else {
@@ -68,4 +68,4 @@ func TestXAttrErrorHandler(t *testing.T) {
6868
}
6969
})
7070
}
71-
}
71+
}

0 commit comments

Comments
 (0)