github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/decomposedfs/metadata/errors.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package metadata
    20  
    21  import (
    22  	"io/fs"
    23  	"os"
    24  	"syscall"
    25  
    26  	"github.com/cs3org/reva/v2/pkg/errtypes"
    27  	"github.com/pkg/errors"
    28  	"github.com/pkg/xattr"
    29  )
    30  
    31  // IsNotExist checks if there is a os not exists error buried inside the xattr error,
    32  // as we cannot just use os.IsNotExist().
    33  func IsNotExist(err error) bool {
    34  	if _, ok := err.(errtypes.IsNotFound); ok {
    35  		return true
    36  	}
    37  	if os.IsNotExist(errors.Cause(err)) {
    38  		return true
    39  	}
    40  	if xerr, ok := errors.Cause(err).(*xattr.Error); ok {
    41  		if serr, ok2 := xerr.Err.(syscall.Errno); ok2 {
    42  			return serr == syscall.ENOENT
    43  		}
    44  	}
    45  	return false
    46  }
    47  
    48  // IsAttrUnset checks the xattr.ENOATTR from the xattr package which redifines it as ENODATA on platforms that do not natively support it (eg. linux)
    49  // see https://github.com/pkg/xattr/blob/8725d4ccc0fcef59c8d9f0eaf606b3c6f962467a/xattr_linux.go#L19-L22
    50  func IsAttrUnset(err error) bool {
    51  	if xerr, ok := errors.Cause(err).(*xattr.Error); ok {
    52  		if serr, ok2 := xerr.Err.(syscall.Errno); ok2 {
    53  			return serr == xattr.ENOATTR
    54  		}
    55  	}
    56  	return false
    57  }
    58  
    59  // The os error is buried inside the fs.PathError error
    60  func IsNotDir(err error) bool {
    61  	if perr, ok := errors.Cause(err).(*fs.PathError); ok {
    62  		if serr, ok2 := perr.Err.(syscall.Errno); ok2 {
    63  			return serr == syscall.ENOTDIR
    64  		}
    65  	}
    66  	if xerr, ok := errors.Cause(err).(*xattr.Error); ok {
    67  		if serr, ok2 := xerr.Err.(syscall.Errno); ok2 {
    68  			return serr == syscall.ENOTDIR
    69  		}
    70  	}
    71  	return false
    72  }