Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FIFO handling on Solaris #70

Merged
merged 3 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions xattr_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
)

func getxattr(path string, name string, data []byte) (int, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0)
f, err := openNonblock(path)
if err != nil {
return 0, err
}
Expand All @@ -50,7 +50,7 @@ func fgetxattr(f *os.File, name string, data []byte) (int, error) {
}

func setxattr(path string, name string, data []byte, flags int) error {
f, err := os.OpenFile(path, os.O_RDONLY, 0)
f, err := openNonblock(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -87,7 +87,8 @@ func fsetxattr(f *os.File, name string, data []byte, flags int) error {
}

func removexattr(path string, name string) error {
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_XATTR, 0)
mode := unix.O_RDONLY | unix.O_XATTR | unix.O_NONBLOCK | unix.O_CLOEXEC
fd, err := unix.Open(path, mode, 0)
if err != nil {
return err
}
Expand All @@ -114,7 +115,7 @@ func fremovexattr(f *os.File, name string) error {
}

func listxattr(path string, data []byte) (int, error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0)
f, err := openNonblock(path)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -151,8 +152,17 @@ func flistxattr(f *os.File, data []byte) (int, error) {
return copy(data, buf), nil
}

// Like os.Open, but passes O_NONBLOCK to the open(2) syscall.
func openNonblock(path string) (*os.File, error) {
fd, err := unix.Open(path, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NONBLOCK, 0)
if err != nil {
return nil, err
}
return os.NewFile(uintptr(fd), path), err
}

// stringsFromByteSlice converts a sequence of attributes to a []string.
// On Darwin and Linux, each entry is a NULL-terminated string.
// We simulate Linux/Darwin, where each entry is a NULL-terminated string.
func stringsFromByteSlice(buf []byte) (result []string) {
offset := 0
for index, b := range buf {
Expand Down
27 changes: 27 additions & 0 deletions xattr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"syscall"
"testing"

"golang.org/x/sys/unix"
)

const UserPrefix = "user."
Expand Down Expand Up @@ -260,6 +263,30 @@ func TestLargeVal(t *testing.T) {
}
}

// Get should work on a FIFO that is not opened by any other process.
// This is mainly relevant on Solaris, where getting the attributes
// requires opening the file and doing that the wrong way blocks the caller.
func TestFIFO(t *testing.T) {
d, err := ioutil.TempDir("", "pkg-xattr-testfifo-*")
if err != nil {
t.Fatal(err)
}
t.Log("tempdir:", d)
defer os.RemoveAll(d)

fifo := filepath.Join(d, "fifo")
err = unix.Mkfifo(fifo, 0777)
if err != nil {
t.Fatal(err)
}

// We only care about this not blocking.
_ = Set(fifo, "foo", []byte("bar"))
_, _ = Get(fifo, "foo")
_, _ = List(fifo)
_ = Remove(fifo, "foo")
}

// checkIfError calls t.Skip() if the underlying syscall.Errno is
// ENOTSUP or EOPNOTSUPP. It calls t.Fatal() on any other non-zero error.
func checkIfError(t *testing.T, err error) {
Expand Down