Skip to content

Commit

Permalink
Fix building on FreeBSD
Browse files Browse the repository at this point in the history
Closes owncloud/ocis#1217

* Extracted common error methods to `errtypes` package
* Fixed type error on Bavail ( https://golang.org/src/syscall/ztypes_freebsd_arm64.go#L125 )
* Alias ENODATA to ENOATTR

How to test this on Linux:

```
export GOOS=freebsd
make
```
  • Loading branch information
zaynetro committed Apr 12, 2021
1 parent 1765ae8 commit dfd9276
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build

SHELL := /bin/bash
SHELL := bash
BUILD_DATE=`date +%FT%T%z`
GIT_COMMIT=`git rev-parse --short HEAD`
GIT_BRANCH=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
Expand Down
5 changes: 5 additions & 0 deletions changelog/unreleased/freebsd-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: Fix build on FreeBSD

FreeBSD doesn't define ENODATA and Statfs.Bavail is int64

https://github.com/cs3org/reva/pull/1499
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/ReneKroon/ttlcache/v2 v2.3.0
github.com/aws/aws-sdk-go v1.38.13
github.com/c-bata/go-prompt v0.2.5
github.com/c-bata/go-prompt v0.2.6
github.com/cheggaaa/pb v1.0.29
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ github.com/bmatcuk/doublestar/v2 v2.0.3/go.mod h1:QMmcs3H2AUQICWhfzLXz+IYln8lRQm
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40 h1:y4B3+GPxKlrigF1ha5FFErxK+sr6sWxQovRMzwMhejo=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/c-bata/go-prompt v0.2.5 h1:3zg6PecEywxNn0xiqcXHD96fkbxghD+gdB2tbsYfl+Y=
github.com/c-bata/go-prompt v0.2.5/go.mod h1:vFnjEGDIIA/Lib7giyE4E9c50Lvl8j0S+7FVlAwDAVw=
github.com/c-bata/go-prompt v0.2.6 h1:POP+nrHE+DfLYx370bedwNhsqmpCUynWPxuHi0C5vZI=
github.com/c-bata/go-prompt v0.2.6/go.mod h1:/LMAke8wD2FsNu9EXNdHxNLbd9MedkPnCdfpU9wwHfY=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
Expand Down Expand Up @@ -872,8 +872,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pkg/term v1.1.0 h1:xIAAdCMh3QIAy+5FrE8Ad8XoDhEU4ufwbaSozViP9kk=
github.com/pkg/term v1.1.0/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
github.com/pkg/term v1.2.0-beta.2 h1:L3y/h2jkuBVFdWiJvNfYfKmzcCnILw7mJWm2JQuMppw=
github.com/pkg/term v1.2.0-beta.2/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
github.com/pkg/xattr v0.4.3 h1:5Jx4GCg5ABtqWZH8WLzeI4fOtM1HyX4RBawuCoua1es=
github.com/pkg/xattr v0.4.3/go.mod h1:sBD3RAqlr8Q+RC3FutZcikpT8nyDrIEEBw2J744gVWs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
29 changes: 29 additions & 0 deletions pkg/errtypes/constants_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018-2021 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// +build freebsd

package errtypes

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

// ENODATA is not defined on FreeBSD. Instead it is frequently aliased to ENOATTR.
// References:
// - https://groups.google.com/g/muc.lists.freebsd.hackers/c/70x4p3g6h4Y
// - https://github.com/dotnet/corefx/pull/17091
const ENODATA = unix.ENOATTR
26 changes: 26 additions & 0 deletions pkg/errtypes/constants_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018-2021 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// +build !windows,!freebsd

package errtypes

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

// ENODATA has an override on FreeBSD
const ENODATA = unix.ENODATA
48 changes: 48 additions & 0 deletions pkg/errtypes/syscall_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018-2021 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// +build !windows

package errtypes

import (
"github.com/pkg/xattr"
"golang.org/x/sys/unix"
)

// XattrIsNoData checks if underlying error is ENODATA.
func XattrIsNoData(err error) bool {
if xerr, ok := err.(*xattr.Error); ok {
if serr, ok2 := xerr.Err.(unix.Errno); ok2 {
return serr == ENODATA
}
}
return false
}

// XattrIsNotFound checks if underlying error is ENOENT.
// The os not exists error is buried inside the xattr error,
// so we cannot just use os.IsNotExists().
func XattrIsNotFound(err error) bool {
if xerr, ok := err.(*xattr.Error); ok {
if serr, ok2 := xerr.Err.(unix.Errno); ok2 {
return serr == unix.ENOENT
}
}
return false
}
31 changes: 31 additions & 0 deletions pkg/errtypes/syscall_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2018-2021 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

// +build windows

package errtypes

// XattrIsNoData checks if underlying error is ENODATA.
func XattrIsNoData(err error) bool {
return false
}

// XattrIsNotFound checks if underlying error is ENOENT.
func XattrIsNotFound(err error) bool {
return false
}
61 changes: 20 additions & 41 deletions pkg/storage/fs/owncloud/owncloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
Expand Down Expand Up @@ -769,7 +768,7 @@ func (fs *ocfs) GetPathByID(ctx context.Context, id *provider.ResourceId) (strin
return "", errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return "", errtypes.NotFound(fs.toStoragePath(ctx, ip))
}
return "", errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -808,7 +807,7 @@ func (fs *ocfs) AddGrant(ctx context.Context, ref *provider.Reference, g *provid
return errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return errtypes.NotFound(fs.toStoragePath(ctx, ip))
}
return errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -917,7 +916,7 @@ func (fs *ocfs) readPermissions(ctx context.Context, ip string) (p *provider.Res
case err == nil:
addPermissions(aggregatedPermissions, e.Grant().GetPermissions())
appctx.GetLogger(ctx).Debug().Str("ipath", np).Str("principal", strings.TrimPrefix(attrs[i], sharePrefix)).Interface("permissions", aggregatedPermissions).Msg("adding permissions")
case isNoData(err):
case errtypes.XattrIsNoData(err):
err = nil
appctx.GetLogger(ctx).Error().Str("ipath", np).Str("principal", strings.TrimPrefix(attrs[i], sharePrefix)).Interface("attrs", attrs).Msg("no permissions found on node, but they were listed")
default:
Expand Down Expand Up @@ -952,26 +951,6 @@ func (fs *ocfs) readPermissions(ctx context.Context, ip string) (p *provider.Res
return aggregatedPermissions, nil
}

func isNoData(err error) bool {
if xerr, ok := err.(*xattr.Error); ok {
if serr, ok2 := xerr.Err.(syscall.Errno); ok2 {
return serr == syscall.ENODATA
}
}
return false
}

// The os not exists error is buried inside the xattr error,
// so we cannot just use os.IsNotExists().
func isNotFound(err error) bool {
if xerr, ok := err.(*xattr.Error); ok {
if serr, ok2 := xerr.Err.(syscall.Errno); ok2 {
return serr == syscall.ENOENT
}
}
return false
}

func (fs *ocfs) readACE(ctx context.Context, ip string, principal string) (e *ace.ACE, err error) {
var b []byte
if b, err = xattr.Get(ip, sharePrefix+principal); err != nil {
Expand Down Expand Up @@ -1018,7 +997,7 @@ func (fs *ocfs) ListGrants(ctx context.Context, ref *provider.Reference) (grants
return nil, errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return nil, errtypes.NotFound(fs.toStoragePath(ctx, ip))
}
return nil, errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1056,7 +1035,7 @@ func (fs *ocfs) RemoveGrant(ctx context.Context, ref *provider.Reference, g *pro
return errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return errtypes.NotFound(fs.toStoragePath(ctx, ip))
}
return errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1088,7 +1067,7 @@ func (fs *ocfs) UpdateGrant(ctx context.Context, ref *provider.Reference, g *pro
return errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return errtypes.NotFound(fs.toStoragePath(ctx, ip))
}
return errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1148,7 +1127,7 @@ func (fs *ocfs) CreateDir(ctx context.Context, sp string) (err error) {
return errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1231,7 +1210,7 @@ func (fs *ocfs) SetArbitraryMetadata(ctx context.Context, ref *provider.Referenc
return errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1374,7 +1353,7 @@ func (fs *ocfs) UnsetArbitraryMetadata(ctx context.Context, ref *provider.Refere
return errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return errtypes.NotFound(fs.toStoragePath(ctx, ip))
}
return errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1467,7 +1446,7 @@ func (fs *ocfs) Delete(ctx context.Context, ref *provider.Reference) (err error)
return errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1561,7 +1540,7 @@ func (fs *ocfs) Move(ctx context.Context, oldRef, newRef *provider.Reference) (e
return errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(oldIP)))
}
return errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1614,7 +1593,7 @@ func (fs *ocfs) GetMD(ctx context.Context, ref *provider.Reference, mdKeys []str
return nil, errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return nil, errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1643,7 +1622,7 @@ func (fs *ocfs) getMDShareFolder(ctx context.Context, sp string, mdKeys []string
return nil, errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return nil, errors.Wrap(err, "ocfs: error reading permissions")
Expand All @@ -1663,7 +1642,7 @@ func (fs *ocfs) getMDShareFolder(ctx context.Context, sp string, mdKeys []string
m.Type = provider.ResourceType_RESOURCE_TYPE_REFERENCE
ref, err := xattr.Get(ip, mdPrefix+"target")
if err != nil {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return nil, errtypes.NotFound(fs.toStorageShadowPath(ctx, ip))
}
return nil, err
Expand Down Expand Up @@ -1711,7 +1690,7 @@ func (fs *ocfs) listWithNominalHome(ctx context.Context, ip string, mdKeys []str
return nil, errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return nil, errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1762,7 +1741,7 @@ func (fs *ocfs) listHome(ctx context.Context, home string, mdKeys []string) ([]*
return nil, errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return nil, errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1806,7 +1785,7 @@ func (fs *ocfs) listShareFolderRoot(ctx context.Context, sp string, mdKeys []str
return nil, errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return nil, errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1885,7 +1864,7 @@ func (fs *ocfs) Download(ctx context.Context, ref *provider.Reference) (io.ReadC
return nil, errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return nil, errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1913,7 +1892,7 @@ func (fs *ocfs) ListRevisions(ctx context.Context, ref *provider.Reference) ([]*
return nil, errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return nil, errors.Wrap(err, "ocfs: error reading permissions")
Expand Down Expand Up @@ -1975,7 +1954,7 @@ func (fs *ocfs) RestoreRevision(ctx context.Context, ref *provider.Reference, re
return errtypes.PermissionDenied("")
}
} else {
if isNotFound(err) {
if errtypes.XattrIsNotFound(err) {
return errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip)))
}
return errors.Wrap(err, "ocfs: error reading permissions")
Expand Down
Loading

0 comments on commit dfd9276

Please sign in to comment.