github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/osfs/osfs_cfg.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  package osfs
    18  
    19  import (
    20  	"github.com/avfs/avfs"
    21  	"github.com/avfs/avfs/idm/osidm"
    22  )
    23  
    24  // New returns a new OS file system with the default Options.
    25  // Don't use this for a production environment, prefer NewWithNoIdm.
    26  func New() *OsFS {
    27  	return NewWithOptions(&Options{Idm: osidm.New()})
    28  }
    29  
    30  // NewWithNoIdm returns a new OS file system with no identity management.
    31  // Use this for production environments.
    32  func NewWithNoIdm() *OsFS {
    33  	return NewWithOptions(&Options{})
    34  }
    35  
    36  // NewWithOptions returns a new memory file system (MemFS) with the selected Options.
    37  func NewWithOptions(opts *Options) *OsFS {
    38  	if opts == nil {
    39  		opts = &Options{}
    40  	}
    41  
    42  	idm := opts.Idm
    43  	if idm == nil {
    44  		idm = avfs.NotImplementedIdm
    45  	}
    46  
    47  	features := avfs.FeatRealFS | avfs.FeatSymlink | avfs.FeatHardlink | idm.Features()
    48  	vfs := &OsFS{}
    49  
    50  	_ = vfs.SetFeatures(features)
    51  	_ = vfs.SetIdm(idm)
    52  	vfs.setErrors()
    53  
    54  	return vfs
    55  }
    56  
    57  // setErrors sets OsFS errors depending on the operating system.
    58  func (vfs *OsFS) setErrors() {
    59  	switch vfs.OSType() {
    60  	case avfs.OsWindows:
    61  		vfs.permDeniedError = avfs.ErrWinAccessDenied
    62  	default:
    63  		vfs.permDeniedError = avfs.ErrPermDenied
    64  	}
    65  }
    66  
    67  // Name returns the name of the fileSystem.
    68  func (*OsFS) Name() string {
    69  	return ""
    70  }
    71  
    72  // Type returns the type of the fileSystem or Identity manager.
    73  func (*OsFS) Type() string {
    74  	return "OsFS"
    75  }