From 4429570388d0f767c93a5a3ccc76e70668e9fecc Mon Sep 17 00:00:00 2001 From: Ben McClelland Date: Thu, 18 Apr 2024 18:17:17 -0700 Subject: [PATCH] fix: use xattr.ENOATTR check for posix xattrs The xattr package has a more universal error type for xattrs not existing. Use this for better platform compatibility. This also adds the xattr.XATTR_SUPPORTED check for platform xattr suport in xattr package. Fixes #527 --- backend/meta/xattr.go | 19 +++++++++++++++---- backend/meta/xattr_with_enodata.go | 24 ------------------------ backend/meta/xattr_without_enodata.go | 24 ------------------------ cmd/versitygw/posix.go | 6 +++--- 4 files changed, 18 insertions(+), 55 deletions(-) delete mode 100644 backend/meta/xattr_with_enodata.go delete mode 100644 backend/meta/xattr_without_enodata.go diff --git a/backend/meta/xattr.go b/backend/meta/xattr.go index c7c16d2..c3ea577 100644 --- a/backend/meta/xattr.go +++ b/backend/meta/xattr.go @@ -2,6 +2,7 @@ package meta import ( "errors" + "fmt" "path/filepath" "strings" "syscall" @@ -23,7 +24,7 @@ type XattrMeta struct{} // RetrieveAttribute retrieves the value of a specific attribute for an object in a bucket. func (x XattrMeta) RetrieveAttribute(bucket, object, attribute string) ([]byte, error) { b, err := xattr.Get(filepath.Join(bucket, object), xattrPrefix+attribute) - if errors.Is(err, errNoData) { + if errors.Is(err, xattr.ENOATTR) { return nil, ErrNoSuchKey } return b, err @@ -37,7 +38,7 @@ func (x XattrMeta) StoreAttribute(bucket, object, attribute string, value []byte // DeleteAttribute removes the value of a specific attribute for an object in a bucket. func (x XattrMeta) DeleteAttribute(bucket, object, attribute string) error { err := xattr.Remove(filepath.Join(bucket, object), xattrPrefix+attribute) - if errors.Is(err, errNoData) { + if errors.Is(err, xattr.ENOATTR) { return ErrNoSuchKey } return err @@ -70,7 +71,17 @@ func isUserAttr(attr string) bool { } // Test is a helper function to test if xattrs are supported. -func (x XattrMeta) Test(path string) bool { +func (x XattrMeta) Test(path string) error { + // check for platform support + if !xattr.XATTR_SUPPORTED { + return fmt.Errorf("xattrs are not supported on this platform") + } + + // check if the filesystem supports xattrs _, err := xattr.Get(path, "user.test") - return !errors.Is(err, syscall.ENOTSUP) + if errors.Is(err, syscall.ENOTSUP) { + return fmt.Errorf("xattrs are not supported on this filesystem") + } + + return nil } diff --git a/backend/meta/xattr_with_enodata.go b/backend/meta/xattr_with_enodata.go deleted file mode 100644 index e492a7b..0000000 --- a/backend/meta/xattr_with_enodata.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2024 Versity Software -// This file is licensed under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -//go:build !freebsd && !openbsd && !netbsd -// +build !freebsd,!openbsd,!netbsd - -package meta - -import "syscall" - -var ( - errNoData = syscall.ENODATA -) diff --git a/backend/meta/xattr_without_enodata.go b/backend/meta/xattr_without_enodata.go deleted file mode 100644 index 706e188..0000000 --- a/backend/meta/xattr_without_enodata.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2024 Versity Software -// This file is licensed under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -//go:build freebsd || openbsd || netbsd -// +build freebsd openbsd netbsd - -package meta - -import "syscall" - -var ( - errNoData = syscall.ENOATTR -) diff --git a/cmd/versitygw/posix.go b/cmd/versitygw/posix.go index be0167b..9d234c8 100644 --- a/cmd/versitygw/posix.go +++ b/cmd/versitygw/posix.go @@ -64,9 +64,9 @@ func runPosix(ctx *cli.Context) error { } gwroot := (ctx.Args().Get(0)) - ok := meta.XattrMeta{}.Test(gwroot) - if !ok { - return fmt.Errorf("posix backend requires extended attributes support") + err := meta.XattrMeta{}.Test(gwroot) + if err != nil { + return fmt.Errorf("posix xattr check: %v", err) } be, err := posix.New(gwroot, meta.XattrMeta{}, posix.PosixOpts{