github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/osfs/osfs_linux.go (about)

     1  //
     2  //  Copyright 2020 The AVFS authors
     3  //
     4  //  Licensed under the Apache License, Version 2.0 (the "License");
     5  //  you may not use this file except in compliance with the License.
     6  //  You may obtain a copy of the License at
     7  //
     8  //  	http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  //  Unless required by applicable law or agreed to in writing, software
    11  //  distributed under the License is distributed on an "AS IS" BASIS,
    12  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  //  See the License for the specific language governing permissions and
    14  //  limitations under the License.
    15  //
    16  
    17  //go:build linux
    18  
    19  package osfs
    20  
    21  import (
    22  	"io/fs"
    23  	"syscall"
    24  
    25  	"github.com/avfs/avfs"
    26  )
    27  
    28  // Chroot changes the root to that specified in path.
    29  // If there is an error, it will be of type *PathError.
    30  func (vfs *OsFS) Chroot(path string) error {
    31  	const op = "chroot"
    32  
    33  	if !vfs.HasFeature(avfs.FeatIdentityMgr) {
    34  		return &fs.PathError{Op: op, Path: path, Err: avfs.ErrOpNotPermitted}
    35  	}
    36  
    37  	err := syscall.Chroot(path)
    38  	if err != nil {
    39  		return &fs.PathError{Op: op, Path: path, Err: err}
    40  	}
    41  
    42  	return nil
    43  }
    44  
    45  // ToSysStat takes a value from fs.FileInfo.Sys() and returns a value that implements interface avfs.SysStater.
    46  func (vfs *OsFS) ToSysStat(info fs.FileInfo) avfs.SysStater {
    47  	return &LinuxSysStat{Sys: info.Sys().(*syscall.Stat_t)} //nolint:forcetypeassert // type assertion must be checked
    48  }
    49  
    50  // LinuxSysStat implements SysStater interface returned by fs.FileInfo.Sys() for a Linux file system.
    51  type LinuxSysStat struct {
    52  	Sys *syscall.Stat_t
    53  }
    54  
    55  // Gid returns the group id.
    56  func (lst *LinuxSysStat) Gid() int {
    57  	return int(lst.Sys.Gid)
    58  }
    59  
    60  // Uid returns the user id.
    61  func (lst *LinuxSysStat) Uid() int {
    62  	return int(lst.Sys.Uid)
    63  }
    64  
    65  // Nlink returns the number of hard links.
    66  func (lst *LinuxSysStat) Nlink() uint64 {
    67  	return uint64(lst.Sys.Nlink) //nolint:unconvert // required for 32 bits systems.
    68  }