github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/mountfs/mountfs_cfg.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  	"os"
    21  	"strings"
    22  
    23  	"github.com/avfs/avfs"
    24  )
    25  
    26  func New(rootFS avfs.VFS, basePath string) *MountFS {
    27  	rootMnt := &mount{
    28  		vfs:      rootFS,
    29  		mntPath:  "",
    30  		basePath: basePath,
    31  	}
    32  
    33  	vfs := &MountFS{
    34  		rootFS:  rootFS,
    35  		mounts:  make(mounts),
    36  		rootMnt: rootMnt,
    37  		curMnt:  rootMnt,
    38  	}
    39  
    40  	_ = vfs.SetFeatures(rootFS.Features() &^ (avfs.FeatSymlink | avfs.FeatIdentityMgr))
    41  	_ = vfs.SetOSType(avfs.CurrentOSType())
    42  	_ = vfs.SetCurDir("/")
    43  
    44  	return vfs
    45  }
    46  
    47  // Mount mounts an existing file system mntVFS on mntPath.
    48  func (vfs *MountFS) Mount(mntVFS avfs.VFS, mntPath, basePath string) error {
    49  	const op = "mount"
    50  
    51  	vfs.mu.Lock()
    52  	defer vfs.mu.Unlock()
    53  
    54  	absMntPath, _ := vfs.rootMnt.vfs.Abs(mntPath)
    55  
    56  	_, ok := vfs.mounts[absMntPath]
    57  	if ok {
    58  		return &os.PathError{Op: op, Path: mntPath, Err: avfs.ErrFileExists}
    59  	}
    60  
    61  	absBasePath, _ := mntVFS.Abs(basePath)
    62  
    63  	mnt := &mount{
    64  		vfs:      mntVFS,
    65  		mntPath:  absMntPath,
    66  		basePath: absBasePath,
    67  	}
    68  
    69  	vfs.mounts[absMntPath] = mnt
    70  
    71  	return nil
    72  }
    73  
    74  // Umount unmounts a mounted file system.
    75  func (vfs *MountFS) Umount(mntPath string) error {
    76  	const op = "umount"
    77  
    78  	vfs.mu.Lock()
    79  	defer vfs.mu.Unlock()
    80  
    81  	absMntPath, _ := vfs.Abs(mntPath)
    82  
    83  	mnt, ok := vfs.mounts[absMntPath]
    84  	if !ok {
    85  		return &os.PathError{Op: op, Path: mntPath, Err: avfs.ErrNoSuchFileOrDir}
    86  	}
    87  
    88  	mnt.vfs = nil
    89  
    90  	delete(vfs.mounts, absMntPath)
    91  
    92  	return nil
    93  }
    94  
    95  func (vfs *MountFS) String() string {
    96  	var buf strings.Builder
    97  
    98  	for _, mount := range vfs.mounts {
    99  		buf.WriteString("\nMount = ")
   100  		buf.WriteString(mount.mntPath)
   101  		buf.WriteString(", Type = ")
   102  		buf.WriteString(mount.vfs.Type())
   103  		buf.WriteString(", Name = ")
   104  		buf.WriteString(mount.vfs.Name())
   105  	}
   106  
   107  	return buf.String()
   108  }
   109  
   110  // Name returns the name of the fileSystem.
   111  func (vfs *MountFS) Name() string {
   112  	return vfs.name
   113  }
   114  
   115  // OSType returns the operating system type of the file system.
   116  func (*MountFS) OSType() avfs.OSType {
   117  	return avfs.OsLinux
   118  }
   119  
   120  // Type returns the type of the fileSystem or Identity manager.
   121  func (*MountFS) Type() string {
   122  	return "MountFS"
   123  }