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

     1  //
     2  //  Copyright 2022 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  package mountfs
    18  
    19  import (
    20  	"io/fs"
    21  	"os"
    22  
    23  	"github.com/avfs/avfs"
    24  )
    25  
    26  // pathToMount resolves a path to a mount point nmt and a path of the mounted file system.
    27  func (vfs *MountFS) pathToMount(path string) (mnt *mount, vfsPath string) {
    28  	absPath, _ := vfs.Abs(path)
    29  
    30  	pi := avfs.NewPathIterator(vfs, absPath)
    31  	lm := vfs.rootMnt
    32  	lp := path
    33  
    34  	for pi.Next() {
    35  		mp := pi.LeftPart()
    36  
    37  		m, ok := vfs.mounts[mp]
    38  		if ok {
    39  			lm = m
    40  			lp = pi.Right()
    41  		}
    42  	}
    43  
    44  	return lm, lp
    45  }
    46  
    47  func (mnt *mount) toAbsPath(path string) string {
    48  	return mnt.vfs.Join(mnt.mntPath, mnt.basePath, path)
    49  }
    50  
    51  // restoreError restore paths in errors if necessary.
    52  func (mnt *mount) restoreError(err error) error {
    53  	if err == nil {
    54  		return nil
    55  	}
    56  
    57  	switch e := err.(type) {
    58  	case *fs.PathError:
    59  		return &fs.PathError{
    60  			Op:   e.Op,
    61  			Path: mnt.toAbsPath(e.Path),
    62  			Err:  e.Err,
    63  		}
    64  	case *os.LinkError:
    65  		return &os.LinkError{
    66  			Op:  e.Op,
    67  			Old: mnt.toAbsPath(e.Old),
    68  			New: mnt.toAbsPath(e.New),
    69  			Err: e.Err,
    70  		}
    71  	default:
    72  		return err
    73  	}
    74  }